ROS与Qt5人机交互界面开发-添加rviz显示界面
ROS与Qt5人机交互界面开发-添加rviz显示界面
说明:
- 介绍如何添加rviz显示界面
步骤:
- 实现效果:
- 核心代码
- 创建librviz界面类:
- qrviz_widget.h内容如下:
#ifndef QRVIZ_WIDGET_H
#define QRVIZ_WIDGET_H
#include <QWidget>
#include <rviz/visualization_manager.h>
#include <rviz/render_panel.h>
#include <rviz/display.h>
#include <rviz/tool_manager.h>
#include <rviz/visualization_manager.h>
#include <rviz/render_panel.h>
#include <rviz/display.h>
#include<rviz/tool_manager.h>
#include <rviz_visual_tools/rviz_visual_tools.h>
namespace Ui {
class QRviz_widget;
}
class QRviz_widget : public QWidget
{
Q_OBJECT
public:
explicit QRviz_widget(QWidget *parent = 0);
~QRviz_widget();
private:
Ui::QRviz_widget *ui;
};
#endif // QRVIZ_WIDGET_H
- qrviz_widget.cpp内容如下:
#include "../include/cyrobot_monitor/qrviz_widget.h"
#include "ui_qrviz_widget.h"
QRviz_widget::QRviz_widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::QRviz_widget)
{
ui->setupUi(this);
int argc;
char **argv;
ros::init(argc,argv,"QRviz",ros::init_options::AnonymousName);
//创建rviz容器
rviz::RenderPanel *render_panel_=new rviz::RenderPanel;
//设置鼠标形状
render_panel_->setCursor(Qt::PointingHandCursor);
//向layout添加widget
ui->verticalLayout->addWidget(render_panel_);
//初始化rviz控制对象
rviz::VisualizationManager* manager_=new rviz::VisualizationManager(render_panel_);
//初始化camera 这行代码实现放大 缩小 平移等操作
render_panel_->initialize(manager_->getSceneManager(),manager_);
// //工具管理
rviz::ToolManager* tool_man;
// connect( tool_man, SIGNAL( toolAdded( Tool* )), this, SLOT( addTool( Tool* )));
// connect( tool_man, SIGNAL( toolRemoved( Tool* )), this, SLOT( removeTool( Tool* )));
// connect( tool_man, SIGNAL( toolRefreshed( Tool* )), this, SLOT( refreshTool( Tool* )));
// connect( tool_man, SIGNAL( toolChanged( Tool* )), this, SLOT( indicateToolIsCurrent( Tool* )));
manager_->initialize();
manager_->removeAllDisplays();
rviz::Display* grid_ = manager_->createDisplay( "rviz/Grid", "adjustable grid", true );
ROS_ASSERT( grid_ != NULL );
// Configure the GridDisplay the way we like it.
grid_->subProp( "Line Style" )->setValue( "Billboards" );
grid_->subProp( "Color" )->setValue(QColor(125,125,125));
manager_->startUpdate();
}
QRviz_widget::~QRviz_widget()
{
delete ui;
}
- 新建widget对象:
qrviz=new QRviz_widget(ui.widget_rviz);
qrviz->showFullScreen();
qrviz->show();
- 如图:
- 其中,为了实现鼠标放大缩小移动的功能,本来以为需要自己监听鼠标事件,读了读rviz源码,发现这一行代码就能实现:
//初始化camera 这行代码实现放大 缩小 平移等操作
render_panel_->initialize(manager_->getSceneManager(),manager_);
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号