环境
- 系统:Ubuntu 18.04
- QT版本:QT 5.12.4
- QT Creator-ROS版本:4.9.2 该版本嵌入了对ROS的支持
前言
主要参考了 librviz_tutorial 的代码,但是在我的环境下运行存在问题,故做了一些修改并添加 PointCloud2
类型的点云话题消息的订阅显示,原项目是基于 QT4 ,这里我将其修改为 QT5 。
最终实现效果如下(左边是 QT 窗口,右边是与 Rviz 对比):
项目结构
仅关注 myviz.h
、myviz.cpp
和 main.cpp
这几个文件即可,其他文件与此实验无关。
myviz.h
创建成员变量 cloud
用于点云显示。
#ifndef MYVIZ_H
#define MYVIZ_H
#include <QWidget>
namespace rviz
{
class Display;
class RenderPanel;
class VisualizationManager;
}
// BEGIN_TUTORIAL
// Class "MyViz" implements the top level widget for this example.
class MyViz: public QWidget
{
Q_OBJECT
public:
MyViz( QWidget* parent = 0 );
virtual ~MyViz();
private Q_SLOTS:
void setThickness( int thickness_percent );
void setCellSize( int cell_size_percent );
private:
rviz::VisualizationManager* manager_;
rviz::RenderPanel* render_panel_;
rviz::Display* grid_;
rviz::Display* cloud;
};
// END_TUTORIAL
#endif // MYVIZ_H
myviz.cpp
- 添加固定坐标系为
livox_frame
,否则你的点云可能无法显示; - 添加点云的显示参数,参考 Rviz 中的参数进行填写即可。
#include <QSlider>
#include <QLabel>
#include <QGridLayout>
#include <QVBoxLayout>
#include "rviz/visualization_manager.h"
#include "rviz/render_panel.h"
#include "rviz/display.h"
#include "../include/qtgui/myviz.h"
// BEGIN_TUTORIAL
// Constructor for MyViz. This does most of the work of the class.
MyViz::MyViz( QWidget* parent )
: QWidget( parent )
{
// Construct and lay out labels and slider controls.
QLabel* thickness_label = new QLabel( "Line Thickness" );
QSlider* thickness_slider = new QSlider( Qt::Horizontal );
thickness_slider->setMinimum( 1 );
thickness_slider->setMaximum( 100 );
QLabel* cell_size_label = new QLabel( "Cell Size" );
QSlider* cell_size_slider = new QSlider( Qt::Horizontal );
cell_size_slider->setMinimum( 1 );
cell_size_slider->setMaximum( 100 );
QGridLayout* controls_layout = new QGridLayout();
controls_layout->addWidget( thickness_label, 0, 0 );
controls_layout->addWidget( thickness_slider, 0, 1 );
controls_layout->addWidget( cell_size_label, 1, 0 );
controls_layout->addWidget( cell_size_slider, 1, 1 );
// Construct and lay out render panel.
render_panel_ = new rviz::RenderPanel();
QVBoxLayout* main_layout = new QVBoxLayout;
main_layout->addLayout( controls_layout );
main_layout->addWidget( render_panel_ );
// Set the top-level layout for this MyViz widget.
setLayout( main_layout );
// Make signal/slot connections.
connect( thickness_slider, SIGNAL( valueChanged( int )), this, SLOT( setThickness( int )));
connect( cell_size_slider, SIGNAL( valueChanged( int )), this, SLOT( setCellSize( int )));
// Next we initialize the main RViz classes.
//
// The VisualizationManager is the container for Display objects,
// holds the main Ogre scene, holds the ViewController, etc. It is
// very central and we will probably need one in every usage of
// librviz.
manager_ = new rviz::VisualizationManager( render_panel_ );
render_panel_->initialize( manager_->getSceneManager(), manager_ );
manager_->setFixedFrame("livox_frame");
manager_->initialize();
manager_->startUpdate();
// Create a Grid display.
grid_ = manager_->createDisplay( "rviz/Grid", "adjustable grid", true );
ROS_ASSERT( grid_ != NULL );
cloud = manager_->createDisplay( "rviz/PointCloud2", "point cloud", true );
ROS_ASSERT( cloud != NULL );
cloud->subProp("Topic")->setValue("/livox/lidar");
cloud->subProp("Style")->setValue("Points");
cloud->subProp("Size (Pixels)")->setValue("2");
cloud->subProp("Color Transformer")->setValue("Intensity");
cloud->subProp("Invert Rainbow")->setValue("true");
cloud->subProp("Decay Time")->setValue("1");
// Configure the GridDisplay the way we like it.
grid_->subProp( "Line Style" )->setValue( "Billboards" );
grid_->subProp( "Color" )->setValue( QVariant::fromValue(Qt::yellow) );
// Initialize the slider values.
thickness_slider->setValue( 25 );
cell_size_slider->setValue( 10 );
}
// Destructor.
MyViz::~MyViz()
{
delete manager_;
}
// This function is a Qt slot connected to a QSlider's valueChanged()
// signal. It sets the line thickness of the grid by changing the
// grid's "Line Width" property.
void MyViz::setThickness( int thickness_percent )
{
if( grid_ != NULL )
{
grid_->subProp( "Line Style" )->subProp( "Line Width" )->setValue( thickness_percent / 100.0f );
}
}
// This function is a Qt slot connected to a QSlider's valueChanged()
// signal. It sets the cell size of the grid by changing the grid's
// "Cell Size" Property.
void MyViz::setCellSize( int cell_size_percent )
{
if( grid_ != NULL )
{
grid_->subProp( "Cell Size" )->setValue( cell_size_percent / 10.0f );
}
}
main.cpp
注意添加头文件。
/**
* @file /src/main.cpp
*
* @brief Qt based gui.
*
* @date November 2010
**/
/*****************************************************************************
** Includes
*****************************************************************************/
#include <QtGui>
#include <QApplication>
#include <ros/ros.h>
#include "../include/qtgui/myviz.h"
int main(int argc, char **argv)
{
if( !ros::isInitialized() )
{
ros::init( argc, argv, "myviz", ros::init_options::AnonymousName );
}
QApplication app( argc, argv );
MyViz* myviz = new MyViz();
myviz->show();
app.exec();
delete myviz;
}
CMakeLists.txt
- 项目名称要改为自己的,这里我的是
qtgui
; - 从官方示例中的 QT4 改为了 QT5;
- 根据自己项目结构添加头文件路径。
cmake_minimum_required(VERSION 2.8.3)
project(qtgui)
find_package(catkin REQUIRED COMPONENTS rviz roscpp)
catkin_package()
include_directories(${catkin_INCLUDE_DIRS})
link_directories(${catkin_LIBRARY_DIRS})
## This plugin includes Qt widgets, so we must include Qt like so:
find_package(Qt5 REQUIRED Core Widgets)
set(QT_LIBRARIES Qt5::Widgets)
#include(${QT_USE_FILE})
## I prefer the Qt signals and slots to avoid defining "emit", "slots",
## etc because they can conflict with boost signals, so define QT_NO_KEYWORDS here.
add_definitions(-DQT_NO_KEYWORDS)
## Here we specify which header files need to be run through "moc",
## Qt's meta-object compiler.
qt5_wrap_cpp(MOC_FILES
include/qtgui/myviz.h
)
## Here we specify the list of source files, including the output of
## the previous command which is stored in ``${MOC_FILES}``.
set(SOURCE_FILES
src/myviz.cpp
src/main.cpp
${MOC_FILES}
)
## Add the "myviz" executable and specify the list of source files we
## collected above in ``${SOURCE_FILES}``.
add_executable(myviz ${SOURCE_FILES})
## Link the myviz executable with whatever Qt libraries have been defined by
## the ``find_package(Qt4 ...)`` line above, and with whatever libraries
## catkin has included.
target_link_libraries(myviz ${QT_LIBRARIES} ${catkin_LIBRARIES})
## END_TUTORIAL
## Install
install(TARGETS myviz DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})
package.xml
- 项目名称改为自己的,这里我的是
qtgui
; - 在这里添加对
rviz
的依赖。
<package>
<name>qtgui</name>
<version>0.8.4</version>
<description>
Tutorial showing how to compile your own C++ program with RViz displays and features.
</description>
<license>BSD</license>
<url>http://ros.org/wiki/librviz_tutorial</url>
<author>Dave Hershberger</author>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>rviz</build_depend>
<run_depend>roscpp</run_depend>
<run_depend>rviz</run_depend>
<export>
<rosdoc config="${prefix}/rosdoc.yaml"/>
</export>
</package>
评论(0)
您还未登录,请登录后发表或查看评论