yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
Raspberry Pi 4B📖
1.Preparation before use
  • 1.1 Download image
  • 1.2 Write image into SD card
  • 1.3 Start up Raspberry Pi
  • 1.4 Linux common commands
  • 1.5 Precautions
2.Basic course
  • 2.1 Log into Raspberry Pi system
  • 2.2 Change password and root account
  • 2.3 Remote transfer file
  • 2.4 Backup image file
  • 2.5 Fixing Raspberry Pi IP
  • 2.6 Set screen keep on
  • 2.7 Get Pi temperature
  • 2.8 Connect WIFI
  • 2.9 Modify Pi time
  • 2.10 Set the boot self-starting program
3.Expand course
  • 3.1 Serial port communicates with external devices
  • 3.2 2.Serial port login Pi
  • 3.3 Configured as a router
  • 3.4 Install Jupyter Lab
  • 3.5 Install Jupyter Widgets
  • 3.6 More Detail for Widgets
  • 3.7 Install OpenCV
  • 3.8 Install Tensorflow
4.Programming course
C language
  • 1.Development environment
  • 2.Print Hello World
  • 3.Pin output level
  • 4.Read pin level
  • 5.PWM output
Python language
  • 1.Development environment
  • 2.Print basic usage
  • 3.Pin output level
  • 4.Read pin level
  • 5.PWM output
5.AI vision course
A.Getting started with OpenCV
  • 6.Picture Rotation
  • 1.Introduction of OpenCV
  • 2.OpenCV image read and display
  • 3.OpenCV image write
  • 4.OpenCV image quality
  • 5.OpenCV pixel operation
B.OpenCV Geometric transformat
  • 1.Picture Zoom
  • 2.Picture Cut
  • 3.Picture Shift
  • 4.Picture Mirroring
  • 5.Affine Map
  • 7.Perspective Transformation
C.OpenCV picture processing
  • 1.Grayscale processing
  • 2.Image Binarization
  • 3.Edge detection
  • 4.Drawing line segment
  • 5.Drawing rectangle circle
  • 6.Drawing text picture
D.OpenCV image beautification
  • 1.Histogram of color pictures
  • 2.Histogram equalization
  • 3.Image restoration
  • 4.Brightness enhancement
  • 5.Skin whitening
  • 6.Gaussian mean filtering
  • 7.Median filtering
F.AI Advanced Course (Ubuntu system)
  • 1. QR code
  • 2. Human posture estimation
  • 3. Target detection
  • 4. Mediapipe development
  • 5. yolov4-tiny
E.Install TensorFlow
  • Install TensorFlow
6.ROS1 coursenew
Basic Course
  • 1.ROS introduction
  • 2.Project file structure
  • 3.Common commands and tools
  • 4.Publisher
  • 5.Subscribers
  • 6.Customize topic messages and use
  • 7.Client
  • 8.Server
  • 9.Custom service messages and usage
  • 10.TF release and monitoring
Visual Image Processing
  • 1. AR vision
  • 2. AR QR code
  • 3. ROS+Opencv foundation
  • 4. ROS+Opencv application
  • 5. MediaPipe development
7.ROS2 coursenew
Docker Course
  • 1.Overview and installation
  • 2.Common commands
  • 3.Understand and publish images
  • 4.Hardware interaction data processing
  • 5.Enter docker container
  • 6.Update docker images
Basic Course
  • 1.Introduction to ROS2
  • 2.Commands and tools
  • 3.ROS2 Topic communications
  • 4.ROS2 service communications
  • 5.ROS2launch file starts
OpenCV Course
  • 1.OpenCV Basic Course
  • 2.ROS+opencv application
  • 3.QR code recognition
  • 4.AR Vision
  • 5.Mediapipe
8.Accessories installation
  • 1. Installation of heat sink
  • 2. Wiring of cooling fan
  • 3. Double fan armor case Installation
  • 4. Argon NEO aluminum case installation
  • 5. Argon ONE aluminum alloy case installation
  • 6. Metal shell with built-in fan installation
  • 7. Acrylic case with cooling fan installation
  • 8. White-red ABS protective case installation
Download
  • Tools
  • C_code
  • Python code
  • ROS System
Download ZIP

Welcome to Raspberry Pi 4B repository

5.ROS2launch file starts

5、ROS2launch file starts

