Basic Tools

In this chapter, you will learn about the common command tools of ROS2.

Topics

ROS 2 breaks down complex systems into many modular nodes. Topics are an important element of the ROS graph, acting as a bus for nodes to exchange messages. Topics are one of the main ways data moves between nodes, and therefore between different parts of the system.

Specific reference: Official tutorial

  • topics help
ros2 topics -h
  • Start turtlesim and keyboard control
ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key
  • Node relationship graph
rqt_graph
  • Understand topics related commands
ros2 topics -h
  • Topic list
ros2 topic list
ros2 topic list -t # Display the corresponding message type
  • View topic content
ros2 topic echo <topic_name>
ros2 topic echo /turtle1/cmd_vel
  • Display topic related information, type
ros2 topic info <topic_name>
# Output /turtle1/cmd_vel topic interface related information
ros2 topic info /turtle1/cmd_vel
  • Display interface related information
ros2 interface show <msg_type>
# Output geometry_msgs/msg/Twist interface related information
ros2 interface show geometry_msgs/msg/Twist
  • Publish command
ros2 topic pub <topic_name> <msg_type> '<args>'
# Publish speed command
ros2 topic pub --once /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}}"
# Publish speed commands at a certain frequency
ros2 topic pub --rate 1 /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}}"
  • View the frequency of topic publishing
ros2 topic hz <topic_name>
#Output/turtle1/cmd_vel publishing frequency
ros2 topic pub --rate 1 /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}}"

Nodes

Each node in ROS should be responsible for a single module purpose (e.g., one node for controlling wheel motors, one node for controlling laser rangefinders, etc.). Each node can send and receive data to other nodes through topics, services, actions, or parameters. A complete robotic system consists of many nodes working together. In ROS 2, a single executable file (C++ program, Python program, etc.) can contain one or more nodes.

Specific reference: Official tutorial

  • nodes help
ros2 nodes -h
  • Start turtlesim and keyboard control
ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key
  • View the node list
ros2 node list
  • View the node relationship graph
rqt_graph
  • Remap
ros2 run turtlesim turtlesim_node --ros-args --remap __node:=my_turtle
ros2 node list
  • View node information
ros2 node info <node_name>
ros2 node info /my_turtle

Services

Services are another way for nodes in the ROS graph to communicate. Services are based on a call and response model, rather than the publisher-subscriber model of topics. While topics allow nodes to subscribe to a stream of data and get continuous updates, services only provide data when specifically called by a client.

Specific reference: Official tutorial

  • services help
ros2 service -h
  • Start turtlesim and keyboard control
ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key
  • View the service list
ros2 service list
# Display service list and message type
ros2 service list -t
  • View the message type received by the service
ros2 service type <service_name>
ros2 service type /clear
  • Find services that use a certain message type
ros2 service find <type_name>
ros2 service find std_srvs/srv/Empty
  • View service message type definition
ros2 interface show <type_name>.srv
ros2 interface show std_srvs/srv/Empty.srv
  • Call service command, clear walking track
ros2 service call <service_name> <service_type>
ros2 service call /clear std_srvs/srv/Empty
  • Generate a new turtle
ros2 service call /spawn turtlesim/srv/Spawn "{x: 2, y: 2, theta: 0.2, name: 'turtle2'}"

Parameters

Parameters are configuration values ​​for a node. You can think of parameters as node settings. Nodes can store parameters as integers, floating point numbers, Boolean values, strings, and lists. In ROS 2, each node maintains its own parameters. For more background on parameters, see the concepts documentation.

Specific reference: Official tutorial

  • parameters help
ros2 param -h
  • Start turtlesim and keyboard control
ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key
  • View service list
ros2 param list
  • Get parameter value
ros2 param get <node_name> <parameter_name>
ros2 param get /turtlesim background_g
  • Set parameter value
ros2 param set <node_name> <parameter_name> <value>
ros2 param set /turtlesim background_r 150
  • Export parameter values
ros2 param dump <node_name>
ros2 param dump /turtlesim
  • Import parameters independently
ros2 param load <node_name> <parameter_file>
ros2 param load /turtlesim ./turtlesim.yaml
  • Start node and import parameters
ros2 run <package_name> <executable_name> --ros-args --params-file <file_name>
ros2 run turtlesim turtlesim_node --ros-args --params-file ./turtlesim.yaml

Actions

Actions are a type of communication in ROS 2 used for long-running tasks. They consist of three parts: a goal, a response, and a result.

Actions are based on topics and services. They function like services, except that actions are preemptible (you can cancel them while they are executing). They also provide steady feedback, rather than services that return a single response.

Actions use a client-server model, similar to the publisher-subscriber model (described in the topic tutorial). An "action client" node sends a target to an "action server" node, which confirms the target and returns a stream of feedback and results.

Specific reference: Official tutorial

  • action help
ros2 action -h
  • Start turtlesim and keyboard control
ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key

Press G|B|V|C|D|E|R|T to rotate, press F to cancel

  • View the server and client of the node action
