QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
MavCommandQueue.h
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <optional>
5
6#include <QtCore/QElapsedTimer>
7#include <QtCore/QList>
8#include <QtCore/QObject>
9#include <QtCore/QTimer>
10
11#include "VehicleTypes.h"
12
13class Vehicle;
14
22class MavCommandQueue : public QObject, public VehicleTypes
23{
24 Q_OBJECT
25
26public:
27 explicit MavCommandQueue(Vehicle* vehicle);
29
30 // Public send API — mirrors Vehicle's historical sendMavCommand* surface.
31 void sendCommand (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);
32 void sendCommandDelayed (int compId, MAV_CMD command, bool showError, int milliseconds, 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);
33 void sendCommandInt (int compId, MAV_CMD command, MAV_FRAME frame, bool showError, float param1, float param2, float param3, float param4, double param5, double param6, float param7);
34
35 void sendCommandWithHandler (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);
36 void sendCommandIntWithHandler (const MavCmdAckHandlerInfo_t* ackHandlerInfo, int compId, MAV_CMD command, MAV_FRAME frame, float param1 = 0.0f, float param2 = 0.0f, float param3 = 0.0f, float param4 = 0.0f, double param5 = 0.0, double param6 = 0.0, float param7 = 0.0f);
37
38 void sendCommandWithLambdaFallback(std::function<void()> lambda, 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);
39 void sendCommandIntWithLambdaFallback(std::function<void()> lambda, int compId, MAV_CMD command, MAV_FRAME frame, bool showError, float param1 = 0.0f, float param2 = 0.0f, float param3 = 0.0f, float param4 = 0.0f, double param5 = 0.0, double param6 = 0.0, float param7 = 0.0f);
40
44 void sendWorker(bool commandInt, bool showError, const MavCmdAckHandlerInfo_t* ackHandlerInfo,
45 int compId, MAV_CMD command, MAV_FRAME frame,
46 float param1, float param2, float param3, float param4, double param5, double param6, float param7);
47
49 bool isPending(int targetCompId, MAV_CMD command) const;
50
52 int findEntryIndex(int targetCompId, MAV_CMD command) const;
53
55 void handleCommandAck(const mavlink_message_t& message, const mavlink_command_ack_t& ack);
56
59 void stop();
60
61 static QString failureCodeToString(MavCmdResultFailureCode_t failureCode);
62 static void showCommandAckError(const mavlink_command_ack_t& ack);
63
64 static constexpr int kMaxRetryCount = 3;
65
69 static void setTestAckTimeoutOverride(std::optional<int> msecs);
70 static std::optional<int> testAckTimeoutOverride();
71
72signals:
74 void commandResult(int vehicleId, int targetComponent, int command, int ackResult, int failureCode);
75
76private slots:
77 void _responseTimeoutCheck();
78
79private:
80 typedef struct MavCommandListEntry {
81 int targetCompId = 0;
82 bool useCommandInt = false;
83 MAV_CMD command;
84 MAV_FRAME frame;
85 float rgParam1 = 0;
86 float rgParam2 = 0;
87 float rgParam3 = 0;
88 float rgParam4 = 0;
89 double rgParam5 = 0;
90 double rgParam6 = 0;
91 float rgParam7 = 0;
92 bool showError = true;
93 MavCmdAckHandlerInfo_t ackHandlerInfo;
94 int maxTries = kMaxRetryCount;
95 int tryCount = 0;
96 QElapsedTimer elapsedTimer;
97 int ackTimeoutMSecs = 0;
98 } MavCommandListEntry_t;
99
100 void sendCommandWithLambdaFallbackWorker(std::function<void()> lambda, bool commandInt, int compId, MAV_CMD command, MAV_FRAME frame, bool showError, float param1, float param2, float param3, float param4, double param5, double param6, float param7);
101
102 void _sendFromList(int index);
103 static bool _shouldRetry(MAV_CMD command);
104 static bool _canBeDuplicated(MAV_CMD command);
105 static int _responseCheckIntervalMSecs();
106 static int _ackTimeoutMSecs();
107 static QString _formatCommand(MAV_CMD command, float param1);
108
109 Vehicle* _vehicle = nullptr;
110 QList<MavCommandListEntry_t> _list;
111 QTimer _responseCheckTimer;
112 bool _stopped = false; // set by stop(), gates all send paths
113
114 static constexpr int _ackTimeoutMSecsDefault = 1200;
115 static constexpr int _ackTimeoutMSecsHighLatency = 120000;
116 static std::optional<int> _testAckTimeoutOverride;
117};
struct __mavlink_message mavlink_message_t
struct __mavlink_command_ack_t mavlink_command_ack_t
Owns the COMMAND_LONG / COMMAND_INT send/retry/ack pipeline for a single Vehicle.
void sendCommandInt(int compId, MAV_CMD command, MAV_FRAME frame, bool showError, float param1, float param2, float param3, float param4, double param5, double param6, float param7)
static QString failureCodeToString(MavCmdResultFailureCode_t failureCode)
void commandResult(int vehicleId, int targetComponent, int command, int ackResult, int failureCode)
Emitted for every terminal ack that has no user-provided resultHandler.
void sendCommandIntWithHandler(const MavCmdAckHandlerInfo_t *ackHandlerInfo, int compId, MAV_CMD command, MAV_FRAME frame, float param1=0.0f, float param2=0.0f, float param3=0.0f, float param4=0.0f, double param5=0.0, double param6=0.0, float param7=0.0f)
static std::optional< int > testAckTimeoutOverride()
void sendCommandWithHandler(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)
static constexpr int kMaxRetryCount
static void setTestAckTimeoutOverride(std::optional< int > msecs)
void sendWorker(bool commandInt, bool showError, const MavCmdAckHandlerInfo_t *ackHandlerInfo, int compId, MAV_CMD command, MAV_FRAME frame, float param1, float param2, float param3, float param4, double param5, double param6, float param7)
void handleCommandAck(const mavlink_message_t &message, const mavlink_command_ack_t &ack)
Process a COMMAND_ACK — match it to a pending entry and fire callbacks.
void sendCommandDelayed(int compId, MAV_CMD command, bool showError, int milliseconds, 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)
bool isPending(int targetCompId, MAV_CMD command) const
True if a matching (targetCompId, command) is already queued or awaiting ack.
static void showCommandAckError(const mavlink_command_ack_t &ack)
void sendCommand(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)
int findEntryIndex(int targetCompId, MAV_CMD command) const
Index of a matching entry in the pending queue, or -1. Exposed for test use.
void sendCommandIntWithLambdaFallback(std::function< void()> lambda, int compId, MAV_CMD command, MAV_FRAME frame, bool showError, float param1=0.0f, float param2=0.0f, float param3=0.0f, float param4=0.0f, double param5=0.0, double param6=0.0, float param7=0.0f)
void sendCommandWithLambdaFallback(std::function< void()> lambda, 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)
Callback info bundle for sendMavCommandWithHandler.