QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
Autotune.cpp
Go to the documentation of this file.
1#include "Autotune.h"
2#include "MAVLinkLib.h"
3#include "AppMessages.h"
4#include "Vehicle.h"
5
6//-----------------------------------------------------------------------------
8 QObject(vehicle)
9 , _vehicle(vehicle)
10{
11 _pollTimer.setInterval(1000); // 1s for the polling interval
12 _pollTimer.setSingleShot(false);
13 connect(&_pollTimer, &QTimer::timeout, this, &Autotune::sendMavlinkRequest);
14}
15
16
17//-----------------------------------------------------------------------------
19{
21
22 startTimers();
23 _autotuneInProgress = true;
24 _autotuneStatus = tr("Autotune: In progress");
25
26 emit autotuneChanged();
27}
28
29
30//-----------------------------------------------------------------------------
31void Autotune::ackHandler(void* resultHandlerData, int compId, const mavlink_command_ack_t& ack, VehicleTypes::MavCmdResultFailureCode_t failureCode)
32{
33 Q_UNUSED(compId);
34 Q_UNUSED(failureCode);
35
36 auto * autotune = static_cast<Autotune *>(resultHandlerData);
37
38 if (autotune->_autotuneInProgress) {
39 if (failureCode == Vehicle::MavCmdResultCommandResultOnly) {
40 if ((ack.result == MAV_RESULT_IN_PROGRESS) || (ack.result == MAV_RESULT_ACCEPTED)) {
41 autotune->handleAckStatus(ack.progress);
42 }
43 else if (ack.result == MAV_RESULT_FAILED) {
44 autotune->handleAckFailure();
45 }
46 else {
47 autotune->handleAckError(ack.result);
48 }
49 } else {
50 autotune->handleAckFailure();
51 }
52 emit autotune->autotuneChanged();
53 } else {
54 qWarning() << "Ack received for a command different from MAV_CMD_DO_AUTOTUNE_ENABLE ot wrong UI state.";
55 }
56}
57
58void Autotune::progressHandler(void* progressHandlerData, int compId, const mavlink_command_ack_t& ack)
59{
60 Q_UNUSED(compId);
61
62 auto * autotune = static_cast<Autotune *>(progressHandlerData);
63
64 if (autotune->_autotuneInProgress) {
65 autotune->handleAckStatus(ack.progress);
66 emit autotune->autotuneChanged();
67 } else {
68 qWarning() << "Ack received for a command different from MAV_CMD_DO_AUTOTUNE_ENABLE ot wrong UI state.";
69 }
70}
71
72//-----------------------------------------------------------------------------
73void Autotune::handleAckStatus(uint8_t ackProgress)
74{
75 _autotuneProgress = ackProgress/100.f;
76
77 if (ackProgress < 20) {
78 _autotuneStatus = tr("Autotune: initializing");
79 }
80 else if (ackProgress < 40) {
81 _autotuneStatus = tr("Autotune: roll");
82 }
83 else if (ackProgress < 60) {
84 _autotuneStatus = tr("Autotune: pitch");
85 }
86 else if (ackProgress < 80) {
87 _autotuneStatus = tr("Autotune: yaw");
88 }
89 else if (ackProgress == 95) {
90 _autotuneStatus = tr("Wait for disarm");
91
92 if(!_disarmMessageDisplayed) {
93 QGC::showAppMessage(tr("Land and disarm the vehicle in order to apply the parameters."));
94 _disarmMessageDisplayed = true;
95 }
96 }
97 else if (ackProgress < 100) {
98 _autotuneStatus = tr("Autotune: in progress");
99 }
100 else { // success or unknown error
101 stopTimers();
102 _autotuneInProgress = false;
103
104 if (ackProgress == 100) {
105 _autotuneStatus = tr("Autotune: Success");
106
107 QGC::showAppMessage(tr("Autotune successful."));
108 }
109 else {
110 _autotuneStatus = tr("Autotune: Unknown error");
111 }
112 }
113}
114
115
116//-----------------------------------------------------------------------------
117void Autotune::handleAckFailure()
118{
119 stopTimers();
120 _autotuneInProgress = false;
121 _disarmMessageDisplayed = false;
122 _autotuneStatus = tr("Autotune: Failed");
123}
124
125
126//-----------------------------------------------------------------------------
127void Autotune::handleAckError(uint8_t ackError)
128{
129 stopTimers();
130 _autotuneInProgress = false;
131 _disarmMessageDisplayed = false;
132 _autotuneStatus = tr("Autotune: Ack error %1").arg(ackError);
133}
134
135
136//-----------------------------------------------------------------------------
137void Autotune::startTimers()
138{
139 _pollTimer.start();
140}
141
142
143//-----------------------------------------------------------------------------
144void Autotune::stopTimers()
145{
146 _pollTimer.stop();
147}
148
149
150//-----------------------------------------------------------------------------
152{
153 Vehicle::MavCmdAckHandlerInfo_t handlerInfo = {};
154 handlerInfo.resultHandler = ackHandler;
155 handlerInfo.resultHandlerData = this;
156 handlerInfo.progressHandler = progressHandler;
157 handlerInfo.progressHandlerData = this;
158
160 &handlerInfo,
161 MAV_COMP_ID_AUTOPILOT1, // the ID of the autopilot
162 MAV_CMD_DO_AUTOTUNE_ENABLE, // the mavlink command
163 1, // request autotune
164 0, // unused parameter
165 0, // unused parameter
166 0, // unused parameter
167 0, // unused parameter
168 0, // unused parameter
169 0);
170}
struct __mavlink_command_ack_t mavlink_command_ack_t
static void ackHandler(void *resultHandlerData, int compId, const mavlink_command_ack_t &ack, VehicleTypes::MavCmdResultFailureCode_t failureCode)
Definition Autotune.cpp:31
static void progressHandler(void *progressHandlerData, int compId, const mavlink_command_ack_t &ack)
Definition Autotune.cpp:58
Autotune(Vehicle *vehicle)
Definition Autotune.cpp:7
void autotuneChanged()
void sendMavlinkRequest()
Definition Autotune.cpp:151
Q_INVOKABLE void autotuneRequest()
Definition Autotune.cpp:18
void sendMavCommandWithHandler(const MavCmdAckHandlerInfo_t *ackHandlerInfo, int compId, MAV_CMD command, 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)
Sends the command and calls the callback with the result.
Definition Vehicle.cc:2170
void showAppMessage(const QString &message, const QString &title)
Modal application message. Queued if the UI isn't ready yet.
Definition AppMessages.cc:9
Callback info bundle for sendMavCommandWithHandler.
void * progressHandlerData
nullptr for no handler
MavCmdProgressHandler progressHandler
MavCmdResultHandler resultHandler
nullptr for no handler
@ MavCmdResultCommandResultOnly
commandResult specifies full success/fail info