QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
MockLinkPX4Calibration.cc
Go to the documentation of this file.
2#include "MAVLinkLib.h"
3#include "MockLink.h"
4
5#include <algorithm>
6
7// Side names in PX4 detect_orientation_return order (see PX4 detect_orientation_str())
8static constexpr const char *kSideNames[MockLinkPX4Calibration::kSideCount] = {
9 "back", // tail down
10 "front", // nose down
11 "left",
12 "right",
13 "up", // upside down
14 "down", // right side up
15};
16
17// Ideal accel readings per side, same order as kSideNames (see PX4 accel_calibration_worker())
19 "[9.810 0.000 0.000]",
20 "[-9.810 0.000 0.000]",
21 "[0.000 9.810 0.000]",
22 "[0.000 -9.810 0.000]",
23 "[0.000 0.000 9.810]",
24 "[0.000 0.000 -9.810]",
25};
26
27// Device ids stored in CAL_MAG0_ID/CAL_ACC0_ID after a successful calibration,
28// matching the values in PX4MockLink.params
29static constexpr int32_t kMagDeviceId = 197388;
30static constexpr int32_t kAccelDeviceId = 1310988;
31static constexpr int32_t kBaroDeviceId = 6619404;
32static constexpr int32_t kGyroDeviceId = 2293772;
33static constexpr float kLevelRollOffsetDeg = 1.5f;
34static constexpr float kLevelPitchOffsetDeg = -0.8f;
35
37 : _mockLink(mockLink)
38{
39}
40
42{
43 _startCalibration(CalType::Mag);
44}
45
47{
48 _startCalibration(CalType::Accel);
49}
50
52{
53 {
54 QMutexLocker locker(&_mutex);
55 _baroFails = fails;
56 }
57 _startCalibration(CalType::Baro);
58}
59
61{
62 {
63 QMutexLocker locker(&_mutex);
64 _levelFails = fails;
65 }
66 _startCalibration(CalType::Level);
67}
68
70{
71 {
72 QMutexLocker locker(&_mutex);
73 _gyroFails = fails;
74 }
75 _startCalibration(CalType::Gyro);
76}
77
79{
80 QMutexLocker locker(&_mutex);
81 _inlineStalls = stalls;
82}
83
84QString MockLinkPX4Calibration::_calTypeName(CalType calType)
85{
86 switch (calType) {
87 case CalType::Mag:
88 return QStringLiteral("mag");
89 case CalType::Accel:
90 return QStringLiteral("accel");
91 case CalType::Baro:
92 return QStringLiteral("baro");
93 case CalType::Level:
94 return QStringLiteral("level");
95 case CalType::Gyro:
96 return QStringLiteral("gyro");
97 case CalType::None:
98 break;
99 }
100 return QString();
101}
102
103void MockLinkPX4Calibration::_startCalibration(CalType calType)
104{
105 QMutexLocker locker(&_mutex);
106
107 _calType = calType;
108 _requestedPose = -1;
109 _activeSide = -1;
110 _sideTickCount = 0;
111 // The barometer, gyroscope and level calibrations have no sides: they go straight to the finishing phase
112 _finishTickCount = (calType == CalType::Baro || calType == CalType::Level || calType == CalType::Gyro) ? 0 : -1;
113 std::fill(std::begin(_sideDone), std::end(_sideDone), false);
114
115 _mockLink->sendStatusTextMessage(MAV_SEVERITY_INFO,
116 QStringLiteral("[cal] calibration started: 2 %1").arg(_calTypeName(calType)));
117}
118
120{
121 QMutexLocker locker(&_mutex);
122
123 if (_calType == CalType::None) {
124 return false;
125 }
126
127 _calType = CalType::None;
128 _requestedPose = -1;
129 _activeSide = -1;
130 _finishTickCount = -1;
131
132 _mockLink->sendStatusTextMessage(MAV_SEVERITY_CRITICAL, QStringLiteral("[cal] calibration cancelled"));
133 return true;
134}
135
137{
138 QMutexLocker locker(&_mutex);
139
140 if (_calType == CalType::None) {
141 return false;
142 }
143
144 const QString name = _calTypeName(_calType);
145 _calType = CalType::None;
146 _requestedPose = -1;
147 _activeSide = -1;
148 _finishTickCount = -1;
149
150 _mockLink->sendStatusTextMessage(MAV_SEVERITY_CRITICAL, QStringLiteral("[cal] calibration failed: %1").arg(name));
151 return true;
152}
153
155{
156 QMutexLocker locker(&_mutex);
157 return _calType != CalType::None;
158}
159
161{
162 QMutexLocker locker(&_mutex);
163 _requestedPose = static_cast<int>(pose);
164}
165
167{
168 QMutexLocker locker(&_mutex);
169
170 if (_calType == CalType::None) {
171 return;
172 }
173
174 if (_finishTickCount >= 0) {
175 // Finishing phase: simulates the firmware calculating the final calibration
176 // values before reporting completion. Gives the UI time to display the last
177 // side as completed while the calibration is still active.
178 if (_inlineStalls && (_calType == CalType::Baro || _calType == CalType::Level || _calType == CalType::Gyro)) {
179 return;
180 }
181 _finishTickCount++;
182 if (_finishTickCount >= kTicksToFinish) {
183 const CalType calType = _calType;
184 _calType = CalType::None;
185 _finishTickCount = -1;
186 _finishCalibration(calType);
187 }
188 return;
189 }
190
191 if (_activeSide >= 0) {
192 _sideTick();
193 } else {
194 _detectPose();
195 }
196}
197
198void MockLinkPX4Calibration::_finishCalibration(CalType calType)
199{
200 if (calType == CalType::Baro && _baroFails) {
201 _mockLink->sendStatusTextMessage(MAV_SEVERITY_CRITICAL,
202 QStringLiteral("[cal] calibration failed: GPS required for baro cal"));
203 return;
204 }
205
206 if (calType == CalType::Level || calType == CalType::Gyro) {
207 _mockLink->sendStatusTextMessage(MAV_SEVERITY_INFO, QStringLiteral("[cal] progress <100>"));
208 const bool fails = (calType == CalType::Level) ? _levelFails : _gyroFails;
209 if (fails) {
210 // The firmware reports the reason as a plain message, then the failure
211 const QString reason = (calType == CalType::Level) ? QStringLiteral("motion during calibration")
212 : QStringLiteral("ERROR: Motion during calibration");
213 _mockLink->sendStatusTextMessage(MAV_SEVERITY_CRITICAL, reason);
214 _mockLink->sendStatusTextMessage(MAV_SEVERITY_CRITICAL,
215 QStringLiteral("[cal] calibration failed: %1").arg(_calTypeName(calType)));
216 return;
217 }
218 }
219
220 // Store the calibration result like the firmware does, before reporting
221 // done so the subsequent param refresh by QGC picks it up
222 switch (calType) {
223 case CalType::Mag:
224 _mockLink->setInt32ParamValue(MAV_COMP_ID_AUTOPILOT1, QStringLiteral("CAL_MAG0_ID"), kMagDeviceId);
225 // The mag fit calculation reports its own final progress
226 _mockLink->sendStatusTextMessage(MAV_SEVERITY_INFO, QStringLiteral("[cal] progress <100>"));
227 break;
228 case CalType::Accel:
229 _mockLink->setInt32ParamValue(MAV_COMP_ID_AUTOPILOT1, QStringLiteral("CAL_ACC0_ID"), kAccelDeviceId);
230 break;
231 case CalType::Baro:
232 _mockLink->setInt32ParamValue(MAV_COMP_ID_AUTOPILOT1, QStringLiteral("CAL_BARO0_ID"), kBaroDeviceId);
233 break;
234 case CalType::Level:
235 _mockLink->setFloatParamValue(MAV_COMP_ID_AUTOPILOT1, QStringLiteral("SENS_BOARD_X_OFF"),
237 _mockLink->setFloatParamValue(MAV_COMP_ID_AUTOPILOT1, QStringLiteral("SENS_BOARD_Y_OFF"),
239 break;
240 case CalType::Gyro:
241 _mockLink->setInt32ParamValue(MAV_COMP_ID_AUTOPILOT1, QStringLiteral("CAL_GYRO0_ID"), kGyroDeviceId);
242 break;
243 case CalType::None:
244 return;
245 }
246
247 _mockLink->sendStatusTextMessage(MAV_SEVERITY_INFO,
248 QStringLiteral("[cal] calibration done: %1").arg(_calTypeName(calType)));
249}
250
251int MockLinkPX4Calibration::_doneCount() const
252{
253 int count = 0;
254 for (bool done : _sideDone) {
255 if (done) {
256 count++;
257 }
258 }
259 return count;
260}
261
263void MockLinkPX4Calibration::_detectPose()
264{
265 const int pose = _requestedPose;
266 if ((pose < 0) || (pose >= kSideCount)) {
267 return;
268 }
269 _requestedPose = -1; // consume
270
271 if (_sideDone[pose]) {
272 _mockLink->sendStatusTextMessage(MAV_SEVERITY_INFO, QStringLiteral("[cal] %1 side already completed").arg(QLatin1String(kSideNames[pose])));
273 return;
274 }
275
276 _mockLink->sendStatusTextMessage(MAV_SEVERITY_INFO, QStringLiteral("[cal] %1 orientation detected").arg(QLatin1String(kSideNames[pose])));
277 if (_calType == CalType::Accel) {
278 // The accel worker starts sampling immediately (see PX4 accel_calibration_worker())
279 _mockLink->sendStatusTextMessage(MAV_SEVERITY_INFO, QStringLiteral("[cal] Hold still, measuring %1 side").arg(QLatin1String(kSideNames[pose])));
280 }
281 _activeSide = pose;
282 _sideTickCount = 0;
283}
284
287void MockLinkPX4Calibration::_sideTick()
288{
289 _sideTickCount++;
290
291 switch (_calType) {
292 case CalType::Mag:
293 _magSideTick();
294 break;
295 case CalType::Accel:
296 _accelSideTick();
297 break;
298 case CalType::Baro:
299 case CalType::Level:
300 case CalType::Gyro:
301 case CalType::None:
302 return;
303 }
304
305 if (_doneCount() == kSideCount) {
306 // All sides collected: enter the finishing phase
307 _finishTickCount = 0;
308 }
309}
310
312void MockLinkPX4Calibration::_magSideTick()
313{
314 if (_sideTickCount < kTicksPerSide) {
315 // Same progress calculation as PX4 mag_calibration_worker()
316 const unsigned progress = (_doneCount() * 100 / kSideCount) + ((100 / kSideCount) * _sideTickCount / kTicksPerSide);
317 _mockLink->sendStatusTextMessage(MAV_SEVERITY_INFO, QStringLiteral("[cal] %1 side calibration: progress <%2>").arg(QLatin1String(kSideNames[_activeSide])).arg(progress));
318 return;
319 }
320
321 _sideDone[_activeSide] = true;
322 _mockLink->sendStatusTextMessage(MAV_SEVERITY_INFO, QStringLiteral("[cal] %1 side done, rotate to a different side").arg(QLatin1String(kSideNames[_activeSide])));
323 _activeSide = -1;
324}
325
328void MockLinkPX4Calibration::_accelSideTick()
329{
330 if (_sideTickCount < kTicksPerSide) {
331 return;
332 }
333
334 _sideDone[_activeSide] = true;
335 _mockLink->sendStatusTextMessage(MAV_SEVERITY_INFO, QStringLiteral("[cal] %1 side result: %2").arg(QLatin1String(kSideNames[_activeSide]), QLatin1String(kSideAccelResults[_activeSide])));
336 // Same progress calculation as PX4 accel_calibration_worker(): 17 * doneCount.
337 // Intentionally reaches 102 after the final side, matching firmware; the UI
338 // progress bar clamps it to 100%.
339 _mockLink->sendStatusTextMessage(MAV_SEVERITY_INFO, QStringLiteral("[cal] progress <%1>").arg(17 * _doneCount()));
340 _mockLink->sendStatusTextMessage(MAV_SEVERITY_INFO, QStringLiteral("[cal] %1 side done, rotate to a different side").arg(QLatin1String(kSideNames[_activeSide])));
341 _activeSide = -1;
342}
static constexpr float kLevelPitchOffsetDeg
static constexpr int32_t kGyroDeviceId
static constexpr const char * kSideNames[MockLinkPX4Calibration::kSideCount]
static constexpr float kLevelRollOffsetDeg
static constexpr const char * kSideAccelResults[MockLinkPX4Calibration::kSideCount]
static constexpr int32_t kBaroDeviceId
static constexpr int32_t kAccelDeviceId
static constexpr int32_t kMagDeviceId
static constexpr int kSideCount
MockLinkPX4Calibration(MockLink *mockLink)
Pose
Vehicle poses. Order matches the PX4 detect_orientation_return enum.
void startGyroCalibration(bool fails)
void startLevelCalibration(bool fails)
void startBaroCalibration(bool fails)
void run10HzTasks()
Called by MockLink::run10HzTasks on the worker thread.
void setInlineCalibrationStalls(bool stalls)
Makes the simulated barometer, level horizon and gyroscope calibrations run until they are cancelled.