QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
VehicleReadiness.cc
Go to the documentation of this file.
1#include "VehicleReadiness.h"
2
3#include "AppSettings.h"
4#include "AutoPilotPlugin.h"
5#include "Fact.h"
6#include "FirmwarePlugin.h"
9#include "SettingsManager.h"
10#include "Vehicle.h"
11
12QGC_LOGGING_CATEGORY(VehicleReadinessLog, "Vehicle.VehicleReadiness")
13
15 : QObject(vehicle)
16 , _vehicle(vehicle)
17{
20 connect(_vehicle, &Vehicle::initialConnectComplete, this, [this]() {
21 _healthReportWaitTimer.start();
22 update();
23 });
27
28 HealthAndArmingCheckReport* const report = _vehicle->healthAndArmingCheckReport();
29 if (report) {
31 }
32
33 AutoPilotPlugin* const autopilotPlugin = _vehicle->autopilotPlugin();
34 if (autopilotPlugin) {
35 connect(autopilotPlugin, &AutoPilotPlugin::setupCompleteChanged, this, &VehicleReadiness::update);
36 }
37
38 AppSettings* const appSettings = SettingsManager::instance()->appSettings();
39 connect(appSettings->useChecklist(), &Fact::rawValueChanged, this, &VehicleReadiness::update);
40 connect(appSettings->enforceChecklist(), &Fact::rawValueChanged, this, &VehicleReadiness::update);
41
42 _healthReportWaitTimer.setSingleShot(true);
43 _healthReportWaitTimer.setInterval(_healthReportWaitMs);
44 connect(&_healthReportWaitTimer, &QTimer::timeout, this, &VehicleReadiness::_healthReportWaitTimeout);
45}
46
47void VehicleReadiness::_healthReportWaitTimeout()
48{
49 _healthReportWaitExpired = true;
50
51 const Inputs inputs = _gatherInputs();
52 if (inputs.healthAndArmingChecksSupported && !inputs.healthAndArmingChecksReported) {
53 qCWarning(VehicleReadinessLog) << "No health and arming check report received, falling back to other sources";
54 }
55 update();
56}
57
58VehicleReadiness::Result VehicleReadiness::_evaluate(const Inputs& inputs)
59{
60 Result result;
61
62 if (inputs.healthAndArmingChecksReported) {
63 const auto modeGroupSet = [](quint64 modeGroups, int modeGroup) {
64 if ((modeGroup < 0) || (modeGroup >= HealthAndArmingCheckReport::MaxModeGroups)) {
65 return false;
66 }
67 return (modeGroups & (1ull << modeGroup)) != 0;
68 };
69 const auto actionReady = [&inputs, &modeGroupSet](const ActionModeGroups& modeGroups) {
70 const int armModeGroup = (modeGroups.armModeGroup < 0) ? inputs.currentModeGroup : modeGroups.armModeGroup;
71 const int targetModeGroup =
72 (modeGroups.targetModeGroup < 0) ? inputs.currentModeGroup : modeGroups.targetModeGroup;
73
74 // An armed vehicle only switches into the target mode
75 if (inputs.armed) {
76 return modeGroupSet(inputs.canRunModeGroups, targetModeGroup);
77 }
78
79 // An action which switches mode after arming also needs to be able to run in the target mode. libevents
80 // only promises can_run is meaningful while armed, so this part is best effort.
81 const bool canArmModeGroup = modeGroupSet(inputs.canArmModeGroups, armModeGroup);
82 if (armModeGroup == targetModeGroup) {
83 return canArmModeGroup;
84 }
85 return canArmModeGroup && modeGroupSet(inputs.canRunModeGroups, targetModeGroup);
86 };
87
88 result.canArm = actionReady(ActionModeGroups{});
89 result.canTakeoff = actionReady(inputs.takeoff);
90 result.canStartMission = actionReady(inputs.startMission);
91 if (inputs.currentModeGroup < 0) {
92 // The current mode is not in the vehicle's metadata, so its readiness is unknown
93 result.status = Status::Unknown;
94 } else {
95 result.status = result.canArm ? Status::Ready : Status::NotReady;
96 }
97 } else if (!inputs.healthReportWaitExpired && inputs.healthAndArmingChecksSupported) {
98 result.status = Status::Unknown;
99 } else if (inputs.prearmCheckAvailable) {
100 result.canArm = inputs.prearmCheckPassed;
101 result.canTakeoff = inputs.prearmCheckPassed;
102 result.canStartMission = inputs.prearmCheckPassed;
103 result.status = inputs.prearmCheckPassed ? Status::Ready : Status::NotReady;
104 } else {
105 result.canArm = true;
106 result.canTakeoff = true;
107 result.canStartMission = true;
108 // Sensor health and setup are pre-flight hints, so they never mark an armed vehicle not ready
109 result.status =
110 (inputs.armed || (inputs.sensorsHealthy && inputs.setupComplete)) ? Status::Ready : Status::NotReady;
111 }
112
113 return result;
114}
115
116void VehicleReadiness::_applyLockouts(Result& result, const Inputs& inputs)
117{
118 // Vehicle state is incomplete until initial connect finishes, so nothing pre-flight is allowed yet
119 if (!inputs.initialConnectComplete) {
120 result = Result{.status = Status::Syncing};
121 return;
122 }
123
124 // The checklist is pre-flight, so it only blocks a disarmed vehicle
125 if (!inputs.armed && inputs.checklistBlocksArming) {
126 result.canArm = false;
127 result.canTakeoff = false;
128 result.canStartMission = false;
129 result.status = Status::NotReady;
130 }
131}
132
133VehicleReadiness::Inputs VehicleReadiness::_gatherInputs() const
134{
135 Inputs inputs;
136
137 inputs.armed = _vehicle->armed();
138 inputs.initialConnectComplete = _vehicle->isInitialConnectComplete();
139 inputs.healthReportWaitExpired = _healthReportWaitExpired;
140
141 AppSettings* const appSettings = SettingsManager::instance()->appSettings();
142 inputs.checklistBlocksArming = appSettings->useChecklist()->rawValue().toBool() &&
143 appSettings->enforceChecklist()->rawValue().toBool() &&
144 (_vehicle->checkListState() != Vehicle::CheckListPassed);
145
146 inputs.healthAndArmingChecksSupported = _vehicle->healthAndArmingChecksSupported(MAV_COMP_ID_AUTOPILOT1);
147 const HealthAndArmingCheckReport* const report = _vehicle->healthAndArmingCheckReport();
148 if (report && report->supported()) {
149 const HealthAndArmingCheckReport::ModeGroupResults& modeGroupResults = report->modeGroupResults();
150 inputs.healthAndArmingChecksReported = true;
151 inputs.currentModeGroup = modeGroupResults.currentModeGroup;
152 inputs.canArmModeGroups = modeGroupResults.canArm;
153 inputs.canRunModeGroups = modeGroupResults.canRun;
154 inputs.takeoff = _actionModeGroups(GuidedAction::Takeoff);
155 inputs.startMission = _actionModeGroups(GuidedAction::StartMission);
156 }
157
158 inputs.prearmCheckAvailable = _vehicle->readyToFlyAvailable();
159 inputs.prearmCheckPassed = _vehicle->readyToFly();
160
161 inputs.sensorsHealthy = _vehicle->allSensorsHealthy();
162 const AutoPilotPlugin* const autopilotPlugin = _vehicle->autopilotPlugin();
163 inputs.setupComplete = autopilotPlugin && autopilotPlugin->setupComplete();
164
165 return inputs;
166}
167
168VehicleReadiness::ActionModeGroups VehicleReadiness::_actionModeGroups(GuidedAction action) const
169{
170 const ActionFlightModes flightModes = _actionFlightModes(action);
171 return ActionModeGroups{.armModeGroup = _modeGroup(flightModes.armFlightMode),
172 .targetModeGroup = _modeGroup(flightModes.targetFlightMode)};
173}
174
175int VehicleReadiness::_modeGroup(const QString& flightMode) const
176{
177 if (flightMode.isEmpty()) {
178 return -1;
179 }
180
181 uint8_t baseMode = 0;
182 uint32_t customMode = 0;
183 if (!_vehicle->firmwarePlugin()->setFlightMode(flightMode, &baseMode, &customMode)) {
184 return -1;
185 }
186 return _vehicle->healthAndArmingCheckModeGroup(customMode);
187}
188
190{
191 const Inputs inputs = _gatherInputs();
192 qCDebug(VehicleReadinessLog) << "armed:" << inputs.armed
193 << "initialConnectComplete:" << inputs.initialConnectComplete
194 << "checklistBlocksArming:" << inputs.checklistBlocksArming
195 << "healthChecks supported/reported:" << inputs.healthAndArmingChecksSupported
196 << inputs.healthAndArmingChecksReported
197 << "currentModeGroup:" << inputs.currentModeGroup
198 << "takeoff arm/target:" << inputs.takeoff.armModeGroup
199 << inputs.takeoff.targetModeGroup
200 << "startMission arm/target:" << inputs.startMission.armModeGroup
201 << inputs.startMission.targetModeGroup << "canArm:" << Qt::hex
202 << inputs.canArmModeGroups << "canRun:" << inputs.canRunModeGroups << Qt::dec
203 << "prearm available/passed:" << inputs.prearmCheckAvailable
204 << inputs.prearmCheckPassed << "sensorsHealthy:" << inputs.sensorsHealthy
205 << "setupComplete:" << inputs.setupComplete;
206
207 const Result generic = _evaluate(inputs);
208 Result result{.canArm = _canArm(generic.canArm),
209 .canTakeoff = _canTakeoff(generic.canTakeoff),
210 .canStartMission = _canStartMission(generic.canStartMission),
211 .status = _status(generic.status)};
212 _applyLockouts(result, inputs);
213
214 if (result == _result) {
215 return;
216 }
217
218 _result = result;
219 qCDebug(VehicleReadinessLog) << "canArm:" << _result.canArm << "canTakeoff:" << _result.canTakeoff
220 << "canStartMission:" << _result.canStartMission << "status:" << _result.status;
221 emit readinessChanged();
222}
#define QGC_LOGGING_CATEGORY(name, categoryStr)
Application Settings.
Definition AppSettings.h:10
The AutoPilotPlugin class is an abstract base class which represents the methods and objects which ar...
void setupCompleteChanged()
bool setupComplete() const
void rawValueChanged(const QVariant &value)
const ModeGroupResults & modeGroupResults() const
static SettingsManager * instance()
AppSettings * appSettings() const
virtual bool _canStartMission(bool genericCanStartMission) const
virtual bool _canTakeoff(bool genericCanTakeoff) const
virtual ActionFlightModes _actionFlightModes(GuidedAction) const
Firmware returns the modes its plugin uses for the action. Unknown modes fall back to the current mod...
@ Syncing
Initial connect is still in progress, so every action is locked out.
@ Unknown
Waiting for a health report the vehicle is expected to send, or the current mode is unknown to it.
void update()
Re-evaluates readiness. Vehicle calls this once the firmware's subclass is fully constructed.
void readinessChanged()
virtual Status _status(Status genericStatus) const
VehicleReadiness(Vehicle *vehicle)
virtual bool _canArm(bool genericCanArm) const
void readyToFlyAvailableChanged(bool readyToFlyAvailable)
void initialConnectComplete()
void readyToFlyChanged(bool readyToFy)
void armedChanged(bool armed)
void checkListStateChanged()
void allSensorsHealthyChanged(bool allSensorsHealthy)
@ CheckListPassed
Definition Vehicle.h:138
quint64 canArm
One bit per mode group: arming in that group would be accepted.
quint64 canRun
One bit per mode group: switching into that group while armed would be accepted.
Flight modes a guided action uses, as the firmware plugin performs it.