标签:cube this scale something write 发布者 scribe ram 没有
#include <ros/ros.h> #include <visualization_msgs/Marker.h> int main( int argc, char** argv ) { ros::init(argc, argv, "basic_shapes"); ros::NodeHandle n; ros::Rate r(1); ros::Publisher marker_pub = n.advertise<visualization_msgs::Marker>("visualization_marker", 1); // Set our initial shape type to be a cube uint32_t shape = visualization_msgs::Marker::CUBE; while (ros::ok()) { visualization_msgs::Marker marker; // Set the frame ID and timestamp. See the TF tutorials for information on these. marker.header.frame_id = "/my_frame"; marker.header.stamp = ros::Time::now(); // Set the namespace and id for this marker. This serves to create a unique ID // Any marker sent with the same namespace and id will overwrite the old one marker.ns = "basic_shapes"; marker.id = 0; // Set the marker type. Initially this is CUBE, and cycles between that and SPHERE, ARROW, and CYLINDER marker.type = shape; // Set the marker action. Options are ADD, DELETE, and new in ROS Indigo: 3 (DELETEALL) marker.action = visualization_msgs::Marker::ADD; // Set the pose of the marker. This is a full 6DOF pose relative to the frame/time specified in the header marker.pose.position.x = 0; marker.pose.position.y = 0; marker.pose.position.z = 0; marker.pose.orientation.x = 0.0; marker.pose.orientation.y = 0.0; marker.pose.orientation.z = 0.0; marker.pose.orientation.w = 1.0; // Set the scale of the marker -- 1x1x1 here means 1m on a side marker.scale.x = 1.0; marker.scale.y = 1.0; marker.scale.z = 1.0; // Set the color -- be sure to set alpha to something non-zero! marker.color.r = 0.0f; marker.color.g = 1.0f; marker.color.b = 0.0f; marker.color.a = 1.0; marker.lifetime = ros::Duration(); // Publish the marker while (marker_pub.getNumSubscribers() < 1) { if (!ros::ok()) { return 0; } ROS_WARN_ONCE("Please create a subscriber to the marker"); sleep(1); } marker_pub.publish(marker); // Cycle between different shapes switch (shape) { case visualization_msgs::Marker::CUBE: shape = visualization_msgs::Marker::SPHERE; break; case visualization_msgs::Marker::SPHERE: shape = visualization_msgs::Marker::ARROW; break; case visualization_msgs::Marker::ARROW: shape = visualization_msgs::Marker::CYLINDER; break; case visualization_msgs::Marker::CYLINDER: shape = visualization_msgs::MarkCUBE; break; } r.sleep(); } }
本质上, 这玩意儿应该是一个消息, message。
首先, 引入头文件:
#include <ros/ros.h> #include <visualization_msgs/Marker.h>
然后, 初始化ros, 名字叫basic_sharpes, 然后建立一主题发布者publisher,叫一个makerpub, 消息类型是Marker.
int main( int argc, char** argv ) { ros::init(argc, argv, "basic_shapes"); ros::NodeHandle n; ros::Rate r(1); ros::Publisher marker_pub = n.advertise<visualization_msgs::Marker>("visualization_marker", 1);
然后就是起第一个形状:
uint32_t shape = visualization_msgs::Marker::CUBE;
新建一个消息, 指定frame ID跟时间戳.
while (ros::ok()) { visualization_msgs::Marker marker; // Set the frame ID and timestamp. See the TF tutorials for information on these. marker.header.frame_id = "/my_frame"; marker.header.stamp = ros::Time::now();
获取命名空间:
marker.ns = "basic_shapes"; marker.id = 0;
然后是形状:
marker.type = shape;
然后是动作.
marker.action = visualization_msgs::Marker::ADD;
姿势:
marker.pose.position.x = 0; marker.pose.position.y = 0; marker.pose.position.z = 0; marker.pose.orientation.x = 0.0; marker.pose.orientation.y = 0.0; marker.pose.orientation.z = 0.0; marker.pose.orientation.w = 1.0;
缩放:
marker.scale.x = 1.0; marker.scale.y = 1.0; marker.scale.z = 1.0;
颜色:
marker.color.r = 0.0f; marker.color.g = 1.0f; marker.color.b = 0.0f; marker.color.a = 1.0;
存活周期:
marker.lifetime = ros::Duration();
然后就是发布这个marker, 如果没有订阅的, 就直接printf错误信息.
while (marker_pub.getNumSubscribers() < 1) { if (!ros::ok()) { return 0; } ROS_WARN_ONCE("Please create a subscriber to the marker"); sleep(1); } marker_pub.publish(marker);
接着就是换形状, 如果当前是立方体, 就换球, 如果是球就换箭头:
switch (shape) { case visualization_msgs::Marker::CUBE: shape = visualization_msgs::Marker::SPHERE; break; case visualization_msgs::Marker::SPHERE: shape = visualization_msgs::Marker::ARROW; break; case visualization_msgs::Marker::ARROW: shape = visualization_msgs::Marker::CYLINDER; break; case visualization_msgs::Marker::CYLINDER: shape = visualization_msgs::Marker::CUBE; break; }
中间休眠1秒:
r.sleep(); }
下一次继续深入rviz.
标签:cube this scale something write 发布者 scribe ram 没有
原文地址:http://www.cnblogs.com/Montauk/p/6859854.html