QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GimbalController.h
Go to the documentation of this file.
1#pragma once
2
3#include <QtCore/QTimer>
4#include <QtQmlIntegration/QtQmlIntegration>
5
6#include "Gimbal.h"
8#include "VehicleTypes.h"
9
11class Vehicle;
12
13class GimbalController : public QObject
14{
15 Q_OBJECT
16 QML_ELEMENT
17 QML_UNCREATABLE("")
18 Q_MOC_INCLUDE("QmlObjectListModel.h")
19
21
23 Q_PROPERTY(QmlObjectListModel* gimbals READ gimbals CONSTANT)
24
25public:
26 GimbalController(Vehicle *vehicle);
28
29 // These are public for creating unit tests
30 static constexpr int kRateResendIntervalMs = 500;
31 static constexpr int kTestRateResendIntervalMs = 50;
33 static constexpr int kStatusRequestSpacingMs = 1000;
34 static constexpr int kTestStatusRequestSpacingMs = 50;
35
36 Gimbal *activeGimbal() const { return _activeGimbal; }
37 QmlObjectListModel *gimbals() const { return _gimbals; }
38
39 void setActiveGimbal(Gimbal *gimbal);
40
41 void sendPitchYawFlags(uint32_t flags);
42 Q_INVOKABLE void gimbalOnScreenControl(float panpct, float tiltpct, bool clickAndPoint, bool clickAndDrag, bool rateControl, bool retract = false, bool neutral = false, bool yawlock = false);
43 Q_INVOKABLE void sendPitchBodyYaw(float pitch, float yaw, bool showError = true);
44 Q_INVOKABLE void sendPitchAbsoluteYaw(float pitch, float yaw, bool showError = true);
45 Q_INVOKABLE void setGimbalRetract(bool set);
46 Q_INVOKABLE void setGimbalYawLock(bool set);
47 Q_INVOKABLE void acquireGimbalControl();
48 Q_INVOKABLE void releaseGimbalControl();
49 Q_INVOKABLE void sendRate();
50
54 Q_INVOKABLE void sendGimbalRate(float pitch_rate_deg_s, float yaw_rate_deg_s);
55
56signals:
58 void showAcquireGimbalControlPopup(); // This triggers a popup in QML asking the user for aproval to take control
59
60public slots:
61 // These slots are conected with joysticks for button control
62 void gimbalYawLock(bool yawLock) { setGimbalYawLock(yawLock); }
63 Q_INVOKABLE void centerGimbal();
64 Q_INVOKABLE void tilt90Gimbal();
65 void gimbalPitchStart(int direction);
66 void gimbalYawStart(int direction);
67 void gimbalPitchStop();
68 void gimbalYawStop();
69
70private slots:
71 void _initialConnectCompleted();
72 void _mavlinkMessageReceived(const mavlink_message_t& message);
73 void _rateSenderTimeout();
74
75private:
76 struct GimbalPairId {
77 GimbalPairId() = default;
78 GimbalPairId(uint8_t _managerCompid, uint8_t _deviceId)
79 : managerCompid(_managerCompid)
80 , deviceId(_deviceId) {}
81
82 // In order to use this as a key, we need to implement <,
83 bool operator<(const GimbalPairId &other) const {
84 // We compare managerCompid primarily, if they are equal, we compare the deviceId
85 if (managerCompid < other.managerCompid) {
86 return true;
87 } else if (managerCompid > other.managerCompid) {
88 return false;
89 } else if (deviceId < other.deviceId) {
90 return true;
91 } else {
92 return false;
93 }
94 }
95
96 bool operator==(const GimbalPairId &other) const {
97 return (managerCompid == other.managerCompid) && (deviceId == other.deviceId);
98 }
99
100 uint8_t managerCompid = 0;
101 uint8_t deviceId = 0;
102 };
103
105 bool _requestGimbalInformation(uint8_t compid);
106 static void _requestMessageResultHandler(void* resultHandlerData, MAV_RESULT result, VehicleTypes::RequestMessageResultHandlerFailureCode_t failureCode, const mavlink_message_t& message);
107 static void _requestDeviceInformationResultHandler(
108 void* resultHandlerData, MAV_RESULT result, VehicleTypes::RequestMessageResultHandlerFailureCode_t failureCode,
109 const mavlink_message_t& message);
110 void _handleHeartbeat(const mavlink_message_t &message);
111 void _handleGimbalManagerInformation(const mavlink_message_t &message);
112 void _handleGimbalManagerStatus(const mavlink_message_t &message);
113 void _handleGimbalDeviceAttitudeStatus(const mavlink_message_t &message);
114 void _handleGimbalDeviceInformation(const mavlink_message_t& message);
115 void _checkComplete(Gimbal &gimbal, GimbalPairId pairId);
116 bool _tryGetGimbalControl();
117 bool _yawInVehicleFrame(uint32_t flags);
118 static bool _gcsSystemIdSupportsGimbalControl();
119 static void _showGcsSystemIdUnsupportedMessage();
120
121 void _sendGimbalAttitudeRates(float pitch_rate_deg_s, float yaw_rate_deg_s);
122
123 QTimer _rateSenderTimer;
124 Vehicle *_vehicle = nullptr;
125 Gimbal *_activeGimbal = nullptr;
126 int _pendingInformationRequestCompId = -1;
127
128 struct PotentialGimbalManager {
129 unsigned requestGimbalManagerInformationRetries = 6;
130 bool receivedGimbalManagerInformation = false;
131 };
132 QMap<uint8_t, PotentialGimbalManager> _potentialGimbalManagers; // key is compid
133
134 QMap<GimbalPairId, Gimbal*> _potentialGimbals;
135 QmlObjectListModel *_gimbals = nullptr;
136 bool _initialConnectComplete = false;
137 bool _gcsSystemIdUnsupportedMessageShown = false;
138
139 static constexpr const char *_gimbalFactGroupNamePrefix = "gimbal";
140};
struct __mavlink_message mavlink_message_t
static constexpr int kTestRateResendIntervalMs
void sendPitchYawFlags(uint32_t flags)
Q_INVOKABLE void sendGimbalRate(float pitch_rate_deg_s, float yaw_rate_deg_s)
Q_INVOKABLE void setGimbalYawLock(bool set)
QmlObjectListModel * gimbals() const
Q_INVOKABLE void centerGimbal()
Gimbal * activeGimbal() const
void activeGimbalChanged()
Q_INVOKABLE void sendRate()
static constexpr int kRateResendIntervalMs
Rate command resend period while an axis is moving.
Q_INVOKABLE void sendPitchAbsoluteYaw(float pitch, float yaw, bool showError=true)
void gimbalYawLock(bool yawLock)
static constexpr int kTestStatusRequestSpacingMs
Shortened status request spacing in unit tests.
friend class GimbalControllerDiscoveryTest
GimbalController(Vehicle *vehicle)
Q_INVOKABLE void gimbalOnScreenControl(float panpct, float tiltpct, bool clickAndPoint, bool clickAndDrag, bool rateControl, bool retract=false, bool neutral=false, bool yawlock=false)
Q_INVOKABLE void acquireGimbalControl()
void setActiveGimbal(Gimbal *gimbal)
void gimbalYawStart(int direction)
static constexpr int kStatusRequestSpacingMs
Minimum time between GIMBAL_MANAGER_STATUS interval requests, per gimbal.
Q_INVOKABLE void setGimbalRetract(bool set)
void showAcquireGimbalControlPopup()
void gimbalPitchStart(int direction)
Q_INVOKABLE void releaseGimbalControl()
Q_INVOKABLE void sendPitchBodyYaw(float pitch, float yaw, bool showError=true)
Q_INVOKABLE void tilt90Gimbal()
Definition Gimbal.h:9
RequestMessageResultHandlerFailureCode_t