basic_shapes.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 // %Tag(FULLTEXT)%
31 // %Tag(INCLUDES)%
32 #include <ros/ros.h>
33 #include <visualization_msgs/Marker.h>
34 // %EndTag(INCLUDES)%
35 
36 // %Tag(INIT)%
37 int main( int argc, char** argv )
38 {
39  ros::init(argc, argv, "basic_shapes");
41  ros::Rate r(1);
42  ros::Publisher marker_pub = n.advertise<visualization_msgs::Marker>("visualization_marker", 1);
43 // %EndTag(INIT)%
44 
45  // Set our initial shape type to be a cube
46 // %Tag(SHAPE_INIT)%
47  uint32_t shape = visualization_msgs::Marker::CUBE;
48 // %EndTag(SHAPE_INIT)%
49 
50 // %Tag(MARKER_INIT)%
51  while (ros::ok())
52  {
53  visualization_msgs::Marker marker;
54  // Set the frame ID and timestamp. See the TF tutorials for information on these.
55  marker.header.frame_id = "/my_frame";
56  marker.header.stamp = ros::Time::now();
57 // %EndTag(MARKER_INIT)%
58 
59  // Set the namespace and id for this marker. This serves to create a unique ID
60  // Any marker sent with the same namespace and id will overwrite the old one
61 // %Tag(NS_ID)%
62  marker.ns = "basic_shapes";
63  marker.id = 0;
64 // %EndTag(NS_ID)%
65 
66  // Set the marker type. Initially this is CUBE, and cycles between that and SPHERE, ARROW, and CYLINDER
67 // %Tag(TYPE)%
68  marker.type = shape;
69 // %EndTag(TYPE)%
70 
71  // Set the marker action. Options are ADD, DELETE, and new in ROS Indigo: 3 (DELETEALL)
72 // %Tag(ACTION)%
73  marker.action = visualization_msgs::Marker::ADD;
74 // %EndTag(ACTION)%
75 
76  // Set the pose of the marker. This is a full 6DOF pose relative to the frame/time specified in the header
77 // %Tag(POSE)%
78  marker.pose.position.x = 0;
79  marker.pose.position.y = 0;
80  marker.pose.position.z = 0;
81  marker.pose.orientation.x = 0.0;
82  marker.pose.orientation.y = 0.0;
83  marker.pose.orientation.z = 0.0;
84  marker.pose.orientation.w = 1.0;
85 // %EndTag(POSE)%
86 
87  // Set the scale of the marker -- 1x1x1 here means 1m on a side
88 // %Tag(SCALE)%
89  marker.scale.x = 1.0;
90  marker.scale.y = 1.0;
91  marker.scale.z = 1.0;
92 // %EndTag(SCALE)%
93 
94  // Set the color -- be sure to set alpha to something non-zero!
95 // %Tag(COLOR)%
96  marker.color.r = 0.0f;
97  marker.color.g = 1.0f;
98  marker.color.b = 0.0f;
99  marker.color.a = 1.0;
100 // %EndTag(COLOR)%
101 
102 // %Tag(LIFETIME)%
103  marker.lifetime = ros::Duration();
104 // %EndTag(LIFETIME)%
105 
106  // Publish the marker
107 // %Tag(PUBLISH)%
108  while (marker_pub.getNumSubscribers() < 1)
109  {
110  if (!ros::ok())
111  {
112  return 0;
113  }
114  ROS_WARN_ONCE("Please create a subscriber to the marker");
115  ros::Duration(1.0).sleep();
116  }
117  marker_pub.publish(marker);
118 // %EndTag(PUBLISH)%
119 
120  // Cycle between different shapes
121 // %Tag(CYCLE_SHAPES)%
122  switch (shape)
123  {
124  case visualization_msgs::Marker::CUBE:
125  shape = visualization_msgs::Marker::SPHERE;
126  break;
127  case visualization_msgs::Marker::SPHERE:
128  shape = visualization_msgs::Marker::ARROW;
129  break;
130  case visualization_msgs::Marker::ARROW:
131  shape = visualization_msgs::Marker::CYLINDER;
132  break;
133  case visualization_msgs::Marker::CYLINDER:
134  shape = visualization_msgs::Marker::CUBE;
135  break;
136  }
137 // %EndTag(CYCLE_SHAPES)%
138 
139 // %Tag(SLEEP_END)%
140  r.sleep();
141  }
142 // %EndTag(SLEEP_END)%
143 }
144 // %EndTag(FULLTEXT)%
void publish(const boost::shared_ptr< M > &message) const
int main(int argc, char **argv)
bool sleep() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
#define ROS_WARN_ONCE(...)
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool sleep()
uint32_t getNumSubscribers() const
static Time now()


visualization_marker_tutorials
Author(s): Josh Faust
autogenerated on Sat May 16 2020 03:49:20