源码
/*
* 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那个函数压根就没用到。
网友评论