一,实现效果

在这里插入图片描述

二,核心代码

主要就是这么几句:

//添加map display
map_=manager_->createDisplay("rviz/Map","QMap",true);
ROS_ASSERT(map_);
//设置display的话题属性
map_->subProp("Topic")->setValue(topic);
//设置display的透明度
map_->subProp("Alpha")->setValue(Alpha);
//设置display的样式属性
map_->subProp("Color Scheme")->setValue(Color_Scheme);
map_->setEnabled(enable);
//rviz控制器更新界面显示
manager_->startUpdate();

相关源文件:
qrviz.hpp

 

#ifndef QRVIZ_H
#define QRVIZ_H

#include <QVBoxLayout>
#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>
#include <QThread>
#include <QDebug>
class QRviz:public QThread
{
Q_OBJECT
public:
QRviz(QVBoxLayout *layout,QString node_name);
void run();
void createDisplay(QString display_name,QString topic_name);
void Display_Grid(bool enable,QColor color=QColor(125,125,125));
void Display_Map(bool enable,QString topic,double Alpha,QString Color_Scheme);

private:
rviz::RenderPanel *render_panel_;
rviz::VisualizationManager *manager_;
rviz::Display* grid_=NULL ;
rviz::Display* map_=NULL ;
QVBoxLayout *layout;
QString nodename;

// rviz::VisualizationManager *manager_=NULL;
// rviz::RenderPanel *render_panel_;

};

 

qrviz.cpp

#include "../include/cyrobot_monitor/qrviz.hpp"

QRviz::QRviz(QVBoxLayout *layout,QString node_name)
{
int argc;
char **argv;
this->layout=layout;
this->nodename=node_name;
ros::init(argc,argv,"QRviz",ros::init_options::AnonymousName);
//创建rviz容器
render_panel_=new rviz::RenderPanel;
//向layout添加widget
layout->addWidget(render_panel_);
//初始化rviz控制对象
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();

}
//显示grid
void QRviz::Display_Grid(bool enable,QColor color)
{
if(grid_==NULL)
{
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(color);

}
else{
grid_->subProp( "Color" )->setValue(color);
}
grid_->setEnabled(enable);
manager_->startUpdate();
}
//显示map
void QRviz::Display_Map(bool enable,QString topic,double Alpha,QString Color_Scheme)
{
if(!enable&&map_)
{
map_->setEnabled(false);
return ;
}
if(map_==NULL)
{
qDebug()<<"map ok";
map_=manager_->createDisplay("rviz/Map","QMap",true);

ROS_ASSERT(map_);
map_->subProp("Topic")->setValue(topic);
map_->subProp("Alpha")->setValue(Alpha);
map_->subProp("Color Scheme")->setValue(Color_Scheme);
}
map_->setEnabled(enable);
manager_->startUpdate();
}
void QRviz::createDisplay(QString display_name,QString topic_name)
{


}
void QRviz::run()
{

}

在mainwindow使用:

QComboBox *topic_box=(QComboBox *) ui.treeWidget_rviz->itemWidget(parentItem->child(1),1);
QLineEdit *alpha=(QLineEdit *) ui.treeWidget_rviz->itemWidget(parentItem->child(2),1);
QComboBox *scheme=(QComboBox *) ui.treeWidget_rviz->itemWidget(parentItem->child(3),1);
map_rviz->Display_Map(enable,topic_box->currentText(),alpha->text().toDouble(),scheme->currentText());

三,完整开源项目

在我自己学习的过程中目前发现没有相关类似完整开源项目,为了帮助其他人少走弯路,我决定将自己的完整项目开源:
github

转载自:

蒋程扬的部落格