c++实现ros通信

这里用的到是自定义的msg+cpp发布消息
主要包括两个msg,一个订阅者和一个发布者,以及cmakelists的相应修改。

首先是自定义的msg,功能包里面来自定义msg也是可以的:
新建功能包

catkin_create_pkg pkg roscpp std_msgs message_generation message_runtime

请添加图片描述
这里路劲没选对,是在src里面创建功能包。
其次就是新建一个msg文件夹,然后把一下msg添加进去:

##GlobalPathPlanningInterface.msg
float64  timestamp  #时间戳
float32[]  startpoint   #起点位置
float32[]   endpoint    #终点位置
string[]   routedata   #路径集合(所有途径点的集合)
float32  process_time    # 进程处理时间
int32 Global_Path_Segment_Index # 从1开始累加

int32 len_path # 全局路径的长度



#int8 Global_Path_End_Flag #终点置1,其他0
float32[]   last_point

int8 plan_over #0-等待规划完成 1-本次规划结束
##GpsImuAddInterface.msg
std_msgs/Header header       # ros时间戳
int64  gps_time              # 时间戳
float64  AngRateRawX         # 角速度x
float64  AngRateRawY         # 角速度y
float64  AngRateRawZ         # 角速度z
float64  AccelRawX             # x轴加速度
float64  AccelRawY             # y轴加速度
float64  AccelRawZ             # z轴加速度
float64 PosESigma           #东向位置标准差
float64 PosNSigma          #北向位置标准差
float64 PosUSigma          #天向位置标准差
float64  VelESigma         # 东向速度标准差
float64  VelNSigma        # 北向速度标准差
float64  VelUSigma        # 天向速度标准差
float64  VelSigma          # 车辆速度标准差
float64  AccelX         # 车辆坐标系x轴加速度
float64  AccelY         # 车辆坐标系y轴加速度
float64  AccelZ         # 车辆坐标系z轴加速度
float64  AngleHeadingSigma         # 偏航角标准差
float64  AnglePitchSigma        # 俯仰角标准差
float64  AngleRollSigma         # 横滚角标准差
float64  AngRateX                  # 车辆坐标系x轴角速度
float64  AngRateY                  # 车辆坐标系y轴角速度
float64  AngRateZ                  # 车辆坐标系z轴角速度
int32  GpsNumSats2Used      #辅天线使用卫星数
int32  GpsNumSats                #主天线搜星数
int32  GpsNumSats2              #副天线搜星数
float64 Rear_posX                  #后轴中心点UTM坐标x
float64 Rear_posY                  #后轴中心点UTM坐标y
float64 Rear_posZ                  #后轴中心点UTM坐标z
float32  process_time    # 进程处理时间

然后在CMakeLists.txt里面先把自定义的msg搞定:

##大部分都可以直接使用。
find_package(catkin REQUIRED COMPONENTS
  message_generation
  message_runtime
  roscpp
  std_msgs
  sensor_msgs
)
add_message_files(
  FILES
  GlobalPathPlanningInterface.msg
  GpsImuInterface.msg
)
generate_messages(
  DEPENDENCIES
  std_msgs
  sensor_msgs
)
catkin_package(
 CATKIN_DEPENDS message_generation message_runtime roscpp std_msgs
)

package.xml:
主要是添加message_runtime和message_generation的依赖内容

  <build_depend>message_generation</build_depend>
  <build_depend>message_runtime</build_depend>
  <exec_depend>message_runtime</exec_depend>
  <exec_depend>message_generation</exec_depend>

然后再是主程序:

//publisher.cpp

#include<iostream>
#include<ros/ros.h>
#include<pkg/GlobalPathPlanningInterface.h>
#include<pkg/GpsImuInterface.h>

using namespace std;

int main(int argc, char *argv[])
{
    ros::init(argc, argv, "pub");//初始化节点
    ros::NodeHandle nh;//初始化句柄nh
    ros::Publisher pub = nh.advertise<pkg::GlobalPathPlanningInterface>("global", 10);//话题名

    ros::Rate loop_rate(10);//设置发送频率
    while ( ros::ok())
    {
        ROS_INFO("success");
        pkg::GlobalPathPlanningInterface msg;
        msg.timestamp = 100;
        pub.publish(msg);
        ros::spinOnce();
        loop_rate.sleep();

    }
    return 0;
}

然后是订阅者:

//subscriber.cpp
#include<iostream>
#include<ros/ros.h>
#include<pkg/GlobalPathPlanningInterface.h>
#include<pkg/GpsImuInterface.h>

using namespace std;

void call_back(const pkg::GlobalPathPlanningInterface)
{
    ROS_INFO("sub success");
}

int main(int argc, char *argv[])
{
    ros::init(argc, argv, "sub");//初始化节点
    ros::NodeHandle nh;//初始化句柄nh
    ros::Publisher pub = nh.advertise<pkg::GpsImuInterface>("global2", 10);//话题名
    
    ros::Subscriber sub = nh.subscribe<pkg::GlobalPathPlanningInterface>("global", 10, call_back);
    ros::Rate loop_rate(10);//设置发送频率
    while ( ros::ok())
    {
        ROS_INFO("success");
        pkg::GpsImuInterface msg;
        msg.gps_time = 1000;
        pub.publish(msg);
        ros::spinOnce();
        loop_rate.sleep();

    }
    return 0;
}

接下来再修改一个CMakeLists.txt:
add_executable(velocity_publisher src/velocity_publisher.cpp)
target_link_libraries(velocity_publisher ${catkin_LIBRARIES})
有几个cpp就添加几个这个,向这里就这样添加:

add_executable(publisher src/publisher.cpp)
target_link_libraries(publisher ${catkin_LIBRARIES})

add_executable(subscriber src/subscriber.cpp)
target_link_libraries(subscriber ${catkin_LIBRARIES})

结束,编译就好了。
请添加图片描述

测试:
请添加图片描述
通信和数据都是没有问题的。
以上过程其实并没有什么难点,问题在于熟悉就行、

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白云千载尽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值