QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
AirframeComponentController.cc
Go to the documentation of this file.
3#include <QtGui/QGuiApplication>
6#include "AppMessages.h"
7#include "LinkManager.h"
8#include "Fact.h"
9#include "Vehicle.h"
10#include "ParameterManager.h"
11
12#include <QtCore/QThread>
13#include <QtCore/QVariant>
14#include <QtGui/QCursor>
15
16bool AirframeComponentController::_typesRegistered = false;
17
19 _currentVehicleIndex(0),
20 _autostartId(0),
21 _showCustomConfigPanel(false)
22{
23 if (!_typesRegistered) {
24 _typesRegistered = true;
25 }
26
27 QStringList usedParams;
28 usedParams << "SYS_AUTOSTART" << "SYS_AUTOCONFIG";
30 return;
31 }
32
33 // Load up member variables
34
35 bool autostartFound = false;
36 _autostartId = getParameterFact(ParameterManager::defaultComponentId, "SYS_AUTOSTART")->rawValue().toInt();
37 _currentVehicleName = QString::number(_autostartId); // Temp val. Replaced with actual vehicle name if found
38
40 AirframeType* airframeType = new AirframeType(pType->name, pType->imageResource, this);
41 Q_CHECK_PTR(airframeType);
42
43 for (int index = 0; index < pType->rgAirframeInfo.count(); index++) {
44 const AirframeComponentAirframes::AirframeInfo_t* pInfo = pType->rgAirframeInfo.at(index);
45 Q_CHECK_PTR(pInfo);
46
47 if (_autostartId == pInfo->autostartId) {
48 if (autostartFound) {
49 qWarning() << "AirframeComponentController::AirframeComponentController duplicate ids found:" << _autostartId;
50 }
51 autostartFound = true;
52 _currentAirframeType = pType->name;
53 _currentVehicleName = pInfo->name;
54 _currentVehicleIndex = index;
55 }
56 airframeType->addAirframe(pInfo->name, pInfo->autostartId);
57 }
58
59 _airframeTypes.append(QVariant::fromValue(airframeType));
60 }
61
62 if (_autostartId != 0 && !autostartFound) {
63 _showCustomConfigPanel = true;
65 }
66}
67
72
74{
75 if (MultiVehicleManager::instance()->vehicles()->count() > 1) {
76 QGC::showAppMessage(tr("You cannot change airframe configuration while connected to multiple vehicles."));
77 return;
78 }
79
80 QGuiApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
81
82 Fact* sysAutoStartFact = getParameterFact(-1, "SYS_AUTOSTART");
83 Fact* sysAutoConfigFact = getParameterFact(-1, "SYS_AUTOCONFIG");
84
85 // We need to wait for the vehicleUpdated signals to come back before we reboot
86 _waitParamWriteSignalCount = 0;
87 connect(sysAutoStartFact, &Fact::vehicleUpdated, this, &AirframeComponentController::_waitParamWriteSignal);
88 connect(sysAutoConfigFact, &Fact::vehicleUpdated, this, &AirframeComponentController::_waitParamWriteSignal);
89
90 // We use forceSetValue to ensure params are sent even if the previous value is that same as the new value
91 sysAutoStartFact->forceSetRawValue(_autostartId);
92 sysAutoConfigFact->forceSetRawValue(1);
93}
94
95void AirframeComponentController::_waitParamWriteSignal(QVariant value)
96{
97 Q_UNUSED(value);
98
99 _waitParamWriteSignalCount++;
100 if (_waitParamWriteSignalCount == 2) {
101 // Now that both params have made it to the vehicle we can reboot it. All these signals are flying
102 // around on the main thread, so we need to allow the stack to unwind back to the event loop before
103 // we reboot.
104 QTimer::singleShot(800, this, &AirframeComponentController::_rebootAfterStackUnwind);
105 }
106}
107
108void AirframeComponentController::_rebootAfterStackUnwind(void)
109{
110 _vehicle->sendMavCommand(_vehicle->defaultComponentId(), MAV_CMD_PREFLIGHT_REBOOT_SHUTDOWN, true /* showError */, 1.0f);
111 QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
112 for (unsigned i = 0; i < 2000; i++) {
113 QThread::usleep(500);
114 QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
115 }
116 QGuiApplication::restoreOverrideCursor();
118}
119
120AirframeType::AirframeType(const QString& name, const QString& imageResource, QObject* parent) :
121 QObject(parent),
122 _name(name),
123 _imageResource(imageResource)
124{
125
126}
127
132
133void AirframeType::addAirframe(const QString& name, int autostartId)
134{
135 Airframe* airframe = new Airframe(name, autostartId);
136 Q_CHECK_PTR(airframe);
137
138 _airframes.append(QVariant::fromValue(airframe));
139}
140
141Airframe::Airframe(const QString& name, int autostartId, QObject* parent) :
142 QObject(parent),
143 _name(name),
144 _autostartId(autostartId)
145{
146
147}
148
150{
151
152}
static QList< AirframeType_t * > sortedTypes()
void showCustomConfigPanelChanged(bool show)
AirframeType(const QString &name, const QString &imageResource, QObject *parent=nullptr)
void addAirframe(const QString &name, int autostartId)
Airframe(const QString &name, int autostartId, QObject *parent=nullptr)
bool _allParametersExists(int componentId, const QStringList &names) const
Q_INVOKABLE Fact * getParameterFact(int componentId, const QString &name, bool reportMissing=true) const
A Fact is used to hold a single value within the system.
Definition Fact.h:17
void forceSetRawValue(const QVariant &value)
Sets and sends new value to vehicle even if value is the same.
Definition Fact.cc:111
void vehicleUpdated(const QVariant &value)
Signalled when the param write ack comes back from the vehicle.
QVariant rawValue() const
Value after translation.
Definition Fact.h:85
static LinkManager * instance()
void disconnectAll()
static MultiVehicleManager * instance()
static constexpr int defaultComponentId
void sendMavCommand(int compId, MAV_CMD command, bool showError, float param1=0.0f, float param2=0.0f, float param3=0.0f, float param4=0.0f, float param5=0.0f, float param6=0.0f, float param7=0.0f)
Definition Vehicle.cc:2144
int defaultComponentId() const
Definition Vehicle.h:682
void showAppMessage(const QString &message, const QString &title)
Modal application message. Queued if the UI isn't ready yet.
Definition AppMessages.cc:9