In ROS2, launch is used for multi-node startup and configuring program running parameters, and ROS2's launch file formats include XML, Yaml and Python formats. This lesson takes the launch file in Python format as an example, compared to the other two formats, the Python format is more flexible:

  • Python has a large number of libraries that can be used in startup files;
  • ROS2 generic boot features and specific boot features are written in Python, so XML and YAML boot features that may not be exposed can be accessed;

Using python language to write ROS2 launch files, the most important thing is to abstract each node, file, script, etc. into an action, with a unified interface to start, the main structure is,

def generate_launch_description():
    return LaunchDescription([
        action_1,
        action_2,
        ...
        action_n
    ])

1、Create a folder to store the launch file

Under the path of the function package created earlier, we create a new folder to store the launch file and terminal input.

xxxxxxxxxx
cd ~/ros2_ws/src/topic_pkg
mkdir launch

image-20230504151311185

Launch files are often named in LaunchName_launch.py, where LaunchName is custom, _launch.py is often considered fixed. You need to modify the setup.py file under the function pack, modify the content to add the file under the launch path, and compile to generate the executed .py file.

xxxxxxxxxx
#1、Import the relevant header files
import os
from glob import glob
#2、In the list of data_files, add the launch path and the launch.py files under the path
(os.path.join('share',package_name,'launch'),glob(os.path.join('launch','*launch.py')))

image-20230504151546895

2、Write a launch for a single node node

terminal input,

xxxxxxxxxx
cd ~/ros2_ws/src/topic_pkg/launch
gedit single_node_launch.py

Copy the following into the file,

​x
from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
    turtle_node = Node(
            package='turtlesim',
            executable='turtlesim_node',
                )
    launch_description = LaunchDescription([turtle_node])
    return launch_description

2.1、Compile the workspace

terminal input,

xxxxxxxxxx
cd ~/ros2_ws
colcon build

After the compilation is completed, refresh the environment variables of the workspace.

xxxxxxxxxx
source ~/ros2_ws/install/setup.bash

2.2、Run the program

terminal input,

xxxxxxxxxx
ros2 launch topic_pkg single_node_launch.py

image-20230504153403183

After the program runs, it will run the baby turtle node.

2.3、Source code analysis

1) 、Import related libraries
xxxxxxxxxx
from launch import LaunchDescription
from launch_ros.actions import Node
2)、Defines a function generate_launch_description and returns a launch_description
xxxxxxxxxx
def generate_launch_description():
    turtle_node = Node(
            package='turtlesim',
            executable='turtlesim_node',
                )
    launch_description = LaunchDescription([turtle_node])
    return launch_description

Define a variable turtle_node return value for starting as a node, call the Node function, and launch two important parameters, package and executable.

  • package: Indicates the function package, representing the name of the feature package.
  • executable: indicates the executed program, the name of the executable program.

Then define a variable launch_description as the return value after calling the LaunchDescription function, and add it after multiple nodes are started.

xxxxxxxxxx
launch_description = LaunchDescription([turtle_node])

Finally, return launch_description.

3、Write launch for multiple node nodes

terminal input,

xxxxxxxxxx
cd ~/ros2_ws/src/topic_pkg/launch
gedit multi_node_launch.py

Copy the following into the file,

xxxxxxxxxx
from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
    pub_node = Node(
            package='topic_pkg',
            executable='publisher_demo',
            output='screen'
                )
    sub_node = Node(
            package='topic_pkg',
            executable='subscriber_demo',
            output='screen'
                )
    launch_description = LaunchDescription([pub_node,sub_node])
    return launch_description

3.1、compile the workspace

terminal input,

xxxxxxxxxx
cd ~/ros2_ws
colcon build

After the compilation is completed, refresh the environment variables of the workspace.

xxxxxxxxxx
source ~/ros2_ws/install/setup.bash

3.2、Run the program

terminal input,

xxxxxxxxxx
ros2 launch topic_pkg multi_node_launch.py

The terminal does not print content, we can see which nodes are started to verify that there is a successful startup, the terminal inputs,

xxxxxxxxxx
ros2 node list

image-20230504175912359

As can be seen from the above figure, two nodes are started, which correspond to the two programs in the launch file.

3.3、Source code parsing

It's about the same as simple_node_launch.py, but with one more node, and an extra node in launch_description = LaunchDescription([pub_node,sub_node]).

4、Topic name mapping in launch files

terminal input,

xxxxxxxxxx
cd ~/ros2_ws/src/topic_pkg/launch
gedit remap_name_launch.py

