ROS Qt5 librviz人机交互界面开发六(显示激光雷达点云数据)

1039
1
2020年4月20日 22时36分

一,实现效果:

在这里插入图片描述

二,核心代码

如果按照本人前几篇博客的话进程的话,就很容易实现这个功能了,主要就是用manager添加一个display:
核心代码:

//显示激光雷达
void QRviz::Display_LaserScan(bool enable,QString topic)
{
	if(laser_==NULL) {
		laser_=manager_->createDisplay("rviz/LaserScan","QLaser",enable);
		ROS_ASSERT(laser_);
		laser_->subProp("Topic")->setValue(topic);
	} else {
		delete laser_;
		laser_=manager_->createDisplay("rviz/LaserScan","QLaser",enable);
		ROS_ASSERT(laser_);
		laser_->subProp("Topic")->setValue(topic);
	}
	qDebug()<<"topic:"<<topic;
	laser_->setEnabled(enable);
	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);
//显示Grid
	void Display_Grid(bool enable,QString Reference_frame,int Plan_Cell_count,QColor color=QColor(125,125,125));
//显示map
	void Display_Map(bool enable,QString topic,double Alpha,QString Color_Scheme);
//设置全局显示属性
	void SetGlobalOptions(QString frame_name,QColor backColor,int frame_rate);
//显示激光雷达点云
	void Display_LaserScan(bool enable,QString topic);

private:
	rviz::RenderPanel *render_panel_;
	rviz::VisualizationManager *manager_;
	rviz::Display* grid_=NULL ;
	rviz::Display* map_=NULL ;
	rviz::Display* laser_=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();

}
//显示grid
void QRviz::Display_Grid(bool enable,QString Reference_frame,int Plan_Cell_count,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);
		grid_->subProp( "Reference Frame" )->setValue(Reference_frame);
		grid_->subProp("Plane Cell Count")->setValue(Plan_Cell_count);

	} else {
		delete grid_;
		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);
		grid_->subProp( "Reference Frame" )->setValue(Reference_frame);
		grid_->subProp("Plane Cell Count")->setValue(Plan_Cell_count);
	}
	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) {
		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);
	} else {
		ROS_ASSERT(map_);
		qDebug()<<"asdasdasd:"<<topic<<Alpha;

		delete map_;
		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::Display_LaserScan(bool enable,QString topic)
{
	if(laser_==NULL) {
		laser_=manager_->createDisplay("rviz/LaserScan","QLaser",enable);
		ROS_ASSERT(laser_);
		laser_->subProp("Topic")->setValue(topic);
	} else {
		delete laser_;
		laser_=manager_->createDisplay("rviz/LaserScan","QLaser",enable);
		ROS_ASSERT(laser_);
		laser_->subProp("Topic")->setValue(topic);
	}
	qDebug()<<"topic:"<<topic;
	laser_->setEnabled(enable);
	manager_->startUpdate();
}
//设置全局显示
void QRviz::SetGlobalOptions(QString frame_name,QColor backColor,int frame_rate)
{
	manager_->setFixedFrame(frame_name);
	manager_->setProperty("Background Color",backColor);
	manager_->setProperty("Frame Rate",frame_rate);
	manager_->startUpdate();
}
void QRviz::createDisplay(QString display_name,QString topic_name)
{


}
void QRviz::run()
{

}

 

三,完整开源项目

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

转载自:

蒋程扬的部落格

发表评论

后才能评论

评论列表(1条)

  • dtvee_6515 2020年4月21日 上午10:31

    你好,这个库有windows版本的吗?