2 ******************************************************************************
4 * @file telemetrymanager.cpp
5 * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016.
6 * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
7 * @addtogroup GCSPlugins GCS Plugins
9 * @addtogroup UAVTalkPlugin UAVTalk Plugin
11 * @brief The UAVTalk protocol plugin
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "telemetrymanager.h"
30 #include "telemetry.h"
31 #include "telemetrymonitor.h"
32 #include <extensionsystem/pluginmanager.h>
33 #include <coreplugin/icore.h>
34 #include <coreplugin/threadmanager.h>
36 TelemetryManager::TelemetryManager() : QObject(), m_connectionState(TELEMETRY_DISCONNECTED
)
38 moveToThread(Core::ICore::instance()->threadManager()->getRealTimeThread());
40 // Get UAVObjectManager instance
41 ExtensionSystem::PluginManager
*pm
= ExtensionSystem::PluginManager::instance();
42 m_uavobjectManager
= pm
->getObject
<UAVObjectManager
>();
44 // connect to start stop signals
45 connect(this, SIGNAL(myStart()), this, SLOT(onStart()), Qt::QueuedConnection
);
46 connect(this, SIGNAL(myStop()), this, SLOT(onStop()), Qt::QueuedConnection
);
49 TelemetryManager::~TelemetryManager()
52 bool TelemetryManager::isConnected() const
54 return m_connectionState
== TELEMETRY_CONNECTED
;
57 TelemetryManager::ConnectionState
TelemetryManager::connectionState() const
59 return m_connectionState
;
62 void TelemetryManager::start(QIODevice
*dev
)
64 m_connectionState
= TELEMETRY_CONNECTING
;
67 m_telemetryDevice
= dev
;
69 // take ownership of the device by moving it to the TelemetryManager thread (see TelemetryManager constructor)
70 // this removes the following runtime Qt warning and incidentally fixes GCS crashes:
71 // QObject: Cannot create children for a parent that is in a different thread.
72 // (Parent is QSerialPort(0x56af73f8), parent's thread is QThread(0x23f69ae8), current thread is QThread(0x2649cfd8)
73 m_telemetryDevice
->moveToThread(thread());
77 void TelemetryManager::onStart()
79 m_uavTalk
= new UAVTalk(m_telemetryDevice
, m_uavobjectManager
);
81 // UAVTalk must be thread safe and for that:
82 // 1- all public methods must lock a mutex
83 // 2- the reader thread must lock that mutex too
84 // The reader thread locks the mutex once a packet is read and decoded.
85 // It is assumed that the UAVObjectManager is thread safe
87 // Create the reader and move it to the reader thread
88 IODeviceReader
*reader
= new IODeviceReader(m_uavTalk
);
89 reader
->moveToThread(&m_telemetryReaderThread
);
90 // The reader will be deleted (later) when the thread finishes
91 connect(&m_telemetryReaderThread
, &QThread::finished
, reader
, &QObject::deleteLater
);
92 // Connect IO device to reader
93 connect(m_telemetryDevice
, SIGNAL(readyRead()), reader
, SLOT(read()));
94 // start the reader thread
95 m_telemetryReaderThread
.start();
97 // Connect IO device to reader
98 connect(m_telemetryDevice
, SIGNAL(readyRead()), m_uavTalk
, SLOT(processInputStream()));
101 m_telemetry
= new Telemetry(m_uavTalk
, m_uavobjectManager
);
102 m_telemetryMonitor
= new TelemetryMonitor(m_uavobjectManager
, m_telemetry
);
104 connect(m_telemetryMonitor
, SIGNAL(connected()), this, SLOT(onConnect()));
105 connect(m_telemetryMonitor
, SIGNAL(disconnected()), this, SLOT(onDisconnect()));
106 connect(m_telemetryMonitor
, SIGNAL(telemetryUpdated(double, double)), this, SLOT(onTelemetryUpdate(double, double)));
109 void TelemetryManager::stop()
111 m_connectionState
= TELEMETRY_DISCONNECTING
;
112 emit
disconnecting();
116 m_telemetryReaderThread
.quit();
117 m_telemetryReaderThread
.wait();
121 void TelemetryManager::onStop()
123 m_telemetryMonitor
->disconnect(this);
124 delete m_telemetryMonitor
;
130 void TelemetryManager::onConnect()
132 m_connectionState
= TELEMETRY_CONNECTED
;
136 void TelemetryManager::onDisconnect()
138 m_connectionState
= TELEMETRY_DISCONNECTED
;
142 void TelemetryManager::onTelemetryUpdate(double txRate
, double rxRate
)
144 emit
telemetryUpdated(txRate
, rxRate
);
147 IODeviceReader::IODeviceReader(UAVTalk
*uavTalk
) : m_uavTalk(uavTalk
)
150 void IODeviceReader::read()
152 m_uavTalk
->processInputStream();