前文实现的launch较简单,对比ROS1的launch确实了参数传入的功能
<launch>
<arg name="baudrate" default="$(env PIBOT_DRIVER_BAUDRATE)" doc="pibot driver baudrate"/>
<arg name="publish_odom_tf" default="true" doc="pub tf"/>
<arg name="use_imu" default="true" doc="use imu"/>
<arg name="use_accelerometer" default="true" doc="use acce"/>
<arg name="use_gyroscope" default="true" doc="use gyro"/>
<arg name="use_magnetometer" default="true" doc="use mag"/>
<arg name="cali_imu_startup" default="true" doc="use mag"/>
<!-- PIBOT DRIVER -->
<node name="pibot_driver" pkg="pibot_bringup" type="pibot_driver" output="screen">
<param name="base_frame" value="base_link" />
<param name="baudrate" value="$(arg baudrate)" />
<param name="cmd_vel_topic" value="cmd_vel" />
<param name="odom_frame" value="odom" />
<param name="odom_topic" value="odom" if="$(arg publish_odom_tf)"/>
<param name="odom_topic" value="wheel_odom" unless="$(arg publish_odom_tf)" />
<param name="out_pid_debug_enable" value="false" />
<param name="port" value="/dev/pibot" />
<param name="publish_odom_tf" value="$(arg publish_odom_tf)" />
<param name="freq" value="100" />
<rosparam if="$(arg use_imu)">
imu/accelerometer_bias: {x: 0.005436, y: 0.014684, z: -0.395418}
imu/gyroscope_bias: {x: -0.035592, y: 0.080670, z: 0.001216}
</rosparam>
<param name="imu/use_accelerometer" value="$(arg use_accelerometer)" if="$(arg use_imu)"/>
<param name="imu/use_gyroscope" value="$(arg use_gyroscope)" if="$(arg use_imu)"/>
<param name="imu/use_magnetometer" value="$(arg use_magnetometer)" if="$(arg use_imu)"/>
<param name="imu/perform_calibration" value="$(arg cali_imu_startup)" if="$(arg use_imu)"/>
</node>
</launch>
ROS1中我们可以执行roslaunch时传入相应的参数,例如
roslaunch pibot_bringup bringup.launch use_imu:=false
我们中的ROS1中的launch文件是xml格式的,http://wiki.ros.org/roslaunch/XML这里我们可以看到其定义
参考这里https://docs.ros.org/en/galactic/Tutorials/Launch/Using-ROS2-Launch-For-Large-Projects.html#id7我们添加对应的参数
baudrate_arg = DeclareLaunchArgument(
"baudrate", default_value=TextSubstitution(text="115200")
)
use_imu_arg = DeclareLaunchArgument(
"use_imu", default_value=TextSubstitution(text="true")
)
...
return LaunchDescription([
baudrate_arg,
Node(
package="pibot_bringup",
executable="pibot_driver",
name="pibot_driver",
output="screen",
emulate_tty=True,
parameters=[
...
{"imu/use_accelerometer": LaunchConfiguration('use_imu')},
{"imu/use_gyroscope": LaunchConfiguration('use_imu')},
{"imu/use_magnetometer": LaunchConfiguration('use_imu')},
...
]
)
...
对比ROS1中我们是从环境变量PIBOT_DRIVER_BAUDRATE
读取波特率, 稍作修改, 因launch文件是python
对比之前xml
我们就可以更大发挥空间了
import os
...
default_baudrate = os.getenv('PIBOT_DRIVER_BAUDRATE')
if default_baudrate is None:
default_baudrate = "115200"
baudrate_arg = DeclareLaunchArgument(
"baudrate", default_value=TextSubstitution(text=default_baudrate)
)
export PIBOT_DRIVER_BAUDRATE=921600
ros2 launch pibot_bringup bringup_launch.py use_imu:=true
ros2 launch pibot_bringup bringup_launch.py baudrate:=921600 use_imu:=false
本文代码https://gitee.com/pibot/pibot_bringup/tree/278aaab26a3b51425496de26dc6a885d0b240cc4