0%

511 ROS Programming from the Beginning

Learn ROS by Example using the little GoPiGo3

This is the first of a serie of articles part of ROS GoPiGo3 Wiki for people who want to learn state-of-the-art robotics with a physical robot. Since we are starting from scratch we wouldn’t like you had to deal with hardware complexities at this point.
Hence, the approach is let’s make the robot work in the simulation, then reproduce the exercise with the actual hardware.

NOTE 1
This article extends the concepts explained in my book Hands-On ROS for Robotics Programming. You may get your copy in electronic and/or paperback versions at Packt Publishing or Amazon.com. The book gently guides you through process of learning ROS programming from scratch, using the affordable GoPiGo3 robot.
image20

Let’s get started explaining the conceptual approach of how ROS framework understands robotics. ROS builds the structure of a robot under the skeleton of a graph. Let’s dive into this concept in detail.

The ROS graph

A graph is a simple mathematical artifact that consists of a set of nodes and edges connecting them, where:

  • nodes represent entities, and
  • edges stand for relationships between nodes

For example, you could represent your family with a graph, where the nodes are the persons and the edges are the relationships between them (X is the mother of Y, Z is the brother of T, etc.).
As well as you can describe order in nature with graphs, they can also describe chaotic scenarios like the case of an online chat room where a lof of people are interacting during a YouTube live event posting comments about it.

Hence, graphs have a lot of expressive power and they stand for a very general way of describing the reality. They intrinsically do not place any restriction of what can or cannot be represented. It is up to the developer the way to describe what he needs to model.

A robot evolving in a physical environment can face many different situations, most of them will be unexpected as the environment is intrinsically chaotic. This is the reason why ROS framework chose to abstract robots under a graph pattern. When using a graph to program robots with ROS:

  • nodes represent code snippets written in C++, Python, Node or Java, while
  • Edges stand for exchange of message between such code blocks

That simple, that complex… Because ROS does not place any restriction regarding how to program a robot. The developer is free to take every design decision. Let’s illustrate this point with an example ROS graph like the one below:

image511-2

NOTE 2
Whether you want to reproduce this graph, you have to:
1. Issue the command $ roslaunch gopigo3_description gopigo3_rviz.launch after setting up the software as explained in the next wiki article. This launches the ROS Visualization Tool rviz window as shown shown below
2. From another terminal launch the graph visualization with the command $ rqt_graph

Elliptical shapes stand for ROS nodes, while rectangular shapes represent ROS topics. The robot visualization behind this graph is shown in the following image:
image511-1

This window shows the user interface of the ROS visualization tool, called rviz. Let’s explore now ROS nodes and topics in the graph.

ROS Nodes

From a bash terminal we can list the nodes in the ROS graph as follows:

1
2
3
4
5
$ rosnode list
/joint_state_publisher
/robot_state_publisher
/rosout
/rviz

NOTE: We suggest you practice the these commands only after completing the practical part of the next wiki article

There are four nodes, but find that /rosout is not shown in the graph. This node is part of the roscore process- the first node in every ROS graph- and is known as the ROS master because it allows every node to communicate wih each other, as well as keeping updated the ROS parameter server (at point it is enough that you know that the parameter server is the robot configuration stored in variables, i.e. parameters).
To get detailed information of a node, you should use the rosnode info command followed by the node name:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ rosnode info /rviz
----------------------------------------------------------
Node [/rviz]
Publications:
* /clicked_point [geometry_msgs/PointStamped]
* /initialpose [geometry_msgs/PoseWithCovarianceStamped]
* /move_base_simple/goal [geometry_msgs/PoseStamped]
* /rosout [rosgraph_msgs/Log]

Subscriptions:
* /tf [tf2_msgs/TFMessage]
* /tf_static [tf2_msgs/TFMessage]
...

rviz node contains the ROS visualization tool. Do you rememeber that ROS nodes are blocks/ snippets of code? They may contain a single Python function up to a full application like this case of rviz.

NOTE: The output of rosnode info is more verbose, but we focus here only on what has to do with ROS topics

