标签:file button dial undefined add inux display pkg tar
1. 搭建QT开发环境,参照创客智造教程https://www.ncnynl.com/archives/201903/2863.html。
需要建立ROS工作空间,这样可以使用CMAKE进行编译并自动生成CMakeLists.txt文件。
$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; CMakeLists.txt:(为了支持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
参见https://github.com/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‘&");
}
方式二(不使用按钮):参考linux公社的Qt5下实现摄像头预览及捕获图像方法实例,https://www.linuxidc.com/Linux/2016-03/128792.html
直接参照原文改得话编译并不能通过,报错:QCamera:No such file or directory。确定问题是找不到外部库和头文件的问题。
在CMakeLists.txt:
include_directories(/usr/include/x86_64_linux-gnu/qt5/QtMultimedia)
添加后错误排除,但在link时有错误:undefined reference to ‘QCamera:: ‘
再添加:target_link_libraries(... Qt5::Multimedia),只剩下QCameraViewfinder没找到外部库。
再跑去学习CMakeLists.txt的资料,还是无法解决,倒是找到另一种方式specify the c++ standard: set(CMAKE_CXX_STANDARD 11)。
转而先实现QT界面中调用摄像头,先实现功能,不在ROS空间中完成:
step1:新建Qt widgets application 工程
step2:https://www.cnblogs.com/annt/p/ant_000.html
step3:运行时报错,安装必要的库。完成功能
cam.pro:
QT += core gui multimedia multimediawidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = cam
TEMPLATE = app
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("capturing..."), 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文件注意事项)
CMakeLists.txt:
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
标签:file button dial undefined add inux display pkg tar
原文地址:https://www.cnblogs.com/gangyin/p/12146667.html