ROS与Qt5人机交互界面开发-订阅map话题
ROS与Qt5人机交互界面开发-订阅map话题
说明:
- 介绍如何订阅map话题
步骤:
- 这一篇教程在教程四的基础上进行修改,修改了部分bug,实现了map图层的添加,显示,放大,缩小等
- 实现效果
- 核心代码
- 主要就是这么几句:
//添加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_;
};
#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());
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号