在ros中,用nav_msgs使rviz中显示四个路径点,并在每个路径点旁边标上a.b.c.d,具体的代码怎么写?
时间: 2023-03-14 20:32:43 浏览: 244
智能车项目基于C++在ROS中实现A星三维路径规划源码+使用说明.zip
你可以使用以下代码:#include <ros/ros.h>
#include <nav_msgs/Path.h>int main(int argc, char **argv){
ros::init(argc, argv, "path_marker_node");
ros::NodeHandle nh;
// Create a path message
nav_msgs::Path path;
// Add four points to the path
geometry_msgs::PoseStamped p1, p2, p3, p4;
p1.pose.position.x = 0.0;
p1.pose.position.y = 0.0;
p2.pose.position.x = 1.0;
p2.pose.position.y = 0.0;
p3.pose.position.x = 1.0;
p3.pose.position.y = 1.0;
p4.pose.position.x = 0.0;
p4.pose.position.y = 1.0;
path.poses.push_back(p1);
path.poses.push_back(p2);
path.poses.push_back(p3);
path.poses.push_back(p4);
// Set the header of the path
path.header.frame_id = "map";
// Publish the path
ros::Publisher path_pub = nh.advertise<nav_msgs::Path>("/path", 1);
// Create a marker array message
visualization_msgs::MarkerArray ma;
// Add the markers to the message
visualization_msgs::Marker m1, m2, m3, m4;
m1.header.frame_id = "map";
m1.type = visualization_msgs::Marker::TEXT_VIEW_FACING;
m1.pose.position.x = 0.0;
m1.pose.position.y = 0.0;
m1.pose.position.z = 0.3;
m1.text = "a";
m2.header.frame_id = "map";
m2.type = visualization_msgs::Marker::TEXT_VIEW_FACING;
m2.pose.position.x = 1.0;
m2.pose.position.y = 0.0;
m2.pose.position.z = 0.3;
m2.text = "b";
m3.header.frame_id = "map";
m3.type = visualization_msgs::Marker::TEXT_VIEW_FACING;
m3.pose.position.x = 1.0;
m3.pose.position.y = 1.0;
m3.pose.position.z = 0.3;
m3.text = "c";
m4.header.frame_id = "map";
m4.type = visualization_msgs::Marker::TEXT_VIEW_FACING;
m4.pose.position.x = 0.0;
m4.pose.position.y = 1.0;
m4.pose.position.z = 0.3;
m4.text = "d";
ma.markers.push_back(m1);
ma.markers.push_back(m2);
ma.markers.push_back(m3);
ma.markers.push_back(m4);
// Publish the marker array
ros::Publisher ma_pub = nh.advertise<visualization_msgs::MarkerArray>("/markers", 1);
// Send the messages
while (ros::ok()){
path_pub.publish(path);
ma_pub.publish(ma);
ros::spinOnce();
}
return 0;
}
阅读全文