源碼
/*
* 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.
*/
// %Tag(fullSource)%
#include <ros/ros.h>
#include <interactive_markers/interactive_marker_server.h>
void processFeedback(
const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
{
ROS_INFO_STREAM( feedback->marker_name << " is now at "
<< feedback->pose.position.x << ", " << feedback->pose.position.y
<< ", " << feedback->pose.position.z );
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "simple_marker");
// create an interactive marker server on the topic namespace simple_marker
interactive_markers::InteractiveMarkerServer server("simple_marker");
// create an interactive marker for our server
visualization_msgs::InteractiveMarker int_marker;
int_marker.header.frame_id = "base_mk";
int_marker.header.stamp=ros::Time::now();
int_marker.name = "my_marker";
int_marker.description = "Simple king Control";
// create a grey box marker
visualization_msgs::Marker box_marker;
box_marker.type = visualization_msgs::Marker::CUBE;
box_marker.scale.x = 0.45;
box_marker.scale.y = 0.45;
box_marker.scale.z = 0.45;
box_marker.color.r = 1.0;
box_marker.color.g = 0.0;
box_marker.color.b = 0.0;
box_marker.color.a = 1.0;
// create a non-interactive control which contains the box
visualization_msgs::InteractiveMarkerControl box_control;
box_control.always_visible = true;
box_control.markers.push_back( box_marker );
// add the control to the interactive marker
int_marker.controls.push_back( box_control );
// create a control which will move the box
// this control does not contain any markers,
// which will cause RViz to insert two arrows
visualization_msgs::InteractiveMarkerControl rotate_control;
rotate_control.name = "move_x";
rotate_control.interaction_mode =
visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
// add the control to the interactive marker
int_marker.controls.push_back(rotate_control);
// add the interactive marker to our collection &
// tell the server to call processFeedback() when feedback arrives for it
server.insert(int_marker, &processFeedback);
// 'commit' changes and send to all clients
server.applyChanges();
// start the ROS main loop
ros::spin();
}
// %Tag(fullSource)%
分析
#include <ros/ros.h>
#include <interactive_markers/interactive_marker_server.h>
首先包含了頭文件延刘,主要是包含了<interactive_markers/interactive_marker_server.h>
void processFeedback(
const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
{
ROS_INFO_STREAM( feedback->marker_name << " is now at "
<< feedback->pose.position.x << ", " << feedback->pose.position.y
<< ", " << feedback->pose.position.z );
}
這個在運行的時候就可以看出,是返回交互信息的。
主函數(shù)里:
ros::init(argc, argv, "simple_marker");
ros初始化魏保。
interactive_markers::InteractiveMarkerServer server("simple_marker");
首先建立一個服務(wù)器,它負責(zé)把所有改動告知rviz.
visualization_msgs::InteractiveMarker int_marker;
int_marker.header.frame_id = "base_mk";
int_marker.header.stamp=ros::Time::now();
int_marker.name = "my_marker";
int_marker.description = "Simple king Control";
創(chuàng)建一個交互式marker——int_marker;定義了它的frame_id什么的摸屠。
visualization_msgs::Marker box_marker;
box_marker.type = visualization_msgs::Marker::CUBE;
box_marker.scale.x = 0.45;
box_marker.scale.y = 0.45;
box_marker.scale.z = 0.45;
box_marker.color.r = 1.0;
box_marker.color.g = 0.0;
box_marker.color.b = 0.0;
box_marker.color.a = 1.0;
這個只是個方塊囱淋,用來包含我們的交互式marker,好有個外形餐塘。下面的設(shè)定前面都見過。我現(xiàn)在知道的也不多皂吮,有新的理解再來補充戒傻。
visualization_msgs::InteractiveMarkerControl box_control;
box_control.always_visible = true;
box_control.markers.push_back( box_marker );
這個是非交互式控制,是為了在移動方塊時仍然能顯示方塊蜂筹。最后一句需纳,把這個控制塊添加到了前面建的方塊marker box_marker上。應(yīng)該是把方塊添加到了這個控制上艺挪。
int_marker.controls.push_back( box_control );
又把非交互式控制添加到了交互的marker上不翩。
visualization_msgs::InteractiveMarkerControl rotate_control;
rotate_control.name = "move_x";
rotate_control.interaction_mode =
visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
又創(chuàng)建了一個交互,用來移動方塊,最后一句口蝠,是創(chuàng)建兩個箭頭來移動器钟。
int_marker.controls.push_back(rotate_control);
把這個控制也添加給交互式marker。
server.insert(int_marker, &processFeedback);
把int_marker添加到了服務(wù)器的收集器妙蔗,并且讓服務(wù)器在有feedback到來的時候調(diào)用反饋函數(shù)傲霸。
server.applyChanges();
服務(wù)器提交改變并發(fā)送到所有的客戶端。
ros::spin();
開啟ROS主循環(huán)
總結(jié)
整個交互式的marker分為以下幾部分:
- 交互式部分:就是交互式marker的主體眉反,所有的其他部分都要添加到它上面來昙啄,組成一個交互式主體,它的信息也就是主體信息寸五。
- Marker部分:這個是顯示的主體梳凛,相當(dāng)于是個外包裝。
- non-control部分:它是用來承載marker部分的梳杏。
我原本想把中間一段程序改成這樣:
// create a non-interactive control which contains the box
//visualization_msgs::InteractiveMarkerControl box_control;
//box_control.always_visible = true;
//box_control.markers.push_back( box_marker );
// add the control to the interactive marker
//int_marker.controls.push_back( box_control );
int_marker.markers.push_back( box_marker );
結(jié)果發(fā)現(xiàn)int_marker沒有markers這個屬性韧拒,所以需要一個non-control。
- 一個control,這個control就是用來控制可交互marker的
最后裁赠,將marker注冊到服務(wù)器的collection摩桶,服務(wù)器會監(jiān)視它的狀態(tài),它有什么改變雇初,都會被通告服務(wù)器調(diào)用processFeedback
然后使服務(wù)器將改變發(fā)送到所有客戶端。