상세 컨텐츠

본문 제목

rviz tutorial 1: Marker

rviz

by Riella 2020. 7. 13. 02:26

본문

728x90

reference: http://wiki.ros.org/rviz/Tutorials/Markers%3A%20Basic%20Shapes

This is the original document, and it helps you to visualize data in rviz, without interpreting it.

 

Before doing anything, open a bash shell then type

roscore

 

Now, open another bash shell, and download the rviz visual tools.

sudo apt-get install ros-melodic-rviz-visual-tools

If it does not work, run below code then try again.

sudo apt update

 

Then, make the package in working directory.

In /catkin_ws/src$

catkin_create_pkg using_markers roscpp visualization_msgs

 

Create basic_shapes.cpp file (on /using_markers/src$).

echo "blah" > basic_shapes.cpp

Then edit the code:

nano basic_shapes.cpp

Copy and paste the source code below:

#include <ros/ros.h>
#include <visualization_msgs/Marker.h>

int main( int argc, char** argv )
{
  ros::init(argc, argv, "basic_shapes");
  ros::NodeHandle n;
  ros::Rate r(1);
  ros::Publisher marker_pub = n.advertise<visualization_msgs::Marker>("visualization_marker", 1);

  // Set our initial shape type to be a cube
  uint32_t shape = visualization_msgs::Marker::CUBE;

  while (ros::ok())
  {
    visualization_msgs::Marker marker;
    // Set the frame ID and timestamp.  See the TF tutorials for information on these.
    marker.header.frame_id = "/my_frame";
    marker.header.stamp = ros::Time::now();

    // Set the namespace and id for this marker.  This serves to create a unique ID
    // Any marker sent with the same namespace and id will overwrite the old one
    marker.ns = "basic_shapes";
    marker.id = 0;

    // Set the marker type.  Initially this is CUBE, and cycles between that and SPHERE, ARROW, and CYLINDER
    marker.type = shape;

    // Set the marker action.  Options are ADD, DELETE, and new in ROS Indigo: 3 (DELETEALL)
    marker.action = visualization_msgs::Marker::ADD;

    // Set the pose of the marker.  This is a full 6DOF pose relative to the frame/time specified in the header
    marker.pose.position.x = 0;
    marker.pose.position.y = 0;
    marker.pose.position.z = 0;
    marker.pose.orientation.x = 0.0;
    marker.pose.orientation.y = 0.0;
    marker.pose.orientation.z = 0.0;
    marker.pose.orientation.w = 1.0;

    // Set the scale of the marker -- 1x1x1 here means 1m on a side
    marker.scale.x = 1.0;
    marker.scale.y = 1.0;
    marker.scale.z = 1.0;

    // Set the color -- be sure to set alpha to something non-zero!
    marker.color.r = 0.0f;
    marker.color.g = 1.0f;
    marker.color.b = 0.0f;
    marker.color.a = 1.0;

    marker.lifetime = ros::Duration();

    // Publish the marker
    while (marker_pub.getNumSubscribers() < 1)
    {
      if (!ros::ok())
      {
        return 0;
      }
      ROS_WARN_ONCE("Please create a subscriber to the marker");
      sleep(1);
    }
    marker_pub.publish(marker);

    // Cycle between different shapes
    switch (shape)
    {
    case visualization_msgs::Marker::CUBE:
      shape = visualization_msgs::Marker::SPHERE;
      break;
    case visualization_msgs::Marker::SPHERE:
      shape = visualization_msgs::Marker::ARROW;
      break;
    case visualization_msgs::Marker::ARROW:
      shape = visualization_msgs::Marker::CYLINDER;
      break;
    case visualization_msgs::Marker::CYLINDER:
      shape = visualization_msgs::Marker::CUBE;
      break;
    }

    r.sleep();
  }
}

Ctrl+S, Ctrl+X to save file.

 

Also, in src file, you need to add this code to CMakeLists.txt

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

 

Navigate to the catkin_ws$ then build the source code

catkin_make

Then source the setup.bash file

source devel/setup.bash

Then run the code

rosrun using_markers basic_shapes

 

Then you need to be able to see this in rviz.

Turn on another bash shell.

Type following codes.

#Turn on xming first
export DISPLAY=:0
rosrun rviz rviz
#if the program fails, type:
export LIBGL_ALWAYS_INDIRECT=0
#Then try again typing:
rosrun rviz rviz

 

When rviz is turned on, press 'add' button, click 'marker', press 'ok'.

Then on the left side, there is fixed frame.

Click that and change info '/my_frame'

 

Now you'll be able to see the shape changes in the origin of the grid.

댓글 영역