ROS-Qt GUI - 如何分发线程

ROS-Qt GUI - How to distribute the Threads?

本文关键字:线程 GUI ROS-Qt      更新时间:2023-10-16

我正在开发一个C++Qt GUI来远程控制ROS机器人。我已经读过 ros::spin() 命令应该在单独的线程中发出,所以我基本上有通常的 MainWindow 派生自QMainWindow,其构造函数设置 GUI 元素,使订阅者对象订阅它们各自的主题(例如 image_transport::Subscriber sensor_msgs/Image主题),并启动另一个线程。为此,我从QThread派生了一个"RosThread"类,该类除了在调用RosThread::run()时启动ros:MultiThreadedSpinner外什么都不做。

正如你可能知道的那样,我在编程方面并不完全有经验,所以我的问题是,我的项目背后的基本概念对你来说是否有意义?特别是我是否应该将 NodeHandle 和订阅者对象保留在 MainWindow 中,并从 MainWindow 构造函数设置订阅?

相关代码片段:

主窗口.cpp:

#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), itLeft(nh), itArm(nh)
{
    //subscribe to cameras
    imageSubLeft = itLeft.subscribe("/camera_1/image_raw", 1000, &MainWindow::camCallbackLeft, this);
    imageSubArm = itArm.subscribe("/camera_2/image_raw", 1000, &MainWindow::camCallbackArm, this);
    pagestack = new QStackedWidget;
    page1 = new QWidget;
    grid = new QGridLayout;
    page1->setLayout(grid);
    pagestack->addWidget(page1);
    labelLeft = new QLabel;
    labelMid = new QLabel;
    grid->addWidget(labelLeft, 0, 0);
    grid->addWidget(labelMid, 0, 1);
    this->startSpinThread(); //starts the seperate Thread where run() is executed
    this->setCentralWidget(pagestack);
    this->setWindowState(Qt::WindowMaximized);
    this->setMinimumSize(1024, 768);
}    
MainWindow::~MainWindow(){}    
void MainWindow::camCallbackLeft(const sensor_msgs::Image::ConstPtr &msg){/*some code*/}
void MainWindow::camCallbackArm(const sensor_msgs::Image::ConstPtr &msg){/*some code*/}
void MainWindow::closeEvent(QCloseEvent *event){/*some code*/}  
void MainWindow::startSpinThread()
{
    if(rosSpin.isRunning())
    {
        return;
    }
    //rosSpin is an Object of the of QThread derived class
    rosSpin.start();
}

rosthread.h:

#ifndef ROSTHREAD_H
#define ROSTHREAD_H
#include <ros/ros.h>
#include <QThread>

class RosThread : public QThread
{
    Q_OBJECT
public:
    RosThread();
protected:
    void run();
private:
    ros::MultiThreadedSpinner spinner;
};
#endif // ROSTHREAD_H

Rosthread.cpp:

#include "rosthread.h"
RosThread::RosThread()
{
}
void RosThread::run() {
    spinner.spin();
}

主.cpp:

#include "mainwindow.h"
#include <QApplication>
#include <ros/ros.h>
int main(int argc, char **argv)
{
  ros::init(argc, argv, "gui_node");
  QApplication app (argc, argv);
  MainWindow *win = new MainWindow();
  win->show();
  return app.exec();
}

实际上,QThread 并不打算以这种方式使用。看看这篇博客文章和这个例子。

但无论如何,我会建议使用标准C++线程。将std::unique_ptr<std::thread> thread;添加到 MainWindow 类而不是 RosThread 对象。要启动线程,请使用 thread.reset(new std::thread([](){ static ros::MultiThreadedSpinner spinner; spinner.spin(); }); 。智能指针std::unique_ptr将自动删除线程对象,尽管您不应忘记在重置/销毁std::unique_ptr对象之前使用std::thread::join()std::thread::detach()。另一种解决方案是将ros::MultiThreadedSpinner对象放入 MainWindow 类中,并使用 thread.reset(new std::thread(&ros::MultiThreadedSpinner::spin, spinner)); 创建一个std::thread

在我看来,你应该把你的NodeHandle和Subscriber对象放到另一个类中,如果它们不直接属于MainWindow,则使用这个类的对象作为MainWindow的成员。