Tech Arcanist
Get in touch

30Chapter 30 of 74

ROS 2 Control: Moving the BumperBot

In the previous chapter, we prepared our BumperBot for control using ros2_control.

We created:

  • The controller package
  • The controller YAML configuration
  • The ros2_control interfaces
  • The Gazebo control plugin

Now we will actually start the controllers and send velocity commands to the wheels.

By the end of this chapter, we will be able to control:

wheel_left_joint
wheel_right_joint

through ROS 2.

Open VS Code

Open the bumperbot_controller package in VS Code.

Inside the package, create a folder named:

launch

Inside the new launch folder, create:

controller.launch.py

Write the Launch File

Paste the following code into controller.launch.py:

import os
from launch import LaunchDescription
from launch_ros.actions import Node


def generate_launch_description():

    joint_state_broadcaster_spawner = Node(
        package="controller_manager",
        executable="spawner",
        arguments=[
            "joint_state_broadcaster",
            "--controller-manager",
            "/controller_manager",
        ],
    )

    simple_controller = Node(
        package="controller_manager",
        executable="spawner",
        arguments=["simple_velocity_controller",
                "--controller-manager",
                "/controller_manager"
        ]
    )

    return LaunchDescription(
        [
            joint_state_broadcaster_spawner,
            simple_controller,
        ]
    )

Explanation

Visualizer

Add the Launch Folder to CMakeLists.txt

Open:

bumperbot_controller/CMakeLists.txt

We previously installed the config directory.

Now we also need to install the launch directory.

Change the install section to:

install(
  DIRECTORY config launch
  DESTINATION share/${PROJECT_NAME}
)

Add Dependencies to package.xml

Open:

bumperbot_controller/package.xml

Add:

  <exec_depend>ros2launch</exec_depend>
  <exec_depend>controller_manager</exec_depend>

Build the Workspace

Open a terminal and go to the workspace. Then build:

cd bumperbot_ws
colcon build

Start the Gazebo Simulation

Before starting the controllers, Gazebo and the robot's control system need to be running.

Open another terminal.

Source the workspace and start the Gazebo simulation:

. install/setup.bash
ros2 launch bumperbot_description gazebo.launch.py

At this point, the Gazebo simulation should be running with the BumperBot.

Start the Controllers

Now open another terminal.

Source the workspace
Then launch the controller launch file:

. install/setup.bash
ros2 launch bumperbot_controller controller.launch.py

Check the Controller Terminal

The important result is:

joint_state_broadcaster
        ↓
Configured and activated

simple_velocity_controller
        ↓
Configured and activated

So our controllers are now ready.

Check the Gazebo Terminal

This confirms that the controller manager has successfully:

Loaded
   ↓
Configured
   ↓
Activated

our velocity controller.

Check What ROS 2 Control Provides

Now that our controllers are running, we can inspect the control system from the terminal.

Open another terminal.

Source the workspace
Then type:

ros2 control

Press Tab twice.

You will see the commands available under:

ros2 control

This is a good way to discover the ROS 2 Control command-line interface instead of memorizing every command.

List the Controllers

Run:

ros2 control list_controllers

This shows the controllers currently known to the controller manager.

List the Hardware Components

Run:

ros2 control list_hardware_components

This lets us inspect the hardware components registered with ROS 2 Control.

For our simulated robot, this is connected to the simulated hardware system that was configured earlier.

List the Hardware Interfaces

Run:

ros2 control list_hardware_interfaces

This is especially useful for our example because we want to understand what can be commanded and what state can be read.

Find the Controller's Command Topic

Run:

ros2 topic list

Among the topics, you should see:

/simple_velocity_controller/commands

This is the topic through which our velocity controller receives commands.

So the flow is now:

ROS 2 command
      ↓
/simple_velocity_controller/commands
      ↓
simple_velocity_controller
      ↓
wheel_left_joint
wheel_right_joint
      ↓
Gazebo

Inspect the Command Topic

Run:

ros2 topic pub /simple_velocity_controller/commands

Press Tab twice to see the available message types.

For our controller, we use:

std_msgs/msg/Float64MultiArray

So the command becomes:

ros2 topic pub /simple_velocity_controller/commands std_msgs/msg/Float64MultiArray

Then leave a space and start typing:

'l

Press Tab twice.

The terminal can autocomplete the message structure:

'layout:
dim: []
data_offset: 0
data: []

So we can provide the velocity values inside:

data: []

Send [1, 0]

Set:

data: [1, 0]

Here the two values correspond to:

[1, 0]
 │  │
 │  └── right wheel = 0
 └───── left wheel  = 1

So the left wheel receives a velocity command of 1, while the right wheel receives 0.

The left wheel rotates while the right wheel remains at zero velocity. This causes the robot to turn rather than move straight.

Make the Wheels Rotate in Opposite Directions

Now try:

data: [1, -1]

This means:

left wheel  → +1
right wheel → -1

So the two wheels rotate in opposite directions. According to this test, this produces a stronger rotational movement of the robot.