在之前创建talker的src文件夹中创建person.cpp并编写如下
#include "ros/ros.h"
#include "learning_communication/Person.h"
#include <sstream>
int main(int argc,char *argv[]){
setlocale(LC_ALL,"");//避免乱码
ros::init(argc,argv,"person");//初始化节点
ros::NodeHandle n;//创建句柄
ros::Publisher pub=n.advertise<learning_communication::Person>("chatting",1000);//创建发布者对象
ros::Rate rate(2);
learning_communication::Person person;
person.name="Bob";
person.age=18;
person.height=180.5;//此处与上文中score位置对应,可自行修改
ros::Duration(0.5).sleep();
while(ros::ok()){
pub.publish(person);
ROS_INFO("name:%s age:%d height:%.1f",person.name.c_str(),person.age,person.height);//".c_str()"转换为字符串
person.age++;
rate.sleep();
ros::spinOnce();
}
return 0;
}
之后在CMakeList.txt中按照格式添加各项依赖和目标
add_exec

最低0.47元/天 解锁文章
1898

被折叠的 条评论
为什么被折叠?



