menu.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 
00031 #include <ros/ros.h>
00032 
00033 #include <interactive_markers/interactive_marker_server.h>
00034 #include <interactive_markers/menu_handler.h>
00035 
00036 #include <tf/transform_broadcaster.h>
00037 #include <tf/tf.h>
00038 
00039 #include <math.h>
00040 
00041 using namespace visualization_msgs;
00042 using namespace interactive_markers;
00043 
00044 boost::shared_ptr<InteractiveMarkerServer> server;
00045 float marker_pos = 0;
00046 
00047 MenuHandler menu_handler;
00048 
00049 MenuHandler::EntryHandle h_first_entry;
00050 MenuHandler::EntryHandle h_mode_last;
00051 
00052 
00053 void enableCb( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
00054 {
00055   MenuHandler::EntryHandle handle = feedback->menu_entry_id;
00056   MenuHandler::CheckState state;
00057   menu_handler.getCheckState( handle, state );
00058 
00059   if ( state == MenuHandler::CHECKED )
00060   {
00061     menu_handler.setCheckState( handle, MenuHandler::UNCHECKED );
00062     ROS_INFO("Hiding first menu entry");
00063     menu_handler.setVisible( h_first_entry, false );
00064   }
00065   else
00066   {
00067     menu_handler.setCheckState( handle, MenuHandler::CHECKED );
00068     ROS_INFO("Showing first menu entry");
00069     menu_handler.setVisible( h_first_entry, true );
00070   }
00071   menu_handler.reApply( *server );
00072   ros::Duration(2.0).sleep();
00073   ROS_INFO("update");
00074   server->applyChanges();
00075 }
00076 
00077 void modeCb( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
00078 {
00079   menu_handler.setCheckState( h_mode_last, MenuHandler::UNCHECKED );
00080   h_mode_last = feedback->menu_entry_id;
00081   menu_handler.setCheckState( h_mode_last, MenuHandler::CHECKED );
00082 
00083   ROS_INFO("Switching to menu entry #%d", h_mode_last);
00084 
00085   menu_handler.reApply( *server );
00086   server->applyChanges();
00087 }
00088 
00089 
00090 
00091 Marker makeBox( InteractiveMarker &msg )
00092 {
00093   Marker marker;
00094 
00095   marker.type = Marker::CUBE;
00096   marker.scale.x = msg.scale * 0.45;
00097   marker.scale.y = msg.scale * 0.45;
00098   marker.scale.z = msg.scale * 0.45;
00099   marker.color.r = 0.5;
00100   marker.color.g = 0.5;
00101   marker.color.b = 0.5;
00102   marker.color.a = 1.0;
00103 
00104   return marker;
00105 }
00106 
00107 InteractiveMarkerControl& makeBoxControl( InteractiveMarker &msg )
00108 {
00109   InteractiveMarkerControl control;
00110   control.always_visible = true;
00111   control.markers.push_back( makeBox(msg) );
00112   msg.controls.push_back( control );
00113 
00114   return msg.controls.back();
00115 }
00116 
00117 InteractiveMarker makeEmptyMarker( bool dummyBox=true )
00118 {
00119   InteractiveMarker int_marker;
00120   int_marker.header.frame_id = "base_link";
00121   int_marker.pose.position.y = -3.0 * marker_pos++;;
00122   int_marker.scale = 1;
00123 
00124   return int_marker;
00125 }
00126 
00127 void makeMenuMarker( std::string name )
00128 {
00129   InteractiveMarker int_marker = makeEmptyMarker();
00130   int_marker.name = name;
00131 
00132   InteractiveMarkerControl control;
00133 
00134   control.interaction_mode = InteractiveMarkerControl::BUTTON;
00135   control.always_visible = true;
00136 
00137   control.markers.push_back( makeBox( int_marker ) );
00138   int_marker.controls.push_back(control);
00139 
00140   server->insert( int_marker );
00141 }
00142 
00143 void deepCb( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
00144 {
00145   ROS_INFO("The deep sub-menu has been found.");
00146 }
00147 
00148 void initMenu()
00149 {
00150   h_first_entry = menu_handler.insert( "First Entry" );
00151   MenuHandler::EntryHandle entry = menu_handler.insert( h_first_entry, "deep" );
00152   entry = menu_handler.insert( entry, "sub" );
00153   entry = menu_handler.insert( entry, "menu", &deepCb );
00154   
00155   menu_handler.setCheckState( menu_handler.insert( "Show First Entry", &enableCb ), MenuHandler::CHECKED );
00156 
00157   MenuHandler::EntryHandle sub_menu_handle = menu_handler.insert( "Switch" );
00158 
00159   for ( int i=0; i<5; i++ )
00160   {
00161     std::ostringstream s;
00162     s << "Mode " << i;
00163     h_mode_last = menu_handler.insert( sub_menu_handle, s.str(), &modeCb );
00164     menu_handler.setCheckState( h_mode_last, MenuHandler::UNCHECKED );
00165   }
00166   //check the very last entry
00167   menu_handler.setCheckState( h_mode_last, MenuHandler::CHECKED );
00168 }
00169 
00170 int main(int argc, char** argv)
00171 {
00172   ros::init(argc, argv, "menu");
00173 
00174   server.reset( new InteractiveMarkerServer("menu","",false) );
00175 
00176   initMenu();
00177 
00178   makeMenuMarker( "marker1" );
00179   makeMenuMarker( "marker2" );
00180 
00181   menu_handler.apply( *server, "marker1" );
00182   menu_handler.apply( *server, "marker2" );
00183   server->applyChanges();
00184 
00185   ros::spin();
00186 
00187   server.reset();
00188 }


interactive_marker_tutorials
Author(s): David Gossow
autogenerated on Thu Jun 6 2019 17:45:02