ROS2学习笔记

  • 参考:https://fishros.com

  • 一般使用步骤

    1.创建工作空间,这和ros1没有区别
    2.创建功能包,ros2 pkg create 包名 –build-type para1 –dependencies para2

    1.如果build-type什么都不写,ros2会默认类型为ament_cmake.
    2.rclpy完成这一步骤之后,在功能包中能看见三个文件夹,分别是resource、test、功能包同名文件夹;rclcpp创建完成后,在文件夹有include和src,这和ros1也是一样的
    3.para1是功能包的编译类型,para2是rclpy或者rclcpp

3.创建python文件,之后修改resource目录下的setup.py

编译

  1. colcon build
    编译工作空间里的所有功能包
  2. colcon build --packages-select PKG_NAME
    单独编译某一个包
  3. colcon build --packages-select YOUR_PKG_NAME --cmake-args -DBUILD_TESTING=0
    不编译测试单元
  4. colcon test
    运行编译的包的测试
    5.刷新环境变量
    ‘’’
    source ./intall/setup.sh
    ‘’’

功能包的编译类型

  1. ament_python适用于python程序
  2. cmake适用于c++
  3. ament_cmake,适用于c++程序,是cmake的增强版

消息

  1. ros2 interface show geometry_msgs/msg/Twist
    查看消息info
  2. ros2 topic list -t
    查看所有topic和对应的消息类型
  3. ros2 topic pub arg para1 para2 para3
    手动发布指令,para1为话题,para2为消息类型,para3为消息

接口(常用的CLI指令)

1.ros2 interface list
查看当前环境下的接口列表
2.ros2 interface packages
查看接口的所有包
3.ros2 interface packages std_msgs
查看某一个包下的所有接口
4.ros2 interface show std_msgs/msg/string
查看某一个接口的详细内容
5.ros2 interface proto sensor_msgs/msg/Image
输出一个接口的所有属性

服务

1.ros2 service list
查看服务列表
2.ros2 run examples_rclpy_minimal_service service
启用服务器(这是一个示例)
3.ros2 service call /add_two_ints example_interfaces/srv/AddTwoInts "{a: 5,b: 10}"
CLI界面启用服务
4.ros2 service type /add_two_ints
查看接口服务类型
5.ros2 service find example_interfaces/srv/AddTwoInts
查找使用某一个接口的服务

Launch

‘’’
在ROS2中launch文件支持使用python进行编写,🍀,K毛鸡
‘’’

  1. 需要导入两个库,一个叫做LaunchDescription,用于对launch文件内容进行描述,一个是Node,用于声明节点所在的位置
    from launch import LaunchDescription
    from launch_ros.actions import Node
  2. 这里要定一个名字叫做generate_launch_description的函数,ROS2会对该函数名字做识别。
    def generate_launch_description():

补充

RCL(ROS Client Library)ROS客户端库,其实就是ROS的一种API,提供了对ROS话题、服务、参数、Action等接口。比如前面用到的rclcpp,rclpy
也就是说,在ROS2中提供了话题服务动作参数这几种通信机制

踩坑点(太重要了,真是被自己菜哭了)

  • 1.ros2 python 节点编译时出现No excutable found
    使用colcon build --symlink-install进行编译
    对于其他的这种情况,可能的原因有很多,通常来说是自己的文件install的路径错了,修改cmake文件
  • 2.相关API发生变化
    ros::Rate=>rclcpp::WallRate
  • 3.回调函数形参的变化

    //ros1:
    void callback(const std_msgs::String::ConstPtr & msg);
    //ros2:
    void callback
    (std_msgs::msg::String::ConstSharedPtr msg);

    1. cmake未定义的引用报错

      错误:undefined reference to tf2_ros::TransformBroadcaster::sendTransform(geometry_msgs::msg::TransformStamped_<std::allocator<void> > const&)
      这种情况是因为cmakelist文件里面的依赖和动态库没有添加全
      找两个地方:
      1.find_packages{}
      2.ament_target_dependencies{}

ROS2仿真相关笔记

  • 环境:ROS2支持的仿真环境很多,如gazebo、ignition gazebo、webots等等,现先迁移gazebo,其他的看情况而定

    sudo apt install gazebo11

  • 仿真demo下载:

    sudo apt install ros-foxy-gazebo-*

  • 运行此demo

    gazebo /opt/ros/foxy/share/gazebo_plugins/worlds/gazebo_ros_diff_drive_demo.world

  • 卸载(略)

TF2(2022-6-29学习,后续补全)

在ros2中,已经完全弃用ros1中的tf,所以仿真包里面的tf一直没有修改我是真的不懂,纯纯的给我找麻烦

diagnostics

diagnostics意为诊断,该模块旨在从系统中收集各种状态信息和硬件信息,以进行分析,故障排除和记录。 工具链中包含用于收集,发布,分析和查看diagnostics数据的工具。

