28Chapter 28 of 74
ROS 2 Control: Prepare the Robot for Control
From Simulation to Controlling the Robot
We already have our robot running inside Gazebo. Now we want to control the wheels of the robot from ROS 2.
Detailed Explanation
This chapter focuses on the practical implementation of ROS 2 Control for our BumperBot.
The detailed explanation of the concepts, code, parameters, interfaces, and how all the components work together is covered in Chapter 29.
Create the Controller Package
We will keep the controller configuration in a separate package called:
bumperbot_controllerThis package will contain the configuration files required for our robot controllers.
Go to the src Folder

Open a terminal and run:
cd bumperbot_ws
cd srcCreate the Controller Package

Run:
ros2 pkg create --build-type ament_cmake bumperbot_controllerBuild the Package

Go back to the workspace and build
cd ..
colcon buildCreate the config Folder

Open the workspace in VS Code.
Inside:
bumperbot_controllercreate a folder called:
configCreate the Controller YAML File

Inside the config folder, create:
bumperbot_controllers.yamlConfigure the Controllers

Put this in bumperbot_controllers.yaml:
controller_manager:
ros__parameters:
update_rate: 100 # Hz
use_sim_time: true
joint_state_broadcaster:
type: joint_state_broadcaster/JointStateBroadcaster
simple_velocity_controller:
type: velocity_controllers/JointGroupVelocityController
simple_velocity_controller:
ros__parameters:
joints:
- wheel_left_joint
- wheel_right_jointUnderstanding YAML
Install the config Folder