Copy the following into the file,

xxxxxxxxxx
from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
    turtle_node = Node(
            package='turtlesim',
            executable='turtlesim_node',
            remappings=[("/turtle1/cmd_vel", "/cmd_vel")]
                )
    launch_description = LaunchDescription([turtle_node])
    return launch_description

4.1、compile the workspace

terminal input,

xxxxxxxxxx
cd ~/ros2_ws
colcon build

After the compilation is completed, refresh the environment variables of the workspace.

xxxxxxxxxx
source ~/ros2_ws/install/setup.bash

4.2、Run the program

Let's first see what is the name of the little turtle's speed topic before remapping the topic, terminal input,

xxxxxxxxxx
ros2 launch topic_pkg single_node_launch.py
ros2 topic list

image-20230504181824461

The topic here is /turtle1/cmd_vel.

Then run the program after remapping the topic, see what is the name of the speed topic that the little turtle subscribes to, and enter it in the terminal,

xxxxxxxxxx
ros2 launch topic_pkg remap_name_launch.py
ros2 topic list

image-20230504182212983

As can be seen from the figure above, the speed topic name is remapped, and the mapped baby turtle speed topic name is /cmd_vel.

4.3、Source code analysis

Modifications have been made on the basis of the original single_node_launch.py, mainly adding the following parts,

xxxxxxxxxx
remappings=[("/turtle1/cmd_vel", "/cmd_vel")]

This is to remap the original /turtle1/cmd_vel to /cmd_vel. The original topic name in front is the topic name we want to change to.

5、 Edit the launch file

Terminal input

xxxxxxxxxx
cd ~/ros2_ws/src/topic_pkg/launch
gedit include_launch.py

Copy the following into the file

xxxxxxxxxx
import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
def generate_launch_description():
    node1 = IncludeLaunchDescription(
    PythonLaunchDescriptionSource([os.path.join(
    get_package_share_directory('topic_pkg'), 'launch'),
    '/multi_node_launch.py'])
    )
    node2 = IncludeLaunchDescription(
    PythonLaunchDescriptionSource([os.path.join(
    get_package_share_directory('topic_pkg'), 'launch'),
    '/single_node_launch.py'])
    )
    return LaunchDescription([node1,node2])

5.1、compile the workspace

terminal input,

xxxxxxxxxx
cd ~/ros2_ws
colcon build

After the compilation is completed, refresh the environment variables of the workspace.

xxxxxxxxxx
source ~/ros2_ws/install/setup.bash

5.2、Run the program

terminal input,

xxxxxxxxxx
ros2 launch topic_pkg include_launch.py

This launch file will contain two launch files, simple_node_launch.py and multi_node_launch.py. You can check whether the node of these launch files is started by the following command, terminal input,

xxxxxxxxxx
ros2 node list

image-20230504191621429

It is true that three nodes are started, so the startup is successful.

5.3、Source code analysis

