ROS 2 for Robot Arms, step 7
Controllers and actions
Switch the arm to its trajectory controller and pick a ball into the cup with FollowJointTrajectory and GripperCommand goals.
ros2_control
On a real arm, ros2_control sits between ROS and the motors. The hardware offers command interfaces such as base/position, and each controller claims the ones it drives. The controller manager activates and deactivates controllers, and refuses to let two claim the same joint:
| Controller | Claims | At start-up |
|---|---|---|
forward_position_controller | the four arm joints | active |
joint_trajectory_controller | the four arm joints | inactive |
gripper_controller | finger_l | active |
So swapping controllers is one controller_manager_msgs/srv/SwitchController request: activate_controllers, deactivate_controllers, and strictness=SwitchController.Request.STRICT, which makes it all or nothing. An inactive controller rejects goals, as on a real robot.
Actions
A service is too short for a motion that takes seconds. An action is a long-running goal that streams feedback while it runs, ends with a result, and can be cancelled. send_goal_async(goal, feedback_callback=…) gives a future of a goal handle; check handle.accepted, then handle.get_result_async(). Spinning on each future in main() is fine, because main() isn't a callback.
Trajectories
A trajectory_msgs/msg/JointTrajectory lists joint_names and points. Each point has positions and a time_from_start (Duration(seconds=1.5).to_msg()). Positions alone give straight-line interpolation; adding velocities gives smooth cubic splines, and zero end velocities make the arm ease to a stop. A new goal pre-empts the running one, so wait for each result.
Gripping
control_msgs/action/GripperCommand sets command.position, here each finger's opening (0 to 0.045 m). Closing on a ball can't reach 0: the fingers stall. This arm's controller is configured to treat that as success:
gripper_controller:
ros__parameters:
allow_stalling: true
stall_timeout: 0.5
Your task
Finish Picker: switch to the trajectory controller, write move(q, seconds) and grip(position), then put the red ball in the cup. Open, approach 10 cm above it, descend, close, lift, carry over the cup, lower, open and retreat. The IK and detection calls are given; log the first feedback of each trajectory goal.