menu
源碼
/*
* Copyright (c) 2011, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <ros/ros.h>
#include <interactive_markers/interactive_marker_server.h>
#include <interactive_markers/menu_handler.h>
#include <tf/transform_broadcaster.h>
#include <tf/tf.h>
#include <math.h>
using namespace visualization_msgs;
using namespace interactive_markers;
boost::shared_ptr<InteractiveMarkerServer> server;
float marker_pos = 0;
MenuHandler menu_handler;
MenuHandler::EntryHandle h_first_entry;
MenuHandler::EntryHandle h_mode_last;
void enableCb( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
{
MenuHandler::EntryHandle handle = feedback->menu_entry_id;
MenuHandler::CheckState state;
menu_handler.getCheckState( handle, state );
if ( state == MenuHandler::CHECKED )
{
menu_handler.setCheckState( handle, MenuHandler::UNCHECKED );
ROS_INFO("Hiding first menu entry");
menu_handler.setVisible( h_first_entry, false );
}
else
{
menu_handler.setCheckState( handle, MenuHandler::CHECKED );
ROS_INFO("Showing first menu entry");
menu_handler.setVisible( h_first_entry, true );
}
menu_handler.reApply( *server );
ros::Duration(2.0).sleep();
ROS_INFO("update");
server->applyChanges();
}
void modeCb( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
{
menu_handler.setCheckState( h_mode_last, MenuHandler::UNCHECKED );
h_mode_last = feedback->menu_entry_id;
menu_handler.setCheckState( h_mode_last, MenuHandler::CHECKED );
ROS_INFO("Switching to menu entry #%d", h_mode_last);
menu_handler.reApply( *server );
server->applyChanges();
}
Marker makeBox( InteractiveMarker &msg )
{
Marker marker;
marker.type = Marker::CUBE;
marker.scale.x = msg.scale * 0.45;
marker.scale.y = msg.scale * 0.45;
marker.scale.z = msg.scale * 0.45;
marker.color.r = 0.5;
marker.color.g = 0.5;
marker.color.b = 0.5;
marker.color.a = 1.0;
return marker;
}
InteractiveMarkerControl& makeBoxControl( InteractiveMarker &msg )
{
InteractiveMarkerControl control;
control.always_visible = true;
control.markers.push_back( makeBox(msg) );
msg.controls.push_back( control );
return msg.controls.back();
}
InteractiveMarker makeEmptyMarker( bool dummyBox=true )
{
InteractiveMarker int_marker;
int_marker.header.frame_id = "base_link";
int_marker.pose.position.y = -3.0 * marker_pos++;;
int_marker.scale = 1;
return int_marker;
}
void makeMenuMarker( std::string name )
{
InteractiveMarker int_marker = makeEmptyMarker();
int_marker.name = name;
InteractiveMarkerControl control;
control.interaction_mode = InteractiveMarkerControl::BUTTON;
control.always_visible = true;
control.markers.push_back( makeBox( int_marker ) );
int_marker.controls.push_back(control);
server->insert( int_marker );
}
void deepCb( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
{
ROS_INFO("The deep sub-menu has been found.");
}
void initMenu()
{
h_first_entry = menu_handler.insert( "First Entry" );
MenuHandler::EntryHandle entry = menu_handler.insert( h_first_entry, "deep" );
entry = menu_handler.insert( entry, "sub" );
entry = menu_handler.insert( entry, "menu", &deepCb );
menu_handler.setCheckState( menu_handler.insert( "Show First Entry", &enableCb ), MenuHandler::CHECKED );
MenuHandler::EntryHandle sub_menu_handle = menu_handler.insert( "Switch" );
for ( int i=0; i<5; i++ )
{
std::ostringstream s;
s << "Mode " << i;
h_mode_last = menu_handler.insert( sub_menu_handle, s.str(), &modeCb );
menu_handler.setCheckState( h_mode_last, MenuHandler::UNCHECKED );
}
//check the very last entry
menu_handler.setCheckState( h_mode_last, MenuHandler::CHECKED );
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "menu");
server.reset( new InteractiveMarkerServer("menu","",false) );
initMenu();
makeMenuMarker( "marker1" );
makeMenuMarker( "marker2" );
menu_handler.apply( *server, "marker1" );
menu_handler.apply( *server, "marker2" );
server->applyChanges();
ros::spin();
server.reset();
}
源碼分析
#include <ros/ros.h>
#include <interactive_markers/interactive_marker_server.h>
#include <interactive_markers/menu_handler.h>
#include <tf/transform_broadcaster.h>
#include <tf/tf.h>
#include <math.h>
using namespace visualization_msgs;
using namespace interactive_markers;
這次的頭文件包含要多一些,看名字就知道作用,還引入兩個命名空間。
boost::shared_ptr<InteractiveMarkerServer> server;
float marker_pos = 0;
MenuHandler menu_handler;
MenuHandler::EntryHandle h_first_entry;
MenuHandler::EntryHandle h_mode_last;
- 1 實例化InteractiveMarkerServer,使用shared_ptr可以使server在程序結束之后自動釋放所有資源,另外server還將有一些shared_ptr的成員函數。
- 2 后面實例化了EntryHandle ,上面那個還不知道是什么意思,誰看懂了告訴我MenuHandler menu_handler是怎么回事。
void enableCb( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
{
MenuHandler::EntryHandle handle = feedback->menu_entry_id;
MenuHandler::CheckState state;
menu_handler.getCheckState( handle, state );
if ( state == MenuHandler::CHECKED )
{
menu_handler.setCheckState( handle, MenuHandler::UNCHECKED );
ROS_INFO("Hiding first menu entry");
menu_handler.setVisible( h_first_entry, false );
}
else
{
menu_handler.setCheckState( handle, MenuHandler::CHECKED );
ROS_INFO("Showing first menu entry");
menu_handler.setVisible( h_first_entry, true );
}
menu_handler.reApply( *server );
ros::Duration(2.0).sleep();
ROS_INFO("update");
server->applyChanges();
}
這個是用來返回消息的函數。
我們看到第一句獲取了handle = feedback->menu_entry_id;然后獲取了handle的狀態,并存儲在state。這里的menu_handler應該就是菜單,解釋了之前的那個實例化。
下面是判斷有沒有被選擇。并且配置選擇狀態和是否顯示。
然后再次運行服務,ros運行2秒后sleep。然后顯示信息,服務器上傳改動。
void modeCb( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
{
menu_handler.setCheckState( h_mode_last, MenuHandler::UNCHECKED );
h_mode_last = feedback->menu_entry_id;
menu_handler.setCheckState( h_mode_last, MenuHandler::CHECKED );
ROS_INFO("Switching to menu entry #%d", h_mode_last);
menu_handler.reApply( *server );
server->applyChanges();
}
這個也是用來負責顯示選擇的。
其實通過上面兩個可以看出,服務器堅挺到feedback,feedback中會包含選擇的信息。函數的作用是,把原來打鉤的地方顯示成沒有勾,再把該打的勾打上。
然后申請重新運行服務器,再讓服務器提交改變信息。
Marker makeBox( InteractiveMarker &msg )
{
Marker marker;
marker.type = Marker::CUBE;
marker.scale.x = msg.scale * 0.45;
marker.scale.y = msg.scale * 0.45;
marker.scale.z = msg.scale * 0.45;
marker.color.r = 0.5;
marker.color.g = 0.5;
marker.color.b = 0.5;
marker.color.a = 1.0;
return marker;
}
這個前面見過了,新建一個marker,是一個灰色方塊。
InteractiveMarkerControl& makeBoxControl( InteractiveMarker &msg )
{
InteractiveMarkerControl control;
control.always_visible = true;
control.markers.push_back( makeBox(msg) );
msg.controls.push_back( control );
return msg.controls.back();
}
這個就是用來建立一個控制,把前面的marker添加在上面。
InteractiveMarker makeEmptyMarker( bool dummyBox=true )
{
InteractiveMarker int_marker;
int_marker.header.frame_id = "base_link";
int_marker.pose.position.y = -3.0 * marker_pos++;;
int_marker.scale = 1;
return int_marker;
}
新建了一個可交互marker。
void makeMenuMarker( std::string name )
{
InteractiveMarker int_marker = makeEmptyMarker();
int_marker.name = name;
InteractiveMarkerControl control;
control.interaction_mode = InteractiveMarkerControl::BUTTON;
control.always_visible = true;
control.markers.push_back( makeBox( int_marker ) );
int_marker.controls.push_back(control);
server->insert( int_marker );
}
這里將可交互marker和灰框組裝了起來,我不明白之前的makeboxcontrol函數干嗎用了。
void deepCb( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
{
ROS_INFO("The deep sub-menu has been found.");
}
這個明顯是后面用的,被選擇之后的處理函數,顯示一個信息。
void initMenu()
{
h_first_entry = menu_handler.insert( "First Entry" );
MenuHandler::EntryHandle entry = menu_handler.insert( h_first_entry, "deep" );
entry = menu_handler.insert( entry, "sub" );
entry = menu_handler.insert( entry, "menu", &deepCb );
menu_handler.setCheckState( menu_handler.insert( "Show First Entry", &enableCb ), MenuHandler::CHECKED );
MenuHandler::EntryHandle sub_menu_handle = menu_handler.insert( "Switch" );
for ( int i=0; i<5; i++ )
{
std::ostringstream s;
s << "Mode " << i;
h_mode_last = menu_handler.insert( sub_menu_handle, s.str(), &modeCb );
menu_handler.setCheckState( h_mode_last, MenuHandler::UNCHECKED );
}
//check the very last entry
menu_handler.setCheckState( h_mode_last, MenuHandler::CHECKED );
}
前四句就做出來一個層層鑲套的菜單,然后在最后一個綁定了之前那個函數。
下面一句就是在菜單里添加一個新的欄目,并且開啟選擇框,原來cd是checkbox的意思。后面又新增加一個欄目,循環添加了很多選項。
后面就是主函數了:
int main(int argc, char** argv)
{
ros::init(argc, argv, "menu");
server.reset( new InteractiveMarkerServer("menu","",false) );
initMenu();
makeMenuMarker( "marker1" );
makeMenuMarker( "marker2" );
menu_handler.apply( *server, "marker1" );
menu_handler.apply( *server, "marker2" );
server->applyChanges();
ros::spin();
server.reset();
}
首先初始化ROS,然后初始化服務器。
初始化菜單,新建可交互方塊。
然后讓服務器更新變化。
好像上面那個makeboxcontrol那個函數壓根就沒用到。