使⽤QT搭建ROS程序界⾯(C++)
需要建⽴ROS⼯作空间,这样可以使⽤CMAKE进⾏编译并⾃动⽣成⽂件。
$mkdir -p ~/catkin_ws/src  $cd ~/catkin_ws/src  $catkin_init_workspace  $cd ~/catkin_ws  $catkin_make
$cd /src  $catkin_create_qt_pkg robomap
2.⽬标是为机器⼈开发⼀个SLAM建图界⾯和⾃主导航显⽰界⾯。参考创客智造教程中的“ROS与QT语⾔⼊门教程-显⽰ROS发布的地图消息”那⼀篇,还需要⾃⼰定义⼀个速度调节滑动条。
  2.1 在mapp.ui界⾯加⼊Slider,类名改为ForBackSlider;修改下述代码。最后在qnode.cpp中调⽤Slider的值,使得节点发布的geometry_msgs::Twist消息的值等于ForBackSlider。
mapp.cpp:
slider->setMinimum(0);
slider->setMaximum(100);
ui->ForBackSlider->setMinimum(0);
QObject::connect(ui->ForBackSlider,SIGNAL(valueChanged(int)),this,SLOT(set_speed_value(int));
void MAPP::set_speed_value(int slider_value)
{
ForBackSpeed=slider_value;
}
mapp.h:
private slots:
void set_speed_value(int s);
private:
float ForBackSpeed=0.0;
<:(为了⽀持C++11特性)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  -std=c++11)
  2.2 按钮启动ROS的节点,例如:$roscore 、$roslaunch usb_cam usb_cam_node 等,即不⽤新开终端并输⼊⼀⼤串命令。
main_window.ui:
插⼊button,改名为roscore。
main_window.h:
private slots:
void on_roscore_clicked();
main_window.cpp:
void  robomap::MainWindow::on_roscore_clicked()
{
system("gnome-terminal -x bash -c 'roscore' &");
}
如果想⼀个按钮启动多个命令,得分别写对应个数的system(),因为是新开终端。
  2.3 订阅摄像头。
    ⽅式⼀(取巧,使⽤按钮调⽤命令):
$roslaunch usb_cam usb_cam_test.launch
参见github/ros-visualization/rqt_image_view
main_window.cpp:
void robomap::Maindow::on_camera_clicked()
{
  system("gnome -terminal -x bash -c 'source ~/catkin_ws/devel/setup.bash; rosrun usb_cam usb_cam_node'&");
  system("gnome -terminal -x bash -c 'rqt_image_view'&");
}
直接参照原⽂改得话编译并不能通过,报错:QCamera:No such file or directory。确定问题是不到外部库和头⽂件的问题。
在:
include_directories(/usr/include/x86_64_linux-gnu/qt5/QtMultimedia)
添加后错误排除,但在link时有错误:undefined reference to 'QCamera:: '
再添加:target_link_libraries(... Qt5::Multimedia),只剩下QCameraViewfinder没到外部库。
再跑去学习的资料,还是⽆法解决,倒是到另⼀种⽅式specify the c++ standard: set(CMAKE_CXX_STANDARD 11)。
转⽽先实现QT界⾯中调⽤摄像头,先实现功能,不在ROS空间中完成:
step1:新建Qt widgets application ⼯程
step2:wwwblogs/annt/p/ant_000.html
cmake如何使用step3:运⾏时报错,安装必要的库。完成功能
cam.pro:
QT      += core gui multimedia multimediawidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = cam
TEMPLATE = app
SOURCES += main.cpp\
SOURCES += main.cpp\
mainwindow.cpp
HEADERS  += mainwindow.h
FORMS    += mainwindow.ui
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QFileDialog>
namespace Ui {
class MainWindow;
}
class QCamera;
class QCameraViewfinder;
class QCameraImageCapture;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void captureImage();
void displayImage(int,QImage);
void saveImage();
private:
Ui::MainWindow *ui;
QCamera *camera;
QCameraViewfinder *viewfinder;
QCameraImageCapture *imageCapture;
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
camera=new QCamera(this);
viewfinder=new QCameraViewfinder(this);
imageCapture=new QCameraImageCapture(camera);
ui->ImageView->addWidget(viewfinder);
ui->ImageCapture->setScaledContents(true);
camera->setViewfinder(viewfinder);
camera->start();
connect(imageCapture, SIGNAL(imageCaptured(int,QImage)), this, SLOT(displayImage(int,QImage))); connect(ui->buttonCapture, SIGNAL(clicked()), this, SLOT(captureImage()));
connect(ui->buttonSave, SIGNAL(clicked()), this, SLOT(saveImage()));
connect(ui->buttonQuit, SIGNAL(clicked()), qApp, SLOT(quit()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::captureImage()
{
ui->statusBar->showMessage(tr(""), 1000);
ui->statusBar->showMessage(tr(""), 1000);
imageCapture->capture();
}
void MainWindow::displayImage(int , QImage image)
{
ui->ImageCapture->setPixmap(QPixmap::fromImage(image));
ui->statusBar->showMessage(tr("capture OK!"), 5000);
}
void MainWindow::saveImage()
{
QString fileName=QFileDialog::getSaveFileName(this, tr("save file"), QDir::homePath(), tr("jpegfile(*.jpg)"));
if(fileName.isEmpty()) {
ui->statusBar->showMessage(tr("save cancel"), 5000);
return;
}
const QPixmap* pixmap=ui->ImageCapture->pixmap();
if(pixmap) {
pixmap->save(fileName);
ui->statusBar->showMessage(tr("save OK"), 5000);
}
}
功能可以实现。调⽤摄像头,截图,保存等。
接着在ROS空间中进⾏代码修改。(搜到⼀篇博客,是⽤CMAKE编译⼯程的,需要在CMakeLists⽂件添加qt库)(Ubuntu下Qt Multimedia 播放mp3⽂件注意事项):
find_package(Qt5 REQUIRED Core Widgets SerialPort Multimedia)
set(QT_LIBRARIES Qt5::Widgets Qt5::SerialPort Qt5::Multimedia)
参照博客中的写法,加⼊MultimediaWidgets模块,编译成功
3.开启turtlebot仿真,验证程序。
sudo apt-get install ros-kinetic-turtlebot ros-kinetic-turtlebot-apps ro-kinetic-turtlebot-interactions ros-kinetic-turtlebot-sumulatio ros-kinetic-kobuki-ftdi
source /opt/ros/kinetic/setup.bash
roslaunch turtlebot_gazebo turtlebot_world.launch 需要等⼏分钟加载
roslaunch turtlebot_gazebo gmapping_demo.launch
roslaunch turtlebot_teleop keyboard_teleop.launch

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。