ROS 学习笔记(二):自定义消息msg+Publisher+Subscriber 示例运行
ROS 學習筆記(二):自定義消息msg+Publisher+Subscriber 示例運行
一、自定義消息:
1.新建msg文件夾,創建定義Person.msg 文件
mkdir -p ~/catkin_ws/src/mypackage/msg cd ~/catkin_ws/src/mypackage/msg touch Person.msg打開 Person.msg 文件,添加內容( std_msgs/Header header 是一個標準格式的頭信息(也可以不加))
std_msgs/Header header string name uint8 sex uint8 age2.修改添加~/catkin_ws/src/mypackage/package.xml 文件中內容
...<build_depend>message_generation</build_depend><exec_depend>message_runtime</exec_depend> ...3.修改添加~/catkin_ws/src/mypackage/CMakeList.txt 文件中內容
// 添加生成依賴的功能包 find_package(catkin REQUIRED COMPONENTS ... message_generation )// 添加 catkin 依賴 catkin_package( ... CATKIN_DEPENDS roscpp rospy std_msgs message_runtime )// 添加需要編譯的.msg文件 add_message_files( FILES Person.msg )generate_messages( DEPENDENCIES std_msgs )4.查看msg
cd ~/catkin_ws rosmsg show Person顯示:
[mypackage/Person]: string name uint8 sex uint8 age二、Publisher+Subscriber 示例:
1,~/catkin_ws/mypackage/src 目錄中,編寫倆個源文件talker.cpp和listener.cpp
(編譯器編寫或文本編寫)
talker.cpp 源碼:
#include "ros/ros.h" #include "std_msgs/ " #include "mypackage/Person.h" // 包含自定義信息頭文件 #include <sstream> #include <string.h>int main(int argc, char** argv) {//ROS 節點初始化//創建節點句柄ros::init(argc, argv, "talker"); // 節點名稱ros::NodeHandle n;// 話題topic的名稱:chatterros::Publisher chatter_pub = n.advertise<mypackage::Person>("chatter", 1000);ros::Rate loop_rate(10); // 話題發布循環時間int count = 0; // 計數std::string name("Allison");while( ros::ok() ){mypackage::Person msg;msg.header.seq = count;// 順序標識msg.header.stamp = ros::Time::now();// 時間戳msg.header.frame_id = "t_d_m";// IDmsg.sex = 1;msg.age = 12;msg.name = name;// 終端顯示自己發布的消息(可以不加)ROS_INFO("Publish/\n");ROS_INFO("Header/\n");ROS_INFO(" seq [%d]\n", msg.header.seq);ROS_INFO(" frame_id [%s]\n", msg.header.frame_id.c_str() );ROS_INFO(" name [%s]\n", msg.name.c_str() );ROS_INFO(" sex [%d]\n", msg.sex);ROS_INFO(" age [%d]\n", msg.age);// Publisher對象 chatter_pub 消息發布chatter_pub.publish(msg);ros::spinOnce();// 發布一次loop_rate.sleep();++count;// 計數}return 0; }listener.cpp 源碼:
#include <ros/ros.h> #include "std_msgs/String.h" #include "mypackage/Person.h"// 建立回調函數,當ros::spin()被觸發時,該函數被激活,處理接收到的消息 void chatterCalllback(const msg_json::json msg) {// 顯示訂閱的話題 "chatter" 發布的信息ROS_INFO("Header/\n");ROS_INFO(" seq [%d]\n", msg.header.seq);ROS_INFO(" frame_id [%s]\n", msg.header.frame_id.c_str() );ROS_INFO(" name [%s]\n", msg.name.c_str() );ROS_INFO(" sex [%d]\n", msg.sex);ROS_INFO(" age [%d]\n", msg.age); }int main(int argc, char** argv) {//ROS 節點初始化//創建節點句柄ros::init(argc, argv, "listener");ros::NodeHandle n; // 創建 publisherros::Subscriber sub = n.subscribe( "chatter", 1000, chatterCalllback );// 觸發回調函數,并循環處理,若處理一次則使用ros::spinOnce()ros::spin();return 0; }2 編譯功能包源碼(catkin_make方式編譯)
打開 CMakeList.txt 文件:
1)設置頭文件相對位置,默認為功能包的所在目錄
2)生成可執行文件 talker 和 listener
3)設置鏈接庫(系統或第三方庫函數)此處并沒有使用其他庫
4)設置依賴
5) 生成talker和listener 兩個可執行文件于 ~/catkin_ws/devel/lib/mypackage 中
CMakeList.txt 添加代碼如下:
## Build talker and listener include_directories(include ${catkin_INCLUDE_DIRS})add_executable(talker src/talker.cpp) target_link_libraries(talker ${catkin_LIBRARIES}) add_dependencies(talker ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})add_executable(listener src/listener.cpp) target_link_libraries(listener ${catkin_LIBRARIES}) add_dependencies(talker ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})3.終端工作空間根目錄 ~/catkin_ws 開始編譯
cd ~/catkin_ws catkin_make source devel/setup.bash // 已在于終端環境變量文件中設置的無需重新設置三、運行 talker listener
運行ros master
cd ~/catkin_ws roscore運行talker
rosrun mypackage talker運行listenter
rosrun mypackage listener四、創建launch 啟動文件
4.創建launch文件夾,創建啟動文件 mypackage.launch
cd catkin_ws/src/mypackage/ mkdir launch cd launch/ touch mypackage.launchmypackage.launch文件內容,設置同時啟動兩個節點:
<launch><node pkg="mypackage" type="talker" name="talker"/><node pkg="mypackage" type="listener" name="listener" output="screen"/> </launch>5.運行launch文件并查看消息(默認會運行roscore)
roslaunch mypackage mypackage.launch五、其他指令
1.顯示活動中的話題列表:
rostopic list2.顯示話題消息內容:(不運行 listener)
rostopic echo /chatter3.發布話題內容:(不運行 talker)
rostopic pub /chatter總結
以上是生活随笔為你收集整理的ROS 学习笔记(二):自定义消息msg+Publisher+Subscriber 示例运行的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IAR STM32工程报错Error[P
- 下一篇: tensorflow中创建多个计算图(G