Using ROS from the command line - LCAS/RBT1001 GitHub Wiki

General

ROS 2 relies on the notion of "workspaces" as the locations where the code and configurations are stored. In ROS 2 you can have multiple workspaces active concurrently. ROS workspaces are created and built using the colcon tools that are callable from a terminal.

For this module, you will not learn how to create and build workspaces because we want you to focus more on the theoretical aspects of the fundamentals of robotics. You are going to use ROS 2 as a tool to implement the theory.

Important: anytime we will ask you to modify a source code, a configuration or description file in ROS, for this and future workshops, you will need to re-build the workspace. To facilitate this task, we have created a utility function in your ROS environment that does that automatically for you. To built, you simply need to run rebuild from a terminal. You will see the output of building the workspace, and possibly some warning or errors. If you see warnings, it is usually fine; if you see errors, you have done something wrong and the workspace has not been built correctly.

Nodes

Launching

To launch an executable, a node usually, the template to follow is:

ros2 run <package_name> <executable_name>

For example, try:

ros2 run turtlesim turtlesim_node

If you open the VNC window, you will see that the turtlesim window has appeared.

Screenshot from 2024-01-29 17-27-45

Now open a new terminal, without closing the previous, and launch:

ros2 run turtlesim turtle_teleop_key

Following the instructions you will be able to teleoperate the turtlebot.

Inspecting

Open a new terminal, and launch:

ros2 node list

This will show the two nodes that you have launched before. If you go back to one of the previous terminals, kill a node (Ctrl+c followed by Enter), and list the nodes again you will see that the list has been updated.

To explore all the connections that a node has within the ROS graph, you can launch:

ros2 node info <node_name>

This will list all the topics is subscribes to and publishes, and all the server/client services it has.

Topics

Visualising the topics graph

With both turtlesim_node and turtle_teleop_key running, open a new terminal and launch:

rqt_graph

Screenshot from 2024-01-29 17-41-01

It shows you the nodes currently running, the topics that connects them and the topics directions.

You can also list all the topics available with:

ros2 topic list

Echo and publish

Topics are channels of communication in which messages are exchanged. To see the stream of messages run:

ros2 topic echo <topic_name>

e.g.: ros2 topic echo /turtle1/cmd_vel.

You can publish messages with the following command:

ros2 topic pub /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}"

Note that here we also need to specify the type of the message, and we have to fill in the arguments of the message. The message itself is always a dictionary and from the terminal is provided with its string representation. The above command is launched in a loop, but you can add the argument --once to publish only one time.

Services

Similarly to publishing topic messages, you can call services with the following command:

ros2 service call /clear std_srvs/srv/Empty

In this case, the request is Empty. But if we have a different request type we need to provide the message as well. For example:

ros2 service call /spawn turtlesim/srv/Spawn "{x: 2, y: 2, theta: 0.2, name: ''}"

Other functionalities

To learn the details of a msg or srv type, you can launch:

ros2 interface show geometry_msgs/msg/Twist
⚠️ **GitHub.com Fallback** ⚠️