ros2 node info /turtlesim
  • View the action list
ros2 action list
ros2 action list -t # DisplayAction type
  • View action information
ros2 action info <action>
ros2 action info /turtle1/rotate_absolute
  • View action message content
ros2 interface show turtlesim/action/RotateAbsolute
  • Send action target information
ros2 action send_goal <action_name> <action_type>
ros2 action send_goal /turtle1/rotate_absolute turtlesim/action/RotateAbsolute "{theta: 1.57}"
# With feedback information
ros2 action send_goal /turtle1/rotate_absolute turtlesim/action/RotateAbsolute "{theta: 0}" --feedback

RQt

RQt is a graphical user interface framework that implements various tools and interfaces in the form of plugins. You can run all your existing GUI tools as dockable windows in RQt! The tools can still be run in the traditional standalone way, but RQt makes it easier to manage all the different windows in a single screen layout.

Specific reference: Official Tutorial

You can easily run any RQt tool/plugin by:

rqt
  • rqt help
rqt -h
  • Start turtlesim and keyboard control
ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key
  • Action browser: / Plugins -> Actions ->Action Type Browser

  • Parameter reconfiguration: / Plugins -> configuration ->Parameter Reconfigure

  • Node graph: /Node Graph

  • Control steering: /Plugins -> Robot Tools -> Robot Steering

  • Service call: /Plugins -> Services -> Service Caller

  • Service type browser: Plugins -> Services -> Service Type Browser

  • Message publishing: Plugins -> Topics -> Message Publisher

  • Message type browser: Plugins -> Topics -> Message Type Browser

  • Topic list: Plugins -> Topics -> Topic Monitor

  • Draw a curve: Plugins -> Visualization -> Plot

  • View log: rqt_console

ros2 run rqt_console rqt_console
ros2 run turtlesim turtlesim_node
ros2 topic pub -r 1 /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: 0.0}}"

TF2

tf2 It is a transformation library that allows users to track multiple coordinate systems over time. tf2 maintains the relationship between coordinate systems in a time-buffered tree structure and lets users transform points, vectors, etc. between any two coordinate systems at any desired time point.

Specific reference: Official Tutorial

Let's start by installing the demo package and its dependencies.

sudo apt-get install ros-foxy-turtle-tf2-py ros-foxy-tf2-tools ros-foxy-tf-transformations
  • Follow

  • launch starts two turtles, the first turtle automatically follows the second

ros2 launch turtle_tf2_py turtle_tf2_demo.launch.py
  • Control the movement of the first turtle through the keyboard
ros2 run turtlesim turtle_teleop_key
  • View TF tree
ros2 run tf2_tools view_frames.py
evince frames.pdf
  • View the relationship between the two coordinate systems
ros2 run tf2_ros tf2_echo [reference_frame] [target_frame]
ros2 run tf2_ros tf2_echo turtle2 turtle1
  • View TF relations on rviz
ros2 run rviz2 rviz2 -d $(ros2 pkg prefix --share turtle_tf2_py)/rviz/turtle_rviz.rviz

URDF

URDF is the Unified Robot Description Format, which is used to specify robot geometry and organization in ROS.

Specific reference: Official tutorial

  • Full syntax
<robot>
    # describe:
    # Parameters: name=""
    #  Child node:
        <link>
            # Description:
            # Parameters:name=""
            # Child node:
                <visual>
                    # describe:
                    # Parameters:
                    # child nodes: 
                        <geometry>
                            # description
                            # parameters
                            # Child node: 
                                <cylinder />
                                    # Description:
                                    # Parameters:
                                        # length="0.6"
                                        # radius="0.2"
                                <box />
                                    # description
                                    # Parameters:size="0.6 0.1 0.2"
                                <mesh />
                                    #  Description
                                    #Parameters: filename="package://urdf_tutorial/meshes/l_finger_tip.dae"
                        <collision>
                            # Description: collision element, prioritized
                            # parameters
                            # child node 
                                <geometry>
                        <inertial>
                            # description
                            # parameters
                            # Child nodes: 
                                <mass />
                                    # description: mass
                                    # Parameters: value=10
                                <inertia />
                                    # Description: Inertia
                                    # Parameters: i+"Cartesian product of xyz" (9 in total)="0.4"
                        <origin />
                            # Description:
                            # Parameters:
                                # rpy="0 1.5 0"
                                # xyz="0 0 -0.3"
                        <material />
                            # Description
                            # Parameters:name="blue"
        <joint>
            # Description
            # Parameters:
                # name=""
                # type=""
                    # fixed
                    # prismatic
            # child node
                <parent />
                    # Description
                    # Parameters:link=""
                <child />
                    # Description:
                    # Parameters:link=""
                <origin />
                    # Description:
                    # Parameters:xyz="0 -0.2 0.25"
                <limit />
                    # Description
                    # Parameters:
                        # effort="1000.0"    maximum effort
                        # lower="-0.38"      Joint upper limit (radians)
                        # upper="0"          Joint lower limit (radians)
                        # velocity="0.5"     Maximum velocity 
                <axis />
                    # Description: Press ? axis rotation
                    # Parameters:xyz="0 0 1",along the Z axis
        <material>
            # Description:
            # Parameters:name="blue"
            # child node:
                <color />
                    # description:
                    # Parameters:rgba="0 0 0.8 1"
  • Download source code
