menu.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, 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 #include <ros/ros.h>
32 
35 
37 #include <tf/tf.h>
38 
39 #include <math.h>
40 
41 using namespace visualization_msgs;
42 using namespace interactive_markers;
43 
45 float marker_pos = 0;
46 
48 
51 
52 
53 void enableCb( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
54 {
55  MenuHandler::EntryHandle handle = feedback->menu_entry_id;
57  menu_handler.getCheckState( handle, state );
58 
59  if ( state == MenuHandler::CHECKED )
60  {
61  menu_handler.setCheckState( handle, MenuHandler::UNCHECKED );
62  ROS_INFO("Hiding first menu entry");
63  menu_handler.setVisible( h_first_entry, false );
64  }
65  else
66  {
67  menu_handler.setCheckState( handle, MenuHandler::CHECKED );
68  ROS_INFO("Showing first menu entry");
69  menu_handler.setVisible( h_first_entry, true );
70  }
71  menu_handler.reApply( *server );
72  ros::Duration(2.0).sleep();
73  ROS_INFO("update");
74  server->applyChanges();
75 }
76 
77 void modeCb( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
78 {
79  menu_handler.setCheckState( h_mode_last, MenuHandler::UNCHECKED );
80  h_mode_last = feedback->menu_entry_id;
81  menu_handler.setCheckState( h_mode_last, MenuHandler::CHECKED );
82 
83  ROS_INFO("Switching to menu entry #%d", h_mode_last);
84 
85  menu_handler.reApply( *server );
86  server->applyChanges();
87 }
88 
89 
90 
91 Marker makeBox( InteractiveMarker &msg )
92 {
93  Marker marker;
94 
95  marker.type = Marker::CUBE;
96  marker.scale.x = msg.scale * 0.45;
97  marker.scale.y = msg.scale * 0.45;
98  marker.scale.z = msg.scale * 0.45;
99  marker.color.r = 0.5;
100  marker.color.g = 0.5;
101  marker.color.b = 0.5;
102  marker.color.a = 1.0;
103 
104  return marker;
105 }
106 
107 InteractiveMarkerControl& makeBoxControl( InteractiveMarker &msg )
108 {
109  InteractiveMarkerControl control;
110  control.always_visible = true;
111  control.markers.push_back( makeBox(msg) );
112  msg.controls.push_back( control );
113 
114  return msg.controls.back();
115 }
116 
117 InteractiveMarker makeEmptyMarker( bool dummyBox=true )
118 {
119  InteractiveMarker int_marker;
120  int_marker.header.frame_id = "base_link";
121  int_marker.pose.position.y = -3.0 * marker_pos++;;
122  int_marker.scale = 1;
123 
124  return int_marker;
125 }
126 
127 void makeMenuMarker( std::string name )
128 {
129  InteractiveMarker int_marker = makeEmptyMarker();
130  int_marker.name = name;
131 
132  InteractiveMarkerControl control;
133 
134  control.interaction_mode = InteractiveMarkerControl::BUTTON;
135  control.always_visible = true;
136 
137  control.markers.push_back( makeBox( int_marker ) );
138  int_marker.controls.push_back(control);
139 
140  server->insert( int_marker );
141 }
142 
143 void deepCb( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
144 {
145  ROS_INFO("The deep sub-menu has been found.");
146 }
147 
148 void initMenu()
149 {
150  h_first_entry = menu_handler.insert( "First Entry" );
151  MenuHandler::EntryHandle entry = menu_handler.insert( h_first_entry, "deep" );
152  entry = menu_handler.insert( entry, "sub" );
153  entry = menu_handler.insert( entry, "menu", &deepCb );
154 
155  menu_handler.setCheckState( menu_handler.insert( "Show First Entry", &enableCb ), MenuHandler::CHECKED );
156 
157  MenuHandler::EntryHandle sub_menu_handle = menu_handler.insert( "Switch" );
158 
159  for ( int i=0; i<5; i++ )
160  {
161  std::ostringstream s;
162  s << "Mode " << i;
163  h_mode_last = menu_handler.insert( sub_menu_handle, s.str(), &modeCb );
164  menu_handler.setCheckState( h_mode_last, MenuHandler::UNCHECKED );
165  }
166  //check the very last entry
167  menu_handler.setCheckState( h_mode_last, MenuHandler::CHECKED );
168 }
169 
170 int main(int argc, char** argv)
171 {
172  ros::init(argc, argv, "menu");
173 
174  server.reset( new InteractiveMarkerServer("menu","",false) );
175 
176  initMenu();
177 
178  makeMenuMarker( "marker1" );
179  makeMenuMarker( "marker2" );
180 
181  menu_handler.apply( *server, "marker1" );
182  menu_handler.apply( *server, "marker2" );
183  server->applyChanges();
184 
185  ros::spin();
186 
187  server.reset();
188 }
bool getCheckState(EntryHandle handle, CheckState &check_state) const
XmlRpcServer s
bool sleep() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool apply(InteractiveMarkerServer &server, const std::string &marker_name)
ROSCPP_DECL void spin(Spinner &spinner)
bool setVisible(EntryHandle handle, bool visible)
bool setCheckState(EntryHandle handle, CheckState check_state)
#define ROS_INFO(...)
EntryHandle insert(const std::string &title, const FeedbackCallback &feedback_cb)
bool reApply(InteractiveMarkerServer &server)


interactive_marker_tutorials
Author(s): David Gossow
autogenerated on Fri Feb 1 2019 03:33:34