Skip to content

Practical 0 (Completed)

Adrian Vecina edited this page Oct 8, 2026 · 1 revision

Practical 0 is all about getting comfortable with ROS2. This practical is not assessed, and is only for the first lab session. You don't need to save/upload this code, but it may be useful as reference.

Before you begin

Bring up the Docker container on the B85 lab machine. follow ROS2 Basics if you need a reminder, or check the readme in the ~/COMP4034/src folder on the lab machine

Goals

By the end of this practical, you should be able to:

  • Understand basic ROS2 CLI commands like ros2 run, ros2 node, ros2 topic
  • Implement a basic subscriber/publisher
  • Understand how this forms a basis for ROS2

Writing a simple publisher and subscriber

For this session, we will use the excellent ROS2 Humble Hawksbill documentation and tutorials. As a level 4 module, you are expected to browse, understand, and use this (and other) sources of documentation to help you implement your later practicals.

Note

You will see reference to "ros2 workspace". We have already set this up for you! Your workspace is ros2_ws, and ALL code (packages) need to be in ros2_ws/src.

Make sure the docker container is running, and you are in a sourced terminal.

Basic Publisher and Subscriber

Please work through this tutorial Creating a subcriber+publisher. Make sure you always follow the linux and python options.

Don't rush through the content, please read the code and descriptions in detail for your own understanding.

ROS2 CLI

ROS2 comes with a set of useful CLI tools you can use to debug and test parts of any ROS2 system. Keep your publisher and subscriber from above running, then open and source a new terminal.

ros2 run <package_name> <executable_name> [arguments]

You've already used this one! In ROS2, all code is organized into packages, not dissimilar from python libraries. To run a ros2 python file, it needs to be added as an entry point in the packages setup.py file.

Note

Typically, by convention, each executable python file in a package contains a single Node which is ran in the main() function.

In the tutorial, you added the entry point talker = py_pubsub.publisher_member_function:main All this means is that you've named your executable talker, and when ran, it finds the file at py_pubsub/publisher_member_function.py and then runs the main() function.

Hence, when we want to run a ROS2 script, we must provide the package, and executable name. It will rapidly become annoying to need to ros2 run in multiple terminals for each node. Later we will see ros2 launch which enables us to run multiple at once.

ros2 node

This set of CLI tools enables us to get some information on the Nodes in an active ROS2 system.

In your new terminal, run ros2 node list. This will give you a list of the currently running nodes. You should see your /minimal_publisher and /minimal_subscriber (if not, make sure your two nodes are running).

❓ Where in the python code is this name set?

The next useful command is ros2 node info <node_name>. In the same terminal, run ros2 node info /minimal_publisher. This command gives us some important details about a running node, including what topics it subcribes/publishes. This is a useful command to check if your nodes are talking to eachother!

ros2 topic

This will be the most useful CLI command for you to understand. All data in ROS2 moves as topics, and being able to introspect topics becomes an important skill for this module.

Firstly, ros2 topic list gives you a list of all currently active topics. You can also use ros2 topic list -t to also see the message types for each topic. ros2 topic info <topic_name> tells us how many nodes are subscribed or publishing to a topic. Running ros2 topic info <topic_name> --verbose will also give you data about those nodes.

ros2 topic echo <topic_name> turns your terminal into a mini-subscriber, and outputs everything on that topic for you to see. Please run ros2 topic echo /topic, you should see what your subscriber is publishing. we encourage you to run this command on any topic you need to understand or debug

We can use ros2 topic pub <topic_name> <message_type> <message> to publish to a topic from a terminal. Find the terminal running your publisher, and run ctrl+C to kill it. Your subscriber should no longer be receiving any data. Now, in the same terminal, run ros2 topic pub /topic std_msgs/msg/String "{data: 'hello world'}" you should see this repeatedly send to your subcriber. (you can use --rate N or --once to change the behaviour)

ros2 interface

Topics in ROS2 have predefined message structures, and being able to understand what these messages look like is important. For this you can use ros2 interface show <message_type. In your case you can try ros2 interface show std_msgs/msg/String. This might be useful later when controlling the robot.


That's all! If you still have time, feel free to start checking out Practical 1.


Resources

Clone this wiki locally