当前位置: 首页 > news >正文

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

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

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

1、构建消息体

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

string name
float64 height
float64 weight

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

 2、修改CMakelists.txt文件


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)

3、重新构建talker.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这个结构体即可。

4、重新构建listener.cpp文件

#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这个结构体就行。

5、顶层编译

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

6、准备运行talker和listener节点

        在运行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

http://www.lryc.cn/news/173262.html

相关文章:

  • 思科路由器:NAT的基础配置
  • 动态代理。
  • Learn Prompt-GPT-4:能力
  • iOS——ViewController的生命周期
  • SkyWalking内置参数与方法
  • 【C++面向对象侯捷】12.虚函数与多态 | 13.委托相关设计【设计模式 经典做法,类与类之间关联起来,太妙了,不断的想,不断的写代码】
  • 基于若依ruoyi-nbcio增加flowable流程待办消息的提醒,并提供右上角的红字数字提醒(五)
  • hive数据初始化
  • React+Node——next.js 构建前后端项目
  • CRM系统主要包括哪些功能?
  • Nginx location 精准匹配URL = /
  • 使用JAXB将Java对象转xml
  • Atlas 200 DK开发板问题总结
  • uniapp——实现二维码生成+保存二维码图片——基础积累
  • 零基础学前端(六)重点讲解 JavaScript
  • 数据库问题记录(粗略版)oracle、mysql等主流数据库通用
  • ORACLE多列中取出数据最大的一条
  • Xamarin.Android实现App内版本更新
  • 运维工程师面经
  • stm32之智能垃圾桶实战
  • 【C++面向对象侯捷下】2.转换函数 | 3.non-explicit-one-argument ctor
  • UOS Deepin Ubuntu Linux 开启 ssh 远程登录
  • Postman应用——接口请求和响应(Get和Post请求)
  • Linux查看哪些进程占用的系统 buffer/cache 较高 (hcache,lsof)命令
  • (Vue2)自定义创建项目、ESLint、Vuex
  • LLaMa
  • API(九)基于协程的并发编程SDK
  • JavaWeb 学习笔记 7:Filter
  • 【AI视野·今日Robot 机器人论文速览 第三十五期】Mon, 18 Sep 2023
  • Elasticsearch 在bool查询中使用分词器