• SLAM从入门到精通(消息传递)


    【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】

            前面我们只是编写了一个publisher节点,以及一个subscribe节点。有了这两个节点,它们之间就可以通信了。在实际生产中,我们除了简单的通信之外,要传递的数据可能还有很多。这个时候,我们就要构建一个消息体。这个消息体可以是各种脚本,但需要同时能被publisher和subscribe访问到。幸好,ros给我们提供了这样的一种形式。

    1、构建消息体

            在package下面创建一个msg文件夹。文件夹中保存一个Student.msg文件,它的内容如下所示,

    1. string name
    2. float64 height
    3. float64 weight

            这个文件其实只是一个脚本。通过在workspace顶层输入catkin_make,就可以生成对应的头文件Student.h。注意,头文件保存的位置位于./devel/include/beginner_tutorials/Student.h。编译之前也要修改下CMakelists.txt文件,如下面2所示。

     2、修改CMakelists.txt文件

    1. cmake_minimum_required(VERSION 2.8.3)
    2. project(beginner_tutorials)
    3. ## Find catkin and any catkin packages
    4. find_package(catkin REQUIRED COMPONENTS message_generation roscpp rospy std_msgs genmsg)
    5. add_message_files(FILES Student.msg)
    6. ## Declare ROS messages and services
    7. #add_message_files(FILES Num.msg)
    8. #add_service_files(FILES AddTwoInts.srv)
    9. ## Generate added messages and services
    10. generate_messages(DEPENDENCIES std_msgs)
    11. ## Declare a catkin package
    12. catkin_package()
    13. ## Build talker and listener
    14. include_directories(include ${catkin_INCLUDE_DIRS})
    15. add_executable(talker src/talker.cpp)
    16. target_link_libraries(talker ${catkin_LIBRARIES})
    17. add_dependencies(talker beginner_tutorials_generate_messages_cpp)
    18. add_executable(listener src/listener.cpp)
    19. target_link_libraries(listener ${catkin_LIBRARIES})
    20. add_dependencies(listener beginner_tutorials_generate_messages_cpp)

    3、重新构建talker.cpp文件

    1. #include "ros/ros.h"
    2. #include "beginner_tutorials/Student.h"
    3. #include <cstdlib>
    4. using namespace std;
    5. int main(int argc, char* argv[])
    6. {
    7. ros::init(argc, argv, "node_MyMsgPub");
    8. if(argc!=4)
    9. {
    10. cout << "Error command parameter!\n" \
    11. "Please run command eg:\n" \
    12. "rosrun begin_tutorials talker LiLei 180 160" << endl;
    13. return 1;
    14. }
    15. ros::NodeHandle nh;
    16. ros::Publisher MyMsgPub = nh.advertise<beginner_tutorials::Student>("MyMsg", 100);
    17. beginner_tutorials::Student sdtMsg;
    18. sdtMsg.name = argv[1];
    19. sdtMsg.height = atof(argv[2]);
    20. sdtMsg.weight = atof(argv[3]);
    21. ros::Rate rate(10);
    22. while(ros::ok())
    23. {
    24. MyMsgPub.publish(sdtMsg);
    25. rate.sleep();
    26. }
    27. return 0;
    28. }

            代码大部分和之前差不多,只要注意beginner_tutorials::Student这个结构体即可。

    4、重新构建listener.cpp文件

    1. #include "ros/ros.h"
    2. #include "beginner_tutorials/Student.h"
    3. void MyMsgCallback(const beginner_tutorials::Student& sdtInfo)
    4. {
    5. ROS_INFO("Thestudent information is:\n"
    6. "name:%s--height:%f--weight:%f\n",
    7. sdtInfo.name.c_str(),
    8. sdtInfo.height,
    9. sdtInfo.weight);
    10. }
    11. int main(int argc, char* argv[])
    12. {
    13. ros::init(argc, argv, "node_MyMsgSub");
    14. ros::NodeHandle nh;
    15. ros::Subscriber MyMsgSub = nh.subscribe("MyMsg", 1000, &MyMsgCallback);
    16. ros::spin();
    17. return 0;
    18. }

            和talker.cpp一样,代码中只需要关注下beginner_tutorials::Student这个结构体就行。

    5、顶层编译

            cd到workspace的顶层,直接输入catkin_make即可。

    6、准备运行talker和listener节点

            在运行talker和listener节点之前,有两件事情要做。第一步,打开roscore;第二步,source ./dev/setup.sh文件。最后,分别打开talker和listenner即可。

    1. rosrun beginner_tutorials talker LiLei 180 160
    2. rosrun beginner_tutorials listener

            没什么意外的化,你会看到这样的打印。如果能看到这些打印,基本上说明我们已经学会使用了ros的消息功能。

    1. [ INFO] [1695351213.590856004]: Thestudent information is:
    2. name:LiLei--height:180.000000--weight:160.000000
    3. [ INFO] [1695351213.692688848]: Thestudent information is:
    4. name:LiLei--height:180.000000--weight:160.000000
    5. [ INFO] [1695351213.791460924]: Thestudent information is:
    6. name:LiLei--height:180.000000--weight:160.000000
    7. [ INFO] [1695351213.891075380]: Thestudent information is:
    8. name:LiLei--height:180.000000--weight:160.000000
    9. [ INFO] [1695351213.990753652]: Thestudent information is:
    10. name:LiLei--height:180.000000--weight:160.000000
    11. [ INFO] [1695351214.090438037]: Thestudent information is:
    12. name:LiLei--height:180.000000--weight:160.000000
    13. [ INFO] [1695351214.190497066]: Thestudent information is:
    14. name:LiLei--height:180.000000--weight:160.000000
    15. [ INFO] [1695351214.291007141]: Thestudent information is:
    16. name:LiLei--height:180.000000--weight:160.000000
    17. [ INFO] [1695351214.390375087]: Thestudent information is:
    18. name:LiLei--height:180.000000--weight:160.000000
    19. [ INFO] [1695351214.490385779]: Thestudent information is:
    20. name:LiLei--height:180.000000--weight:160.000000

  • 相关阅读:
    自然语言处理:提取长文本进行文本主要内容(文本意思)概括 (两种方法,但效果都一般)
    1. js中let、var、const定义变量区别与方式
    【Python】【Flask】提交表单后报500错误
    kettle从入门到精通 第五十三课 ETL之kettle MQTT/RabbitMQ consumer实战
    Unix Network Programming Episode 58
    k8s各种配置文件详解,以及其使用方法
    用ffmpeg将视频转成gif动图
    软考 系统架构设计师系列知识点之边缘计算(3)
    黑马程序员spring+springMVC+Maven高级+springboot+MyBatisPlus总结之SpringMvc参数传递
    Java面试八股之Java中为什么没有全局变量
  • 原文地址:https://blog.csdn.net/feixiaoxing/article/details/133161803