PIBOT移植ROS2记录(3)

作者: PIBOT导航机器人 | 来源:发表于2022-05-06 23:10 被阅读0次

    1. 新增launch文件

    参考https://gitee.com/pibot/ros2_tutorials/blob/master/src/cpp_parameters/launch/cpp_parameters_launch.py
    不同于ROS1xml格式的launch文件,ROS2launch文件直接是一个python文件

    对照ROS1的launch

    <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" />
        ...
    </node>
    

    对应ROS2中LaunchDescription传入的Node实例,接受参数根据名字容易理解,
    package executable name output parameters,且ROS1的launch都能找到对应的字段

    在launch下新增bringup_launch.py文件,如下

    from launch import LaunchDescription
    from launch_ros.actions import Node
    
    def generate_launch_description():
        return LaunchDescription([
            Node(
                package="pibot_bringup",
                executable="pibot_driver",
                name="pibot_driver",
                output="screen",
                emulate_tty=True,
                parameters=[
                    {"base_frame": "base_link"},
                    {"port": "/dev/pibot"},
                    {"baudrate": 115200}
                ]
            )
        ])
    

    执行命令os2 launch pibot_bringup bringup_launch.py运行,提示无法找到launch文件

    ➜  . install/setup.bash                                                                                    
    ➜  ros2 launch  pibot_bringup bringup_launch.py                                                                                     
    file 'bringup_launch.py' was not found in the share directory of package 'pibot_bringup' which is at '/home/pibot/ros2_ws/install/pibot_bringup/share/pibot_bringup'
    

    对比https://gitee.com/pibot/ros2_tutorials/blob/master/src/cpp_parameters/CMakeLists.txt发现有个新增launch的install

    install(
      DIRECTORY launch
      DESTINATION share/${PROJECT_NAME}
    )
    

    新增后重新编译再次运行

    ➜  ros2 launch  pibot_bringup bringup_launch.py
    [INFO] [launch]: All log files can be found below /home/pibot/.ros/log/2022-05-04-11-25-41-814156-david-ThinkPad-E420-45992
    [INFO] [launch]: Default logging verbosity is set to INFO
    [INFO] [pibot_driver-1]: process started with pid [46004]
    [pibot_driver-1] [INFO] [1651634742.385702144] [pibot_driver]: BaseDriver startup...
    [pibot_driver-1] [INFO] [1651634742.385829107] [pibot_driver]: subscribe cmd topic on [/cmd_vel]
    [pibot_driver-1] [INFO] [1651634742.386822348] [pibot_driver]: init ok
    

    2. 参数读取

    串口文件名,波特率通过launch的参数传入的,我们新增在代码中读取该参数。
    参考https://gitee.com/pibot/ros2_tutorials/blob/master/src/cpp_parameters/src/cpp_parameters_node.cpp 添加参数的声明

      node->declare_parameter<std::string>("port", "/dev/ttyACM0");
      node->declare_parameter<int32_t>("baudrate", 115200);
    

    编译运行

    ros2 launch  pibot_bringup bringup_launch.py
    

    打开另外的窗口输入ros2 param get /pibot_driver port命令可以查看到参数的值

    ➜  ros2 param list
    /pibot_driver:
      baudrate
      port
      qos_overrides./parameter_events.publisher.depth
      qos_overrides./parameter_events.publisher.durability
      qos_overrides./parameter_events.publisher.history
      qos_overrides./parameter_events.publisher.reliability
      use_sim_time
    ➜  ros2 param get /pibot_driver port
    String value is: /dev/pibot
    

    也可以输入ros2 run rqt_reconfigure rqt_reconfigure查看
    [图片上传失败...(image-3706a2-1651673658574)]

    3. 添加参数约束

    这些通过launch传入的参数,在初始化时使用,我们添加只读属性避免被改导致二义性

      rcl_interfaces::msg::ParameterDescriptor descriptor;
      descriptor.read_only = true;
    
      node->declare_parameter<std::string>("port", "/dev/ttyACM0", descriptor);
      node->declare_parameter<int32_t>("baudrate", 115200, descriptor);
    

    4. 通讯相关

    通讯(SerialTransport,SimpleDataframe)与ROS无关的,只需要修改相关日志输出的即可

    本文代码https://gitee.com/pibot/pibot_bringup/tree/bc942bdb49ec5a358a0a13f8359eb5e7878e2350

    相关文章

      网友评论

        本文标题:PIBOT移植ROS2记录(3)

        本文链接:https://www.haomeiwen.com/subject/wubgyrtx.html