-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
executable file
·178 lines (150 loc) · 6.14 KB
/
script.py
File metadata and controls
executable file
·178 lines (150 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python3
"""
Allows to use the service dynamixel_command
"""
from numpy import array
import rospy
import time
from std_msgs.msg import String
from dynamixel_workbench_msgs.srv import DynamixelCommand
# keyboard
import termios, sys, os
TERMIOS = termios
def getkey():
"""
Get the last pressed key
"""
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
new = termios.tcgetattr(fd)
new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
new[6][TERMIOS.VMIN] = 1
new[6][TERMIOS.VTIME] = 0
termios.tcsetattr(fd, TERMIOS.TCSANOW, new)
c = None
try:
c = os.read(fd, 1)
finally:
termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
c = str(c).replace('b', "").replace('\'', "")
return c
def jointCommand(command, id_num, addr_name, value, time):
"""
Make a request to a the "dynamixel_command" ros service to modify a
parameter such as position or torque of the motor specified by the "id_num"
parameter.
"""
rospy.wait_for_service('dynamixel_workbench/dynamixel_command')
try:
dynamixel_command = rospy.ServiceProxy('/dynamixel_workbench/dynamixel_command', DynamixelCommand)
result = dynamixel_command(command,id_num,addr_name,value)
rospy.sleep(time)
return result.comm_result
except rospy.ServiceException as exc:
print(str(exc))
def deg2raw(input_list: list = [0,0,0,0,0], min_deg: int = -150, max_deg: int = 150)->list:
"""
Map a list of motor positions in degrees to a 10 bit values from 0 to 1023.
"""
out_list = [0,0,0,0,0]
for i in range(len(input_list)):
out_list[i] = int( ((input_list[i] - min_deg)*1024)/(max_deg-min_deg) )
return out_list
def main(goal_position: list = [30,45,-30,-60,150], home_position: list = [0,0,0,0,0]):
motors_ids = [6,7,8,9,10]
goal_position_raw = deg2raw(goal_position)
home_position_raw = deg2raw(home_position)
# Torque joints config
jointCommand('', motors_ids[0], 'Torque_Limit', 600, 0)
jointCommand('', motors_ids[1], 'Torque_Limit', 400, 0)
jointCommand('', motors_ids[2], 'Torque_Limit', 400, 0)
jointCommand('', motors_ids[3], 'Torque_Limit', 400, 0)
jointCommand('', motors_ids[4], 'Torque_Limit', 400, 0)
# Initial state
selected_link = "waist"
print("--------------------------------")
print("link: ",selected_link)
# State machine
while(True):
# Key pressed event
key = getkey()
if(selected_link == "waist"):
if(key == "w"):
selected_link = "shoulder"
print("--------------------------------")
print("link: ",selected_link)
elif(key == "s"):
print("--------------------------------")
selected_link = "gripper"
print("link: ",selected_link)
elif(key == "d"):
print("movement to goal ...")
jointCommand('', motors_ids[0], 'Goal_Position', goal_position_raw[0], 0.5)
elif(key == "a"):
print("movement to home ...")
jointCommand('', motors_ids[0], 'Goal_Position', home_position_raw[0], 0.5)
elif(selected_link == "shoulder"):
if(key == "w"):
selected_link = "elbow"
print("--------------------------------")
print("link: ",selected_link)
elif(key == "s"):
print("--------------------------------")
selected_link = "waist"
print("link: ",selected_link)
elif(key == "d"):
print("movement to goal ...")
jointCommand('', motors_ids[1], 'Goal_Position', goal_position_raw[1], 0.5)
elif(key == "a"):
print("movement to home ...")
jointCommand('', motors_ids[1], 'Goal_Position', home_position_raw[1], 0.5)
elif(selected_link == "elbow"):
if(key == "w"):
selected_link = "wrist"
print("--------------------------------")
print("link: ",selected_link)
elif(key == "s"):
print("--------------------------------")
selected_link = "shoulder"
print("link: ",selected_link)
elif(key == "d"):
print("movement to goal ...")
jointCommand('', motors_ids[2], 'Goal_Position', goal_position_raw[2], 0.5)
elif(key == "a"):
print("movement to home ...")
jointCommand('', motors_ids[2], 'Goal_Position', home_position_raw[2], 0.5)
elif(selected_link == "wrist"):
if(key == "w"):
selected_link = "gripper"
print("--------------------------------")
print("link: ",selected_link)
elif(key == "s"):
print("--------------------------------")
selected_link = "elbow"
print("link: ",selected_link)
elif(key == "d"):
print("movement to goal ...")
jointCommand('', motors_ids[3], 'Goal_Position', goal_position_raw[3], 0.5)
elif(key == "a"):
print("movement to home ...")
jointCommand('', motors_ids[3], 'Goal_Position', home_position_raw[3], 0.5)
elif(selected_link == "gripper"):
if(key == "w"):
selected_link = "waist"
print("--------------------------------")
print("link: ",selected_link)
elif(key == "s"):
print("--------------------------------")
selected_link = "wrist"
print("link: ",selected_link)
elif(key == "d"):
print("movement to goal ...")
jointCommand('', motors_ids[4], 'Goal_Position', goal_position_raw[4], 0.5)
elif(key == "a"):
print("movement to home ...")
jointCommand('', motors_ids[4], 'Goal_Position', home_position_raw[4], 0.5)
if __name__ == '__main__':
try:
main()
except rospy.ROSInterruptException:
pass