Skip to content

Commit a75f05f

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 7ec2129 commit a75f05f

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():
@@ -163,7 +165,6 @@ end
163165
thread servoThread():
164166
textmsg("ExternalControl: Starting servo thread")
165167
state = SERVO_IDLE
166-
local q_last = get_joint_positions()
167168
while control_mode == MODE_SERVOJ:
168169
enter_critical
169170
q = cmd_servo_q
@@ -183,17 +184,11 @@ thread servoThread():
183184
end
184185

185186
q = extrapolate()
186-
if targetWithinLimits(q_last, q, steptime):
187-
servoj(q, t=steptime, {{SERVO_J_REPLACE}})
188-
q_last = q
189-
end
187+
servoj(q, t=steptime, {{SERVO_J_REPLACE}})
190188

191189
elif state == SERVO_RUNNING:
192190
extrapolate_count = 0
193-
if targetWithinLimits(q_last, q, steptime):
194-
servoj(q, t=steptime, {{SERVO_J_REPLACE}})
195-
q_last = q
196-
end
191+
servoj(q, t=steptime, {{SERVO_J_REPLACE}})
197192
else:
198193
extrapolate_count = 0
199194
sync()
@@ -562,7 +557,6 @@ end
562557
thread servoThreadP():
563558
textmsg("Starting pose servo thread")
564559
state = SERVO_IDLE
565-
local q_last = get_joint_positions()
566560
while control_mode == MODE_POSE:
567561
enter_critical
568562
q = cmd_servo_q
@@ -582,15 +576,11 @@ thread servoThreadP():
582576
end
583577

584578
q = extrapolate()
585-
if targetWithinLimits(q_last, q, steptime):
586-
servoj(q, t=steptime, {{SERVO_J_REPLACE}})
587-
end
579+
servoj(q, t=steptime, {{SERVO_J_REPLACE}})
588580

589581
elif state == SERVO_RUNNING:
590582
extrapolate_count = 0
591-
if targetWithinLimits(q_last, q, steptime):
592-
servoj(q, t=steptime, {{SERVO_J_REPLACE}})
593-
end
583+
servoj(q, t=steptime, {{SERVO_J_REPLACE}})
594584
else:
595585
extrapolate_count = 0
596586
sync()
@@ -601,12 +591,6 @@ thread servoThreadP():
601591
stopj(STOPJ_ACCELERATION)
602592
end
603593

604-
def set_servo_pose(pose):
605-
cmd_servo_state = SERVO_RUNNING
606-
cmd_servo_q_last = cmd_servo_q
607-
cmd_servo_q = get_inverse_kin(pose, cmd_servo_q)
608-
end
609-
610594
def tool_contact_detection():
611595
# Detect tool contact in the directions that the TCP is moving
612596
step_back = tool_contact(direction = get_actual_tcp_speed())
@@ -725,6 +709,7 @@ while control_mode > MODE_STOPPED:
725709
join thread_move
726710
end
727711
if control_mode == MODE_SERVOJ:
712+
cmd_servo_q = get_joint_positions()
728713
thread_move = run servoThread()
729714
elif control_mode == MODE_SPEEDJ:
730715
thread_move = run speedThread()
@@ -734,6 +719,7 @@ while control_mode > MODE_STOPPED:
734719
elif control_mode == MODE_SPEEDL:
735720
thread_move = run speedlThread()
736721
elif control_mode == MODE_POSE:
722+
cmd_servo_q = get_joint_positions()
737723
thread_move = run servoThreadP()
738724
end
739725
end
@@ -763,7 +749,7 @@ while control_mode > MODE_STOPPED:
763749
set_speedl(twist)
764750
elif control_mode == MODE_POSE:
765751
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]
766-
set_servo_pose(pose)
752+
set_servo_setpoint(get_inverse_kin(pose, cmd_servo_q))
767753
elif control_mode == MODE_FREEDRIVE:
768754
if params_mult[2] == FREEDRIVE_MODE_START:
769755
textmsg("Entering freedrive mode")

0 commit comments

Comments
 (0)