Skip to content

Commit 2adb339

Browse files
committed
feat: add torque control to urscript
1 parent ebe30d2 commit 2adb339

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

include/ur_client_library/comm/control_mode.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ enum class ControlMode : int32_t
5252
MODE_FREEDRIVE = 6, ///< Set when freedrive mode is active.
5353
MODE_TOOL_IN_CONTACT =
5454
7, ///< Used only internally in the script, when robot is in tool contact, clear by endToolContact()
55+
MODE_TORQUE = 8, ///< Set when torque control is active.
5556
END ///< This is not an actual control mode, but used internally to get the number of control modes
5657
};
5758

@@ -62,9 +63,11 @@ class ControlModeTypes
6263
{
6364
public:
6465
// Control modes that require realtime communication
65-
static const inline std::vector<ControlMode> REALTIME_CONTROL_MODES = {
66-
ControlMode::MODE_SERVOJ, ControlMode::MODE_SPEEDJ, ControlMode::MODE_SPEEDL, ControlMode::MODE_POSE
67-
};
66+
static const inline std::vector<ControlMode> REALTIME_CONTROL_MODES = { ControlMode::MODE_SERVOJ,
67+
ControlMode::MODE_SPEEDJ,
68+
ControlMode::MODE_SPEEDL,
69+
ControlMode::MODE_POSE,
70+
ControlMode::MODE_TORQUE };
6871

6972
// Control modes that doesn't require realtime communication
7073
static const inline std::vector<ControlMode> NON_REALTIME_CONTROL_MODES = { ControlMode::MODE_IDLE,

resources/external_control.urscript

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ MODE_SPEEDL = 4
2525
MODE_POSE = 5
2626
MODE_FREEDRIVE = 6
2727
MODE_TOOL_IN_CONTACT = 7
28+
MODE_TORQUE = 8
2829
# Data dimensions of the message received on the reverse interface
2930
REVERSE_INTERFACE_DATA_DIMENSION = 8
3031

@@ -70,6 +71,7 @@ JOINT_IGNORE_SPEED = 20.0
7071
global violation_popup_counter = 0
7172
global cmd_servo_state = SERVO_UNINITIALIZED
7273
global cmd_servo_qd = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
74+
global cmd_tau = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
7375
global cmd_servo_q = get_joint_positions()
7476
global cmd_servo_q_last = cmd_servo_q
7577
global cmd_twist = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
@@ -225,6 +227,22 @@ thread speedThread():
225227
stopj(STOPJ_ACCELERATION)
226228
end
227229

230+
# Helpers for torque control
231+
def set_torque(tau):
232+
cmd_tau = tau
233+
control_mode = MODE_TORQUE
234+
end
235+
236+
thread torqueThread():
237+
textmsg("ExternalControl: Starting torque thread")
238+
while control_mode == MODE_TORQUE:
239+
tau = cmd_tau
240+
torque_command(tau, friction_comp=True)
241+
end
242+
textmsg("ExternalControl: torque thread ended")
243+
stopj(STOPJ_ACCELERATION)
244+
end
245+
228246
# Function return value (bool) determines whether the robot is moving after this spline segment or
229247
# not.
230248
def cubicSplineRun(end_q, end_qd, time, is_last_point=False, is_first_point=False):
@@ -748,6 +766,8 @@ while control_mode > MODE_STOPPED:
748766
thread_move = run servoThread()
749767
elif control_mode == MODE_SPEEDJ:
750768
thread_move = run speedThread()
769+
elif control_mode == MODE_TORQUE:
770+
thread_move = run torqueThread()
751771
elif control_mode == MODE_FORWARD:
752772
kill thread_move
753773
stopj(STOPJ_ACCELERATION)
@@ -766,6 +786,9 @@ while control_mode > MODE_STOPPED:
766786
elif control_mode == MODE_SPEEDJ:
767787
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]
768788
set_speed(qd)
789+
elif control_mode == MODE_TORQUE:
790+
tau = [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]
791+
set_torque(tau)
769792
elif control_mode == MODE_FORWARD:
770793
if params_mult[2] == TRAJECTORY_MODE_RECEIVE:
771794
kill thread_trajectory

0 commit comments

Comments
 (0)