cd ~/dev_ws
git clone - b ros2 https://github.com/ros/urdf_tutorial.git src/urdf_tutorial
  • Compile source code
colcon build --packages-select urdf_tutorial
  • Run the example
ros2 launch urdf_tutorial display.launch.py ​​model:=urdf/01-myfirst.urdf

Launch

The launch system in ROS 2 is responsible for helping users describe the configuration of their system and then executing it accordingly.The configuration of the system consists of the programs to run, where to run them, the arguments to pass to them, and ROS-specific conventions that make it easy to reuse components throughout the system by providing different configurations for each component. It is also responsible for monitoring the status of launched processes and reporting and/or responding to changes in the status of those processes.

Launch files written in Python, XML, or YAML can start and stop different nodes, as well as trigger and handle various events.

Specific reference: Official Tutorial

Setup

Create a new directory to store your launch file:

` ``bash mkdir launch

from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
    return LaunchDescription([
        Node(
            package='turtlesim',
            namespace='turtlesim1',
            executable='turtlesim_node',
            name='sim'
        ),
        Node(
            package='turtlesim',
            namespace='turtlesim2',
            executable='turtlesim_node',
            name='sim'
        ),
        Node(
            package='turtlesim',
            executable='mimic',
            name='mimic',
            remappings=[
                ('/input/pose', '/turtlesim1/turtle1/pose'),
                ('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel'),
            ]
        )
    ])

Run the ros2 launch file

To run the launch file created above, go to the directory you created earlier and run the following command:

The syntax format is:

ros2 launch <package_name> <launch_file_name>

cd launch
ros2 launch turtlesim_mimic_launch.py
  • launch help
ros2 launch -h
  • Run Node
ros2 launch turtlesim multisim.launch.py
  • Check the parameters of the launch file
ros2 launch turtlebot3_fake_node turtlebot3_fake_node.launch.py ​​-s
ros2 launch turtlebot3_fake_node turtlebot3_fake_node.launch. py --show-arguments
ros2 launch turtlebot3_bringup robot.launch.launch.py ​​-s
  • Run launch file with parameters
ros2 launch turtlebot3_bringup robot.launch.launch.py ​​usb_port:=/dev/ opencr
  • Run the node and debug
ros2 launch turtlesim turtlesim_node.launch.py ​​-d
  • Only output node description
ros2 launch turtlesim turtlesim_node.launch. py -p
  • Run component
ros2 launch composition composition_demo.launch.py

Run

run is used to run a single node, component program.

  • run help
ros2 run -h
  • run node
ros2 run turtlesim turtlesim_node
  • run node with parameters
ros2 run turtlesim turtlesim_node --ros-args -r __node:=turtle2 -r __ns:=/ns2
  • Run component container
ros2 run rclcpp_components component_container
  • Run component ```bash ros2 run composition manual_composition

    Package

    A package can be thought of as a container for your ROS 2 code. If you want to be able to install your code or share it with others, you need to organize it in a Packages allow you to publish your ROS 2 work and allow others to easily build and use it.

    Package creation in ROS 2 uses ament as its build system and colcon as its build tool. You can create packages using the officially supported CMake or Python, but other build types do exist.

    Specific parameters: Official tutorial

    Creating a Workspace

    Create a new directory for each new workspace. The name is not important, but it helps indicate the purpose of the workspace. Let's choose the directory name ros2_ws for the "development workspace":

    mkdir -p ~/ros2_ws/src
    cd ~/ros2_ws/src
    
  • pkg help

    ros2 pkg -h

  • List function packages

ros2 pkg executable turtlesim
  • Output a function package executable program
ros2 pkg executable turtlesim
  • Create Python Packages

Before running the package creation command, make sure you are in the src folder.

cd ~/ros2_ws/src

The command syntax for creating a new package in ROS 2 is:

ros2 pkg create --build-type ament_python <package_name>
# You will use the optional parameter -- node-name Create a simple Hello World type executable file in the package.
ros2 pkg create --build-type ament_python --node-name my_node my_package
  • Build package

Put the package in the workspace Especially valuable because you can build many packages at once by running colcon build in the workspace root. Otherwise, you would have to build each package individually.

# Return to the workspace directory:
cd ~/ros2_ws# Now you can build your package:
colcon build
  • Source setup file

To use your new package and executable, first open a new terminal and source your main ROS 2 installation.

Then, from the ros2_ws directory, run the following command to source your workspace:

source install/setup.bash

Now that your workspace is added to your path, you will be able to use your new package's executable.

  • Use package

To run the executable you created during package creation using the --node-name parameter, enter the following command:

ros2 run my_package my_node

results matching ""

    No results matching ""