Lab 1: Introduction to ROS2 and Turtlebot4
This lab aims to introduce the basic concepts of ROS2 and its main tools. As part of the lab, you will also be introduced to the simulated environment: Turtlebot4.
Introduction
The Robot Operating System (ROS) is a flexible framework for writing robot software. It is a collection of tools, libraries, and conventions that aim to simplify the task of creating complex and robust robot behaviors across a wide variety of robotic platforms. More information can be found on the official website. It is commonly used by many robots in research and industry. ROS can transparently interact with real hardware or with a simulator.
Working on the Lab Assignments
On Campus
On IDA computers we use an Apptainer sif file where all needed software is installed. In the apptainer, ROS files can be found at /opt/ros/humble.
Personal Computer
Consult the lab assistants. There are some limitations when using Gazebo and Nvidia graphics cards: "However, note that since the libraries are bound in from the host, the libc version present in the container and on the host must not be too far apart or there will be libc mismatch errors. Errors happen when the libc in the container is older than on the host and have sometimes also been seen when the libc in the container is much newer than on the host."
ThinLinc
It is also possible to use ThinLinc to remotely access IDA's computers.
To use ThinLinc, follow the installation instructions here and connect to the IDA server: thinlinc.edu.liu.se
. More information can be found here.
ROS
ROS is a framework composed of communication middleware and a set of modules providing different functionalities for robots.
Middleware: With ROS, the various algorithms, interfaces to hardware, and simulators all run in different processes. ROS includes communication libraries for various programming languages: C++ (rclcpp) or Python (rclpy). There are libraries for more programming languages, but they are not as well supported, and it is recommended that you stick to C++ and Python for this course.
ROS programs mainly communicate through the use of topics. ROS topics follow a multi-publishers/multi-subscribers pattern: ROS programs subscribe to individual topics and they will then receive the messages that are published by other programs on the topic. Topics are associated with a specific type of a ROS message (examples of standard messages can be found at std_msgs, it is possible to create custom messages to suit your needs).
In addition to topics, ROS programs can offer service calls, which is the ROS implementation of Remote Procedure Calls (RPC).
Packaging system: ROS also provides a standard method for defining software packages, using either the CMake or setupttools build systems. This package system allows you to easily install any ROS package.
Turtlebot4
TurtleBot 4 is the next-generation of the world’s most popular open-source robotics platform for education and research, offering better computing power, better sensors, and a world-class user experience at an affordable price point.
For this course, the Turtlebot 4 is simulated, using a 2D simulator called simple simulator, which can run well in ThinLinc or the lab computers. The official project uses a 3D simulator, https://gazebosim.org/, which works poorly on the IDA's computers.
Setting up the Environment
.bashrc
file:
Setting up First, you will need to add the following to your .bashrc
file - this has to be done only once:
if [ -f /courses/TDDE05/software/bin/tdde05-setup.sh ]; then
source /courses/TDDE05/software/bin/tdde05-setup.sh
fi
To edit your .bashrc
file, you can use any editor like:
code ~/.bashrc
or
nano ~/.bashrc
ROS Domain id
Since ROS is running inside a container and ROS_LOCALHOST_ONLY is set to 1 there is no risk for conflicts with different users using the same ROS_DOMAIN_ID. So just using the default value is OK.
Starting Apptainer ROS
start_ros
Important Note!
start_ros
needs to be used to start bash
terminal with ROS setup when you open a new Terminal window. If you have logged in using ssh you need to run "bash" so that the .bashrc file is read to make the start_ros command available.
This has to be run always before any commands discussed below when using new Terminal window (as shown in the "Starting the simulator section), otherwise ROS commands will not work.
Starting the Simulator
To start the robot platform with the simple simulator, open new Terminal and run:
start_ros
ros2 launch air_bringup turtle.launch.py
If all goes well, the following image should appear:
The red dot is the robot. The black squares are obstacles, and the dots with numbers are semantic objects used in another lab assignment.
Visualization
The robot is ready to accept commands, but the visualization by the simulator is very limited. However, ROS provides a good visualization tool called RViz
. To start it, run the following command in a terminal:
rviz2
It should start with a default configuration of the tool and look like the following:
We can now customize which information is visualized in Rviz. The following are instructions for adding a visualization of the robot's position, its 3D model, and an environment map.
1. Click me - Adding TF to visualization - relevant coordinate frames!
Adding TF to visualization - relevant coordinate frames!
- Click on the
Add
button in theDisplays
panel and select theTF
visualization. - Once it is added, expand the
TF
in theDisplays
panel. - Look for the
Frames
field. Most likely too many frames will be shown. Deselect all of them (uncheckAll Enabled
). Then, select theodom
and thebase link
. - Now, rviz should show the origin of the world and the current position of the robot.
Here are the screenshots for steps 1-4:
2. Click me - Adding a 3D model of the robot to visualization!
Adding a 3D model of the robot to visualization!
- Click on the
Add
button in theDisplays
panel and select theRobotModel
visualization. - Once it is added, expand the
RobotModel
in theDisplays
panel. - Look for the
Description Topic
field and change its value to/robot_description
.
Here are the screenshots for steps 1-3:
3. Click me - Adding a map of the environment to visualization!
Adding 3D a map of the environment to visualization!
- Click on the
Add
button in theDisplays
panel and select theMap
visualization. - Once it is added, expand the
Map
inDisplays
panel. - Look for the
Topic
field and change its value to/map
.
Here are the screenshots for steps 1-3:
Sending Commands to the Robot
The robot is ready to accept commands. The first commands you can send to the robot are for docking and undocking, in a terminal:
ros2 action send_goal /dock irobot_create_msgs/action/Dock {}
ros2 action send_goal /undock irobot_create_msgs/action/Undock {}
The following action can be used to make the robot move forward with a given velocity:
ros2 action send_goal /drive_distance irobot_create_msgs/action/DriveDistance "{ distance: 2.0, max_translation_speed: 0.3 }"
The following action can be used to move the robot to a specific position:
ros2 action send_goal /navigate_to_position irobot_create_msgs/action/NavigateToPosition "{achieve_goal_heading: true,goal_pose:{pose:{position:{x: 3, y: 4,z: 0.0}, orientation:{x: 0.0,y: 0.0, z: 0.0, w: 1.0}}}}"
Managing Topics from the Command Line
The main communication medium for ROS is through the use of topics. Topics have a name and a type and follow a publisher-subscriber pattern. You can use the following command to list all available topics:
ros2 topic list
If you want detailed information about a topic, i.e. to know the type and which nodes are subscribed/publishing use the following parameters with the topic name at the end. For example:
ros2 topic info -v /cmd_vel
You can listen to a topic from the command line to get the values:
ros2 topic echo /odom
To check what is happening, run one of the commands in the previous section, and you should see the position of the robot in the terminal.
And finally, you can publish on a topic:
ros2 topic pub --rate 100 /cmd_vel geometry_msgs/msg/Twist "{ linear: { x: 0.4, y: 0.0, z: 0.0}, angular: { x: 0.0, y: 0.0, z: 0.3 } }"
After that command, you should be able to see the robot move in a circle in RViz or the simulator window. You should also be able to see the position changes when listening to /odom
topic.
RQT
An alternative approach to using the command line for listening or publishing on topics is to use a tool called RQT. You can start it from the command line simply with:
rqt
Then in the interface, from the Plugins menu, add the following:
Topics > Message Publisher
Topics > Topic Monitor
-
Robot Tools > Robot Steering
you can then set the topic to/cmd_vel
.
The window should look like this:
If you check the /odom
in the topic monitor, you can see it change. In the message publisher, you can add new topics and set the value to publish. Try to add /cmd_vel
and publish the velocity. A more convenient alternative to controlling the velocity is to use the robot's steering panel (note that the robot does not like to move backward).
You can also use rqt to plot topic values. Add a plot with Visualization > Plot
. You can then add /diffdrive_controller/cmd_vel_unstamped/linear/x
and /diffdrive_controller/cmd_vel_unstamped/angular/z
You should see something like this:
GNU Screen Tool
As you can see, we ended up starting a lot of commands from the terminal. It can be annoying to have so many windows and terminals open, a solution is to use GNU Screen, which allows to start all the processes in a single terminal window.
The easiest way to use it is to create a screen configuration file (you can call it screen.tdde05
):
# Configuration
deflogin on
autodetach off
caption always
bindkey ^w screen
bindkey ^p prev
bindkey ^n next
bindkey ^x quit
bind q quit
bindkey ^l windowlist
bindkey ^e copy
# Pre-defined tabs
screen 0
title "simple sim"
stuff "ros2 launch air_bringup turtle.launch.py\015"
screen 2
title "rviz"
stuff "rviz2\015"
screen 3
title "rqt"
stuff "rqt\015"
You can then start it from a terminal with screen -c screen.tdde05
.
The following shortkeys can be used:
-
ctrl+w
to create a new command tab -
ctrl+p
to navigate to the previous tab -
ctrl+n
to navigate to the next tab -
ctrl+x
to quit -
ctrl+l
to show the list of tabs -
ctrl+e
to scroll in the log, using up/down arrow (page up/down works as well), press \verb|ESC| to stop scrolling \end{itemize}
In the screen configuration file:
-
screen 12
indicates a new tab with number12
-
title
indicates the name of the tab (as shown in the list of tabs) -
stuff
indicates the command that is run in the tab, the\015
at the end indicates whether the command is run when starting the screen or if you have to start it manually
As an exercise, add a tab for the action of docking, undocking, and moving to a location (do not add \015
at the end of the command line to make sure you don't start those actions every time you start the screen file).
Demonstration
- Show your
screen
file - Use
rviz
andrqt
, to show how your robot responds to docking, undocking, and moving to a location.