Two kind of topics are provide in the command output:

  • Publishers: These represent information generated in the node that is made available to other nodes via ROS topics. We will review each on them in the next section ROS topics.
  • Subscribers: These represent incoming topics, i.e. messages published by other nodes that are input data to rviz node for further processing.

The last section of this article is devoted to ROS topics, in particular to make sense of the physical meaning of publishers of rviz node.

ROS Topics

You can get the list of current topics by using the following command:

1
2
3
4
5
6
7
8
9
$ rostopic list
/clicked_point
/initialpose
/joint_states
/move_base_simple/goal
/rosout
/rosout_agg
/tf
/tf_static

You can check that within the list appear the four publishers of rviz node. Let’s review each one of them.

Topic /clicked_point

A message is published in thi topic every time you press the Publish point button (find it in the top bar of the RViz window), and then click anywhere on the robot model. The result is that the coordinates of the picked point will be published in that topic.
You can get detailed information of the topic using the rostopic info command followed by the topic name:

1
2
3
4
5
6
7
$ rostopic info /clicked_point 
Type: geometry_msgs/PointStamped

Publishers:
* /rviz (http://laptop:34525/)

Subscribers: None

The output tells you the type of message, geometry_msgs/PointStamped in this case, which node are publishing in this topic (only rviz), and which node are subscribed to it.

Topic /initialpose

This topic is related to the 2D Pose Estimate button of the RViz window. You will use it, for example, when you need to tell ROS the exact position of the robot in a map to perform robot navigation. It not specified, GoPiGo3 is placed in (0,0,0) coordinates in base_footprint, the ground reference frame.
When you press the button and then click a point in RViz, its coordinates will be published in the topic.

Topic /move_base_simple/goal

This topic is related to the 2D Nav Goal button of the RViz window. It is the counterpart of /initialpose (2D Pose Estimate button).
It allows to set the goal pose of the navigation task. In particular it is used in the ROS Navigation Stack.
When you press the button and then click a point in RViz, its coordinates will be published in the topic.

Topic /rosout

It provides the log from RViz, as well as any other node doing something. It is the ROS log system provided by the ROS master node rosout.
For example, after pressing the 2D Pose Estimate button and clicking a point in RViz, a message with its coordinates will be published in the topic /clicked_point as well as a log message in /rosout.
The way to check this is having two open terminals, each one with one of the next commands:

  • T1 $ rostopic echo /initialpose, whose output will be:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    header: 
    seq: 3
    stamp:
    secs: 1592649878
    nsecs: 344120173
    frame_id: "base_footprint"
    pose:
    pose:
    position:
    x: -0.105835594237
    y: -0.284892231226
    z: 0.0
    orientation:
    x: 0.0
    y: 0.0
    z: 0.0
    w: 1.0
    covariance: [0.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.06853891945200942]
    ---

    The characters --- separate consecutive messages.

  • T2 $ rostopic echo /rosout, whose output will be:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    T2 $ rostopic echo /rosout
    header:
    seq: 5
    stamp:
    secs: 1592649878
    nsecs: 344211616
    frame_id: ''
    level: 2
    name: "/rviz"
    msg: "Setting pose: -0,106 -0,285 0,000 [frame=base_footprint]"
    file: "/tmp/binarydeb/ros-kinetic-rviz-1.12.17/src/rviz/default_plugin/tools/initial_pose_tool.cpp"
    function: "InitialPoseTool::onPoseSet"
    line: 79
    topics: [/rosout, /initialpose, /move_base_simple/goal, /clicked_point]
    ---

You can check that the output of the first command T1:

1
2
3
4
5
pose: 
position:
x: -0.105835594237
y: -0.284892231226
z: 0.0

is logged at /rosout as:

1
msg: "Setting pose: -0,106 -0,285 0,000 [frame=base_footprint]"

as part of the output of the command T2.

What’s coming next

The next article of this Wiki will show you how you can simulate the motion of GoPiGo3 in RViz using a new topic called cmd_vel. Every message you publish in that topic will be a command motion to be executed by the robot.