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

c++下的ros通信(cmake的报错问题多)

1.自定义msg

这里的自定义msg和python的其实是一样的:
首先在src目录下

catkin_create_pkg car_interfaces rospy roscpp std_msgs message_runtime message_generation

然后新建一个msg文件夹,然后建立相应的msg文件,接着就可以修改编译所需的东西了
定义的msg就自己想怎么写就怎么写吧
首先是CMakeLists.txt:

cmake_minimum_required(VERSION 3.0.2)
project(car_interfaces)## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTSmessage_generationmessage_runtimeroscpprospystd_msgs
)add_message_files(FILESGlobalPathPlanningInterface.msgGpsImuInterface.msg
)generate_messages(DEPENDENCIESstd_msgs
)catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES car_interfacesCATKIN_DEPENDS message_generation message_runtime roscpp rospy std_msgs
#  DEPENDS system_lib
)include_directories(
# include${catkin_INCLUDE_DIRS}
)

然后是package.xml:

<?xml version="1.0"?>
<package format="2"><name>car_interfaces</name><version>0.0.0</version><description>The car_interfaces package</description><!-- One maintainer tag required, multiple allowed, one person per tag --><!-- Example:  --><!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> --><maintainer email="cyun@todo.todo">cyun</maintainer><!-- One license tag required, multiple allowed, one license per tag --><!-- Commonly used license strings: --><!--   BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 --><license>TODO</license><!-- Url tags are optional, but multiple are allowed, one per tag --><!-- Optional attribute type can be: website, bugtracker, or repository --><!-- Example: --><!-- <url type="website">http://wiki.ros.org/car_interfaces</url> --><!-- Author tags are optional, multiple are allowed, one per tag --><!-- Authors do not have to be maintainers, but could be --><!-- Example: --><!-- <author email="jane.doe@example.com">Jane Doe</author> --><!-- The *depend tags are used to specify dependencies --><!-- Dependencies can be catkin packages or system dependencies --><!-- Examples: --><!-- Use depend as a shortcut for packages that are both build and exec dependencies --><!--   <depend>roscpp</depend> --><!--   Note that this is equivalent to the following: --><!--   <build_depend>roscpp</build_depend> --><!--   <exec_depend>roscpp</exec_depend> --><!-- Use build_depend for packages you need at compile time: --><!--   <build_depend>message_generation</build_depend> --><!-- Use build_export_depend for packages you need in order to build against this package: --><!--   <build_export_depend>message_generation</build_export_depend> --><!-- Use buildtool_depend for build tool packages: --><!--   <buildtool_depend>catkin</buildtool_depend> --><!-- Use exec_depend for packages you need at runtime: --><!--   <exec_depend>message_runtime</exec_depend> --><!-- Use test_depend for packages you need only for testing: --><!--   <test_depend>gtest</test_depend> --><!-- Use doc_depend for packages you need only for building documentation: --><!--   <doc_depend>doxygen</doc_depend> --><buildtool_depend>catkin</buildtool_depend><build_depend>message_generation</build_depend><build_depend>message_runtime</build_depend><build_depend>roscpp</build_depend><build_depend>rospy</build_depend><build_depend>std_msgs</build_depend><build_export_depend>roscpp</build_export_depend><build_export_depend>rospy</build_export_depend><build_export_depend>std_msgs</build_export_depend><exec_depend>message_runtime</exec_depend><exec_depend>message_generation</exec_depend><exec_depend>roscpp</exec_depend><exec_depend>rospy</exec_depend><exec_depend>std_msgs</exec_depend><!-- The export tag contains other, unspecified, tags --><export><!-- Other tools can request additional information be placed here --></export>
</package>

基本上就按照这个结构来写,然后正常编译就可以了

2.ros c++联合编程语言

