包目录下建立目录
mkdir pkg/{msg,srv,action}
在对应目录下制定 .msg .srv .action文件的消息格式.
数据类型的定义参考
-
## 修改 CMakeLists.txt
-
-
find_package(rosidl_default_generators REQUIRED)
-
-
rosidl_generate_interfaces(${PROJECT_NAME}
-
"msg/Num.msg"
-
"srv/AddThreeInts.srv"
-
)
-
ament_export_dependencies(rosidl_default_runtime)
-
还有一种写法, 就是CMakefile设置变量
set(msg_files "msg/AddressBook.msg" )
rosidl_generate_interfaces(${PROJECT_NAME} ${msg_files} )
-
<build_depend>rosidl_default_generators</build_depend>
-
-
<exec_depend>rosidl_default_runtime</exec_depend>
-
-
<member_of_group>rosidl_interface_packages</member_of_group>
-
编译
-
colcon build --packages-select tutorial_interfaces
-
. install/setup.bash
-
-
ros2 interface show tutorial_interfaces/msg/Num
-
ros2 interface show tutorial_interfaces/srv/AddThreeInts
-
被引用的方法
-
代码
-
#include "tutorial_interfaces/msg/num.hpp" // CHANGE
-
-
CMakeLists.txt:
-
find_package(tutorial_interfaces REQUIRED) # CHANGE
-
ament_target_dependencies(talker rclcpp tutorial_interfaces) # CHANGE
-
ament_target_dependencies(listener rclcpp tutorial_interfaces) # CHANGE
-
-
package.xml
-
<depend>tutorial_interfaces</depend> --CPP
-
<exec_depend>tutorial_interfaces</exec_depend> -- python
-
-
------------------------------------------------
-
Srv文件加入
-
代码
-
#include "tutorial_interfaces/srv/add_three_ints.hpp" // CHANGE
-
from tutorial_interfaces.srv import AddThreeInts # CHANGE
-
-
CMakeLists.txt修改
-
find_package(tutorial_interfaces REQUIRED) # CHANGE
-
ament_target_dependencies(server
-
rclcpp tutorial_interfaces) #CHANGE
-
-
package.xml 修改
-
<depend>tutorial_interfaces</depend> -- CPP
-
<exec_depend>tutorial_interfaces</exec_depend> -- python
----------------------------------------------------------------------------------------------------------------------
-
.action 文件的编译
-
-
修改 CMakeLists.txt
-
find_package(rosidl_default_generators REQUIRED)
-
-
rosidl_generate_interfaces(${PROJECT_NAME}
-
"action/Fibonacci.action"
-
)
-
-
修改 package.xml
-
<buildtool_depend>rosidl_default_generators</buildtool_depend>
-
-
<depend>action_msgs</depend>
-
-
<member_of_group>rosidl_interface_packages</member_of_group>
-
-
-
编译后查询是否ok, 例如
-
ros2 interface show action_tutorials/action/Fibonacci
使用 action
阅读(8407) | 评论(0) | 转发(0) |