xxxxxxxxxx
#Import the necessary library files
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
node1 = IncludeLaunchDescription(
    PythonLaunchDescriptionSource([os.path.join(
    get_package_share_directory('topic_pkg'), 'launch'),
    '/multi_node_launch.py'])
    )
  • os.path.join(get_package_share_directory('topic_pkg'): Gets the location of the feature pack, where 'topic_pkg' is the name of the feature pack;
  • launch'): indicates the folder that stores the launch file under the function pack;
  • /multi_node_launch.py': indicates the name of the launch file under the launch file in the package folder, which is /multi_node_launch.py in the example.

6、Launch file parameter configuration rosparam

terminal input,

xxxxxxxxxx
cd ~/ros2_ws/src/topic_pkg/launch
gedit param_launch.py

Copy the following into the file,

xxxxxxxxxx
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration, TextSubstitution
from launch_ros.actions import Node

def generate_launch_description():
    background_r_launch_arg = DeclareLaunchArgument(
      'background_r', default_value=TextSubstitution(text='0'))
    background_g_launch_arg = DeclareLaunchArgument(
      'background_g', default_value=TextSubstitution(text='225'))
    background_b_launch_arg = DeclareLaunchArgument(
      'background_b', default_value=TextSubstitution(text='0'))
    return LaunchDescription([
                            background_r_launch_arg,
                            background_g_launch_arg,
                            background_b_launch_arg,
                            Node(
                                    package='turtlesim',
                                    executable='turtlesim_node',
                                    name='sim',
                                    parameters=[{
                                            'background_r':LaunchConfiguration('background_r'),
                                            'background_g':LaunchConfiguration('background_g'),
                                            'background_b':LaunchConfiguration('background_b'),
                                    }]
                                )
                            ]) 

6.1、compile the workspace

terminal input,

xxxxxxxxxx
cd ~/ros2_ws
colcon build

After the compilation is completed, refresh the environment variables of the workspace.

xxxxxxxxxx
source ~/ros2_ws/install/setup.bash

6.2、Run the program

terminal input,

xxxxxxxxxx
ros2 launch topic_pkg param_launch.py

image-20230505095640547

After the program runs, it will load the set parameters, modify the default RGB parameters, and change the color of the background panel.

6.3、Source code parsing

xxxxxxxxxx
from launch.actions import DeclareLaunchArgument    #Declare the Argument class used in the launch file 
background_r_launch_arg = DeclareLaunchArgument(
      'background_r', default_value=TextSubstitution(text='0')) #Create a Launch file with parameter background_r
      'background_r', default_value=TextSubstitution(text='0')) #Create a Launch file with parameter background_g
      'background_r', default_value=TextSubstitution(text='0')) #Create a Launch file with parameter background_b
      background_r_launch_arg,  #Call the parameter created above
      background_g_launch_arg,
      background_b_launch_arg,
         parameters=[{                                           #ROS parameter list
            'background_r': LaunchConfiguration('background_r'),   #Create parameters background_r
            'background_g': LaunchConfiguration('background_g'),    #Create parameters background_g
            'background_b': LaunchConfiguration('background_b'),    #Create a parameter background_b

Argument and param both mean parameters, but argument is passed in the launch file, and param is passed in the node program.

7、launch file loads the parameter configuration table

First create a new parameter table, terminal input,

xxxxxxxxxx
cd ~/ros2_ws/src/topic_pkg
mkdir config
cd config
gedit turtle_config.yaml

Copy the following contents into the turtle_config.yaml file,

xxxxxxxxxx
sim:
    ros__parameters:
        background_r: 0
        background_g: 0
        background_b: 7

Exit after saving, then modify the setup.py file, the path to the loading parameter file, terminal input,

xxxxxxxxxx
cd ~/ros2_ws/src/topic_pkg
gedit setup.py

image-20230518122218693

In the image above, add the following,

xxxxxxxxxx
(os.path.join('share', package_name, 'config'), glob(os.path.join('config', '*.yaml'))),

Exit after saving, finally write the launch file, terminal input,

xxxxxxxxxx
cd ~/ros2_ws/src/topic_pkg/launch
gedit param_config_launch.py

Copy the following into the file,

xxxxxxxxxx
import os
from launch import LaunchDescription
from launch_ros.actions import Node
from ament_index_python.packages import get_package_share_directory
def generate_launch_description():
    config = os.path.join(             
      get_package_share_directory('topic_pkg'),
      'config',
      'turtle_config.yaml'
      )
    return LaunchDescription([
                            Node(
                                    package='turtlesim',
                                    executable='turtlesim_node',
                                    name='sim',
                                    parameters=[config]
                                )
                            ]) 

7.1、compile the workspace

terminal input,

xxxxxxxxxx
cd ~/ros2_ws
colcon build

After the compilation is completed, refresh the environment variables of the workspace.

xxxxxxxxxx
source ~/ros2_ws/install/setup.bash

7.2、Run the program

terminal input,

xxxxxxxxxx
ros2 launch topic_pkg param_config_launch.py

image-20230505122336162

Running the program can get baby turtles and the background plate color is set to black by parameters.

7.3, Source code parsing

xxxxxxxxxx
#Locate the parameter file location
config = os.path.join(             
      get_package_share_directory('topic_pkg'),
      'config',
      'turtle_config.yaml'
      )
#Load the parameters file
parameters=[config]

Let's look at the parameter file turtle_config.yaml,

xxxxxxxxxx
sim:
    ros__parameters:
        background_r: 0
        background_g: 0
        background_b: 7

The parameter file location is: ~/ros2_ws/src/topic_pkg/config

  • sim: indicates the node name
  • ros__parameters: Indicates the ROS parameter, which is fixed here
  • background_r the parameter name, followed by the value set by the parameter
ROS2 service communications
OpenCV Basic Course

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文