#include<ros/ros.h>
#include<car_interfaces/GlobalPathPlanningInterface.h>
// #include<car_interfaces/GpsImuInterface.h>int main(int argc, char *argv[])
{   ros::init(argc, argv, "plan_node") ;ros::NodeHandle nh;ros::Publisher pub = nh.advertise<car_interfaces::GlobalPathPlanningInterface>("global",10);// ros::Publisher pub2 = nh.advertise<car_interfaces::GpsImuInterface>("gps",10);ros::Rate loop_rate(10);while (ros::ok()){ ROS_INFO("SUCCESS");car_interfaces::GlobalPathPlanningInterface msg1;// car_interfaces::GpsImuInterface msg2;msg1.timestamp = 1000; msg1.process_time = 230;// msg2.gps_time = 10000;pub.publish(msg1);// pub2.publish(msg2);ros::spinOnce(); loop_rate.sleep();} return 0;}

这个是发布的部分,注意思路,将接收的全部开成一个线程,将发布的话题每个都写成一个线程。
然后是发布的数据

#include <ros/ros.h>
#include <car_interfaces/GlobalPathPlanningInterface.h>
#include <car_interfaces/GpsImuInterface.h>// 回调函数
void plan_message_callback(const car_interfaces::GlobalPathPlanningInterface::ConstPtr& msg)
{double timestamp = msg->timestamp;float process_time = msg->process_time;ROS_INFO("Received plan");
}int main(int argc, char* argv[])
{ros::init(argc, argv, "plan_sub");ros::NodeHandle nh;ros::Publisher pub = nh.advertise<car_interfaces::GpsImuInterface>("pub2", 10);ros::Subscriber sub = nh.subscribe("global", 10, plan_message_callback);ros::Rate loop_rate(10);while (ros::ok()){car_interfaces::GpsImuInterface msg;msg.gps_time = 10000;pub.publish(msg);ros::spinOnce();loop_rate.sleep();}return 0;
}

然后修改相应的CMameLists.txt:

cmake_minimum_required(VERSION 3.0.2)
project(planning)## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTScar_interfacesroscpprospystd_msgs
)catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES planning
#  CATKIN_DEPENDS car_interfaces roscpp rospy std_msgs
#  DEPENDS system_lib
)## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
# include${catkin_INCLUDE_DIRS}
)add_executable(${PROJECT_NAME}_node src/plan.cpp)
add_executable(plan_sub_node src/plan_sub.cpp)add_dependencies(${PROJECT_NAME}_node car_interfaces_generate_messages_cpp)# Specify libraries to link a library or executable target against
target_link_libraries(${PROJECT_NAME}_node${catkin_LIBRARIES}
)target_link_libraries(plan_sub_node${catkin_LIBRARIES}
)

package.xml:

<?xml version="1.0"?>
<package format="2"><name>planning</name><version>0.0.0</version><description>The planning package</description><!-- One maintainer tag required, multiple allowed, one person per tag --><!-- Example:  --><!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> --><maintainer email="cyun@todo.todo">cyun</maintainer><!-- One license tag required, multiple allowed, one license per tag --><!-- Commonly used license strings: --><!--   BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 --><license>TODO</license><!-- Url tags are optional, but multiple are allowed, one per tag --><!-- Optional attribute type can be: website, bugtracker, or repository --><!-- Example: --><!-- <url type="website">http://wiki.ros.org/planning</url> --><!-- Author tags are optional, multiple are allowed, one per tag --><!-- Authors do not have to be maintainers, but could be --><!-- Example: --><!-- <author email="jane.doe@example.com">Jane Doe</author> --><!-- The *depend tags are used to specify dependencies --><!-- Dependencies can be catkin packages or system dependencies --><!-- Examples: --><!-- Use depend as a shortcut for packages that are both build and exec dependencies --><!--   <depend>roscpp</depend> --><!--   Note that this is equivalent to the following: --><!--   <build_depend>roscpp</build_depend> --><!--   <exec_depend>roscpp</exec_depend> --><!-- Use build_depend for packages you need at compile time: --><!--   <build_depend>message_generation</build_depend> --><!-- Use build_export_depend for packages you need in order to build against this package: --><!--   <build_export_depend>message_generation</build_export_depend> --><!-- Use buildtool_depend for build tool packages: --><!--   <buildtool_depend>catkin</buildtool_depend> --><!-- Use exec_depend for packages you need at runtime: --><!--   <exec_depend>message_runtime</exec_depend> --><!-- Use test_depend for packages you need only for testing: --><!--   <test_depend>gtest</test_depend> --><!-- Use doc_depend for packages you need only for building documentation: --><!--   <doc_depend>doxygen</doc_depend> --><buildtool_depend>catkin</buildtool_depend><build_depend>car_interfaces</build_depend><build_depend>roscpp</build_depend><build_depend>rospy</build_depend><build_depend>std_msgs</build_depend><build_export_depend>car_interfaces</build_export_depend><build_export_depend>roscpp</build_export_depend><build_export_depend>rospy</build_export_depend><build_export_depend>std_msgs</build_export_depend><exec_depend>car_interfaces</exec_depend><exec_depend>roscpp</exec_depend><exec_depend>rospy</exec_depend><exec_depend>std_msgs</exec_depend><!-- The export tag contains other, unspecified, tags --><export><!-- Other tools can request additional information be placed here --></export>
</package>

