1 Communication type

1.1 Node

  • Function
ros2 run <package_name> <executable_name>
  • Check
ros2 node list 
ros2 node info <node_name>

1.2 Topic

  • Topic is divided into Publisher and Subscriber, many to many model
  • The message type is:. msg file
  • Applicable scenario: based on asynchronous streaming media data
# View graphical nodes
rqt_graph

# View all topics
ros2 topic list

# View the topic being published
ros2 topic echo <topic_name>

# View the specific information of topic
ros2 topic info <topic_name>

# Publish data using topic
ros2 topic pub <topic_name> <msg_type> '<args>'

# View the frequency of topic publishing data
ros2 topic hz <topic_name>

1.3 Service

  • Service is divided into Server and Client, one-to-one model
  • The file type is :. srv file
  • Applicable scenario: Synchronous RPC based communication
# View service
ros2 service list 
ros2 service list -t

# Judge type
ros2 service type <service_name>

# Find
ros2 service find <type_name>

# Show
ros2 interface show <type_name>

# Call
ros2 service call <service_name> <service_type> <arguments>

1.4 Parameters

  • Add parameters at startup
ros2 run <package_name> <executable_name> --ros-args --params-file <file_name>
  • Add parameters from file
ros2 param load <node_name> <parameter_file>
  • Set the parameters of the node
ros2 param set <node_name> <parameter_name> <value>
  • Transfer parameters to a file,node_name.yaml
ros2 param dump <node_name>
  • Query
ros2 param list
ros2 param get <node_name> <parameter_name>

2 Work Space

2.1 Create a work space

mkdir -p colcon_ws/src

2.2 Create Package

ros2 pkg create --build-type ament-cmake <package_name>

2.3 Install dependent package

rosdep install -i --from-path src --rosdistro galactic -y

2.4 Build compilation package

colcon build

2.5 Compilation Option

colcon build
    --packages-select <pkg_name> # Compile only the specified package

    --symlink-install # Use matching links instead of copying files

    --packages-ignore # Ignore the specified package

    --continue-on-error    # Compile other packages after compilation error

    --cmake-args # Pass parameters to the corresponding package
    --ament-cmake-args
    --catkin-cmake-args

3 configuration Information

3.1 package.xml

<name>
<version>
<description>
<maintainer email=””>
<license>                            # Licence
<buildtool_depnd>                    # Compiler dependency
<build_depend>                        # Compilation dependency
    rclpy
    std_msg
    message_generation
<exec_depend>                        # Execution dependency
    turtlesim
    rclpy
    std_msgs
    message_runtime
    sensor_msgs
    open3d
    geometry_msgs            # tf2 correlation
    tf_transformations
    tf_ros
    launch                    # Startup file
    launch_ros
<test_depend>                        # Test Dependency
<build_export_depend>                # Export Dependency
    ament_python
<run_depend>                    # Run Dependency

3.2 C++(CMakeLists.txt)

# Add Dependent Package
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(example_interfaces REQUIRED)

# Add Executable
add_executable(server src/add_two_ints_server.cpp)
ament_target_dependencies(server
  rclcpp example_interfaces)

add_executable(client src/add_two_ints_client.cpp)
ament_target_dependencies(client
  rclcpp example_interfaces)

# Install Executable
install(TARGETS
  server
  client
  DESTINATION lib/${PROJECT_NAME})

3.3 Python(setup.py)

data_files
    Start file: (os.path.join('share', package_name, "launch"), glob('launch/*.launch.py')),
    Configure file: (os.path.join('share', package_name, "config"), glob('config/*')),
    3D model file: (os.path.join('share', package_name, "resource"), glob('resource/*.ply')),
    Robot description file: (os.path.join('share', package_name), glob('urdf/*'))

entry_points
    Node name = package name.file name:main

4 Interface document

4.1 Data type of the interface

Type Name C++ Python DDS Type
bool bool bool boolean
byte uint8_t bytes octet
char char str char
float32 float float float
float64 double float double
int8 int8_t int octet
uint8 uint8_t int octet
int16 int16_t int short
uint16 uint16_t int unsigned short
int32 int32_t int long
uint32 uint32_t int unsigned long
int64 int64_t int long long
uint64 uint64_t int unsigned long long
string std::string str string
wstring std::u16string str wstring

