first, Create a package--conduct marker exercises
1. Create Ros Workspaces and packages
mkdir -p ~/catkin_ws/src #创建工作空间目录 # Create ROS packet catkin_create_pkg using_markers roscpp visualization_ Msgs ~/catkin_wscatkin_make
2, Write CPP file, send data to Rviz
Vim ~/catkin_ws/src/using_marker/src/using_markers. CPP
Paste in code with relevant comments attached to the code
#include <ros/ros.h>#include<visualization_msgs/Marker.h>//Visualization ofintMainintargcChar**Argv) { //Initialize ros, and create a ros::P ublisher on the topic Visualization_markerRos::init (argc, argv,"Basic_shapes"); Ros::nodehandle n; Ros::rate R (1); Ros::P ublisher marker_pub= N.advertise<visualization_msgs::marker> ("Visualization_marker",1); //Set Our initial shape type to be a cube//Initialize shape to cubeuint32_t shape =visualization_msgs::marker::cube; while(ros::ok ()) {//Instantiate a markerVisualization_msgs::marker Marker; //Set the frame ID and Timestamp. See the TF tutorials for information on These. //Set frame ID and timestampmarker.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 would overwrite the old one//set a unique ID for this marker, and a marker receives the same NS and ID will replace the old one with new information .MARKER.NS ="Basic_shapes"; Marker.id=0; //Set the marker type. Initially this is the CUBE, and cycles between that and SPHERE, ARROW, and CYLINDER//sets the marker type, which initializes the Cube. The Loop will beMarker.type =shape; //Set the marker Action. Options is ADD, DELETE, and new in ROS Indigo:3 (DELETEALL)Marker.action =visualization_msgs::marker::add; //Set the pose of the Marker. This was a full 6DOF pose relative to the frame/time specified in the header//Setting the location of the markerMarker.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//set the size of the markermarker.scale.x =1.0; Marker.scale.y=1.0; Marker.scale.z=1.0; //set the color--is sure to Set Alpha to something non-zero! //set the color of the markerMARKER.COLOR.R =0.0f; MARKER.COLOR.G=1.0f; Marker.color.b=0.0f; MARKER.COLOR.A=1.0; //Cancel Automatic deletionMarker.lifetime =Ros::D uration (); //Publish the marker//a Subscriber is required to post a message 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//continuous change of shape Switch(shape) { caseVisualization_msgs::marker::cube:shape=visualization_msgs::marker::sphere; break; caseVisualization_msgs::marker::sphere:shape=visualization_msgs::marker::arrow; break; caseVisualization_msgs::marker::arrow:shape=visualization_msgs::marker::cylinder; break; caseVisualization_msgs::marker::cylinder:shape=visualization_msgs::marker::cube; break; } r.sleep (); }}
In the CMakeList.txt file, add
Add_executable (basic_shapes src/basic_shapes.cpp) target_link_libraries (basic_shapes ${catkin_LIBRARIES})
3, the Rviz set
(1) Open Roscore
(2) run the written publisher
Rosrun Using_marker Basic_shapes
(3) Reset rviz, Run Rviz
Rosmake Rvizrosrun Rviz Rviz
(4) setting in Rviz
4, Rviz Final effect display: 4 graphs for continuous transformation
first, Create a package--conduct marker exercises
1. Create Ros Workspaces and packages
Mkdir-p ~/catkin_ws/src #创建工作空间目录 # creating ROS packet catkin_create_pkg using_markers roscpp visualization_msgs # Open the package root directory and compile the CD ~/catkin_wscatkin_make
2, Write CPP file, send data to Rviz
Vim ~/catkin_ws/src/using_marker/src/using_markers. CPP
Paste in code with relevant comments attached to the code
#include <ros/ros.h>#include <visualization_msgs/Marker.h>//Visualization ofint main (int argc,char**Argv) {//Initialize ros, and create a ros::P ublisher on the topic visualization_marker ros::init (argc, argv,"Basic_shapes"); Ros::nodehandle n; Ros::rate R (1); Ros::P ublisher marker_pub = n.advertise<visualization_msgs::marker> ("Visualization_marker",1);//Set our initial shape type to be a cube//Initialize shape to cube uint32_t shape =visualization_msgs::marker::cube;While(ros::ok ()) {//Instantiate a markerVisualization_msgs::marker Marker;//Set the frame ID and Timestamp. See the TF tutorials for information on These.//Set frame ID and timestamp 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 would overwrite the old one//To set a unique ID for this marker, a marker receives the same NS and ID replaces the old MARKER.NS with the new information ="Basic_shapes"; Marker.id =0;//Set the marker type. Initially this is the CUBE, and cycles between that and SPHERE, ARROW, and CYLINDER//Sets the marker type, which initializes the Cube. The loop will be Marker.type =Shape//Set the marker Action. Options is ADD, DELETE, and new in ROS indigo:3 (DELETEALL) marker.action =visualization_msgs::marker::add;//Set the pose of the Marker. This was a full 6DOF pose relative to the frame/time specified in the header//Set the position of the marker 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//Set the size of the marker marker.scale.x =1.0; Marker.scale.y =1.0; Marker.scale.z =1.0;//Set the color--is sure to set Alpha to something non-zero!//Set the color of the marker MARKER.COLOR.R =0.0f; MARKER.COLOR.G =1.0f; Marker.color.b =0.0f; MARKER.COLOR.A =1.0;//Cancel Automatic deletion of Marker.lifetime =Ros::D uration ();//Publish the Marker//A subscriber is required to post a messageWhile (marker_pub.getnumsubscribers () <1) {If (!Ros::ok ()) {Return0; } ros_warn_once ("Please create a subscriber to the marker"); Sleep1); } marker_pub.publish (marker);//Cycle between different shapes//Continuous change of shapeSwitch(shape) {CaseVisualization_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
In the CMakeList.txt file, add
Add_executable (basic_shapes src/basic_shapes.cpp) target_link_libraries (basic_shapes ${catkin_LIBRARIES})
3, the Rviz set
(1) Open Roscore
(2) run the written publisher
Rosrun Using_marker Basic_shapes
(3) Reset rviz, Run Rviz
Rosmake Rvizrosrun Rviz Rviz
(4) setting in Rviz
4, Rviz Final effect display: 4 graphs for continuous transformation
Rviz Study notes (i)--markers:sending Basic Shapes (c + +) send basic shapes