Skip to content

Commit 8a0dc52

Browse files
committed
Move limit check to receiving thread instead of servo thread
The servo thread might run at a lower frequency than the receiving thread, as servoj will be blocking for the scaled time. Thus, the new command will be compared with one that is older. We want to make sure that the commands that we receive are smooth.
1 parent 2d52636 commit 8a0dc52

File tree

1 file changed

+11
-25
lines changed

1 file changed

+11
-25
lines changed

resources/external_control.urscript

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,10 @@ end
150150

151151
def set_servo_setpoint(q):
152152
cmd_servo_state = SERVO_RUNNING
153-
cmd_servo_q_last = cmd_servo_q
154-
cmd_servo_q = q
153+
if targetWithinLimits(cmd_servo_q, q, steptime):
154+
cmd_servo_q_last = cmd_servo_q
155+
cmd_servo_q = q
156+
end
155157
end
156158

157159
def extrapolate():
@@ -165,7 +167,6 @@ end
165167
thread servoThread():
166168
textmsg("ExternalControl: Starting servo thread")
167169
state = SERVO_IDLE
168-
local q_last = get_joint_positions()
169170
while control_mode == MODE_SERVOJ:
170171
enter_critical
171172
q = cmd_servo_q
@@ -185,17 +186,11 @@ thread servoThread():
185186
end
186187

187188
q = extrapolate()
188-
if targetWithinLimits(q_last, q, steptime):
189-
servoj(q, t=steptime, {{SERVO_J_REPLACE}})
190-
q_last = q
191-
end
189+
servoj(q, t=steptime, {{SERVO_J_REPLACE}})
192190

193191
elif state == SERVO_RUNNING:
194192
extrapolate_count = 0
195-
if targetWithinLimits(q_last, q, steptime):
196-
servoj(q, t=steptime, {{SERVO_J_REPLACE}})
197-
q_last = q
198-
end
193+
servoj(q, t=steptime, {{SERVO_J_REPLACE}})
199194
else:
200195
extrapolate_count = 0
201196
sync()
@@ -564,7 +559,6 @@ end
564559
thread servoThreadP():
565560
textmsg("Starting pose servo thread")
566561
state = SERVO_IDLE
567-
local q_last = get_joint_positions()
568562
while control_mode == MODE_POSE:
569563
enter_critical
570564
q = cmd_servo_q
@@ -584,15 +578,11 @@ thread servoThreadP():
584578
end
585579

586580
q = extrapolate()
587-
if targetWithinLimits(q_last, q, steptime):
588-
servoj(q, t=steptime, {{SERVO_J_REPLACE}})
589-
end
581+
servoj(q, t=steptime, {{SERVO_J_REPLACE}})
590582

591583
elif state == SERVO_RUNNING:
592584
extrapolate_count = 0
593-
if targetWithinLimits(q_last, q, steptime):
594-
servoj(q, t=steptime, {{SERVO_J_REPLACE}})
595-
end
585+
servoj(q, t=steptime, {{SERVO_J_REPLACE}})
596586
else:
597587
extrapolate_count = 0
598588
sync()
@@ -603,12 +593,6 @@ thread servoThreadP():
603593
stopj(STOPJ_ACCELERATION)
604594
end
605595

606-
def set_servo_pose(pose):
607-
cmd_servo_state = SERVO_RUNNING
608-
cmd_servo_q_last = cmd_servo_q
609-
cmd_servo_q = get_inverse_kin(pose, cmd_servo_q)
610-
end
611-
612596
def tool_contact_detection():
613597
# Detect tool contact in the directions that the TCP is moving
614598
step_back = tool_contact(direction = get_actual_tcp_speed())
@@ -727,6 +711,7 @@ while control_mode > MODE_STOPPED:
727711
join thread_move
728712
end
729713
if control_mode == MODE_SERVOJ:
714+
cmd_servo_q = get_joint_positions()
730715
thread_move = run servoThread()
731716
elif control_mode == MODE_SPEEDJ:
732717
thread_move = run speedThread()
@@ -736,6 +721,7 @@ while control_mode > MODE_STOPPED:
736721
elif control_mode == MODE_SPEEDL:
737722
thread_move = run speedlThread()
738723
elif control_mode == MODE_POSE:
724+
cmd_servo_q = get_joint_positions()
739725
thread_move = run servoThreadP()
740726
end
741727
end
@@ -765,7 +751,7 @@ while control_mode > MODE_STOPPED:
765751
set_speedl(twist)
766752
elif control_mode == MODE_POSE:
767753
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]
768-
set_servo_pose(pose)
754+
set_servo_setpoint(get_inverse_kin(pose, cmd_servo_q))
769755
elif control_mode == MODE_FREEDRIVE:
770756
if params_mult[2] == FREEDRIVE_MODE_START:
771757
textmsg("Entering freedrive mode")

0 commit comments

Comments
 (0)