4.2 Naming rules

1.Field names must be lowercase letters and numeric characters with underscores to separate words.
2.Field names must begin with an alphabetic character, cannot end with an underscore, and can never have two consecutive underscores.

4.3 Set Default

uint8 x 42
int16 y -2000
string full_name "John Doe"
int32[] samples [-200, -100, 0, 100, 200]

4.4 Set Constant

int32 X=123
int32 Y=-123
string FOO="foo"
string EXAMPLE='bar'

4.5 Create interface package

ros2 pkg create --build-type ament_cmake tutorial_interfaces

4.6 Create interface directory

mkdir msg
mkdir srv
mkdir action

4.7 Define Interface

  • Num.msg file in msg directory
int num
  • AddThreeInts.srv file in srv directory
int64 a
int64 b
int64 c
---
int64 sum
  • Fibonacci.action file in action directory
int32 order
---
int32[] sequence
---
int32[] partial_sequence

4.8 CMakeLists.txt build

find_package(rosidl_default_generators REQUIRED)

rosidl_generate_interfaces(${PROJECT_NAME}
  "msg/Num.msg"
  "srv/AddThreeInts.srv"
  "action/Fibonacci.action"
)

4.9 package.xml ddd dependency

<build_depend>rosidl_default_generators</build_depend>

<exec_depend>rosidl_default_runtime</exec_depend>

<depend>action_msgs</depend>

<member_of_group>rosidl_interface_packages</member_of_group>

4.10 Build software package

colcon build --packages-select tutorial_interfaces

5 rclpy

5.1 Initialization

# Initialize node
rclpy.init()

# Start Node
rclpy.spin()

# Start a node
rclpy.spin_once()

# Start the node until the work is completed
rclpy.spin_until_future_complete()

# Get Executor
rclpy.get_global_executor()

# Close Node
rclpy.shutdown()

5.2 Node

# Import Package
from rclpy.node import Node

# Add Wait
add_waitable

# Count the number of node publishers
count_publishers(topic_name)

# Count the number of node subscribers
count_subscribers(topic_name)

# Destroy node
destroy_node()

5.3 Topic

  • Publisher
# Create Publisher
Node.create_publisher(
    msg_type,                      # Data Type
    topic,                             # Publish Name
    qos_profile,                    #
)

# Set to Active
assert_liveliness()

# Destruction
destroy()

# Get the number of subscribers for this publisher
get_subscription_count()

# Publish data
publish(msg)
  • Subscription
# Create subscriber
Node.create_subscription(
    msg_type,                        # Data Type
    topic,                            # Subscription name
    callback(),                    # Callback function
    qos_profile
)

5.4 Services

  • Client
# Create Client
Node.create_client(
    srv_type,                    #
    srv_name,                    #
    qos_profile                #
)

# Make a request
call(request)

# Asynchronous request
call_async(request)

# Delete Request
remove_pending_request()

# Check whether the service is ready
service_is_ready()

# Wait for the service to start
wait_for_service()
  • Service
# Create server
Node.create-service(
    srv_type,
    srv_name,
    qos_profile
)
send_response()

5.5 Actions

  • Action Client
# Import Package
from rclpy.action import ActionClient

ActionClient(
    action_type,
    action_name,
)

# Add Wait
add_to_wait_set(wait_set)

# Destroy
destroy()

# Entity Number
get_num_entities()

# Ready or not
is_ready(wait_set)

# Send
send_goal(goal)

# Asynchronous sending
send_goal_async()

# Check whether the server is ready
server_is_ready()

# Wait for the server to be ready
wait_for_server()
  • Action Server
ActionServer(
    action_type,
    action_name,
    callback,
)
# Add Wait
add_to_wait_set(wait_set)

# Destroy
destroy()

# Entity Number
get_num_entities()

# Ready or not
is_ready(wait_set)

# Send
send_goal(goal)

# Asynchronous sending
send_goal_async()

# Check whether the server is ready
server_is_ready()

# Wait for the server to be ready
wait_for_server()

