QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
WaitForParamResponseState.cc
Go to the documentation of this file.
2
3#include <utility>
4
5#include "MAVLinkLib.h"
7#include "Vehicle.h"
8
10 Predicate paramValuePredicate,
11 Predicate paramErrorPredicate)
12 : WaitStateBase(QStringLiteral("WaitForParamResponseState"), parent, timeoutMsecs)
13 , _paramValuePredicate(std::move(paramValuePredicate))
14 , _paramErrorPredicate(std::move(paramErrorPredicate))
15{
16}
17
19{
20 connect(vehicle(), &Vehicle::mavlinkMessageReceived, this, &WaitForParamResponseState::_messageReceived, Qt::UniqueConnection);
21}
22
24{
25 if (vehicle()) {
26 disconnect(vehicle(), &Vehicle::mavlinkMessageReceived, this, &WaitForParamResponseState::_messageReceived);
27 }
28}
29
30void WaitForParamResponseState::_messageReceived(const mavlink_message_t &message)
31{
32 if (message.msgid == MAVLINK_MSG_ID_PARAM_VALUE) {
33 if (_paramValuePredicate && !_paramValuePredicate(message)) {
34 return;
35 }
36
37 qCDebug(QGCStateMachineLog) << "Received PARAM_VALUE" << stateName();
39 return;
40 }
41
42 if (message.msgid == MAVLINK_MSG_ID_PARAM_ERROR) {
43 if (_paramErrorPredicate && !_paramErrorPredicate(message)) {
44 return;
45 }
46
47 mavlink_param_error_t paramError{};
48 mavlink_msg_param_error_decode(&message, &paramError);
49
50 _lastParamError = paramError.error;
51 _lastParamErrorString = _paramErrorToString(paramError.error);
52
53 char paramId[MAVLINK_MSG_PARAM_ERROR_FIELD_PARAM_ID_LEN + 1] = {};
54 (void) strncpy(paramId, paramError.param_id, MAVLINK_MSG_PARAM_ERROR_FIELD_PARAM_ID_LEN);
55
56 qCDebug(QGCStateMachineLog) << "Received PARAM_ERROR for" << paramId
57 << "error:" << _lastParamErrorString << stateName();
58 waitFailed();
59 return;
60 }
61}
62
63QString WaitForParamResponseState::_paramErrorToString(uint8_t errorCode)
64{
65 switch (errorCode) {
66 case MAV_PARAM_ERROR_NO_ERROR:
67 return QStringLiteral("No error");
68 case MAV_PARAM_ERROR_DOES_NOT_EXIST:
69 return QStringLiteral("Parameter does not exist");
70 case MAV_PARAM_ERROR_VALUE_OUT_OF_RANGE:
71 return QStringLiteral("Value out of range");
72 case MAV_PARAM_ERROR_PERMISSION_DENIED:
73 return QStringLiteral("Permission denied");
74 case MAV_PARAM_ERROR_COMPONENT_NOT_FOUND:
75 return QStringLiteral("Component not found");
76 case MAV_PARAM_ERROR_READ_ONLY:
77 return QStringLiteral("Parameter is read-only");
78 case MAV_PARAM_ERROR_TYPE_UNSUPPORTED:
79 return QStringLiteral("Parameter type unsupported");
80 case MAV_PARAM_ERROR_TYPE_MISMATCH:
81 return QStringLiteral("Parameter type mismatch");
82 case MAV_PARAM_ERROR_READ_FAIL:
83 return QStringLiteral("Parameter read failed");
84 default:
85 return QStringLiteral("Unknown error (%1)").arg(errorCode);
86 }
87}
struct __mavlink_message mavlink_message_t
QString stateName() const
Vehicle * vehicle() const
void mavlinkMessageReceived(const mavlink_message_t &message)
void disconnectWaitSignal() override
Subclasses override to tear down their signal connections.
WaitForParamResponseState(QState *parent, int timeoutMsecs, Predicate paramValuePredicate, Predicate paramErrorPredicate)
std::function< bool(const mavlink_message_t &message)> Predicate
void connectWaitSignal() override
Subclasses override to set up their signal connections.
Base class for states that wait for something with optional timeout.