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

cube.png
cube.png

源碼

/*
 * 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 <math.h>

#include <tf/LinearMath/Vector3.h>


using namespace visualization_msgs;

boost::shared_ptr<interactive_markers::InteractiveMarkerServer> server;

std::vector< tf::Vector3 > positions;

void processFeedback( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
{
  switch ( feedback->event_type )
  {
    case visualization_msgs::InteractiveMarkerFeedback::POSE_UPDATE:
    {
      //compute difference vector for this cube

      tf::Vector3 fb_pos(feedback->pose.position.x, feedback->pose.position.y, feedback->pose.position.z);
      unsigned index = atoi( feedback->marker_name.c_str() );

      if ( index > positions.size() )
      {
        return;
      }
      tf::Vector3 fb_delta = fb_pos - positions[index];

      // move all markers in that direction
      for ( unsigned i=0; i<positions.size(); i++ )
      {
        float d = fb_pos.distance( positions[i] );
        float t = 1 / (d*5.0+1.0) - 0.2;
        if ( t < 0.0 ) t=0.0;

        positions[i] += t * fb_delta;

        if ( i == index ) {
          ROS_INFO_STREAM( d );
          positions[i] = fb_pos;
        }

        geometry_msgs::Pose pose;
        pose.position.x = positions[i].x();
        pose.position.y = positions[i].y();
        pose.position.z = positions[i].z();

        std::stringstream s;
        s << i;
        server->setPose( s.str(), pose );
      }


      break;
    }
  }
  server->applyChanges();
}

InteractiveMarkerControl& makeBoxControl( InteractiveMarker &msg )
{
  InteractiveMarkerControl control;
  control.always_visible = true;
  control.orientation_mode = InteractiveMarkerControl::VIEW_FACING;
  control.interaction_mode = InteractiveMarkerControl::MOVE_PLANE;
  control.independent_marker_orientation = true;

  Marker marker;

  marker.type = Marker::CUBE;
  marker.scale.x = msg.scale;
  marker.scale.y = msg.scale;
  marker.scale.z = msg.scale;
  marker.color.r = 0.65+0.7*msg.pose.position.x;
  marker.color.g = 0.65+0.7*msg.pose.position.y;
  marker.color.b = 0.65+0.7*msg.pose.position.z;
  marker.color.a = 1.0;

  control.markers.push_back( marker );
  msg.controls.push_back( control );

  return msg.controls.back();
}


void makeCube( )
{
  int side_length = 10;
  float step = 1.0/ (float)side_length;
  int count = 0;

  positions.reserve( side_length*side_length*side_length );

  for ( double x=-0.5; x<0.5; x+=step )
  {
    for ( double y=-0.5; y<0.5; y+=step )
    {
      for ( double z=0.0; z<1.0; z+=step )
      {
        InteractiveMarker int_marker;
        int_marker.header.frame_id = "base_link";
        int_marker.scale = step;

        int_marker.pose.position.x = x;
        int_marker.pose.position.y = y;
        int_marker.pose.position.z = z;

        positions.push_back( tf::Vector3(x,y,z) );

        std::stringstream s;
        s << count;
        int_marker.name = s.str();

        makeBoxControl(int_marker);

        server->insert( int_marker );
        server->setCallback( int_marker.name, &processFeedback );

        count++;
      }
    }
  }
}

int main(int argc, char** argv)
{
  ros::init(argc, argv, "cube");

  server.reset( new interactive_markers::InteractiveMarkerServer("cube") );

  ros::Duration(0.1).sleep();

  ROS_INFO("initializing..");
  makeCube();
  server->applyChanges();
  ROS_INFO("ready.");

  ros::spin();

  server.reset();
}

源碼分析

#include <ros/ros.h>
#include <interactive_markers/interactive_marker_server.h>
#include <math.h>
#include <tf/LinearMath/Vector3.h>
using namespace visualization_msgs;

首先頭文件包含开瞭。

boost::shared_ptr<interactive_markers::InteractiveMarkerServer> server;

std::vector< tf::Vector3 > positions;

建立了一個服務器和一個三維向量positions.

void processFeedback( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
{
  switch ( feedback->event_type )
  {
    case visualization_msgs::InteractiveMarkerFeedback::POSE_UPDATE:
    {
      //compute difference vector for this cube

      tf::Vector3 fb_pos(feedback->pose.position.x, feedback->pose.position.y, feedback->pose.position.z);
      unsigned index = atoi( feedback->marker_name.c_str() );

      if ( index > positions.size() )
      {
        return;
      }
      tf::Vector3 fb_delta = fb_pos - positions[index];

      // move all markers in that direction
      for ( unsigned i=0; i<positions.size(); i++ )
      {
        float d = fb_pos.distance( positions[i] );
        float t = 1 / (d*5.0+1.0) - 0.2;
        if ( t < 0.0 ) t=0.0;

        positions[i] += t * fb_delta;

        if ( i == index ) {
          ROS_INFO_STREAM( d );
          positions[i] = fb_pos;
        }

        geometry_msgs::Pose pose;
        pose.position.x = positions[i].x();
        pose.position.y = positions[i].y();
        pose.position.z = positions[i].z();

        std::stringstream s;
        s << i;
        server->setPose( s.str(), pose );
      }


      break;
    }
  }
  server->applyChanges();
}

這個函數(shù)很熟悉了,這個是反饋函數(shù)罩息。首先當其被服務器調(diào)用時嗤详,先記錄此時方塊的位置,然后計算它移動的增量扣汪。
然后將增量應用到其他塊上断楷。

InteractiveMarkerControl& makeBoxControl( InteractiveMarker &msg )
{
  InteractiveMarkerControl control;
  control.always_visible = true;
  control.orientation_mode = InteractiveMarkerControl::VIEW_FACING;
  control.interaction_mode = InteractiveMarkerControl::MOVE_PLANE;
  control.independent_marker_orientation = true;

  Marker marker;

  marker.type = Marker::CUBE;
  marker.scale.x = msg.scale;
  marker.scale.y = msg.scale;
  marker.scale.z = msg.scale;
  marker.color.r = 0.65+0.7*msg.pose.position.x;
  marker.color.g = 0.65+0.7*msg.pose.position.y;
  marker.color.b = 0.65+0.7*msg.pose.position.z;
  marker.color.a = 1.0;

  control.markers.push_back( marker );
  msg.controls.push_back( control );

  return msg.controls.back();
}

這個之前說過了,就是建立一個帶著marker的控制崭别,并添加到交互式marker上冬筒。

void makeCube( )
{
  int side_length = 10;
  float step = 1.0/ (float)side_length;
  int count = 0;

  positions.reserve( side_length*side_length*side_length );

  for ( double x=-0.5; x<0.5; x+=step )
  {
    for ( double y=-0.5; y<0.5; y+=step )
    {
      for ( double z=0.0; z<1.0; z+=step )
      {
        InteractiveMarker int_marker;
        int_marker.header.frame_id = "base_link";
        int_marker.scale = step;

        int_marker.pose.position.x = x;
        int_marker.pose.position.y = y;
        int_marker.pose.position.z = z;

        positions.push_back( tf::Vector3(x,y,z) );

        std::stringstream s;
        s << count;
        int_marker.name = s.str();

        makeBoxControl(int_marker);

        server->insert( int_marker );
        server->setCallback( int_marker.name, &processFeedback );

        count++;
      }
    }
  }
}

這個就是建立一組cube的程序。

主函數(shù)就不說了茅主,跟上次的一樣舞痰。

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市诀姚,隨后出現(xiàn)的幾起案子响牛,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,602評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件呀打,死亡現(xiàn)場離奇詭異矢赁,居然都是意外死亡,警方通過查閱死者的電腦和手機贬丛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,442評論 2 382
  • 文/潘曉璐 我一進店門撩银,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人豺憔,你說我怎么就攤上這事额获。” “怎么了恭应?”我有些...
    開封第一講書人閱讀 152,878評論 0 344
  • 文/不壞的土叔 我叫張陵抄邀,是天一觀的道長。 經(jīng)常有香客問我昼榛,道長境肾,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,306評論 1 279
  • 正文 為了忘掉前任褒纲,我火速辦了婚禮准夷,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘莺掠。我一直安慰自己,他們只是感情好读宙,可當我...
    茶點故事閱讀 64,330評論 5 373
  • 文/花漫 我一把揭開白布彻秆。 她就那樣靜靜地躺著,像睡著了一般结闸。 火紅的嫁衣襯著肌膚如雪唇兑。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,071評論 1 285
  • 那天桦锄,我揣著相機與錄音扎附,去河邊找鬼。 笑死结耀,一個胖子當著我的面吹牛留夜,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播图甜,決...
    沈念sama閱讀 38,382評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼碍粥,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了黑毅?” 一聲冷哼從身側(cè)響起嚼摩,我...
    開封第一講書人閱讀 37,006評論 0 259
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后枕面,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體愿卒,經(jīng)...
    沈念sama閱讀 43,512評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,965評論 2 325
  • 正文 我和宋清朗相戀三年潮秘,在試婚紗的時候發(fā)現(xiàn)自己被綠了掘猿。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,094評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡唇跨,死狀恐怖稠通,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情买猖,我是刑警寧澤改橘,帶...
    沈念sama閱讀 33,732評論 4 323
  • 正文 年R本政府宣布,位于F島的核電站玉控,受9級特大地震影響飞主,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜高诺,卻給世界環(huán)境...
    茶點故事閱讀 39,283評論 3 307
  • 文/蒙蒙 一碌识、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧虱而,春花似錦筏餐、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,286評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至惠呼,卻和暖如春导俘,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背剔蹋。 一陣腳步聲響...
    開封第一講書人閱讀 31,512評論 1 262
  • 我被黑心中介騙來泰國打工旅薄, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人泣崩。 一個月前我還...
    沈念sama閱讀 45,536評論 2 354
  • 正文 我出身青樓少梁,卻偏偏與公主長得像,于是被迫代替她去往敵國和親律想。 傳聞我的和親對象是個殘疾皇子猎莲,可洞房花燭夜當晚...
    茶點故事閱讀 42,828評論 2 345

推薦閱讀更多精彩內(nèi)容