rviz學習筆記(二)——Markers: Points and Lines (C++) 點和線

來源:互聯網
上載者:User

標籤:height   using   nts   block   images   geo   color   cpp   lock   

一、在using_marker/src中編寫點和線代碼

vim ~/catkin_ws/src/using_marker/src/points_and_lines.cpp

 

  編寫代碼,其中有注釋

#include <ros/ros.h>#include <visualization_msgs/Marker.h>#include <cmath>int main( int argc, char** argv ){  //建立一個發布器  ros::init(argc, argv, "points_and_lines");  ros::NodeHandle n;  ros::Publisher marker_pub = n.advertise<visualization_msgs::Marker>("visualization_marker", 10);  ros::Rate r(30);  float f = 0.0;  while (ros::ok())  {    visualization_msgs::Marker points, line_strip, line_list;    //初始化    points.header.frame_id = line_strip.header.frame_id = line_list.header.frame_id = "/my_frame";    points.header.stamp = line_strip.header.stamp = line_list.header.stamp = ros::Time::now();    points.ns = line_strip.ns = line_list.ns = "points_and_lines";    points.action = line_strip.action = line_list.action = visualization_msgs::Marker::ADD;    points.pose.orientation.w = line_strip.pose.orientation.w = line_list.pose.orientation.w = 1.0;    //分配3個id    points.id = 0;    line_strip.id = 1;    line_list.id = 2;    //初始化形狀    points.type = visualization_msgs::Marker::POINTS;    line_strip.type = visualization_msgs::Marker::LINE_STRIP;    line_list.type = visualization_msgs::Marker::LINE_LIST;    //初始化大小    // POINTS markers use x and y scale for width/height respectively    points.scale.x = 0.2;    points.scale.y = 0.2;    // LINE_STRIP/LINE_LIST markers use only the x component of scale, for the line width    line_strip.scale.x = 0.1;    line_list.scale.x = 0.1;    //初始化顏色    // Points are green    points.color.g = 1.0f;    points.color.a = 1.0;    // Line strip is blue    line_strip.color.b = 1.0;    line_strip.color.a = 1.0;    // Line list is red    line_list.color.r = 1.0;    line_list.color.a = 1.0;    // Create the vertices for the points and lines    for (uint32_t i = 0; i < 100; ++i)    {      float y = 5 * sin(f + i / 100.0f * 2 * M_PI);      float z = 5 * cos(f + i / 100.0f * 2 * M_PI);      geometry_msgs::Point p;      p.x = (int32_t)i - 50;      p.y = y;      p.z = z;      points.points.push_back(p);      line_strip.points.push_back(p);      // The line list needs two points for each line      line_list.points.push_back(p);      p.z += 1.0;      line_list.points.push_back(p);    }    marker_pub.publish(points);    marker_pub.publish(line_strip);    marker_pub.publish(line_list);    r.sleep();    f += 0.04;  }}

 

  在CMakeLists.txt中添加

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

 

 

二、編譯工程

cd ~/catkin_wscatkin_make

 

 

三、測試

1、運行編寫的發布器

rosrun using_markers points_and_lines

 

2、運行rviz進行設定

rosrun rviz rviz

 

3、運行結果

 

rviz學習筆記(二)——Markers: Points and Lines (C++) 點和線

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.