From ebdd072e57774ad4c1d0198c0c4794dede979cd6 Mon Sep 17 00:00:00 2001
From: Lorenz Meier <lm@inf.ethz.ch>
Date: Sat, 24 May 2014 16:11:30 +0200
Subject: [PATCH] Fix up threading of UAS object, WIP

---
 qgroundcontrol.pro              |  6 ++++--
 src/uas/QGCMAVLinkUASFactory.cc |  3 ++-
 src/uas/QGCUASWorker.cc         | 12 ++++++++++++
 src/uas/QGCUASWorker.h          | 15 +++++++++++++++
 src/uas/UAS.cc                  | 12 ++++++------
 5 files changed, 39 insertions(+), 9 deletions(-)
 create mode 100644 src/uas/QGCUASWorker.cc
 create mode 100644 src/uas/QGCUASWorker.h

diff --git a/qgroundcontrol.pro b/qgroundcontrol.pro
index 135006d..f52faf0 100644
--- a/qgroundcontrol.pro
+++ b/qgroundcontrol.pro
@@ -562,7 +562,8 @@ HEADERS += \
     src/ui/designer/QGCXYPlot.h \
     src/ui/menuactionhelper.h \
     src/uas/UASManagerInterface.h \
-    src/uas/QGCUASParamManagerInterface.h
+    src/uas/QGCUASParamManagerInterface.h \
+    src/uas/QGCUASWorker.h
 
 SOURCES += \
     src/main.cc \
@@ -745,4 +746,5 @@ SOURCES += \
     src/ui/px4_configuration/QGCPX4MulticopterConfig.cc \
     src/ui/px4_configuration/QGCPX4SensorCalibration.cc \
     src/ui/designer/QGCXYPlot.cc \
-    src/ui/menuactionhelper.cpp
+    src/ui/menuactionhelper.cpp \
+    src/uas/QGCUASWorker.cc
diff --git a/src/uas/QGCMAVLinkUASFactory.cc b/src/uas/QGCMAVLinkUASFactory.cc
index b7658ae..c55648c 100644
--- a/src/uas/QGCMAVLinkUASFactory.cc
+++ b/src/uas/QGCMAVLinkUASFactory.cc
@@ -1,5 +1,6 @@
 #include "QGCMAVLinkUASFactory.h"
 #include "UASManager.h"
+#include "QGCUASWorker.h"
 
 QGCMAVLinkUASFactory::QGCMAVLinkUASFactory(QObject *parent) :
     QObject(parent)
@@ -21,7 +22,7 @@ UASInterface* QGCMAVLinkUASFactory::createUAS(MAVLinkProtocol* mavlink, LinkInte
 
     UASInterface* uas;
 
-    QThread* worker = new QThread();
+    QGCUASWorker* worker = new QGCUASWorker();
 
     switch (heartbeat->autopilot)
     {
diff --git a/src/uas/QGCUASWorker.cc b/src/uas/QGCUASWorker.cc
new file mode 100644
index 0000000..6fb74b1
--- /dev/null
+++ b/src/uas/QGCUASWorker.cc
@@ -0,0 +1,12 @@
+#include "QGCUASWorker.h"
+
+#include <QGC.h>
+
+QGCUASWorker::QGCUASWorker() : QThread()
+{
+}
+
+void QGCUASWorker::run()
+{
+    QGC::SLEEP::msleep(100);
+}
diff --git a/src/uas/QGCUASWorker.h b/src/uas/QGCUASWorker.h
new file mode 100644
index 0000000..a486728
--- /dev/null
+++ b/src/uas/QGCUASWorker.h
@@ -0,0 +1,15 @@
+#ifndef QGCUASWORKER_H
+#define QGCUASWORKER_H
+
+#include <QThread>
+
+class QGCUASWorker : public QThread
+{
+public:
+    QGCUASWorker();
+
+protected:
+    void run();
+};
+
+#endif // QGCUASWORKER_H
diff --git a/src/uas/UAS.cc b/src/uas/UAS.cc
index 0ff565d..fbc4bb3 100644
--- a/src/uas/UAS.cc
+++ b/src/uas/UAS.cc
@@ -51,7 +51,7 @@ UAS::UAS(MAVLinkProtocol* protocol, QThread* thread, int id) : UASInterface(),
     commStatus(COMM_DISCONNECTED),
     receiveDropRate(0),
     sendDropRate(0),
-    statusTimeout(new QTimer(this)),
+    statusTimeout(new QTimer()),
 
     name(""),
     type(MAV_TYPE_GENERIC),
@@ -138,7 +138,7 @@ UAS::UAS(MAVLinkProtocol* protocol, QThread* thread, int id) : UASInterface(),
 
     airSpeed(std::numeric_limits<double>::quiet_NaN()),
     groundSpeed(std::numeric_limits<double>::quiet_NaN()),
-    waypointManager(this),
+    waypointManager(),
 
     attitudeKnown(false),
     attitudeStamped(false),
@@ -153,7 +153,7 @@ UAS::UAS(MAVLinkProtocol* protocol, QThread* thread, int id) : UASInterface(),
 
 
     paramsOnceRequested(false),
-    paramMgr(this),
+    paramMgr(),
     simulation(0),
 
     // The protected members.
@@ -180,17 +180,17 @@ UAS::UAS(MAVLinkProtocol* protocol, QThread* thread, int id) : UASInterface(),
     // Store a list of available actions for this UAS.
     // Basically everything exposed as a SLOT with no return value or arguments.
 
-    QAction* newAction = new QAction(tr("Arm"), this);
+    QAction* newAction = new QAction(tr("Arm"), thread);
     newAction->setToolTip(tr("Enable the UAS so that all actuators are online"));
     connect(newAction, SIGNAL(triggered()), this, SLOT(armSystem()));
     actions.append(newAction);
 
-    newAction = new QAction(tr("Disarm"), this);
+    newAction = new QAction(tr("Disarm"), thread);
     newAction->setToolTip(tr("Disable the UAS so that all actuators are offline"));
     connect(newAction, SIGNAL(triggered()), this, SLOT(disarmSystem()));
     actions.append(newAction);
 
-    newAction = new QAction(tr("Toggle armed"), this);
+    newAction = new QAction(tr("Toggle armed"), thread);
     newAction->setToolTip(tr("Toggle between armed and disarmed"));
     connect(newAction, SIGNAL(triggered()), this, SLOT(toggleAutonomy()));
     actions.append(newAction);