自定义msg实现话题通信

1、在功能包下创建msg文件夹,在msg文件夹中创建Person.msg文件

内容为:

string name
int32 age
float32 height

编译生成可以被 Python 或 C++ 调用的中间文件

2、编辑配置文件

package.xml中添加编译依赖与执行依赖

  <build_depend>message_generation</build_depend>
  <exec_depend>message_runtime</exec_depend>
  <!-- 
  exce_depend 以前对应的是 run_depend 现在非法
  -->

CMakeLists.txt编辑 msg 相关配置

find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  message_generation
)
# 需要加入 message_generation,必须有 std_msgs
## 配置 msg 源文件
add_message_files(
  FILES
  Person.msg
)
# 生成消息时依赖于 std_msgs
generate_messages(
  DEPENDENCIES
  std_msgs
)
#执行时依赖
catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES demo02_talker_listener
  CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
#  DEPENDS system_lib
)

3.编译

编译后的中间文件查看:

C++ 需要调用的中间文件(.../工作空间/devel/include/包名/xxx.h)

Python 需要调用的中间文件(.../工作空间/devel/lib/python3/dist-packages/包名/msg)

后续调用相关 msg 时,是从这些中间文件调用的

4、vscode 配置

为了方便代码提示以及避免误抛异常,需要先配置 vscode,将前面生成的 head 文件路径配置进 c_cpp_properties.json 的 includepath属性:

{
    "configurations": [
        {
            "browse": {
                "databaseFilename": "",
                "limitSymbolsToIncludedHeaders": true
            },
            "includePath": [
                "/opt/ros/noetic/include/**",
                "/usr/include/**",
                "/xxx/yyy工作空间/devel/include/**" //配置 head 文件的路径 
            ],
            "name": "ROS",
            "intelliSenseMode": "gcc-x64",
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17"
        }
    ],
    "version": 4
}

5、编写分布方

#include "ros/ros.h"
#include "plumbing_pub_sub/Person.h"

/*
    发布方:发布人的消息
    1、包含头文件
         #include "plumbing_pub_sub/Person.h"
    2、初始化ros节点
    3、创建ros句柄
    4、创建发布者对象
    5、编写发布逻辑,发布数据
*/


int main(int argc, char *argv[])
{   
    // 2、初始化ros节点
    ros::init(argc,argv,"banZhuRen");
    setlocale(LC_ALL,"");
    // 3、创建ros句柄
    ros::NodeHandle nh;
    //4、创建发布者对象
    ros::Publisher pub=nh.advertise<plumbing_pub_sub::Person>("liaoTiantian",10);
    //5、编写发布逻辑,发布数据
    //5-1创建被发布的数据
    plumbing_pub_sub::Person person;
    person.name="张三";
    person.age=1;
    person.height=1.73;
    //5-2设置发布频率
    ros::Rate rate(1);
    //5-3循环发布数据
        while(ros::ok)
    {
      //修改数据  
      person.age+=1;  
      //核心:数据发布
      pub.publish(person);
      //休眠
      rate.sleep();
      //建议
      ros::spinOnce();
    }
    
    return 0;
}

查看话题内容:

进入工作空间

1、rostopic list

(liaoTian)

2、source ./devel/setup.bash (因为是自定义的话题,所以要导入环境变量)

3、rostopic  echo liaoTian 

hao@haoBox:~$ cd demo03_ws/
hao@haoBox:~/demo03_ws$ source ./devel/setup.bash 
hao@haoBox:~/demo03_ws$ rostopic echo liaoTian
name: !!python/str "\u5F20\u4E09"
age: 133
height: 1.73000001907
---
 

6、配置发布方

add_executable(demo03_pub_person src/demo03_pub_person.cpp)

add_dependencies(demo03_pub_person ${PROJECT_NAME}_generate_messages_cpp)

target_link_libraries(demo03_pub_person   ${catkin_LIBRARIES}
)

//注意加对位置

 7、编写订阅方

#include "ros/ros.h"
#include "plumbing_pub_sub/Person.h"
/*
    发布方:订阅消息
    1、包含头文件
         #include "plumbing_pub_sub/Person.h"
    2、初始化ros节点
    3、创建ros句柄
    4、创建订阅者对象
    5、编写回调函数,编写订阅数据
    6、调用视频()函数
*/


void doPerson(const plumbing_pub_sub::Person::ConstPtr&person)
{
    ROS_INFO("订阅人的信息:%s,%d,%.2f",person->name.c_str(),person->age,person->height);

}


int main(int argc, char *argv[])
{
    setlocale(LC_ALL,"");
    ROS_INFO("订阅方实现");
    // 2、初始化ros节点
    ros::init(argc,argv,"jiaZhang");
    // 3、创建ros句柄
    ros::NodeHandle nh;
    // 4、创建订阅者对象
    ros::Subscriber sub=nh.subscribe("liaoTian",10,doPerson);
    // 5、编写回调函数,处理订阅数据
    // 6、调用视频spin()函数
    ros::spin();
    return 0;
}

8、配置订阅方

cmakelists.txt三个地方

add_executable(demo04_sub_person src/demo04_sub_person.cpp)

add_dependencies(demo04_sub_person ${PROJECT_NAME}_generate_messages_cpp)

target_link_libraries(demo04_sub_person   ${catkin_LIBRARIES}
)

9、运行

进入工作空间下启动roscore

source ./devel/setup.bash 

rosrun plumbing_pub_sub demo03_pub_personperson 

source ./devel/setup.bash 

rosrun plumbing_pub_sub dem04_sub_personperson 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值