Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/wwwroot/phxin.top/usr/themes/Joe/public/config.php on line 19

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/wwwroot/phxin.top/usr/themes/Joe/public/config.php on line 20

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/wwwroot/phxin.top/usr/themes/Joe/public/config.php on line 21

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/wwwroot/phxin.top/usr/themes/Joe/public/config.php on line 22

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/wwwroot/phxin.top/usr/themes/Joe/public/config.php on line 23
话题通信流程 - Hello World

话题通信流程

清歌
2022-10-31 / 0 评论 / 0 阅读 / 正在检测是否收录...

新建demo

新建package

新建cpp文件

发布方函数
#include "ros/ros.h"
#include "std_msgs/String.h"
int main(int argc, char *argv[])
{
    //初始化
    ros::init(argc,argv,"gouzi");
    //创建句柄
    ros::NodeHandle nh;
    //创建发布者对象
    ros::Publisher pub=nh.advertise<std_msgs::String>("fang",10);
    //创建发布消息类型
    std_msgs::String msg;
    //循环函数
    while (ros::ok())
    {msg.data="hello";
    pub.publish(msg);
    }
    return 0;
}
订阅方函数
#include "ros/ros.h"
#include "std_msgs/String.h"
#include "sstream"
void domsg(const std_msgs::String::ConstPtr &msg)
{
    //通过msg获取并且操作订阅的数据
        ROS_INFO("翠花订阅的数据:%s",msg->data.c_str());
}
int main(int argc, char  *argv[])
{
    //防止乱码
    setlocale(LC_ALL,"");
    //初始化
    ros::init(argc,argv,"cuihua");
       //创建句柄
    ros::NodeHandle nh;
     //创建订阅者对象
    ros::Subscriber sub=nh.subscribe("fang",10,domsg);
    //回调函数
    ros::spin();
    return 0;
}

请输入图片描述

启动节点

请输入图片描述

0

评论 (0)

取消