5.6 Timer

# Import package
from rclpy.timer import Rate,Timer

Rate(timer,*,context)

Timer(
    callback,
    callback_group,
    timer_period_ns,
    clock,
    *,
    context=None
)
cancel()
destroy()
is_canceled()
is_ready()
reset()
time_since_last_call()
time_until_next_call()

5.7 Context

init()

ok()

# Add a callback to call on when is going to shutdown
on_shutdown()

shutdown()

try_shutdown()

6 Start file

6.1 Startup file type

1.Python file
2.XML file
3.YAML file

6.2 Python start

  • Command
ros2 launch <pkg_name> xxx_launch.py
  • Parameter
package="pkg_name",                        # Package Name
executable="exec_name",                    # Executable
name="node_name",                        # Node Name
output="screen"                            # Ensure output at console
emulate_tty=True,                        # Ensure output at console
parameters=[                            # Parameter dictionary list
    {"my_parameter": "earth"}
]
node_executable
namespace                    # Namespace
node_name:
node_namespace
exec_name

remappings                    # Remapping

Find the file through the OS module
import os
from ament_index_python.packages import get_package_share_directory

para_dir = os.path.join(get_package_share_directory(“pkg_name”), “…”)

6.3 Launch class hierarchy

launch
    LaunchDescription                        # Final return

    actions
        DeclareLaunchArgument                # start parameter
        IncludeLaunchDescription            # Import Startup File
        ExecuteProcess                        # Run the specified command
        TimerAction                    

    conditions
        IfCondition

    substitutions
        LaunchConfiguration                    # Import parameters from the startup file
        PathJoinSubstitution                # Link path
        TextSubstitution                    # Parameter value
        PythonExpression

    launch_description_sources
        PythonLaunchDescriptionSource        # Python startup file

lauch_ros
    actions
        Node                                # Node

    FindPackageShare                        # Find Packages

7 URDF file

7.1 Brief Introduction

URDF is a unified robot description format used to specify the geometry and organization of the robot in ROS.

7.2 Grammar

7.2.1 Brief introduction to grammar

material    color

link
    geometry

    origin

    material

joint
    parent

    child

    origin

    limit

    axis

7.2.2 Complete Grammar

<robot>
    # Description:
    # Parameters:name=""
    # Child nodes:
        <link>
            # Description:
            # Parameters:name=""
            # Child nodes:
                <visual>
                    # Description:
                    # Parameters:
                    # Child nodes:
                        <geometry>
                            # Description:
                            # Parameters:
                            # Child nodes:
                                <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 elements, priority
                            # Parameters:
                            # Child nodes:
                                <geometry>
                        <inertial>
                            # Description:
                            # Parameters:
                            # Child nodes:
                                <mass />
                                    # Description:Quality
                                    # 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 nodes
                <parent />
                    # Description
                    # Parameters:link=""
                <child />
                    # Description:
                    # Parameters:link=""
                <origin />
                    # Description:
                    # Parameters:xyz="0 -0.2 0.25"
                <limit />
                    # Description
                    # Parameters:
                        # effort="1000.0"    Maximum force
                        # lower="-0.38"      Upper limit of joint(radian)
                        # upper="0"          Lower limit of joint(radian)
                        # velocity="0.5"     Maximum speed
                <axis />
                    # Description:Rotate by ? axis
                    # Parameters:xyz="0 0 1",rotate along Z axis
        <material>
            # Description:
            # Parameters:name="blue"
            # Child nodes:
                <color />
                    # Description:
                    # Parameters:rgba="0 0 0.8 1"

7.3 Example

7.3.1 Install dependent libraries

sudo apt install ros-foxy-joint-state-publisher-gui ros-foxy-joint-state-publisher
sudo apt install ros-foxy-xacro

7.3.2 Download source code

cd ~/dev_ws
git clone -b ros2 https://github.com/ros/urdf_tutorial.git src/urdf_tutorial

7.3.3 Compile source code

colcon build --packages-select urdf_tutorial

7.3.4 Run Example

ros2 launch urdf_tutorial display.launch.py model:=urdf/01-myfirst.urdf

results matching ""

    No results matching ""