Skip to main content

ROS Development

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 behavior across a wide variety of robotic platforms

https://www.ros.org/

ROS will give us the possibility to write and run different processes (called nodes) that communicate with each other by sending and receiving messages on named buses (called topics) or by calling remote procedures (called services). Please read the ROS/Concepts Wiki page to get a more clear overview of the concepts related to ROS.

This section will describe some basic ROS functionality that can be accomplished with stock Leo Rover.

Prerequisites

📄Connect via SSH
Learn how to establish an SSH connection with your Leo Rover and access its terminal using Putty or OpenSSH.
📄Connect to a local network and the Internet
Learn how to connect your Leo Rover to a local network and the internet to download files and forward internet to your computer.

Introspecting ROS network with command line tools

ROS comes with some command line tools that can help to introspect the current network of running nodes. Some of the available tools are as follows:

  • rosnode - printing information about currently running nodes, killing them, testing connectivity,
  • rostopic - listing and printing information about topics currently in use, printing published messages, publishing data to topics, finding a type of published messages
  • rosservice - listing and printing information about available services, calling the service with provided arguments,
  • rosmsg - displaying the fields of a specified ROS message type.

Let's try running some examples. Before that, connect to Leo Rover via SSH.

Start by reading currently running nodes:

rosnode list

You should see most of all the nodes described in the first section of this tutorial. Among them, the rosserial server node (called /serial_node in this case) "bridges" communication with the CORE2 board, so any topics it publishes or subscribes are created and used in the firmware.

Let's get more information about this node:

rosnode info /serial_node

You should see all the subscribed, published topics and services that the firmware provides. You can learn more about each topic in leo_firmware README page.

Among published topics, you should see the /battery topic. Let's read the published values using rostopic tool:

rostopic echo /firmware/battery

Now, let's look at the /cmd_vel topic. This topic is used by the firmware to receive drive commands. We can look at its type:

rostopic type /cmd_vel

You should get geometry_msgs/Twist. This is a standard message in ROS for commanding velocity controlled ground robots. We can look up the message description using rosmsg tool:

rosmsg show geometry_msgs/Twist

The description should look like this:

geometry_msgs/Vector3 linear
float64 x
float64 y
float64 z
geometry_msgs/Vector3 angular
float64 x
float64 y
float64 z

The linear field represents linear velocity (in meters per second) along x, y, z axes. angular field represents angular velocity (in radians per second) along the same axes.

info

You can read more about standard units of measure and coordinate conventions in REP103

For differential drive robots like Leo Rover, only linear.x and angular.z values are used.

We can use rostopic tool to actually command the rover to move forward, by sending messages to /cmd_vel topic:

rostopic pub -r 10 /cmd_vel geometry_msgs/Twist -- "linear: {x: 0.2}"

The rover should start moving forward with a velocity of 0.2 m/s. To stop message publishing, simply type Ctrl+C.

The -r 10 argument tells the rostopic tool to publish the message repeatedly 10 times per second instead of publishing only one message. This is necessary because the firmware implements a timeout that will stop the rover if it doesn't receive the next command after some time (half a second by default).

Using ROS client library to publish messages

ROS provides several client libraries that let you write ROS nodes in different languages. The most common ones are roscpp for C++ and rospy for Python.

Here is a simple Python node that commands the rover by publishing to /cmd_vel topic:

#!/usr/bin/env python3
import rospy
from geometry_msgs.msg import Twist

# Initialize ROS node
rospy.init_node("test_drive")

# Create ROS publisher
cmd_pub = rospy.Publisher("cmd_vel", Twist, queue_size=1)

# Write a function that drives the Rover with specified
# linear and angular speed for 2 seconds
def drive(linear, angular):
# Initialize ROS message object
twist = Twist()
twist.linear.x = linear
twist.angular.z = angular

for _ in range(20): # repeat 20 times
cmd_pub.publish(twist) # publish message
rospy.sleep(0.1) # sleep for 100ms