以上完成后就可以建立c++的ros通信了

后面要做的事:
1.把这个结构给完全理解并深入掌握
2.按照相应的规则重新写线程

相关文章:

c++下的ros通信(cmake的报错问题多)

1.自定义msg 这里的自定义msg和python的其实是一样的&#xff1a; 首先在src目录下 catkin_create_pkg car_interfaces rospy roscpp std_msgs message_runtime message_generation然后新建一个msg文件夹&#xff0c;然后建立相应的msg文件&#xff0c;接着就可以修改编译所需…...

测试必备 | 测试工程师必知的Linux命令有哪些?

在日常的测试工作中&#xff0c;涉及到测试环境搭建及通过查看日志来定位相关问题时经常会用到Linux&#xff0c;在测试工程师的面试中也经常会有笔试或面试的题目来考查测试人员对Linux的熟悉程度&#xff0c;这里分享下测试工程师需知的 Linux 命令有哪些。 Linux 作为一种常…...

成集云 | 药师帮集成英克ERP接口 | 解决方案

源系统成集云目标系统 业务背景 药师帮是一家专注于医药行业的电商平台&#xff0c;提供医药产品在线采购、销售和物流等一站式服务。药师帮致力于用数字化赋能院外医药市场的参与者&#xff0c;包括药企、药品分销商、药店及基层医疗机构&#xff0c;努力以安全高效…...

