During this lab, you should learn how to use the Apptainer, the tmux, the terminal commands of Robot Operating System (ROS), and how to program a simple python subscriber/publisher node.
Slides used in this lab are available here: https://docs.google.com/presentation/d/1rLQQllG8z78JBPfMsZ-KJiQbpIWFphqULLs1VxF0gbQ/edit?usp=sharing (they are not standalone, they only make sense with the live lab program; use Faculty google account to access them).
Apptainer is software for virtualization via containers that allows you to program and test ARO homeworks and semestral work without installing all ROS dependencies on your computer.
git clone https://gitlab.fel.cvut.cz/robolab/deploy.git ./deploy/scripts/install_apptainer ./deploy/scripts/download_apptainer_image ./deploy/scripts/start_apptainer_aro
The start_apptainer_aro script not only starts the Apptainer container, but also creates appropriate ROS workspace, clones the student packages to the workspace, builds the workspace, and sources the workspace. Details of this process are described here.
After starting the Apptainer container, try to run the simulation which will be part of your first homework using command:
# in 1st terminal window: ros2 run rmw_zenoh_cpp rmw_zenohd # keep zenohd running and call this in the 2nd terminal: ros2 launch aro_reactive_control hw01_sim.launch.xml
You will need to be able to use terminal in Linux. Here are a few tips for what you will need:
cd dir - changes current directory
pwd - prints current directory
mkdir dir - creates directory dir in current directory
chmod +x file.py - make file.py executable
./file.py - execute (run) file.py using the program mentioned on the first line of the file (the shebang)
file.py will not work!
pkill -f “(ros|workspace|aro_|gazebo|ruby)” (beware that it also kills the zenohd which you need to launch again).
htop - view list of running processes (use F9 to terminate selected process)
sudo - this is a program that gives you administrator (root) permission. If you are not eligible, you will not get them! sudo does not work inside Apptainer containers.
terminal. It will launch a terminal app like gnome-terminal, konsole, xterm or similar.
nano, emacs, vim or nvim are console text editors.
<Tab> (one or two presses) shows you autocompletion options (filenames, packages, topics…)
Tmux allows you to switch easily between several windows in one terminal., detach them (they keep running in the background) and reattach them to a different terminal. To start Tmux use:
tmux - starts a new tmux session
tmux a - attaches to an existing session
Inside tmux session, press $prefix (ctrl+b) followed by any of the keys below to control the session:
+ d - detach from the session while it keeps running
+ c - create new window
+ w - list windows with option to select one to see
+ n - next window
+ p - previous window
+ , - name window
+ % - split vertically
+ “ split horizontally
Robot Operating System (ROS) https://www.ros.org/ is a set of software libraries that allows you to build a complex robotic system. It is not an operating system but rather a very convenient middleware for sharing data (messages) between different parts of the robot's software (nodes). When using ROS you can leverage the work of others and use an already very large code base of implemented packages for, e.g., mapping, localization, control, planning, and sensor interfacing.
Key aspects/buzzwords to know are:
rmw_zenoh_cpp which is not based on DDS (and is thus a little simpler to setup). The default RMW is currently rmw_fastrtps_cpp (which is based on DDS).
zenohd is the central discovery broker. You have to have zenohd running for ROS 2 nodes to see each other, even on the same computer.
RCL-something.
rclpy.
rclcpp
rclc
rclrs
.msg files), services (.srv files) and actions (.action files).
.idl files). The ROS Interface definitions (.msg) can be automatically converted to IDL (.idl) files.
sensor_msgs/msg/Image, that the nodes use to send/receive data from.
*.mcap files). There was also an older one based on SQLite (*.db3 files) but it's not suggested anymore.
The code for your projects with ROS is stored in a workspace.
In case you would like to create your own workspace in some folder, you can do it by running the following commands in the started Apptainer container:
cd <where_you_want_the_workspace> mkdir -p new_ws/src cd new_ws # Now source the "base workspace" that your workspace extends source /opt/ros/aro/setup.bash colcon build --symlink-install source install/setup.bashThis code initializes your workspace as an extension of the default ARO workspace. Then it calls the colcon tool to build the workspace. Finally, it sources the workspace to load information about all built packages. Now you can place any of your packages into the src folder and after repeating colcon build and sourcing parts you can use them.
colcon build in the correct folder. You have to call it in the workspace folder (i.e. the folder that contains src and install. We made a convenience script for you that handles this and a few other tricky parts: call ./build.sh instead of colcon build in your ARO workspace.
ROS has many handy command line programs that you can learn. You won't use them to create the actual ROS programs (nodes), but you will be using them routinely when inspecting and debugging running ROS systems.
ros2 run rmw_zenoh_cpp rmw_zenohd - starts the zenohd central broker for rmw_zenoh_cpp. This needs to be running in some terminal all the time!
ros2 node list - shows all running nodes
ros2 node info <node_name> - gives you information about a specified node
ros2 topic list - shows all currently available topic names
ros2 topic info <topic_name> - shows information about a given topic (pass -v parameter to get more info)
ros2 topic hz <topic_name> - shows frequency of messages published on a topic
ros2 topic echo <topic_name> - prints all messages on a given topic (you usually want --flow-style)
ros2 topic type <topic_name> - shows message type of the topic
ros2 topic pub <topic_name> <topic_type> <data> - publishes a message to a topic
<data> is difficult; you should use Tab completion. After typing the topic type, type these two characters: \' and then press the Tab key (once). After that, use only left/right arrows to navigate in the completed data structure. Using up/down arrows will make it disappear!
ros2 interface list - shows all available interfaces (messages/services/actions)
ros2 interface show <message_type> - displays the definition of a given interface type
ros2 interface package - lists interfaces of a certain package
ros2 launch <package_name> <launch_filename> - launches the <launch_filename> of a given <package_name>
ros2 bag record - record a rosbag
ros2 bag play - play existing rosbag (pass the path to the folder, not the MCAP file)
ros2 bag info - gives you information what is inside a rosbag.
ros2 run <package_name> <node_name> - starts node <node_name> from package <package_name>.
colcon build - build workspace you are in (you usually want --symlink-install for local development).
Test the following individual commands. Use the tab for suggestions of node names, topic names, message types, etc.
ros2 run rmw_zenoh_cpp rmw_zenohd .
ros2 run turtlesim turtlesim_node .
ros2 node commands on /turtlesim node.
ros2 topic (including echo) commands on the listed topics in /turtle1 namespace in the same tmux window.
ros2 interface list --only-msgs and find out what messages are in turtlesim_msgs package. Show the definition of one of the message types.
ros2 topic pub /turtle1/cmd_vel geometry_msgs/msg/Twist \' # Now press Tab key to complete the message body and use left/right arrow keys to navigate in it and change values.
In ROS Kilted we will use Python 3.12. The most important library that enables interfacing with ROS is rclpy.
The typical content of a package folder in, e.g., workspace/src/my_package/ is:
launch/ - folder with launch files
msg/ - folder with message definitions
my_package/ - folder with source codes where you can put your nodes
resource/my_package - an empty file that just has to be there for ROS to work
setup.py + setup.cfg - build files defining the available Python packages and scripts (entrypoints)
package.xml - ROS file with information about the package used by the colcon
Always make sure the python node .py file starts with the 'shebang' line #!/usr/bin/env python3.
Always make sure to source install/setup.bash in your workspace after you build your workspace with colcon build or ./build.sh. You need to build the workspace if you have new package, if you change c++ code or, e.g., message definitions. Changing Python code or YAML config contents does not require rebuilding.
At the beginning of a Python script (actually usually at the bottom of the file) you need to initialize ROS with rclpy.init() command.
Afterwards you can initialize all your subscribers, publishers, threaded workers, etc.
Finally you need to run rclpy.spin() which internally runs the messaging system of the node (especially important for subscribers to run callbacks for new messages).
The actual startup code looks like this:
import rclpy from rclpy.executors import ExternalShutdownException from rclpy.node import Node class MyNode(Node): def __init__(self): super().__init__('my_node') # Continue with setting up publishers, subscribers etc. def main(): try: with rclpy.init(), MyNode() as node: rclpy.spin(node) except (KeyboardInterrupt, ExternalShutdownException): # exit nicely if the program is normally terminated pass if __name__ == '__main__': main()
You can print messages to console and log within your package with different severity levels using:
self.get_logger().info('your message')
self.get_logger().warning('your warning message')
self.get_logger().error('your error message')
Find more logging capabilities on https://docs.ros.org/en/kilted/Concepts/Intermediate/About-Logging.html#apis .
Messages are data structures used to send data over topics.
They are composed of either simple or complex data types.
Simple types are, e.g.: bool, int<N>, uint<N>, float<N> (with N being {8, 16, 32, 64}), String, Time, Duration (see standard messages ros2 interface package std_msgs).
Complex data types are composed of simple ones or their arrays (see, e.g., ros2 interface info std_msgs/Header ).
Custom messages can be defined in message files, and their respective C++ headers and Python objects are generated during build.
All released message definitions can be found in the list on index.ros.org.
Publisher is an object assigned to a specific topic and message type that you can use to send messages from your node to other nodes.
You can create publisher object using:
publisher = self.create_publisher(std_msgs.msg.String, 'message', 10).
Then every time you want to send a message you create a new message object
msg = String()
, then fill it with your data (msg.data="ahoj") and publish it to ROS using
publisher.publish(msg).
Also try to publish to topic /turtle1/cmd_vel and control the turtle in turtlesim!
Subscriber is an object that basically assigns a callback function to received messages of a given topic name and message type.
You can create the subscriber using:
subscriber = self.create_subscription(std_msgs.msg.String, 'message', callback, 10)
, where the callback is a custom function (def callback(msg):) that handles the received message.
Also try to subscribe topic /turtle1/pose and print the pose of the turtle!
Implement simple publisher and subscriber nodes as specified below. You can get inspiration on how to write such Python nodes in the following ROS tutorial. Both can be placed inside the aro_reactive_control/scripts of the student-packages.
ros2 run turtlesim turtlesim_node
workspace/src/student-packages/aro_reactive_control/aro_reactive_control/publisher.py that publishes String message from std_msgs package on topic “message” every one second and also publishes cmd_vel for the turtle.
workspace/src/student-packages/aro_reactive_control/setup.py .
workspace/build.sh (or ./build.sh if you are in the workspace directory).
source workspace/install/setup.bash
ros2 run aro_reactive_control publisher.
subscriber.py that receives the String messages and prints them in the terminal. It should also listen to turtle1/pose and print the received values.
subscriber.py to setup.py, rebuild and source the workspace.
ros2 run aro_reactive_control subscriber.py.
Follow the assignment of the homework HW01 which requires only some additional work compared to the lab task above.