Skip to content

Commit 53e9351

Browse files
committed
Update force_mode example
1 parent a325f18 commit 53e9351

File tree

2 files changed

+43
-32
lines changed

2 files changed

+43
-32
lines changed

ur_robot_driver/examples/examples.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ def init_robot(self):
9494
"/scaled_joint_trajectory_controller/follow_joint_trajectory",
9595
FollowJointTrajectory,
9696
)
97+
self.passthrough_trajectory_action_client = waitForAction(
98+
self.node,
99+
"/passthrough_trajectory_controller/follow_joint_trajectory",
100+
FollowJointTrajectory,
101+
)
97102

98103
def set_io(self, pin, value):
99104
"""Test to set an IO."""
@@ -104,7 +109,7 @@ def set_io(self, pin, value):
104109

105110
self.call_service("/io_and_status_controller/set_io", set_io_req)
106111

107-
def send_trajectory(self, waypts, time_vec):
112+
def send_trajectory(self, waypts, time_vec, action_client):
108113
"""Send robot trajectory."""
109114
if len(waypts) != len(time_vec):
110115
raise Exception("waypoints vector and time vec should be same length")
@@ -120,13 +125,13 @@ def send_trajectory(self, waypts, time_vec):
120125

121126
# Sending trajectory goal
122127
goal_response = self.call_action(
123-
self.jtc_action_client, FollowJointTrajectory.Goal(trajectory=joint_trajectory)
128+
action_client, FollowJointTrajectory.Goal(trajectory=joint_trajectory)
124129
)
125130
if not goal_response.accepted:
126131
raise Exception("trajectory was not accepted")
127132

128133
# Verify execution
129-
result = self.get_result(self.jtc_action_client, goal_response)
134+
result = self.get_result(action_client, goal_response)
130135
return result.error_code == FollowJointTrajectory.Result.SUCCESSFUL
131136

132137
def call_service(self, srv_name, request):

ur_robot_driver/examples/force_mode.py

100644100755
Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from rclpy.node import Node
3434
from controller_manager_msgs.srv import SwitchController
3535
from builtin_interfaces.msg import Duration
36+
from geometry_msgs.msg import Twist
3637
from std_msgs.msg import Header
3738
from std_srvs.srv import Trigger
3839

@@ -42,7 +43,6 @@
4243
Pose,
4344
PoseStamped,
4445
Wrench,
45-
WrenchStamped,
4646
Vector3,
4747
)
4848

@@ -55,33 +55,36 @@
5555
node = Node("robot_driver_test")
5656
robot = Robot(node)
5757

58-
# Activate force mode controller
58+
# Add force mode service to service interfaces and re-init robot
59+
robot.service_interfaces.update({"/force_mode_controller/start_force_mode": SetForceMode})
60+
robot.service_interfaces.update({"/force_mode_controller/stop_force_mode": Trigger})
61+
robot.init_robot()
62+
time.sleep(0.5)
63+
# Press play on the robot
64+
robot.call_service("/dashboard_client/play", Trigger.Request())
65+
66+
time.sleep(0.5)
67+
# Start controllers
5968
robot.call_service(
6069
"/controller_manager/switch_controller",
6170
SwitchController.Request(
62-
activate_controllers=["force_mode_controller", "scaled_joint_trajectory_controller"],
71+
deactivate_controllers=["scaled_joint_trajectory_controller"],
72+
activate_controllers=["passthrough_trajectory_controller", "force_mode_controller"],
6373
strictness=SwitchController.Request.BEST_EFFORT,
6474
),
6575
)
6676

67-
# Add force mode service to service interfaces and re-init robot
68-
robot.service_interfaces.update({"/force_mode_controller/start_force_mode": SetForceMode})
69-
robot.init_robot()
70-
time.sleep(2)
71-
# Press play on the robot
72-
robot.call_service("/dashboard_client/play", Trigger.Request())
73-
7477
# Move robot in to position
7578
robot.send_trajectory(
7679
waypts=[[-1.5707, -1.5707, -1.5707, -1.5707, 1.5707, 0]],
77-
time_vec=[Duration(sec=6, nanosec=0)],
80+
time_vec=[Duration(sec=5, nanosec=0)],
81+
action_client=robot.passthrough_trajectory_action_client,
7882
)
7983

8084
# Finished moving
81-
8285
# Create task frame for force mode
8386
point = Point(x=0.0, y=0.0, z=0.0)
84-
orientation = Quaternion(x=0.0, y=0.0, z=0.0, w=0.0)
87+
orientation = Quaternion(x=0.0, y=0.0, z=0.0, w=1.0)
8588
task_frame_pose = Pose()
8689
task_frame_pose.position = point
8790
task_frame_pose.orientation = orientation
@@ -96,17 +99,19 @@
9699
compliance = [False, False, True, False, False, False]
97100

98101
# Create Wrench message for force mode
99-
wrench_vec = Wrench(force=Vector3(x=0.0, y=0.0, z=-10.0), torque=Vector3(x=0.0, y=0.0, z=0.0))
100-
wrench_stamp = WrenchStamped(header=header, wrench=wrench_vec)
102+
wrench_vec = Wrench(force=Vector3(x=0.0, y=0.0, z=-20.0), torque=Vector3(x=0.0, y=0.0, z=0.0))
101103
# Specify interpretation of task frame (no transform)
102104
type_spec = SetForceMode.Request.NO_TRANSFORM
103105

104106
# Specify max speeds and deviations of force mode
105-
limits = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
107+
speed_limits = Twist()
108+
speed_limits.linear = Vector3(x=0.0, y=0.0, z=1.0)
109+
speed_limits.angular = Vector3(x=0.0, y=0.0, z=1.0)
110+
deviation_limits = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
106111

107112
# specify damping and gain scaling
108-
damping_factor = 0.025
109-
gain_scale = 0.5
113+
damping_factor = 0.1
114+
gain_scale = 0.8
110115

111116
req = SetForceMode.Request()
112117
req.task_frame = frame_stamp
@@ -116,21 +121,22 @@
116121
req.selection_vector_rx = compliance[3]
117122
req.selection_vector_ry = compliance[4]
118123
req.selection_vector_rz = compliance[5]
119-
req.wrench = wrench_stamp
124+
req.wrench = wrench_vec
120125
req.type = type_spec
121-
req.limits = limits
126+
req.speed_limits = speed_limits
127+
req.deviation_limits = deviation_limits
122128
req.damping_factor = damping_factor
123129
req.gain_scaling = gain_scale
124130

125131
# Send request to controller
132+
node.get_logger().info(f"Starting force mode with {req}")
126133
robot.call_service("/force_mode_controller/start_force_mode", req)
127-
128-
time.sleep(15)
129-
# Deactivate force mode controller
130-
robot.call_service(
131-
"/controller_manager/switch_controller",
132-
SwitchController.Request(
133-
deactivate_controllers=["force_mode_controller"],
134-
strictness=SwitchController.Request.BEST_EFFORT,
135-
),
134+
robot.send_trajectory(
135+
waypts=[[1.5707, -1.5707, -1.5707, -1.5707, 1.5707, 0]],
136+
time_vec=[Duration(sec=5, nanosec=0)],
137+
action_client=robot.passthrough_trajectory_action_client,
136138
)
139+
140+
time.sleep(3)
141+
node.get_logger().info("Deactivating force mode controller.")
142+
robot.call_service("/force_mode_controller/stop_force_mode", Trigger.Request())

0 commit comments

Comments
 (0)