Move the robot
Goal: make Bumperbot move by publishing speed commands from your own Python node.
The idea
To make the robot move, your program publishes messages on the /cmd_vel topic. The message type for speeds is called Twist. It has two parts you will use:
linear.x: the forward speed, in meters per second.angular.z: the turning speed, in radians per second. Positive turns left.
Two ways in
The pad publishes on /key_vel. Your programs publish on /cmd_vel. If both send speeds at the same time, the pad wins. So do not touch the pad while your node drives.
Look at it live
- Run
ros2 topic echo /key_vel, then drive with the pad. Each press prints aTwistmessage. Stop with Ctrl+C. - Run
ros2 topic info /cmd_vel. It tells you the message type. - Run
ros2 interface show geometry_msgs/msg/Twist. It lists every field of the message. - Send speeds by hand for two seconds:
ros2 topic pub --rate 10 --times 20 /cmd_vel geometry_msgs/msg/Twist "{linear: {x: 0.2}}". The robot drives about 0.4 m. Your node will do the same from Python.
Read the code
This node makes Bumperbot turn on the spot. Read it, then read the notes below it.
import rclpy
from rclpy.node import Node
from geometry_msgs.msg import Twist # the message type for speeds
class Spinner(Node): # a node is a class that extends Node
def __init__(self):
super().__init__('spinner') # the name of the node
# a publisher: puts Twist messages on /cmd_vel (10 = how many may wait in line)
self.cmd_vel_pub = self.create_publisher(Twist, '/cmd_vel', 10)
# a timer: calls self.timer_callback 10 times per second
self.timer = self.create_timer(0.1, self.timer_callback)
def timer_callback(self):
msg = Twist() # every field starts at 0
msg.angular.z = 0.5 # turn left, slowly
self.cmd_vel_pub.publish(msg) # send it
def main():
rclpy.init() # start ROS 2 in this program
node = Spinner()
rclpy.spin(node) # keep the node alive and waiting
node.destroy_node()
rclpy.shutdown()
A callback is a function you write and ROS calls for you when something happens. Here, the event is a timer tick.
- The publisher is the channel you open on
/cmd_vel. You give it the message type and the topic name. - The timer calls
timer_callbackten times per second. The robot needs a new command again and again: this simulation stops the robot half a second after the last one. rclpy.spin(node)keeps the program running. It waits, calls your callbacks when something happens, and waits again.
Your mission
Open starter_kit/move_the_robot.py and complete the TODOs. The structure is already there.
Part A: drive forward
- TODO 1: create a publisher on
/cmd_velusingTwist. - TODO 2: set a small forward speed (for example
0.15). - TODO 4: publish the message.
Part B: drive in a curve
- TODO 3: also set
angular.z, so the robot turns while it moves forward.
Part C: the Ctrl+C problem
- While the robot drives, press Ctrl+C to stop your node. Watch the 3D view, and run
ros2 topic echo /cmd_velin a second terminal (press Alt+E to open one). - Your node stopped publishing, but did it ever tell the robot to stop?
- TODO 5: fix it by publishing one zero
Twistright before your node shuts down.
Why this matters
ROS 2 has no built-in safety net: nothing stops the robot when the node that sent the last command exits. This simulation has a 0.5 s watchdog that catches you after a short coast. Many real robots do not, so always publish a zero command on shutdown.
Run your node from a terminal with ros2 run starter_kit move_the_robot.
What you should see
The robot drives forward in the 3D view, and after Part B it drives in a circle. Before the fix, ros2 topic echo /cmd_vel just stops when you press Ctrl+C. After the fix, it shows a final zero Twist the moment you press Ctrl+C.
Stuck?
Hint 1
The timer calls timer_callback() ten times per second. A message is only sent when you publish it.
Hint 2
Look at the type of the topic with ros2 topic info /cmd_vel.
Submit
When it works, open the Submit tab, choose 1 · Move the Robot, and submit.