# Now let's actually test driving the Rover
# linear speed is in m/s and angular speed in rad/s
drive(0.2, 0.0)
drive(0.0, 0.0)
drive(-0.2, 0.0)
drive(0.0, 0.0)
drive(0.0, 1.0)
drive(0.0, 0.0)
drive(0.0, -1.0)
drive(0.0, 0.0)

Copy this script to Raspberry Pi filesystem.

info

You can paste this to new file when using a terminal. Copy the script to clipboard, then type:

cat > test_drive.py

Type Ctrl+Shift+V when using Linux terminal or Shift+Ins when using Putty. Then type Ctrl+D to end the file.

Add execute permission to the file:

chmod +x test_drive.py

And execute it by typing:

./test_drive.py

The rover should drive forward and backward, then, turn in place in left and right directions.

warning

Make sure you don't have a Web UI running at the moment as it may cause conflicts on /cmd_vel topic

Adding additional functionality to the rover

LeoOS provides an easy mechanism for adding new functionalities without building any of the base packages. The whole process of starting the ROS nodes at boot can be summarized by the following files:

  • /etc/ros/robot.launch - a launch file that starts the robot's functionality. It includes the launch file from the leo_bringup package which starts the base functionality of the rover, but also allows to add additional nodes to be started or parameters to be set on the Parameter Server.
info

A launch file is an XML file that describes a set of nodes to be stared with specified parameters. It can be interpreted with roslaunch tool.

  • /etc/ros/setup.bash - the environment setup file that sets all the environment variables necessary for the successful start of the ROS nodes. It sources the environment setup file from the target ROS distribution (by default /opt/ros/melodic/setup.bash) and sets additional environment variables used by ROS.
  • /etc/ros/urdf/robot.urdf.xacro - the URDF description (in xacro format) that is uploaded to the Parameter Server by the robot.launch file. It includes the robot's model from the leo_description package, but also allows to add additional links or joints to the model.
  • /usr/bin/leo-start - a script that starts the robot's functionality. In short, it sources the /etc/ros/setup.bash file and launches the /etc/ros/robot.launch file.
  • /usr/bin/leo-stop - a script that stops the currently running leo-start process.

On top of that, the leo systemd service starts the leo-start script when the computer boots.

Starting the functionality manually

To start the nodes manually, first, you need to stop the ones that are currently running. You can do this either by using the leo-stop script:

leo-stop

or by stopping the leo service:

sudo systemctl stop leo

If you wish to disable the service from starting at boot, you can type:

sudo systemctl disable leo

To turn the service back on, just type:

sudo systemctl enable leo

Now, to start the nodes manually, type:

leo-start

Type Ctrl+C to stop the nodes and exit the script.

Adding additional nodes to the launch file

To add additional nodes to be started, you can modify the /etc/ros/robot.launch file. Take a look at the launch file XML specification (especially the node and param tags) for reference.

Here's an example that uses node and param tags:

<param name="name_of_the_global_parameter"
value="value_of_the_parameter"/>

<node name="name_of_the_node"
pkg="name_of_the_package"
type="name_of_the_executable">

<param name="name_of_the_private_parameter"
value="value_of_the_parameter"/>
</node>

Modify it to your needs, add it to the /etc/ros/robot.launch file and restart the nodes.

If you want your additional functionality to be easily switchable, you can put these lines, embedded into <launch> tag, into a separate file (e.g. /etc/ros/function1.launch) and add these lines to the /etc/ros/robot.launch file:

/etc/ros/robot.launch
<include if="$(optenv USE_FUNCTION1 false)"
file="/etc/ros/function1.launch"/>

Then, add this line to the /etc/ros/setup.bash file:

/etc/ros/setup.bash
export USE_FUNCTION1=true

Now, you can toggle the functionality simply by changing the USE_FUNCTION1 environment variable and restarting the nodes.

Expanding the URDF model

When integrating a sensor or other device to your rover, you might sometimes want to extend the robot's URDF model to:

  • visualize the device attached to the rover in RViz
  • make the robot aware of device's collision geometry
  • provide additional reference frames (for example for the sensor readings)

