4. ROS2 Command-Line Tools#

Now that we have looked at a few fundamental ROS2 concepts and functionalities, we will learn about essential ROS2 command-line tools to interact with your robotics applications. These tools allow you to run nodes, inspect topics, call services, and manage parameters directly from the terminal.

4.1. ROS2 Run#

This command is used to run a node from a specific package.

Purpose: Launches a ROS2 node without needing a launch file.
Syntax: ros2 run package_name executable_name
Example: ros2 run turtlesim turtlesim_node

The above command starts the Turtlesim simulation.

4.2. ROS2 Topic#

Views, echoes, or publishes to topics.

Purpose: Inspects the data flow between nodes via topics.

4.2.1. Subcommands#

ros2 topic list: List all active topics.
ros2 topic echo topic_name: Displays messages published to a topic.
ros2 topic pub topic_name message_type message: Publishes messages to a topic.

4.2.2. Examples#

Example 1 - List Active Topics
ros2 topic list

Example 2 - Display Position Updates From Turtlesim
ros2 topic echo /turtle1/pose

4.3. ROS2 Service#

Calls or lists available services.

Purpose: Interacts with ROS2 services for specific node operations.

4.3.1. Subcommands#

ros2 service list: Lists all available services.
ros2 service call service_name service type ‘{request}’: Calls a service with a request.

Example: ros2 service call /clear std_srvs/srv/Empty

The above command clears the Turtlesim screen.

4.4. ROS2 Node#

Lists the nodes running in the system.

Purpose: Provides information about active nodes.

4.4.1. Subcommands#

ros2 node list: Lists all active nodes.
ros2 node info node_name: Displays details above a specific node.

Example: ros2 node info /turtlesim

The above command gets information about the Turtlesim node.

4.5. ROS2 Param#

Gets, sets, and lists node parameters. Keep in mind that node parameters are configuration options that allow the user to customize and control the behavior of nodes without needing to modify the code. Parameters are stored within a node and can be dynamically set, retrieved, or modified during runtime, enabling flexibility and reusability of code.

Purpose: Manages parameters for nodes dynamically.

4.5.1. Subcommands#

ros2 param list: Lists all parameters of a node.
ros 2 param get node_name parameter_name: Retrieves the value of a parameter.
ros2 param set node_name parameter_name value: Sets a parameter value.

4.5.2. Examples#

Example 1 - List Parameters for the Turtlesim Node
ros2 param list /turtlesim

Example 2 - Change the Background Color of the Turtlesim Screen
ros2 param set /turtlesim background_r 255

4.6. ROS2 Launch#

Launches multiple nodes using a launch file.

Purpose: Runs multiple nodes simultaneously and allows configuration of parameters.

4.6.1. Examples#

Example 1 - Launch the Nodes in a Package
ros2 launch package_name launch_file

Example 2 - Launch Multiple Instances of the Turtlesim Node
ros2 launch turtlesim multisim.launch.py

4.7. ROS2 Bag#

Records and replays data from topics.

Purpose: Captures data from topics for debugging and analysis, or replays data to simulate a scenario.

4.7.1. Subcommands#

ros2 bag record name_of_topics: Records data from specified topics.
ros 2 bag play bag_file: Replays data from a recorded bag file.
ros2 bag info bag_file: Provides information about a bag file.

4.7.2. Examples#

Example 1 - Record Data from /turtle1/cmd_vel
ros2 bag record /turtle1/cmd_vel

Example 2 - Replay the Data (from some named bag file)
ros2 bag play rosbag2_2025_02_06

4.8. ROS2 Action#

Interacts with ROS2 action servers.

Purpose: Inspects and calls actions in the system.

4.8.1. Subcommands#

ros2 action list: Lists all active actions.
ros2 action send_goal name_of_action type_of_action “{goal_request}”: Sends a goal request to an action.
ros2 action info name_of_action: Provides details about an action.

Example - Send a Goal to Move a Robot Arm
ros2 action send_goal /move_arm /control_msgs/action/FollowJointTrajectory “{trajectory: …}”

4.9. ROS2 Interface#

Displays information about message, service, or action interfaces.

Purpose: Helps understand the structure of ROS2 interfaces.

4.9.1. Subcommands#

ros2 interface show name_of_interface: Displays the details of an interface.
ros2 interface list: Lists all available interfaces.

Example - Show Details of a Message Type
ros2 interface show geometry_msgs/msg/Twist

4.10. ROS2 Pkg#

Manages ROS2 packages. The purpose is to provide tools to find and interact with ROS2 packages.

4.10.1. Subcommands#

ros2 pkg list: Lists all installed packages.
ros2 pkg prefix name_of_package: Displays the installation location of a package.
ros2 pkg create name_of_package: Creates a new package.

4.10.2. Examples#

Example 1 - List Installed Packages
ros2 pkg list

Example 2 - Find the Installation Location of the Turtlesim Package
ros2 pkg prefix turtlesim

4.11. ROS2 Doctor#

Provides a detailed diagnostic report of the ROS2 environment.

4.11.1. Subcommands#

ros2 doctor: Runs a basic diagnostic check on the ROS2 system.
ros2 doctor –report: Provides a detailed diagnostic report, including environment variables, installed packages, and system configurations.

4.11.2. Examples#

Example 1 - Run a Quick ROS2 System Check
ros2 doctor

Example 2 - Generate a Detailed Diagnostic Report
ros2 doctor –report

4.12. Quiz#