首页 > 其他 > 详细

ROS(四)---自定义消息.msg

时间:2020-03-05 10:05:46      阅读:83      评论:0      收藏:0      [点我收藏+]

一、工程结构

技术分享图片

 

 

 新的ros package book

二、定义 msg

技术分享图片

 

技术分享图片

 

编译后,在工作空间下的devel 中include  有头文件 book/Student.h

技术分享图片

 

发布端

#include "ros/ros.h"
#include "book/Student.h"
#include <cstdlib>

using namespace std;
int main(int argc, char** argv)
{
    // initial and name node
    ros::init(argc, argv, "node_MyMsgPub"); // 初始化节点名称
    if(argc!=4)
    {
        cout<<"Error command parameter!\n"<<"please run command eg:\n"<<"rosrun book_message Book_MyMsgPub LiLei 180 160"<<endl;
        return 1;
    }

    //create mode handle
    ros::NodeHandle nh;  // ros node handle 句柄
    ros::Publisher myMsgPub = nh.advertise<book::Student>("MyMsg",100);  //发布器
    book::Student stdMsg;
    stdMsg.name = argv[1];
    stdMsg.height = atof(argv[2]);
    stdMsg.weight = atof(argv[3]);

    ros::Rate rate(10);
    while(ros::ok)
    {
        myMsgPub.publish(stdMsg);
        rate.sleep();
    }
    return 0;
}

接收端口

#include "ros/ros.h"
#include "book/Student.h"

void MyMsgCallback(const book::Student &stdInfo)
{
    ROS_INFO("student message: \n name:%s---height:%f---weight:%f\n",stdInfo.name.c_str(),stdInfo.height
    ,stdInfo.weight);
}

int main(int argc, char ** argv)
{
    // initial and name node
    ros::init(argc, argv, "node_MyMsgSub");
    
    ros::NodeHandle nh;
    // create subscriber
    ros::Subscriber MyMsgSub = nh.subscribe("MyMsg",1000,&MyMsgCallback);
    ros::spin();
    return 0;
}

 

ROS(四)---自定义消息.msg

原文:https://www.cnblogs.com/feihu-h/p/12417683.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!