You can create a separate URDF file for your attached device like this one:

/etc/ros/urdf/sensor.urdf.xacro
<?xml version="1.0"?>
<robot>
<!-- a link representing visual and collision
properties of the sensor -->
<link name="sensor_base_link">
<visual>
<origin xyz="0 0 0.05"/>
<geometry>
<box size="0.05 0.05 0.1"/>
</geometry>
<material name="red">
<color rgba="1 0 0 0.7"/>
</material>
</visual>
<collision>
<origin xyz="0 0 0.05"/>
<geometry>
<box size="0.05 0.05 0.1"/>
</geometry>
</collision>
</link>

<!-- fixed joint that attaches
the sensor to the rover's body -->
<joint name="sensor_base_joint" type="fixed">
<origin xyz="0.08 0 0"/>
<parent link="base_link"/>
<child link="sensor_base_link"/>
</joint>

<!-- reference frame for sensor readings -->
<link name="sensor_frame"/>

<!-- fixed joint that sets the origin
of the reference frame -->
<joint name="sensor_joint" type="fixed">
<origin xyz="0 0 0.06"/>
<parent link="sensor_base_link"/>
<child link="sensor_frame"/>
</joint>

</robot>

And include it in the robot's main URDF file by adding:

/etc/ros/urdf/robot.urdf.xacro
<xacro:include filename="/etc/ros/urdf/sensor.urdf.xacro"/>

Now, when you restart the nodes, a new URDF model should be uploaded to the Parameter Server and you should be able to view the new model in RViz.

RViz window showing added sensor to the model

You can use base_link as a reference frame for other links in the model. The exact position of the base_link origin is defined as the center of this mounting hole on the upper plane of the mounting plate:

Base link location shown inside RViz

The distance can be easily measured in CAD programs or even using physical measuring tools.

For more examples, you can look at these tutorials:

📄Hokuyo URG-04LX-UG01
Learn how to connect a Hokuyo LiDAR sensor to your Leo Rover for mapping, object detection, and SLAM applications.
📄Grove IMU
Integrate an IMU module with Leo Rover to add gyroscope, accelerometer and magnetometer readings. Modify URDF model and calibrate sensors.

Building additional ROS packages

ROS uses its own build system for building packages. To learn about it, read the catkin/conceptual_overview and catkin/workspaces ROS wiki pages. Here's a brief summary:

The packages are the main unit for organizing software in ROS. The current build system used to build ROS packages is catkin. Catkin packages can be built as a standalone project, but the system also provides the concept of workspaces.

When building a catkin workspace, the install targets are placed into an FHS compliant hierarchy inside the result space. A set of environment setup files allow extending your shell environment, so that you can find and use any resources that have been installed to that location.

info

The prebuilt ROS packages (installed from the repository) are placed into /opt/ros/${ROS_DISTRO} directory. To use the environment setup file, just type:

source /opt/ros/${ROS_DISTRO}/setup.bash

If you use LeoOS, this line is already added to ~/.bashrc file, so it will be automatically executed when you log into the terminal session.

The catkin build system also supports an overlay mechanism, where one workspace can extend another result space. An environment setup file from the result space of such workspace will extend your shell environment by packages from both workspaces.

The build system provides a catkin_make command for building workspaces, but we will use catkin command line tool from Python package catkin-tools as it delivers more user-friendly and robust environment for building catkin packages.

In this chapter, we will try to:

  • create workspace that extends your ROS distribution
  • add leo_robot to this workspace and build the packages
  • modify the /etc/ros/setup.bash file to use our overlay

Let's start by creating an empty workspace inside home directory on Raspberry Pi:

mkdir -p ~/ros_ws/src
cd ~/ros_ws
catkin init

We want this workspace to extend the prebuilt packages that are already installed on the system. It should be automatically done if you have already sourced the /opt/ros/${ROS_DISTRO}/setup.bash file, but we can also explicitly point out the space to extend:

