// 基础头文件
#include <ros/ros.h>
// 消息类型头文件
#include <geometry_msgs/PoseWithCovarianceStamped.h>
int main(int argc, char **argv)
{
// 1. 初始化ROS节点
ros::init(argc, argv, "node_name");
// 2. 创建节点句柄
ros::NodeHandle n;
// 3. 创建Publisher
ros::Publisher topic_pub;
// 4. 发布名为/topic_name的topic,消息类型为geometry_msgs::PoseWithCovarianceStamped,队列长度10
topic_pub = n.advertise<geometry_msgs::PoseWithCovarianceStamped>("/topic_name", 10);
// 5. 填充数据,这里以geometry_msgs::PoseWithCovarianceStamped的数据格式为例,根据需求自信更换
geometry_msgs::PoseWithCovarianceStamped initpose_msg;
initpose_msg.header.frame_id = "map";
initpose_msg.header.stamp = ros::Time::now();
initpose_msg.pose.pose.position.x = 0;
initpose_msg.pose.pose.position.y = 0.75;
initpose_msg.pose.pose.position.z = 0;
initpose_msg.pose.pose.orientation.x = 0;
initpose_msg.pose.pose.orientation.y = 0;
initpose_msg.pose.pose.orientation.z = 0;
initpose_msg.pose.pose.orientation.w = 1;
// 6. 日志提醒发布消息
ROS_INFO("/topic_name Publish successfully!!!");
// 7. 设置循环的频率,下面是10HZ = 0.1s
ros::Rate loop_rate(10);
// 8. 循环模板,开始发布
while (ros::ok())
{
initpose_pub.publish(initpose_msg);
loop_rate.sleep(); // 这个一定要有!!!
}
return 0;
}
ROS:发布者(topic)模板(C++版)
猜你喜欢
转载自blog.csdn.net/gls_nuaa/article/details/132017340
今日推荐
周排行