【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】
前面我们只是编写了一个publisher节点,以及一个subscribe节点。有了这两个节点,它们之间就可以通信了。在实际生产中,我们除了简单的通信之外,要传递的数据可能还有很多。这个时候,我们就要构建一个消息体。这个消息体可以是各种脚本,但需要同时能被publisher和subscribe访问到。幸好,ros给我们提供了这样的一种形式。

在package下面创建一个msg文件夹。文件夹中保存一个Student.msg文件,它的内容如下所示,
- string name
- float64 height
- float64 weight
这个文件其实只是一个脚本。通过在workspace顶层输入catkin_make,就可以生成对应的头文件Student.h。注意,头文件保存的位置位于./devel/include/beginner_tutorials/Student.h。编译之前也要修改下CMakelists.txt文件,如下面2所示。
-
- cmake_minimum_required(VERSION 2.8.3)
- project(beginner_tutorials)
-
- ## Find catkin and any catkin packages
- find_package(catkin REQUIRED COMPONENTS message_generation roscpp rospy std_msgs genmsg)
-
- add_message_files(FILES Student.msg)
-
- ## Declare ROS messages and services
- #add_message_files(FILES Num.msg)
- #add_service_files(FILES AddTwoInts.srv)
-
- ## Generate added messages and services
- generate_messages(DEPENDENCIES std_msgs)
-
- ## Declare a catkin package
- catkin_package()
-
- ## 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 beginner_tutorials_generate_messages_cpp)
-
- add_executable(listener src/listener.cpp)
- target_link_libraries(listener ${catkin_LIBRARIES})
- add_dependencies(listener beginner_tutorials_generate_messages_cpp)
-
- #include "ros/ros.h"
- #include "beginner_tutorials/Student.h"
- #include <cstdlib>
-
- using namespace std;
-
- int main(int argc, char* argv[])
- {
- ros::init(argc, argv, "node_MyMsgPub");
- if(argc!=4)
- {
- cout << "Error command parameter!\n" \
- "Please run command eg:\n" \
- "rosrun begin_tutorials talker LiLei 180 160" << endl;
- return 1;
- }
-
- ros::NodeHandle nh;
-
- ros::Publisher MyMsgPub = nh.advertise<beginner_tutorials::Student>("MyMsg", 100);
- beginner_tutorials::Student sdtMsg;
- sdtMsg.name = argv[1];
- sdtMsg.height = atof(argv[2]);
- sdtMsg.weight = atof(argv[3]);
-
- ros::Rate rate(10);
- while(ros::ok())
- {
- MyMsgPub.publish(sdtMsg);
- rate.sleep();
- }
-
- return 0;
- }
代码大部分和之前差不多,只要注意beginner_tutorials::Student这个结构体即可。
- #include "ros/ros.h"
- #include "beginner_tutorials/Student.h"
-
- void MyMsgCallback(const beginner_tutorials::Student& sdtInfo)
- {
- ROS_INFO("Thestudent information is:\n"
- "name:%s--height:%f--weight:%f\n",
- sdtInfo.name.c_str(),
- sdtInfo.height,
- sdtInfo.weight);
- }
-
- int main(int argc, char* argv[])
- {
- ros::init(argc, argv, "node_MyMsgSub");
-
- ros::NodeHandle nh;
- ros::Subscriber MyMsgSub = nh.subscribe("MyMsg", 1000, &MyMsgCallback);
- ros::spin();
-
- return 0;
- }
和talker.cpp一样,代码中只需要关注下beginner_tutorials::Student这个结构体就行。
cd到workspace的顶层,直接输入catkin_make即可。
在运行talker和listener节点之前,有两件事情要做。第一步,打开roscore;第二步,source ./dev/setup.sh文件。最后,分别打开talker和listenner即可。
- rosrun beginner_tutorials talker LiLei 180 160
- rosrun beginner_tutorials listener
没什么意外的化,你会看到这样的打印。如果能看到这些打印,基本上说明我们已经学会使用了ros的消息功能。
- [ INFO] [1695351213.590856004]: Thestudent information is:
- name:LiLei--height:180.000000--weight:160.000000
-
- [ INFO] [1695351213.692688848]: Thestudent information is:
- name:LiLei--height:180.000000--weight:160.000000
-
- [ INFO] [1695351213.791460924]: Thestudent information is:
- name:LiLei--height:180.000000--weight:160.000000
-
- [ INFO] [1695351213.891075380]: Thestudent information is:
- name:LiLei--height:180.000000--weight:160.000000
-
- [ INFO] [1695351213.990753652]: Thestudent information is:
- name:LiLei--height:180.000000--weight:160.000000
-
- [ INFO] [1695351214.090438037]: Thestudent information is:
- name:LiLei--height:180.000000--weight:160.000000
-
- [ INFO] [1695351214.190497066]: Thestudent information is:
- name:LiLei--height:180.000000--weight:160.000000
-
- [ INFO] [1695351214.291007141]: Thestudent information is:
- name:LiLei--height:180.000000--weight:160.000000
-
- [ INFO] [1695351214.390375087]: Thestudent information is:
- name:LiLei--height:180.000000--weight:160.000000
-
- [ INFO] [1695351214.490385779]: Thestudent information is:
- name:LiLei--height:180.000000--weight:160.000000
-