可交互的Marker

#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){
    //初始化节点, 名字叫simple_marker
    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
    // 创建一个可交互的Marker, 对象名int_marker
    visualization_msgs::InteractiveMarker int_marker;
    // 配置inter_marker的基础信息
    int_marker.header.frame_id = "base_link";
    int_marker.header.stamp = ros::Time::now();
    int_marker.name = "my_marker";
    int_marker.description = "Simple 1-DOF Control";

    // create a grey box marker
    // 被控制的对象是一个灰色的marker
    visualization_msgs::Marker box_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 = 0.5;
    box_marker.color.g = 0.5;
    box_marker.color.b = 0.5;
    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 );

    // 把这个控制器加到int_marking的控制矢量中.
    // add the control to the interactive marker
    int_marker.controls.push_back( box_control );
    
    // 创建一个角度控制器, 这个控制器不包含图形, 但是rviz会加两个箭头, 交互模式为移动x轴
    // 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;
    
    // 把这个控制器也放inter_maker的控制矢量中.
    // 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();
}

使用rostopic echo /simple_marker/feedback可以看到数据包:
---
header:
  seq: 1823
  stamp:
    secs: 0
    nsecs:         0
  frame_id: base_link
client_id: /rviz_1494901834015798500/
marker_name: my_marker
control_name: move_x
event_type: 5
pose:
  position:
    x: 0.420606017113
    y: 0.0
    z: 0.0
  orientation:
    x: 0.0
    y: 0.0
    z: 0.0
    w: 1.0
menu_entry_id: 0
mouse_point:
  x: 1.04800105095
  y: 0.0712747573853
  z: 0.0290296077728
mouse_point_valid: True


原文地址:https://www.cnblogs.com/Montauk/p/6861185.html