Search
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
pwd
mkdir dir
dir
chmod +x file.py
file.py
./file.py
pkill -f “(ros|workspace|aro_|gazebo|ruby)”
htop
sudo
terminal
gnome-terminal
konsole
xterm
nano
emacs
vim
nvim
<Tab>
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
tmux a
Inside tmux session, press $prefix (ctrl+b) followed by any of the keys below to control the session:
ctrl+b
+ d
+ c
+ w
+ n
+ p
+ ,
+ %
+ “
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
zenohd
RCL-something
rclpy
rclcpp
rclc
rclrs
.msg
.srv
.action
.idl
sensor_msgs/msg/Image
*.mcap
*.db3
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.bash
colcon build
src
install
./build.sh
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
ros2 node list
ros2 node info <node_name>
ros2 topic list
ros2 topic info <topic_name>
-v
ros2 topic hz <topic_name>
ros2 topic echo <topic_name>
--flow-style
ros2 topic type <topic_name>
ros2 topic pub <topic_name> <topic_type> <data>
<data>
\'
ros2 interface list
ros2 interface show <message_type>
ros2 interface package
ros2 launch <package_name> <launch_filename>
ros2 bag record
ros2 bag play
ros2 bag info
ros2 run <package_name> <node_name>
--symlink-install
Test the following individual commands. Use the tab for suggestions of node names, topic names, message types, etc.
ros2 run turtlesim turtlesim_node
ros2 node
ros2 topic
echo
/turtle1
ros2 interface list --only-msgs
turtlesim_msgs
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:
workspace/src/my_package/
launch/
msg/
my_package/
resource/my_package
setup.py
setup.cfg
package.xml
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.
.py
#!/usr/bin/env python3
source install/setup.bash
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).
rclpy.init()
rclpy.spin()
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.
ros2 interface package std_msgs
ros2 interface info std_msgs/Header
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).
publisher = self.create_publisher(std_msgs.msg.String, 'message', 10)
msg = String()
msg.data="ahoj"
publisher.publish(msg)
Also try to publish to topic /turtle1/cmd_vel and control the turtle in turtlesim!
/turtle1/cmd_vel
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.
subscriber = self.create_subscription(std_msgs.msg.String, 'message', callback, 10)
callback
def callback(msg):
Also try to subscribe topic /turtle1/pose and print the pose of the turtle!
/turtle1/pose
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.
workspace/src/student-packages/aro_reactive_control/aro_reactive_control/publisher.py
workspace/src/student-packages/aro_reactive_control/setup.py
workspace/build.sh
source workspace/install/setup.bash
ros2 run aro_reactive_control publisher
subscriber.py
turtle1/pose
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.