Now we need to make sure ROS 2 installs our YAML configuration file when the package is built.
Open:
CMakeLists.txtand add:
install(
DIRECTORY config
DESTINATION share/${PROJECT_NAME}
)Add ROS 2 Control to the Gazebo Configuration
Now we go back to the package:
bumperbot_descriptionand its:
urdf/folder.
We already have:
bumperbot_gazebo.xacroUpdate bumperbot_gazebo.xacro
Open:
bumperbot_gazebo.xacroAdd the ROS 2 Control section below the existing Gazebo wheel/caster settings:
<?xml version="1.0"?>
<robot name="bumperbot" xmlns:xacro="http://ros.org/wiki/xacro">
<!-- Wheels -->
<gazebo reference="wheel_left_link">
<mu1>1000000000000000.0</mu1>
<mu2>1000000000000000.0</mu2>
<kp>1000000000000.0</kp>
<kd>10.0</kd>
<minDepth>0.001</minDepth>
<maxVel>0.1</maxVel>
<fdir1>1 0 0</fdir1>
</gazebo>
<gazebo reference="wheel_right_link">
<mu1>1000000000000000.0</mu1>
<mu2>1000000000000000.0</mu2>
<kp>1000000000000.0</kp>
<kd>10.0</kd>
<minDepth>0.001</minDepth>
<maxVel>0.1</maxVel>
<fdir1>1 0 0</fdir1>
</gazebo>
<!-- Caster Wheels -->
<gazebo reference="caster_rear_link">
<mu1>0.1</mu1>
<mu2>0.1</mu2>
<kp>1000000.0</kp>
<kd>100.0</kd>
<minDepth>0.001</minDepth>
<maxVel>1.0</maxVel>
</gazebo>
<gazebo reference="caster_front_link">
<mu1>0.1</mu1>
<mu2>0.1</mu2>
<kp>1000000.0</kp>
<kd>100.0</kd>
<minDepth>0.001</minDepth>
<maxVel>1.0</maxVel>
</gazebo>
<!-- ROS 2 Control -->
<gazebo>
<!-- ROS 2 Humble -->
<xacro:if value="$(arg is_ignition)">
<plugin filename="ign_ros2_control-system" name="ign_ros2_control::IgnitionROS2ControlPlugin">
<parameters>$(find bumperbot_controller)/config/bumperbot_controllers.yaml</parameters>
</plugin>
</xacro:if>
<!-- ROS 2 Iron or above -->
<xacro:unless value="$(arg is_ignition)">
<plugin filename="gz_ros2_control-system" name="gz_ros2_control::GazeboSimROS2ControlPlugin">
<parameters>$(find bumperbot_controller)/config/bumperbot_controllers.yaml</parameters>
</plugin>
</xacro:unless>
</gazebo>
</robot>Write bumperbot.urdf.xacro
<?xml version="1.0"?>
<robot name="bumperbot" xmlns:xacro="http://ros.org/wiki/xacro">
<!-- Start GZ or Ignition depending on ROS 2 Version -->
<xacro:arg name="is_ignition" default="true"/>
<!-- Include Gazebo Parameters -->
<xacro:include filename="$(find bumperbot_description)/urdf/bumperbot_gazebo.xacro" />
<!-- Include ros2_control Plugins -->
<xacro:include filename="$(find bumperbot_description)/urdf/bumperbot_ros2_control.xacro" />
<link name="base_footprint"/>
<link name="base_link">
<inertial>
<origin xyz="0 0 0.04" rpy="0.0 0.25 0.3" />
<mass value="8.2573504e-01"/>
<inertia ixx="2.2124416e-02" ixy="-1.2294101e-04" ixz="3.4938785e-04"
iyy="2.1193702e-02" iyz="-5.0120904e-05"
izz="2.0064271e-02" />
</inertial>
<visual>
<origin xyz="0 0 0" rpy="0 0 0" />
<geometry>
<mesh filename="package://bumperbot_description/meshes/base_link.STL" />
</geometry>
</visual>
<collision>
<origin xyz="0 0 0" rpy="0 0 0" />
<geometry>
<mesh filename="package://bumperbot_description/meshes/base_link.STL" />
</geometry>
</collision>
</link>
<joint name="base_joint" type="fixed">
<parent link="base_footprint"/>
<child link="base_link" />
<origin xyz="0 0 0.033" rpy="0 0 0"/>
</joint>
<link name="wheel_right_link">
<inertial>
<origin xyz="0 -0.014 0" rpy="0 0 0" />
<mass value="0.0530086090966721" />
<inertia ixx="1.88176298336665E-05" ixy="-4.8444933029987E-10" ixz="1.88163708851143E-08"
iyy="3.11510738647764E-05" iyz="-6.93560663069607E-11"
izz="1.8801969558182E-05" />
</inertial>
<visual>
<origin xyz="0 0 0" rpy="1.57 0 0" />
<geometry>
<mesh filename="package://bumperbot_description/meshes/wheel_right_link.STL" />
</geometry>
</visual>
<collision>
<origin xyz="0 -0.015 0" rpy="1.57 0 0" />
<geometry>
<sphere radius="0.033"/>
</geometry>
</collision>
</link>
<joint name="wheel_right_joint" type="continuous">
<origin xyz="0 -0.0701101849418637 0" rpy="0 0 0" />
<parent link="base_link" />
<child link="wheel_right_link" />
<axis xyz="0 1 0" />
</joint>
<link
name="wheel_left_link">
<inertial>
<origin xyz="0 0.014 0" rpy="0 0 0" />
<mass value="0.0530086043217644" />
<inertia ixx="1.8792911368909E-05" ixy="3.31256392204584E-10" ixz="-1.14082001662767E-08"
iyy="3.11510738759073E-05" iyz="-3.60499245082192E-10"
izz="1.88266886146847E-05" />
</inertial>
<visual>
<origin xyz="0 0 0" rpy="-1.57 0 0" />
<geometry>
<mesh filename="package://bumperbot_description/meshes/wheel_left_link.STL" />
</geometry>
</visual>
<collision>
<origin xyz="0 0.015 0" rpy="-1.57 0 0" />
<geometry>
<sphere radius="0.033"/>
</geometry>
</collision>
</link>
<joint name="wheel_left_joint" type="continuous">
<origin xyz="0 0.0701101849418642 0" rpy="0 0 0" />
<parent link="base_link" />
<child link="wheel_left_link" />
<axis xyz="0 1 0" />
</joint>
<link name="caster_front_link">
<inertial>
<origin xyz="3.89968128948481E-07 2.40073004698083E-07 0.00219173397061506" rpy="0 0 0" />
<mass value="1.44379265037903E-06" />
<inertia ixx="7.23075276010246E-11" ixy="-6.10956883467859E-16" ixz="2.22849693525031E-15"
iyy="3.8713793085917E-11" iyz="2.93909858781952E-15"
izz="1.00967486676306E-10" />
</inertial>
<visual>
<origin xyz="0 0 0" rpy="0 0 0" />
<geometry>
<mesh filename="package://bumperbot_description/meshes/caster_front_link.STL" />
</geometry>
</visual>
<collision>
<origin xyz="0 0 0" rpy="0 0 0" />
<geometry>
<sphere radius="0.005"/>
</geometry>
</collision>
</link>
<joint name="caster_front_joint" type="fixed">
<origin xyz="0.04755 0 -0.0275" rpy="0 0 0" />
<parent link="base_link" />
<child link="caster_front_link" />
<axis xyz="0 0 0" />
</joint>
<link name="caster_rear_link">
<inertial>
<origin xyz="3.8996812895542E-07 2.40073004698083E-07 0.00219173397061509" rpy="0 0 0" />
<mass value="1.44379265037902E-06" />
<inertia ixx="7.23075276010246E-11" ixy="-6.10956872363424E-16" ixz="2.22849693527207E-15"
iyy="3.87137930859167E-11" iyz="2.939098587861E-15"
izz="1.00967486676306E-10" />
</inertial>
<visual>
<origin xyz="0 0 0" rpy="0 0 0" />
<geometry>
<mesh filename="package://bumperbot_description/meshes/caster_rear_link.STL" />
</geometry>
</visual>
<collision>
<origin xyz="0 0 0" rpy="0 0 0" />
<geometry>
<sphere radius="0.005"/>
</geometry>
</collision>
</link>
<joint name="caster_rear_joint" type="fixed">
<origin xyz="-0.04755 0 -0.0275" rpy="0 0 0" />
<parent link="base_link" />
<child link="caster_rear_link" />
<axis xyz="0 0 0" />
</joint>
</robot>Create bumperbot_ros2_control.xacro