Launch(2022-07-01)

  1. 仿真包需要进行大量launch文件的修改,部分本人还不太熟悉,python git launch学习笔记
    ros2 launch
  2. 本次仿真迁移用的是xml格式的,旨在原来的launch的文件上直接修改就可以,减少了大量的学习成本,若后续python相关API解释文档被补全,再想办法修改为py
    xml参考文档:
    xml csdn 参考
    xml doc 参考2

1. < node > tag

Differences from ROS 1:

  1. type attribute is now exec.
  2. ns attribute is now namespace.
  3. The following attributes aren’t available: machine, respawn_delay, clear_params.

2. < param > tag

  1. There’s no global parameter concept in ROS 2
  2. For that reason, it can only be used nested in a node tag.
  3. Some attributes aren’t supported in ROS 2: type, textfile, binfile, executable, command.
  4. In ROS 2, param tags are allowed to be nested.
  5. param现在只支持name、value 、value-sep、type

3. < rosparam > tag

  1. rosparam Loads parameters from a yaml file. It has been replaced with a from attribute in param tags.

4. < include > tag

  1. Usage: Allows including another launch file.
  2. Available in ROS 1, included content was scoped. In ROS 2, it’s not. Nest includes in group tags to scope them.
  3. ns attribute is not supported. See example of push-ros-namespace tag for a workaround.
  4. arg tags nested in an include tag don’t support conditionals (if or unless).
  5. There is no support for nested env tags. set_env and unset_env can be used instead.
  6. Both clear_params and pass_all_args attributes aren’t supported.

5. < arg > tag

  1. Usage: arg is used for declaring a launch argument, or to pass an argument when using include tags.
  2. value attribute is not allowed. Use let tag for this.
  3. doc is now description.
  4. When nested within an include tag, if and unless attributes aren’t allowed.

6. < ground > tag

  1. Usage: 限制启动范围(英文文档不好理解,这里手打)
  2. 使用let,include,push-ros-namespace这三个tag来实现
  3. 和ROS1的不同之处:
    [1]There is no ns attribute. See the new push-ros-namespace tag as a workaround.
    [2]clear_params attribute isn’t available.
    [3]It doesn’t accept remap nor param tags as children.

7. < push-ros-namespace > tag

  1. include and group tags don’t accept an ns attribute.

‘’’
< group>
< push-ros-namespace namespace=”my_ns”/>


< push-ros-namespace namespace=”another_ns”/>

< push-ros-namespace namespace=”/absolute_ns”/>



< node pkg=”my_pkg” exec=”my_executable” name=”my_node” ns=”/asd”/>
< /group>
‘’’

8. < env > tag

  1. Usage: Sets an environment variable.
  2. env can only be used nested in a node or executable tag. if and unless tags aren’t supported.
  3. set_env can be nested within the root tag launch or in group tags. It accepts the same attributes as env, and also if and unless tags.
  4. unset_env unsets an environment variable. It accepts a name attribute and conditionals.
  5. 本版本迁移过来的基本都是set_env

9. Substitutions

1. env and optenv tags have been replaced by the env tag. $(env <NAME>) will fail if the environment variable doesn’t exist. $(env <NAME> '') does the same as ROS 1’s $(optenv <NAME>). $(env <NAME> <DEFAULT>) does the same as ROS 1’s $(env <NAME> <DEFAULT>) or $(optenv <NAME> <DEFAULT>).

2. find has been replaced with find-pkg-share (substituting the share directory of an installed package). Alternatively find-pkg-prefix will return the root of an installed package.

3. There is a new exec-in-pkg substitution. e.g.: $(exec-in-pkg <package_name> <exec_name>).

4. There is a new find-exec substitution.

5. arg has been replaced with var. It looks at configurations defined either with arg or let tag.

6. eval and dirname substitutions haven’t changed.

7. anon substitution is not supported.

/gazebo

本节点对外提供的服务
/delete_entity
/get_model_list
/spawn_entity
/gazebo/describe_parameters
/gazebo/get_parameter_types
/gazebo/get_parameters
/gazebo/list_parameters
/gazebo/set_parameters
/gazebo/set_parameters_atomically
特别说明
一般使用这三个服务:1. /spawn_entity 2. /get_model_list 3. /delete_entity

仿真包作出的修改

故障排除:
1. 参数能够初始化,没有问题;
2. 服务器参数也可以初始化没有问题;
3. 基本排除是cmakelist文件和package文件的问题;
4. 排除了pos话题缺失的问题;
5. 排除了无法进入timer_callback的问题;
6. 解决了ground_truth_republisher编译错误和改写的问题;
对于仿真的launch文件升级检查内容:
1. 检查语法是否正确;
2. 检查调用的功能包中是否有对应的节点;
3. 检查话题能够对应上上一个版本的;
4. echo这个话题看是否符合标准;
2022.07.07
排除了仿真包可能存在的cmd问题;
明天需要确定current_pose 是否符合规范;