ros2_control Concepts & Simulation
VIDEO
Add ros2_control to Gazebo
Install dependencies
ros-humble-ros2-control
: Core control framework for robot hardware interfaces and controllers.
ros-humble-ros2-controllers
: Predefined controllers that can be used to control robot hardware.
gz_ros2_control
: Integration between ROS 2 control framework and Gazebo
sudo apt update
sudo apt install ros-humble-ros2-control ros-humble-ros2-controllers ros-humble-gz-ros2-control
Create ros2_control.xacro
<? xml version = " 1.0 " ? >
< robot xmlns: xacro = " http://www.ros.org/wiki/xacro " name = " robot " >
< ros2_control name= " GazeboSystem " type= " system " >
< hardware >
< plugin > gazebo_ros2_control/GazeboSystem </plugin>
< /hardware >
< joint name= " left_wheel_joint " >
< command_interface name= " velocity " >
< param name= " min " > -10 < /para m >
< param name= " max " > 10 < /para m >
< /command_interface >
< state_interface name= " position " / >
< state_interface name= " velocity " / >
< /joint >
< joint name= " right_wheel_joint " >
< command_interface name= " velocity " >
< param name= " min " > -10 < /para m >
< param name= " max " > 10 < /para m >
< /command_interface >
< state_interface name= " velocity " / >
< state_interface name= " position " / >
< /joint >
< /ros2_control >
< gazebo >
< plugin name= " gazebo_ros2_control " filename= " libgazebo_ros2_control.so " >
< parameters >$( find differential_drive_robot )/config/my_controllers.yaml < /parameters >
< /plugin >
< /gazebo >
< /robot >
Create my_controllers.yaml
controller_manager :
ros_parameters :
update_rate : 30
use_sim_time : true
diff_cont :
type : diff_drive_controller/DiffDriveController
joint_broad :
type : joint_state_broadcaster/JointStateBroadcaster
diff_cont :
ros__parameters :
publish_rate : 30.0 # You can set this higher than the controller manager update rate, but it will be throttled
base_frame_id : base_link
left_wheel_names : [ ' left_wheel_joint ' ]
right_wheel_names : [ ' right_wheel_joint ' ]
wheel_separation : 0.35
wheel_radius : 0.05
use_stamped_vel : false
# joint_broad:
# ros__parameters:
colcon build --symlink-install
ros2 launch differential_drive_robot launch_sim.launch.py
Interact with ros2_control
ros2 control list_hardware_interfaces
command interfaces
left_wheel_joint/velocity [available] [unclaimed]
right_wheel_joint/velocity [available] [unclaimed]
state interfaces
left_wheel_joint/position
left_wheel_joint/velocity
right_wheel_joint/position
right_wheel_joint/velocity
ros2 run controller_manager spawner diff_cont
ros2 run controller_manager spawner joint_broad
[INFO] [1743934132.244921802] [spawner_diff_cont]: Loaded diff_cont
[INFO] [1743934132.425982699] [spawner_diff_cont]: Configured and activated diff_cont
[INFO] [1743934133.895245790] [spawner_joint_broad]: Loaded joint_broad
[INFO] [1743934134.168910213] [spawner_joint_broad]: Configured and activated joint_broad
Gazebo feedback that it started
[ruby $(which gz) sim-2] [INFO] [1743934132.144893051] [controller_manager]: Loading controller 'diff_cont'
[ruby $(which gz) sim-2] [INFO] [1743934132.248650924] [controller_manager]: Configuring controller 'diff_cont'
[ruby $(which gz) sim-2] [INFO] [1743934133.736394068] [controller_manager]: Loading controller 'joint_broad'
[ruby $(which gz) sim-2] [INFO] [1743934133.898670853] [controller_manager]: Configuring controller 'joint_broad'
[ruby $(which gz) sim-2] [INFO] [1743934133.898985579] [joint_broad]: 'joints' or 'interfaces' parameter is empty. All available state interfaces will be published
Enable teleop_twist_keyboard
by remapping /cmd_vel
topic to /diff_cont/cmd_vel_unstamped
ros2 run teleop_twist_keyboard teleop_twist_keyboard --ros-args -r /cmd_vel:=/diff_cont/cmd_vel_unstamped
Check if controllers are properly loaded
ros2 control list_controllers
joint_broad joint_state_broadcaster/JointStateBroadcaster unconfigured
sudo apt-get install ros-humble-ros-gz-bridge