catkin config --extend /opt/ros/${ROS_DISTRO}

We need to get the sources of the package to build. If the package is available as a git repository (like in our case), you can use the git clone command:

cd src
git clone https://github.com/LeoRover/leo_robot.git

Some of the packages will require installing additional dependencies to build and run them. As the leo_bringup package is already installed on the system, this step is redundant. For any other package, you can use rosdep to automatically install any dependencies:

cd ~/ros_ws
rosdep update
rosdep install --from-paths src -iy

Build the workspace:

catkin build

If everything works, a development space should be created inside the devel directory. Let's source the environment setup file inside it:

source ~/ros_ws/devel/setup.bash

Now, when you execute rospack list, you should see all of the packages installed on your system, but rospack find leo_bringup should point you to the directory on your newly created workspace.

The last step is to modify /etc/ros/setup.bash to use our overlay. Simply edit this file (e.g. with nano) by removing or commenting out the first line and adding:

/etc/ros/setup.bash
# source /opt/ros/melodic/setup.bash
source /home/pi/ros_ws/devel/setup.bash

When you start the nodes with the leo-start script, /etc/ros/setup.bash will use your overlay and the /etc/ros/robot.launch file should use the version of leo_robot that you have built in your workspace.

Connecting another computer to ROS network

ROS is designed with distributed computing in mind. The nodes make no assumption about where in the network they run. Configuring your computer to be able to communicate with ROS network will let you run nodes that interfere with Leo Rover's hardware, as well as graphical tools (like rqt or rviz) directly on your host machine.

To install ROS on your computer, you can follow this tutorial:

📄Install ROS on your computer
Learn how to install the Robot Operating System (ROS) on your computer. Step-by-step guide for beginners.

In this section, we will assume you run Ubuntu 18.04 with ROS Melodic.

First, connect your computer to the same network your rover is connected to. It can be either the Rover's Access Point (LeoRover-XXXX by default) or an external router (if you followed Connect to the Internet tutorial).

To properly communicate over the ROS network, your computer needs to be able to resolve the master.lan hostname. Open a terminal on your computer and type:

getent hosts master.lan

If you don't see any output, that means you cannot resolve the hostname.

If you are connected to Rover's Access Point, you should be able to resolve it, but if there is an issue with DNS server on the rover or you are connected through external router, add this line to the /etc/hosts file on your computer:

10.0.0.1 master.lan
warning

If you are connected through router, you need to change 10.0.0.1 to IP address of the Rover on your local network.

If everything works, you should be able to ping the rover by it's hostname. To check it, type: ping master.lan.

Now, to be connected in ROS network, you need to set some environment variables. Start by sourcing the result space you are using:

source /opt/ros/${ROS_DISTRO}/setup.bash

Specify the address of the master node:

export ROS_MASTER_URI=http://master.lan:11311

And your IP on the network:

export ROS_IP=X.X.X.X

Replace X.X.X.X with your IP address.

tip

You can check your address by typing ip address. Search for your wireless network interface and the inet keyword.

You will need these lines executed at every terminal session you want to use ROS on. To do this automatically at the start of every session, you can add this line to the ~/.bashrc file.

You should now be able to do all the things from the first section of this tutorial on your computer.

Examples of ROS use

Apart from allowing communication between different processes on Raspberry Pi, ROS will give us the possibility to remotely control the rover on your computer, as well as run graphical tools to introspect and visualize the current state of the rover. A lot of these tools are available in distribution packages in the form of rqt and rviz plugins.

Below, are some examples possible to do on the stock Leo Rover:

📄Follow ARTag
Learn how to make a Leo Rover mobile robot follow a printed ARTag. Print the tag and run the example code to get started.
📄Line follower
Discover a step-by-step guide on setting up a line follower example for the stock Leo Rover, including creating a track and installing TensorFlow Lite.
📄Object detection
Detect objects in real-time on the Leo Rover using pre-trained models, with TensorFlow Lite library. Step-by-step tutorial for stock Leo Rover.