## For instruction on writing tutorials ## http://www.ros.org/wiki/WritingTutorials #################################### ##FILL ME IN #################################### ## links to any required tutorials ## note = This tutorial assumes you are comfortable with using roscpp, and have gone through the [[ROS/Tutorials|ROS Tutorials]] ## descriptive title for the tutorial ## title = Markers: Sending Basic Shapes (C++) ## multi-line description to be displayed in search ## description = Shows how to use <> messages to send basic shapes (cube, sphere, cylinder, arrow) to rviz. ## the next tutorial description (optional) ## next = ## links to next tutorial (optional) ## next.0.link= [[rviz/Tutorials/Markers: Points and Lines|Markers: Points and Lines]] ## next.1.link= ## what level user is this tutorial for ## level= BeginnerCategory #################################### <> <> <> <> == Intro == Unlike other displays, the [[rviz/DisplayTypes/Marker|Marker Display]] lets you visualize data in rviz without rviz knowing anything about interpreting that data. Instead, primitive objects are sent to the display through <> messages, which let you show things like arrows, boxes, spheres and lines. This tutorial will show you how to send the four basic shapes (boxes, spheres, cylinders, and arrows). We'll create a program that sends out a new marker every second, replacing the last one with a different shape. Note: The [[https://github.com/PickNikRobotics/rviz_visual_tools|rviz_visual_tools]] package offers convenience functions for C++ users. == Create a Package == Before getting started, let's create a package called `using_markers`, somewhere in your package path: {{{{#!wiki buildsystem catkin {{{ catkin_create_pkg using_markers roscpp visualization_msgs }}} }}}} {{{{#!wiki buildsystem rosbuild {{{ roscreate-pkg using_markers roscpp visualization_msgs }}} }}}} == Sending Markers == === The Code === Paste the following into `src/basic_shapes.cpp`: <> Now edit the `CMakeLists.txt` file in your `using_markers` package, and add: {{{{#!wiki buildsystem rosbuild {{{ rosbuild_add_executable(basic_shapes src/basic_shapes.cpp) }}} }}}} {{{{#!wiki buildsystem catkin {{{ add_executable(basic_shapes src/basic_shapes.cpp) target_link_libraries(basic_shapes ${catkin_LIBRARIES}) }}} }}}} to the bottom. === The Code Explained === Ok, let's break down the code piece by piece: <> You should have seen the `ROS` include by now. We also include the <> message definition. <> This should look familiar. We initialize ROS, and create a `ros::Publisher` on the `visualization_marker` topic. <> Here we create an integer to keep track of what shape we're going to publish. The four types we'll be using here all use the <> message in the same way, so we can simply switch out the shape type to demonstrate the four different shapes. <> This begins the meat of the program. First we create a <>, and begin filling it out. The `header` here is a <>, which should be familiar if you've done the [[tf]] [[tf/Tutorials|tutorials]]. We set the `frame_id` member to `/my_frame` as an example. In a running system this should be the frame relative to which you want the marker's pose to be interpreted. <> The namespace (`ns`) and `id` are used to create a unique name for this marker. If a marker message is received with the same `ns` and `id`, the new marker will replace the old one. <> This `type` field is what specifies the kind of marker we're sending. The available types are enumerated in the <> message. Here we set the type to our `shape` variable, which will change every time through the loop. <> The `action` field is what specifies what to do with the marker. The options are `visualization_msgs::Marker::ADD` and `visualization_msgs::Marker::DELETE`. `ADD` is something of a misnomer, it really means "create or modify". <> A new `action` has been added to delete all markers in the particular Rviz display, regardless of ID or namespace. The value is `3` and in future ROS version the message will change to have value `visualization_msgs::Marker::DELETEALL`. <> Here we set the pose of the marker. The <> message consists of a <> to specify the position and a <> to specify the orientation. Here we set the position to the origin, and the orientation to the identity orientation (note the 1.0 for `w`). <> Now we specify the scale of the marker. For the basic shapes, a scale of 1 in all directions means 1 meter on a side. <> The color of the marker is specified as a <>. Each member should be between 0 and 1. An alpha (`a`) value of 0 means completely transparent (invisible), and 1 is completely opaque. <> The `lifetime` field specifies how long this marker should stick around before being automatically deleted. A value of `ros::Duration()` means never to auto-delete. If a new marker message is received before the `lifetime` has been reached, the `lifetime` will be reset to the value in the new marker message. <> We wait for the marker to have a subscriber and we then publish the marker. Note that you can also use a [[rospy/Overview/Publishers and Subscribers#rospy.Publisher_initialization|latched publisher]] as an alternative to this code. <> This code lets us show all four shapes while just publishing the one marker message. Based on the current shape, we set what the next shape to publish will be. <> Sleep and loop back to the top. === Building the Code === You should be able to build the code with: {{{{#!wiki buildsystem catkin {{{ $ cd %TOP_DIR_YOUR_CATKIN_WORKSPACE% $ catkin_make }}} }}}} {{{{#!wiki buildsystem rosbuild {{{ rosmake using_markers }}} }}}} === Running the Code === You should be able to run the code with: {{{ rosrun using_markers basic_shapes }}} == Viewing the Markers == Now that you're publishing markers, you need to set rviz up to view them. First, make sure rviz is built: {{{ rosmake rviz }}} Now, run rviz: {{{ rosrun using_markers basic_shapes rosrun rviz rviz }}} If you've never used rviz before, please see the [[rviz/UserGuide|User's Guide]] to get you started. {{{#!wiki version hydro_and_newer The first thing to do, because we don't have any [[tf]] transforms setup, is to set the [[rviz/UserGuide#Frames|Fixed Frames]] to the frame we set the marker to above, `/my_frame`. In order to do so, set the Fixed Frame field to "/my_frame". }}} {{{#!wiki version groovy The first thing to do, because we don't have any [[tf]] transforms setup, is to set the [[rviz/UserGuide#Frames|Fixed Frames]] to the frame we set the marker to above, `/my_frame`. In order to do so, expand ".Global Options" in "Displays" area so that you'll see Fixed Frame fields. Then type in "/my_frame" into it. }}} {{{#!wiki version electric The first thing to do, because we don't have any [[tf]] transforms setup, is to set both the [[rviz/UserGuide#Frames|Fixed and Target Frames]] to the frame we set the marker to above, `/my_frame`. In order to do so, first expand ".Global Options" in "Displays" area so that you'll see both "{ Fixed, Target } Frame" fields. Then type in "/my_frame" into each field. }}} {{{#!wiki version fuerte_and_older The first thing to do, because we don't have any [[tf]] transforms setup, is to set both the [[rviz/UserGuide#Frames|Fixed and Target Frames]] to the frame we set the marker to above, `/my_frame`. In order to do so, first expand ".Global Options" in "Displays" area so that you'll see both "{ Fixed, Target } Frame" fields. Then type in "/my_frame" into each field. }}} Next add a [[rviz/DisplayTypes/Marker|Markers]] display. Notice that the default topic specified, `visualization_marker`, is the same as the one being published. You should now see a marker at the origin that changes shape every second: {{attachment:basic_shapes_tutorial.png|Basic Shapes|width="768"}} == More Information == For more information on the different types of markers beyond the ones shown here, please see the [[rviz/DisplayTypes/Marker|Markers Display Page]] ## AUTOGENERATED DO NOT DELETE ## BeginnerCategory ## FILL IN THE STACK TUTORIAL CATEGORY HERE