forked from UniversalRobots/Universal_Robots_Client_Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternal_control.urscript
295 lines (260 loc) · 9.26 KB
/
external_control.urscript
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
{{BEGIN_REPLACE}}
# HEADER_BEGIN
steptime = get_steptime()
textmsg("ExternalControl: steptime=", steptime)
MULT_jointstate = {{JOINT_STATE_REPLACE}}
MULT_time = {{TIME_REPLACE}}
#Constants
SERVO_UNINITIALIZED = -1
SERVO_IDLE = 0
SERVO_RUNNING = 1
MODE_STOPPED = -2
MODE_UNINITIALIZED = -1
MODE_IDLE = 0
MODE_SERVOJ = 1
MODE_SPEEDJ = 2
MODE_FORWARD = 3
MODE_SPEEDL = 4
MODE_POSE = 5
TRAJECTORY_MODE_RECEIVE = 1
TRAJECTORY_MODE_CANCEL = -1
TRAJECTORY_POINT_JOINT = 0
TRAJECTORY_POINT_CARTESIAN = 1
TRAJECTORY_DATA_DIMENSION = 7
TRAJECTORY_RESULT_SUCCESS = 0
TRAJECTORY_RESULT_CANCELED = 1
TRAJECTORY_RESULT_FAILURE = 2
#Global variables are also showed in the Teach pendants variable list
global cmd_servo_state = SERVO_UNINITIALIZED
global cmd_servo_qd = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
global cmd_servo_q = get_actual_joint_positions()
global cmd_servo_q_last = get_actual_joint_positions()
global cmd_twist = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
global extrapolate_count = 0
global extrapolate_max_count = 0
global control_mode = MODE_UNINITIALIZED
global trajectory_points_left = 0
def set_servo_setpoint(q):
cmd_servo_state = SERVO_RUNNING
cmd_servo_q_last = cmd_servo_q
cmd_servo_q = q
end
def extrapolate():
diff = [cmd_servo_q[0] - cmd_servo_q_last[0], cmd_servo_q[1] - cmd_servo_q_last[1], cmd_servo_q[2] - cmd_servo_q_last[2], cmd_servo_q[3] - cmd_servo_q_last[3], cmd_servo_q[4] - cmd_servo_q_last[4], cmd_servo_q[5] - cmd_servo_q_last[5]]
cmd_servo_q_last = cmd_servo_q
cmd_servo_q = [cmd_servo_q[0] + diff[0], cmd_servo_q[1] + diff[1], cmd_servo_q[2] + diff[2], cmd_servo_q[3] + diff[3], cmd_servo_q[4] + diff[4], cmd_servo_q[5] + diff[5]]
return cmd_servo_q
end
thread servoThread():
textmsg("ExternalControl: Starting servo thread")
state = SERVO_IDLE
while control_mode == MODE_SERVOJ:
enter_critical
q = cmd_servo_q
do_extrapolate = False
if (cmd_servo_state == SERVO_IDLE):
do_extrapolate = True
end
state = cmd_servo_state
if cmd_servo_state > SERVO_UNINITIALIZED:
cmd_servo_state = SERVO_IDLE
end
if do_extrapolate:
extrapolate_count = extrapolate_count + 1
if extrapolate_count > extrapolate_max_count:
extrapolate_max_count = extrapolate_count
end
q = extrapolate()
servoj(q, t=steptime, {{SERVO_J_REPLACE}})
elif state == SERVO_RUNNING:
extrapolate_count = 0
servoj(q, t=steptime, {{SERVO_J_REPLACE}})
else:
extrapolate_count = 0
sync()
end
exit_critical
end
textmsg("ExternalControl: servo thread ended")
stopj(4.0)
end
# Helpers for speed control
def set_speed(qd):
cmd_servo_qd = qd
control_mode = MODE_SPEEDJ
end
thread speedThread():
textmsg("ExternalControl: Starting speed thread")
while control_mode == MODE_SPEEDJ:
qd = cmd_servo_qd
speedj(qd, 40.0, steptime)
end
textmsg("ExternalControl: speedj thread ended")
stopj(5.0)
end
thread jointTrajectoryThread():
textmsg("Executing trajectory. Number of points: ", trajectory_points_left)
while trajectory_points_left > 0:
enter_critical
#reading trajectory point + blend radius + type of point (cartesian/joint based)
raw_point = socket_read_binary_integer(TRAJECTORY_DATA_DIMENSION+1+1, "trajectory_socket")
trajectory_points_left = trajectory_points_left - 1
exit_critical
if raw_point[0] > 0:
q = [ raw_point[1] / MULT_jointstate, raw_point[2] / MULT_jointstate, raw_point[3] / MULT_jointstate, raw_point[4] / MULT_jointstate, raw_point[5] / MULT_jointstate, raw_point[6] / MULT_jointstate]
tmptime = raw_point[7] / MULT_time
blend_radius = raw_point[8] / MULT_time
if trajectory_points_left > 0:
if raw_point[9] == TRAJECTORY_POINT_JOINT:
# textmsg("Executing movej with blending")
movej(q, t=tmptime, r=blend_radius)
elif raw_point[9] == TRAJECTORY_POINT_CARTESIAN:
# textmsg("Executing movel with blending")
movel(p[q[0], q[1], q[2], q[3], q[4], q[5]], t=tmptime, r=blend_radius)
end
else:
if raw_point[9] == TRAJECTORY_POINT_JOINT:
# textmsg("Executing movej without blending")
movej(q, t=tmptime, r=0.0)
elif raw_point[9] == TRAJECTORY_POINT_CARTESIAN:
# textmsg("Executing movel without blending")
movel(p[q[0], q[1], q[2], q[3], q[4], q[5]], t=tmptime, r=0.0)
end
end
end
end
socket_send_int(TRAJECTORY_RESULT_SUCCESS, "trajectory_socket")
textmsg("Trajectory finished")
end
def clear_remaining_trajectory_points():
while trajectory_points_left > 0:
raw_point = socket_read_binary_integer(TRAJECTORY_DATA_DIMENSION+2, "trajectory_socket")
trajectory_points_left = trajectory_points_left - 1
end
end
# Helpers for speed control
def set_speedl(twist):
cmd_twist = twist
control_mode = MODE_SPEEDL
end
thread speedlThread():
textmsg("Starting speedl thread")
while control_mode == MODE_SPEEDL:
twist = cmd_twist
speedl(twist, 40.0, steptime)
end
textmsg("speedl thread ended")
stopj(5.0)
end
thread servoThreadP():
textmsg("Starting pose servo thread")
state = SERVO_IDLE
while control_mode == MODE_POSE:
enter_critical
q = cmd_servo_q
do_extrapolate = False
if (cmd_servo_state == SERVO_IDLE):
do_extrapolate = True
end
state = cmd_servo_state
if cmd_servo_state > SERVO_UNINITIALIZED:
cmd_servo_state = SERVO_IDLE
end
if do_extrapolate:
extrapolate_count = extrapolate_count + 1
if extrapolate_count > extrapolate_max_count:
extrapolate_max_count = extrapolate_count
end
q = extrapolate()
servoj(q, t=steptime, {{SERVO_J_REPLACE}})
elif state == SERVO_RUNNING:
extrapolate_count = 0
servoj(q, t=steptime, {{SERVO_J_REPLACE}})
else:
extrapolate_count = 0
sync()
end
exit_critical
end
textmsg("pose servo thread ended")
stopj(4.0)
end
def set_servo_pose(pose):
cmd_servo_state = SERVO_RUNNING
cmd_servo_q_last = cmd_servo_q
cmd_servo_q = get_inverse_kin(pose, cmd_servo_q)
end
# HEADER_END
# NODE_CONTROL_LOOP_BEGINS
socket_open("{{SERVER_IP_REPLACE}}", {{SERVER_PORT_REPLACE}}, "reverse_socket")
socket_open("{{SERVER_IP_REPLACE}}", {{TRAJECTORY_SERVER_PORT_REPLACE}}, "trajectory_socket")
control_mode = MODE_UNINITIALIZED
thread_move = 0
thread_trajectory = 0
trajectory_points_left = 0
global keepalive = -2
params_mult = socket_read_binary_integer(1+6+1, "reverse_socket", 0)
textmsg("ExternalControl: External control active")
keepalive = params_mult[1]
while keepalive > 0 and control_mode > MODE_STOPPED:
enter_critical
params_mult = socket_read_binary_integer(1+6+1, "reverse_socket", 0.02) # steptime could work as well, but does not work in simulation
if params_mult[0] > 0:
keepalive = params_mult[1]
if control_mode != params_mult[8]:
if control_mode == MODE_FORWARD:
kill thread_trajectory
clear_remaining_trajectory_points()
end
control_mode = params_mult[8]
join thread_move
if control_mode == MODE_SERVOJ:
thread_move = run servoThread()
elif control_mode == MODE_SPEEDJ:
thread_move = run speedThread()
elif control_mode == MODE_FORWARD:
kill thread_move
elif control_mode == MODE_SPEEDL:
thread_move = run speedlThread()
elif control_mode == MODE_POSE:
thread_move = run servoThreadP()
end
end
if control_mode == MODE_SERVOJ:
q = [params_mult[2] / MULT_jointstate, params_mult[3] / MULT_jointstate, params_mult[4] / MULT_jointstate, params_mult[5] / MULT_jointstate, params_mult[6] / MULT_jointstate, params_mult[7] / MULT_jointstate]
set_servo_setpoint(q)
elif control_mode == MODE_SPEEDJ:
qd = [params_mult[2] / MULT_jointstate, params_mult[3] / MULT_jointstate, params_mult[4] / MULT_jointstate, params_mult[5] / MULT_jointstate, params_mult[6] / MULT_jointstate, params_mult[7] / MULT_jointstate]
set_speed(qd)
elif control_mode == MODE_FORWARD:
if params_mult[2] == TRAJECTORY_MODE_RECEIVE:
kill thread_trajectory
clear_remaining_trajectory_points()
trajectory_points_left = params_mult[3]
thread_trajectory = run jointTrajectoryThread()
elif params_mult[2] == TRAJECTORY_MODE_CANCEL:
textmsg("cancel received")
kill thread_trajectory
clear_remaining_trajectory_points()
socket_send_int(TRAJECTORY_RESULT_CANCELED, "trajectory_socket")
end
elif control_mode == MODE_SPEEDL:
twist = [params_mult[2] / MULT_jointstate, params_mult[3] / MULT_jointstate, params_mult[4] / MULT_jointstate, params_mult[5] / MULT_jointstate, params_mult[6] / MULT_jointstate, params_mult[7] / MULT_jointstate]
set_speedl(twist)
elif control_mode == MODE_POSE:
pose = p[params_mult[2] / MULT_jointstate, params_mult[3] / MULT_jointstate, params_mult[4] / MULT_jointstate, params_mult[5] / MULT_jointstate, params_mult[6] / MULT_jointstate, params_mult[7] / MULT_jointstate]
set_servo_pose(pose)
end
else:
keepalive = keepalive - 1
end
exit_critical
end
textmsg("ExternalControl: Stopping communication and control")
control_mode = MODE_STOPPED
join thread_move
join thread_trajectory
textmsg("ExternalControl: All threads ended")
socket_close("reverse_socket")
socket_close("trajectory_socket")
# NODE_CONTROL_LOOP_ENDS