本系列教程文章专栏:
ROS机器人GUI程序开发
本系列课程已上线古月学院,欢迎感兴趣的小伙伴订阅:
开发交流QQ群: 797497206
完整项目代码:
github
主要就是这么几句:
//添加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
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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_;
};
#endif // QRVIZ_H
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
创作不易,如果本教程对你有帮助,关注或点个赞吧,或者github标个星哦~~
您的支持就是我最大的动力~