Rviz教程(十):Librviz: Incorporating RViz into a Custom GUI

时间:2024-04-13 18:49:41

Librviz Tutorial

Overview

RViz is not just a visualizer(视觉型的人) application, it is also a library! Much of RViz’s functionality(功能) can be accessed within your own application by linking against librviz.so (or whatever your OS likes to call it).

This tutorial shows a very simple example of creating a 3D visualizer widget(装饰物) (rviz::RenderPanel), programmatically creating a new Grid display within it, then using Qt slider controls to adjust(调整) a couple of the grid’s properties. The app is called “myviz”.

The source code for this tutorial is in the librviz_tutorial package. You can check out the source directly or (if you use Ubuntu) you can just apt-get install(安装) the pre-compiled Debian package like so:

sudo apt-get install ros-hydro-visualization-tutorials

The running application looks like this:

Rviz教程(十):Librviz: Incorporating RViz into a Custom GUI

The Code

The code for myviz is in these files: src/main.cppsrc/myviz.h, and src/myviz.cpp.

main.cpp

The full text of main.cpp is here: src/main.cpp

The main() for this “myviz” example is very simple, it just initializes(初始化) ROS, creates a QApplication, creates the top-level widget(装饰物) (of type “MyViz”), shows it, and runs the Qt event loop(环).

#include <QApplication>
#include <ros/ros.h>
#include "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;
}

myviz.h

The full text of myviz.h is here: src/myviz.h

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_;
};

myviz.cpp

The full text of myviz.cpp is here: src/myviz.cpp

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_->initialize();
manager_->startUpdate();

Create a Grid 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( 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 );
  }
}

Building

The full text of CMakeLists.txt is here: CMakeLists.txt

Running

Just type:

rosrun librviz_tutorial myviz