在rviz中使用点和线可视化轨迹(Markers: Points and Lines)

1 介绍

在做位姿相关的研究时,我们希望直观的可视化轨迹,尤其是对比不同的轨迹。

本教程将向您介绍POINTSLINE_STRIPLINE_LIST标记类型。有关类型的完整列表,请参见“标记显示”页面

2 使用Points, Line Strips, and Line Lists

POINTSLINE_STRIPLINE_LIST标记都使用visualization_msgs/Marker消息的点成员。
POINTS类型在添加的每个点处放置一个点。LINE_STRIP类型将每个点用作一组连接的线中的顶点,其中点0连接到点1,点1连接到点2,点2连接到点3等。LINE_LIST类型在每对点(即点0到1、2到3等)中创建未连接的线。

2.1 代码

#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;

    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;
  }
}

2.2 代码解释

现在,让我们分解代码,跳过上一教程中解释的内容。产生的总体效果是一个旋转的螺旋线,其线从每个顶点向上伸出。

    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;

在这里,我们创建三个visualization_msgs/Marker消息并初始化它们的所有共享数据。我们利用消息成员默认为0且仅设置位姿的成员w这一事实。

    points.id = 0;
    line_strip.id = 1;
    line_list.id = 2;

我们为三个标记分配了三个不同的ID。使用points_and_lines命名空间可确保它们不会与其他广播发生冲突。

    points.type = visualization_msgs::Marker::POINTS;
    line_strip.type = visualization_msgs::Marker::LINE_STRIP;
    line_list.type = visualization_msgs::Marker::LINE_LIST;

在这里,我们将标记类型设置为POINTSLINE_STRIPLINE_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;

标尺(scale)成员对这些标记类型表示不同的含义。POINTS标记分别将x和y成员用于宽度和高度,而LINE_STRIPLINE_LIST标记仅使用x分量来定义线宽。比例尺值以米为单位。

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

我们使用正弦和余弦生成一个螺旋。POINTSLINE_STRIP标记每个顶点只需要一个点,而LINE_LIST标记则需要2个。

2.3 查看标记

(1)创建一个名为using_markers的包

catkin_create_pkg using_markers roscpp visualization_msgs

(2)编辑using_markers包中的CMakeLists.txt文件,并将其添加到底部

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

(3)编译

$ catkin_make

(4)运行

$ rosrun rviz rviz&
$ rosrun using_markers points_and_lines

点击【Add】,添加【Marker】,将【Fixed Frame】设置为【my_frame】,最后应该看到一个看起来像这样的旋转螺旋:
在这里插入图片描述

3 参考

http://wiki.ros.org/rviz/Tutorials/Markers%3A%20Points%20and%20Lines

  • 6
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将MongoDb的x和y数据可视化到web页面,可以使用Python的Flask框架和Plotly库。下面是一个简单的示例代码: 1. 安装必要的库 ``` pip install pymongo flask plotly ``` 2. 创建Flask应用 ```python from flask import Flask, render_template from pymongo import MongoClient import plotly import plotly.graph_objs as go app = Flask(__name__) # 连接MongoDB数据库 client = MongoClient('mongodb://localhost:27017/') db = client['test'] # 选择数据库 collection = db['data'] # 选择集合 @app.route('/') def index(): # 从MongoDB获取数据 x = [] y = [] for data in collection.find(): x.append(data['x']) y.append(data['y']) # 创建Plotly图表 trace = go.Scatter( x=x, y=y, mode='markers' ) data = [trace] layout = go.Layout(title='MongoDB数据可视化') fig = go.Figure(data=data, layout=layout) # 将图表转换为HTML代码 plot_div = plotly.offline.plot(fig, output_type='div') # 渲染模板并将图表嵌入到页面 return render_template('index.html', plot_div=plot_div) if __name__ == '__main__': app.run(debug=True) ``` 3. 创建模板文件 在项目根目录下创建一个名为`templates`的文件夹,然后在其创建一个名为`index.html`的模板文件,代码如下: ```html <!DOCTYPE html> <html> <head> <title>MongoDB数据可视化</title> <script src="https://cdn.plot.ly/plotly-latest.min.js"></script> </head> <body> {{ plot_div|safe }} </body> </html> ``` 4. 运行应用 在终端执行以下命令启动应用: ``` python app.py ``` 然后在浏览器访问http://localhost:5000/,就可以看到MongoDB的x和y数据被可视化了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值