interactive_marker_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, 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 
31 // %Tag(fullSource)%
32 #include <ros/ros.h>
33 
35 
36 // create an interactive marker server on the topic namespace simple_marker
38 
39 visualization_msgs::InteractiveMarker makeMarker( float r, float g, float b )
40 {
41  // create an interactive marker for our server
42  visualization_msgs::InteractiveMarker int_marker;
43  int_marker.header.frame_id = "/base_link";
44  int_marker.name = "my_marker";
45  int_marker.description = "Simple 1-DOF Control";
46 
47  // create a box marker
48  visualization_msgs::Marker box_marker;
49  box_marker.type = visualization_msgs::Marker::CUBE;
50  box_marker.scale.x = 0.45;
51  box_marker.scale.y = 0.45;
52  box_marker.scale.z = 0.45;
53  box_marker.color.r = r;
54  box_marker.color.g = g;
55  box_marker.color.b = b;
56  box_marker.color.a = 1.0;
57 
58  // create a non-interactive control which contains the box
59  visualization_msgs::InteractiveMarkerControl box_control;
60  box_control.always_visible = true;
61  box_control.markers.push_back( box_marker );
62 
63  // add the control to the interactive marker
64  int_marker.controls.push_back( box_control );
65 
66  // create a control which will move the box
67  // this control does not contain any markers,
68  // which will cause RViz to insert two arrows
69  visualization_msgs::InteractiveMarkerControl linear_control;
70  linear_control.name = "move_x";
71  linear_control.interaction_mode =
72  visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
73 
74  // add the control to the interactive marker
75  int_marker.controls.push_back(linear_control);
76 
77  return int_marker;
78 }
79 
80 bool is_red = false;
81 
82 void processFeedback( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
83 {
84  ROS_INFO_STREAM( feedback->marker_name << " is now at "
85  << feedback->pose.position.x << ", " << feedback->pose.position.y
86  << ", " << feedback->pose.position.z );
87 
88  bool changed = false;
89  visualization_msgs::InteractiveMarker int_marker;
90 
91  // red when x < 0, green otherwise. Update marker color when x
92  // crosses boundary.
93  if( feedback->pose.position.x < 0 && !is_red )
94  {
95  printf( "turning red.\n" );
96  is_red = true;
97  int_marker = makeMarker( 1, 0, 0 );
98  changed = true;
99  }
100  if( feedback->pose.position.x >= 0 && is_red )
101  {
102  printf( "turning green.\n" );
103  is_red = false;
104  int_marker = makeMarker( 0, 1, 0 );
105  changed = true;
106  }
107 
108  if( changed )
109  {
110  printf( "changed.\n" );
111  int_marker.pose = feedback->pose;
112  server->insert( int_marker );
113  server->applyChanges();
114  }
115 }
116 
117 visualization_msgs::InteractiveMarker makeCrazyMarker( bool linear )
118 {
119  // create an interactive marker for our server
120  visualization_msgs::InteractiveMarker int_marker;
121  int_marker.header.frame_id = "/base_link";
122  int_marker.name = "crazy_marker";
123  int_marker.description = "Unusual 1-DOF Control";
124 
125  // create a box marker
126  visualization_msgs::Marker box_marker;
127  box_marker.type = visualization_msgs::Marker::CUBE;
128  box_marker.scale.x = 1;
129  box_marker.scale.y = .3;
130  box_marker.scale.z = .3;
131  box_marker.color.r = .3;
132  box_marker.color.g = .1;
133  box_marker.color.b = 1;
134  box_marker.color.a = 1.0;
135  box_marker.pose.position.y = -1.0;
136 
137  // create a non-interactive control which contains the box
138  visualization_msgs::InteractiveMarkerControl box_control;
139  box_control.always_visible = true;
140  box_control.markers.push_back( box_marker );
141  box_control.name = "crazy";
142  if( linear )
143  {
144  box_control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
145  }
146  else
147  {
148  box_control.orientation.w = 1;
149  box_control.orientation.x = 0;
150  box_control.orientation.y = 1;
151  box_control.orientation.z = 0;
152  box_control.interaction_mode = visualization_msgs::InteractiveMarkerControl::ROTATE_AXIS;
153  }
154 
155  // add the control to the interactive marker
156  int_marker.controls.push_back( box_control );
157  int_marker.pose.position.y = 3;
158 
159  return int_marker;
160 }
161 
162 bool is_linear = true;
163 
164 void processCrazyFeedback( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
165 {
166  ROS_INFO_STREAM( feedback->marker_name << " is now at pos "
167  << feedback->pose.position.x << ", "
168  << feedback->pose.position.y << ", "
169  << feedback->pose.position.z << "; quat "
170  << feedback->pose.orientation.x << ", "
171  << feedback->pose.orientation.y << ", "
172  << feedback->pose.orientation.z << ", "
173  << feedback->pose.orientation.w );
174 
175  bool changed = false;
176  visualization_msgs::InteractiveMarker int_marker;
177 
178  // linear when x < 0, rotary otherwise. Update when x
179  // crosses boundary.
180  if( feedback->pose.orientation.z < 0 && !is_linear )
181  {
182  printf( "turning linear.\n" );
183  is_linear = true;
184  int_marker = makeCrazyMarker( true );
185  changed = true;
186  }
187  if( feedback->pose.position.x > 0 && is_linear )
188  {
189  printf( "turning rotary.\n" );
190  is_linear = false;
191  int_marker = makeCrazyMarker( false );
192  changed = true;
193  }
194 
195  if( changed )
196  {
197  printf( "changed.\n" );
198  int_marker.pose.position.x = 0;
199  int_marker.pose.orientation.x = 0;
200  int_marker.pose.orientation.y = 0;
201  int_marker.pose.orientation.z = 0;
202  int_marker.pose.orientation.w = 1;
203  server->insert( int_marker );
204  server->applyChanges();
205  }
206 }
207 
208 int main(int argc, char** argv)
209 {
210  ros::init(argc, argv, "interactive_marker_test");
211 
212  // create an interactive marker server on the topic namespace simple_marker
213  server = new interactive_markers::InteractiveMarkerServer("simple_marker");
214 
215  // create an interactive marker for our server
216  visualization_msgs::InteractiveMarker int_marker = makeMarker(0, 1, 0);
217 
218  // add the interactive marker to our collection &
219  // tell the server to call processFeedback() when feedback arrives for it
220  server->insert(int_marker, &processFeedback);
221 
222  // create an interactive marker for our server
223  visualization_msgs::InteractiveMarker crazy_marker = makeCrazyMarker( true );
224 
225  // add the interactive marker to our collection &
226  // tell the server to call processCrazyFeedback() when feedback arrives for it
227  server->insert(crazy_marker, &processCrazyFeedback);
228 
229  // 'commit' changes and send to all clients
230  server->applyChanges();
231 
232  // start the ROS main loop
233  ros::spin();
234 }
235 // %Tag(fullSource)%
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
visualization_msgs::InteractiveMarker makeCrazyMarker(bool linear)
ROSCPP_DECL void spin(Spinner &spinner)
visualization_msgs::InteractiveMarker makeMarker(float r, float g, float b)
void processFeedback(const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback)
void processCrazyFeedback(const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback)
interactive_markers::InteractiveMarkerServer * server
void insert(const visualization_msgs::InteractiveMarker &int_marker)
#define ROS_INFO_STREAM(args)
int main(int argc, char **argv)


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Wed Aug 28 2019 04:01:51