ICPC 2022 网络赛 d ( 数位dp + 二分

#include<bits/stdc.h> using namespace std; using VI vector<int>; using ll long long; const int mod 998244353;ll n; int d[100]; int dp[60][40][40][2]; set<int> s; //枚举数位&#xff0c;枚举这一位余数是几 //每一位的限制&#xff0c; int d…...

透视俄乌网络战之二:Conti勒索软件集团(下)

透视俄乌网络战之一&#xff1a;数据擦除软件 透视俄乌网络战之二&#xff1a;Conti勒索软件集团&#xff08;上&#xff09; Conti勒索软件集团&#xff08;下&#xff09; 1. 管理面板源代码2. Pony凭证窃取恶意软件3. TTPs4. Conti Locker v2源代码5. Conti团伙培训材料6. T…...

网络安全深入学习第一课——热门框架漏洞(RCE-命令执行)

文章目录 一、RCE二、命令执行/注入-概述三、命令执行-常见函数四、PHP命令执行-常见函数1、exec&#xff1a;2、system3、passthru4、shell_exec5、反引号 backquote 五、PHP命令执行-常见函数总结六、命令执行漏洞成因七、命令执行漏洞利用条件八、命令执行漏洞分类1、代码层…...

应用在电子体温计中的国产温度传感芯片

电子体温计由温度传感芯片&#xff0c;液晶显示器&#xff0c;纽扣电池&#xff0c;专用集成电路及其他电子元器件组成。能快速准确地测量人体体温&#xff0c;与传统的水银玻璃体温计相比&#xff0c;具有读数方便&#xff0c;测量时间短&#xff0c;测量精度高&#xff0c;能…...

JVM 虚拟机 ----> Java 内存模型(JMM)

文章目录 Java 内存模型&#xff08;JMM&#xff09;一、运行时数据区域划分二、程序计数器&#xff08;Program Counter Register&#xff09;计数器的作用 三、Java 虚拟机栈&#xff08;VM Stack&#xff09;四、本地方法栈&#xff08;Native Method Stack&#xff09;五、…...

指针-字符串替换

任务描述 从标准输入读入数据&#xff0c;每行中最多包含一个字符串 “_xy_”&#xff0c;且除了字符串“_xy_”外&#xff0c;输入数据中不包括下划线字符&#xff0c;请将输入行中的 “_xy_” 替换为 “_ab_”, 在标准输出上输出替换后的结果&#xff1b;若没有进行过满足条…...

docker 网络(单机环境)

文章目录 深入理解 Namespace什么是NamespaceNamespace当中的 Network Namespace Libcontainerdocker 网络基础创建两个命名空间创建网络接口 veth pair命名空间添加 veth 接口为 veth 接口分配 IP启动 veth 接口相互 ping bridge 网络搭建网络环境查看docker0 网桥创建网桥 br…...

14、二叉树的morris遍历等

统计热词 有一个包含100亿个URL的大文件&#xff0c;假设每个URL占用64B&#xff0c;请找出其中所有重复的URL 【补充】 某搜索公司一天的用户搜索词汇是海量的(百亿数据量)&#xff0c;请设计一种求出每天热门Top100 词汇的可行办法 多个小文件的大根堆&#xff0c;然后把每…...

BeanFactory与ApplicationContext

BeanFactory与ApplicationContext的区别 使用Alt Ctrl U查看java类图 什么是BeanFactory接口 他是ApplicationContext的父接口他才是Spring 的核心容器&#xff0c;主要的ApplicationContext功能的实现都间接通过BeanFactory接口来实现 在ApplicationContext类中方法的实现是…...

【计算机网络】 粘包问题

文章目录 为什么会产生粘包问题&#xff1f;解决办法先发包大小再发包内容代码示例 为什么会产生粘包问题&#xff1f; tcp是数据流传输&#xff0c;是一种没有边界的&#xff0c;可以合并的传输数据方式。合并就要能拆开&#xff0c;拆不开就是粘包。 解决办法 设置标志位&a…...

valgrind massif 详解(内存分配释放分析)

参考 https://valgrind.org/docs/manual/ms-manual.html 使用格式 valgrind --toolmassif [--massif-opts] prog [prog-args]目的 记录每一次的malloc, free; 概念: malloc申请内存, 实际分配内存(字节对齐, 分配器的记录头, 等等原因) 对内存进行分析, 优化, 以达到资源…...

使用命令行创建一个vue项目卡住不动如何解决

问题 在使用命令去创建一个vue项目&#xff0c; 出现下面卡住不动的一个状态。 解决方案一 首先先ctrlc停止进入创建好的项目文件手动输入npm install 、npm run dev如果npm run dev 的时候 出现 ‘vite’ 相关的错误查看node版本是否是最新的稳定版本node -v查看安装源是否…...

七天学会C语言-第一天(C语言基本语句)

一、固定格式 这个是C程序的基本框架&#xff0c;需要记住&#xff01;&#xff01;&#xff01; #include<stdio.h>int main(){return 0; }二、printf 语句 简单输出一句C程序&#xff1a; #include<stdio.h> int main(){printf("大家好&#xff0c;&quo…...

vue项目部署,出现两个ip的原因

我宁愿靠自己的力量打开我的前途,而不愿求有力者的垂青。——雨果 tags: 篇首语&#xff1a;本文由小常识网(cha138.com)小编为大家整理&#xff0c;主要介绍了vue项目部署&#xff0c;出现两个ip的原因相关的知识&#xff0c;希望对你有一定的参考价值。 参考技术A 在部署v…...

无涯教程-JavaScript - ASIN函数

描述 ASIN函数返回给定数字的反正弦或反正弦,并返回以弧度表示的Angular,介于-π/2和π/2之间。 语法 ASIN (number)争论 Argument描述Required/OptionalNumberThe sine of the angle you want and must be from -1 to 1.Required Notes 如果您希望ASIN函数返回的Angular以…...

MYSQL的SQL优化

insert语句 开启事务 手动控制事务 start transaction; insert into tb_test values(1,Tom),(2,Cat),(3,Jerry); insert into tb_test values(4,Tom),(5,Cat),(6,Jerry); insert into tb_test values(7,Tom),(8,Cat),(9,Jerry); commit; 内存插入 load命令中用 fields te…...

lintcode 553 · 炸弹袭击【中等 数组+bfs+模拟】

题目 https://www.lintcode.com/problem/553 给定一个二维矩阵, 每一个格子可能是一堵墙 W,或者 一个敌人 E 或者空 0 (数字 0), 返回你可以用一个炸弹杀死的最大敌人数. 炸弹会杀死所有在同一行和同一列没有墙阻隔的敌人。 由于墙比较坚固&#xff0c;所以墙不会被摧毁.你只…...

ngx_http_create_request

1 定义 ngx_http_create_request 函数 定义在 ./nginx-1.24.0/src/http/ngx_http_request.cngx_http_request_t * ngx_http_create_request(ngx_connection_t *c) {ngx_http_request_t *r;ngx_http_log_ctx_t *ctx;ngx_http_core_loc_conf_t *clcf;r ngx_http_…...

别再复制粘贴了!手把手教你封装一个可复用的Qt文本编辑器核心组件类

从零封装高复用Qt文本编辑器核心类&#xff1a;工程化实践指南 在Qt开发中&#xff0c;文本编辑器是最常见的功能需求之一。许多开发者习惯将所有逻辑堆砌在MainWindow类中&#xff0c;导致代码臃肿、难以维护和复用。本文将带你从工程化角度重构文本编辑器&#xff0c;将其核心…...

智能产品系统架构分析 - 智能办公系统架构分层

方向&#xff1a;方案分析、架构设计、模块分解 智能产品系统架构分析&#xff1a;智能办公系统架构分层。 对智能办公系统进行架构分层分析&#xff0c;给出实例、UML建模、项目结构等。 “智能产品系统架构分析&#xff1a;智能办公系统架构分层”。 包含设备控制、预约管…...

自用便捷图床 API 分享|支持 Token 鉴权、图片上传、删除,稳定可用

在日常写博客、做笔记、开发项目时&#xff0c;经常需要上传图片获取在线链接&#xff0c;支持获取上传凭证、图片上传、图片删除全套接口&#xff0c;开箱即用&#xff0c;下面完整分享接口文档与调用示例。 图床主页&#xff1a;https://imgbeduser.hlytools.top/ 一、整体…...

2026年搜索引擎大变革:生成式优化服务如何引领未来趋势

随着AI技术的不断进步&#xff0c;搜索引擎领域正在经历一场前所未有的变革。2026年&#xff0c;我们见证了从传统SEO到生成式引擎优化&#xff08;GEO&#xff09;的重大转变。这场变革不仅改变了用户获取信息的方式&#xff0c;也为企业带来了全新的营销机遇。本文将深入探讨…...

计算机视觉导航评估框架:从算法指标到用户体验的完整闭环

1. 项目概述&#xff1a;为什么我们需要一个“导航评估框架”&#xff1f;在计算机视觉辅助视障人士导航这个领域&#xff0c;我见过太多“实验室里的英雄”和“现实中的矮子”。一个算法在精心布置的走廊里识别障碍物准确率高达99.9%&#xff0c;但一到人潮涌动的火车站广场&a…...

GLIGEN图像空间控制:用边界框实现像素级精准生成

1. GLIGEN&#xff1a;不是又一个“AI画图玩具”&#xff0c;而是图像生成控制权的真正移交你有没有试过对着 Stable Diffusion 的提示词框反复修改半小时&#xff0c;就为了把一只猫准确地放在沙发左边、让咖啡杯稳稳立在桌面上、让窗外的梧桐树只出现在画面右上角——结果生成…...

基于OpenClaw的GitHub趋势智能监控器:自动化追踪与AI摘要推送

1. 项目概述&#xff1a;一个为开发者打造的GitHub趋势智能监控器 作为一名长期泡在GitHub上的开发者&#xff0c;我深知每天手动刷“Trending”页面有多低效。热门项目层出不穷&#xff0c;但真正值得关注的往往就那么几个&#xff0c;而且很容易被淹没在信息流里。直到我遇到…...

对比按量计费与Token Plan套餐,哪种方式更适合你的项目

&#x1f680; 告别海外账号与网络限制&#xff01;稳定直连全球优质大模型&#xff0c;限时半价接入中。 &#x1f449; 点击领取海量免费额度 对比按量计费与Token Plan套餐&#xff0c;哪种方式更适合你的项目 在接入大模型服务时&#xff0c;成本控制是每个开发者和团队都…...

基于图特征选择与XGBoost的电动公交预测性维护模型构建

1. 项目概述&#xff1a;从数据洪流到精准预警的挑战在电动公交的日常运营中&#xff0c;车辆控制器局域网&#xff08;CAN&#xff09;总线每秒都在产生海量的传感器数据&#xff0c;从电池电压、电机温度到刹车片厚度&#xff0c;这些数据流如同车辆的“生命体征”。预测性维…...