자율주행/ROS tutorial C++

Interactive Markers: Getting Started

Tony Lim 2021. 2. 7. 14:18

 

#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_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
  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 = 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 );

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

basically you have your base_link == int_marker 

and you append box_marker, box_control, rotate_control

and than you need to edit CMakeLists.txt

find_package(catkin REQUIRED COMPONENTS
  roscpp
  visualization_msgs
  interactive_markers
)

. . .

add_executable(simple_marker src/simple_marker.cpp)
target_link_libraries(simple_marker ${catkin_LIBRARIES})

also package.xml

  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>roscpp</build_depend>
  <build_depend>visualization_msgs</build_depend>
  <build_depend>interactive_markers</build_depend>
  <build_depend>tf</build_depend>

  <build_export_depend>roscpp</build_export_depend>
  <build_export_depend>visualization_msgs</build_export_depend>
  <build_export_depend>interactive_markers</build_export_depend>
  <build_export_depend>tf</build_export_depend>

  <exec_depend>roscpp</exec_depend>
  <exec_depend>visualization_msgs</exec_depend>
  <exec_depend>interactive_markers</exec_depend>
  <exec_depend>tf</exec_depend>

you can now interact with cube and move it according to x axis.

'자율주행 > ROS tutorial C++' 카테고리의 다른 글

Markers:Points and Lines  (0) 2021.02.07
Markers: Basic shapes  (0) 2021.01.10
Writing a Simple Service and Client  (0) 2020.12.30
Writing a Simple Publisher and Subscriber  (0) 2020.12.30