RViz學習筆記(六) - 交互式marker:Simple_marker.cpp分析

Simple_marker

源碼

/*
 * 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 );
}

這個在運行的時候就可以看出,是返回交互信息的。

主函數里:

ros::init(argc, argv, "simple_marker");

ros初始化。

interactive_markers::InteractiveMarkerServer server("simple_marker");

首先建立一個服務器,它負責把所有改動告知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";

創建一個交互式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,好有個外形。下面的設定前面都見過。我現在知道的也不多,有新的理解再來補充。

  visualization_msgs::InteractiveMarkerControl box_control;
  box_control.always_visible = true;
  box_control.markers.push_back( box_marker );

這個是非交互式控制,是為了在移動方塊時仍然能顯示方塊。最后一句,把這個控制塊添加到了前面建的方塊marker box_marker上。應該是把方塊添加到了這個控制上。

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;

又創建了一個交互,用來移動方塊,最后一句,是創建兩個箭頭來移動。

int_marker.controls.push_back(rotate_control);

把這個控制也添加給交互式marker。

server.insert(int_marker, &processFeedback);

把int_marker添加到了服務器的收集器,并且讓服務器在有feedback到來的時候調用反饋函數。

server.applyChanges();

服務器提交改變并發送到所有的客戶端。

ros::spin();

開啟ROS主循環

總結

整個交互式的marker分為以下幾部分:

  • 交互式部分:就是交互式marker的主體,所有的其他部分都要添加到它上面來,組成一個交互式主體,它的信息也就是主體信息。
  • Marker部分:這個是顯示的主體,相當于是個外包裝。
  • 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 );

結果發現int_marker沒有markers這個屬性,所以需要一個non-control。

  • 一個control,這個control就是用來控制可交互marker的

最后,將marker注冊到服務器的collection,服務器會監視它的狀態,它有什么改變,都會被通告服務器調用processFeedback

然后使服務器將改變發送到所有客戶端。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,523評論 25 708
  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標簽默認的外補...
    _Yfling閱讀 13,814評論 1 92
  • 巧書的確是很巧,用了很多很巧的方法讓我們直面自己,直面自己一直想掩蓋的,或者無意掩蓋,但是卻沒有意識的自我。 今天...
    慧子永遠沒有太晚的開始閱讀 433評論 2 5
  • 文/愛貓的胖妞 從我記事起,就能深刻感到國人的愛好總是扎堆成群的,隨之產生的熱潮也總是一波一波的。 近期國內掀起了...
    愛貓的胖妞閱讀 320評論 0 0