Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
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 }