Now create another file inside:
bumperbot_description/urdf/called:
bumperbot_ros2_control.xacroWrite bumperbot_ros2_control.xacro
<?xml version="1.0"?>
<robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="bumperbot">
<ros2_control name="RobotSystem" type="system">
<!-- ros2_control Gazebo -->
<!-- ROS 2 Humble -->
<xacro:if value="$(arg is_ignition)">
<hardware>
<plugin>ign_ros2_control/IgnitionSystem</plugin>
</hardware>
</xacro:if>
<!-- ROS 2 Iron or above -->
<xacro:unless value="$(arg is_ignition)">
<hardware>
<plugin>gz_ros2_control/GazeboSimSystem</plugin>
</hardware>
</xacro:unless>
<!-- ros2_control Common Interfaces -->
<joint name="wheel_right_joint">
<command_interface name="velocity">
<param name="min">-1</param>
<param name="max">1</param>
</command_interface>
<state_interface name="position" />
<state_interface name="velocity" />
</joint>
<joint name="wheel_left_joint">
<command_interface name="velocity">
<param name="min">-1</param>
<param name="max">1</param>
</command_interface>
<state_interface name="position" />
<state_interface name="velocity" />
</joint>
</ros2_control>
</robot>Add the Dependency to package.xml

Open:
bumperbot_description/package.xmlAdd:
<exec_depend condition="$ROS_DISTRO >= iron">gz_ros2_control</exec_depend>
<exec_depend condition="$ROS_DISTRO == humble">ign_ros2_control</exec_depend>Why Are There Two Dependencies?
Because our Xacro file can use two different plugins:
Humble
↓
ign_ros2_control
Iron and above
↓
gz_ros2_controlSo package.xml tells ROS 2:
“Depending on the ROS 2 distribution, this package needs the corresponding control package.”
This is the same idea as our earlier dependency additions.
package.xml
↓
"What ROS 2 packages does this package need?"Update the Gazebo Launch File

Now we need to update:
bumperbot_description/launch/gazebo.launch.pyThe important new part is that the launch file reads the current ROS 2 distribution.
Use:
import os
from pathlib import Path
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, IncludeLaunchDescription, SetEnvironmentVariable
from launch.substitutions import Command, LaunchConfiguration
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch_ros.actions import Node
from launch_ros.parameter_descriptions import ParameterValue
def generate_launch_description():
bumperbot_description = get_package_share_directory("bumperbot_description")
model_arg = DeclareLaunchArgument(name="model", default_value=os.path.join(
bumperbot_description, "urdf", "bumperbot.urdf.xacro"
),
description="Absolute path to robot urdf file"
)
gazebo_resource_path = SetEnvironmentVariable(
name="GZ_SIM_RESOURCE_PATH",
value=[
str(Path(bumperbot_description).parent.resolve())
]
)
ros_distro = os.environ["ROS_DISTRO"]
is_ignition = "True" if ros_distro == "humble" else "False"
robot_description = ParameterValue(Command([
"xacro ",
LaunchConfiguration("model"),
" is_ignition:=",
is_ignition
]),
value_type=str
)
robot_state_publisher_node = Node(
package="robot_state_publisher",
executable="robot_state_publisher",
parameters=[{"robot_description": robot_description,
"use_sim_time": True}]
)
gazebo = IncludeLaunchDescription(
PythonLaunchDescriptionSource([os.path.join(
get_package_share_directory("ros_gz_sim"), "launch"), "/gz_sim.launch.py"]),
launch_arguments=[
("gz_args", [" -v 4", " -r", " empty.sdf"]
)
]
)
gz_spawn_entity = Node(
package="ros_gz_sim",
executable="create",
output="screen",
arguments=["-topic", "robot_description",
"-name", "bumperbot"],
)
gz_ros2_bridge = Node(
package="ros_gz_bridge",
executable="parameter_bridge",
arguments=[
"/clock@rosgraph_msgs/msg/Clock[gz.msgs.Clock",
]
)
return LaunchDescription([
model_arg,
gazebo_resource_path,
robot_state_publisher_node,
gazebo,
gz_spawn_entity,
gz_ros2_bridge,
])Build the Workspace

We have changed multiple package files, so now rebuild the workspace.
cd bumperbot_ws
colcon buildSource the Workspace and Launch

Open another terminal and source the workspace:
. install/setup.bashThen launch the simulation:
ros2 launch bumperbot_description gazebo.launch.pyGazebo Starts

Gazebo should now start with the BumperBot.
But this time something important has changed.
We are no longer launching only the simulated robot.
We have also connected its wheel joints to ros2_control.
Check the Terminal Output

This output is actually a useful verification that our configuration has been loaded.