Where am I?
Goal: listen to the robot's odometry and print its position and heading while it moves.
The idea
In the last step your node published. Now it subscribes: it tells ROS 2 "send me every message that appears on this topic".
Bumperbot counts how much each wheel has turned, and from that it works out where it is compared with where it started. This estimate is called odometry. The robot publishes it on /bumperbot_controller/odom, as an Odometry message.
A message can hold other messages. To reach the x position you write msg.pose.pose.position.x.
The direction the robot faces is stored as four numbers (x, y, z, w) called a quaternion. You do not need the theory: the bonus below turns them into an angle with a library.
Look at it live
- Run
ros2 topic list. Which topic carries the odometry? There is no plain/odom. - Run
ros2 topic info /bumperbot_controller/odomto see its message type. - Run
ros2 topic echo /bumperbot_controller/odom --onceand find the position and the orientation in the message. - Run
ros2 topic echo /bumperbot_controller/odom --field pose.pose.position, then drive with the pad. Watchxandychange. Stop with Ctrl+C.
Read the code
This node listens to what the pad sends, and prints the forward speed it asks for.
import rclpy
from rclpy.node import Node
from geometry_msgs.msg import Twist
class Listener(Node):
def __init__(self):
super().__init__('listener')
# a subscriber: message type, topic name, the callback to call, queue size
self.sub = self.create_subscription(Twist, '/key_vel', self.on_command, 10)
def on_command(self, msg): # ROS calls this for every message that arrives
self.get_logger().info(f'The pad asks for {msg.linear.x} m/s forward')
- The callback
on_commandreceives the message as its argument. Inside it,msgis aTwist, so you readmsg.linear.x. self.get_logger().info(...)prints a line in the terminal, with the time and the node name. Use it instead ofprint.- Unlike a timer, a subscriber callback runs only when a message arrives.
Your mission
Open starter_kit/where_am_i.py and complete the TODOs.
- TODO 1: create a subscriber to
/bumperbot_controller/odomusingOdometry. - TODO 2: read the position (
xandy) frommsg.pose.pose.position. - TODO 3: read the orientation from
msg.pose.pose.orientation. It is a quaternion (x,y,z,w). - TODO 4: print the position and the orientation every time a message arrives.
Bonus: convert the quaternion into a heading angle in degrees. spatialmath is a Python library for rotations, and it is already installed:
import numpy as np
from spatialmath import UnitQuaternion
q = UnitQuaternion(s=w, v=[x, y, z]) # w = scalar part, (x, y, z) = vector part
roll, pitch, yaw = q.rpy() # three rotation angles, in radians
print(np.degrees(yaw)) # Bumperbot drives on the floor: only yaw changes
Run your node with ros2 run starter_kit where_am_i. In a second terminal, run your Challenge 1 node (ros2 run starter_kit move_the_robot) so the robot drives. You can also drive with the pad.
What you should see
The terminal prints an updated position and orientation at least once per second while the robot drives. The numbers change as it moves. With the bonus, the heading changes when the robot turns.
Stuck?
Hint 1
The first argument of create_subscription is the message type, then the topic name, the callback and the queue size. ros2 topic info /bumperbot_controller/odom tells you the type.
Hint 2
A message is an object with fields. ros2 interface show nav_msgs/msg/Odometry lists them all.
Submit
When it works, open the Submit tab, choose 2 · Where Am I?, and submit.