【ROS2】自定义msg的发布和订阅,ROS的自定义消息迁移到ROS2

【ROS2】自定义msg的发布和订阅

  • UBUNTU 20.04 + ROS2 rolling
  • ROS+开发工具一键安装脚本
    wget http://fishros.com/install -O fishros && bash fishros

ROS工程的自定义消息迁移到ROS2

基本操作

1. 创建工作空间

mkdir ~p ~/ros2_example_proj
cd ros2_example_proj
colcon build

2. 创建自定义消息功能包

cd src
ros2 pkg creat --build-type ament_cmake util
cd util
mkdir src
mkdir msg
cd ..

3. 在msg文件夹中创建自定义msg文件 Num.msg

int64 num_test

4. 修改xml和cmakelist

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
  <name>util</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email="crn@todo.todo">crn</maintainer>
  <license>TODO: License declaration</license>

  <buildtool_depend>ament_cmake</buildtool_depend>

  <build_depend>rosidl_default_generators</build_depend>
  <build_depend>rclcpp</build_depend>
  <build_depend>std_msgs</build_depend>
  
  <exec_depend>rosidl_default_runtime</exec_depend>

  <exec_depend>rosidl_default_runtime</exec_depend>
  <exec_depend>std_msgs</exec_depend>

  <member_of_group>rosidl_interface_packages</member_of_group>


  <test_depend>ament_lint_auto</test_depend>
  <test_depend>ament_lint_common</test_depend>

  <export>
    <build_type>ament_cmake</build_type>
  </export>
</package>

cmake_minimum_required(VERSION 3.5)
project(util)

# Default to C99
if(NOT CMAKE_C_STANDARD)
  set(CMAKE_C_STANDARD 99)
endif()

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(std_msgs)

rosidl_generate_interfaces(${PROJECT_NAME}
"msg/Num.msg"
  DEPENDENCIES std_msgs
 )

# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # uncomment the line when a copyright and license is not present in all source files
  #set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # uncomment the line when this package is not in a git repo
  #set(ament_cmake_cpplint_FOUND TRUE)

  find_package(std_msgs REQUIRED)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()

5. 编译util

cd ~/ros2_example_proj
colcon build --packages-select util

特别注意的是!自定义msg的迁移

  • ROS2自定义msg
  • https://docs.ros.org/en/galactic/Tutorials/Single-Package-Define-And-Use-Interface.html

  • Field names must be lowercase alphanumeric characters with underscores for separating words. They must start with an alphabetic character, they must not end with an underscore and never have two consecutive underscores.
  • msg文件中的header,在ros2中需写成std_msgs/Header
  • msg文件名不支持下划线
  • msg文件中的定义不支持大写字母
  • msg文件名首字母应大写 msg文件中不应有=的赋值操作
posted @ 2022-06-07 14:18  你看不见我的blog  阅读(231)  评论(0编辑  收藏  举报  来源