QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
VehicleFactGroup.cc
Go to the documentation of this file.
1#include "VehicleFactGroup.h"
2#include "Vehicle.h"
3#include "QGCMath.h"
4
5#include <cmath>
6
7#include <QtGui/QQuaternion>
8#include <QtGui/QVector3D>
9
51
53{
54 // 0 <= rssi <= 100 is the valid range; 255 (or anything > 100) means "unknown".
55 if (rssi > 100) {
56 if (_rcRSSIFact.rawValue().toUInt() != 255) {
58 }
59 return;
60 }
61
62 // Initialize the filter lazily so the first sample is taken verbatim.
63 if (_rcRSSIStore == 255.0) {
64 _rcRSSIStore = static_cast<double>(rssi);
65 }
66
67 // Low-pass filter to damp RSSI jitter.
68 _rcRSSIStore = (_rcRSSIStore * 0.9) + (static_cast<double>(rssi) * 0.1);
69 uint8_t filteredRSSI = static_cast<uint8_t>(std::ceil(_rcRSSIStore));
70 if (_rcRSSIStore < 0.1) {
71 filteredRSSI = 0;
72 }
73
74 if (_rcRSSIFact.rawValue().toUInt() != filteredRSSI) {
75 _rcRSSIFact.setRawValue(filteredRSSI);
76 }
77}
78
80{
81 switch (message.msgid) {
82 case MAVLINK_MSG_ID_ATTITUDE:
83 _handleAttitude(vehicle, message);
84 break;
85 case MAVLINK_MSG_ID_ATTITUDE_QUATERNION:
86 _handleAttitudeQuaternion(vehicle, message);
87 break;
88 case MAVLINK_MSG_ID_ALTITUDE:
89 _handleAltitude(message);
90 break;
91 case MAVLINK_MSG_ID_VFR_HUD:
92 _handleVfrHud(message);
93 break;
94 case MAVLINK_MSG_ID_NAV_CONTROLLER_OUTPUT:
96 break;
97 case MAVLINK_MSG_ID_RAW_IMU:
98 _handleRawImuTemp(message);
99 break;
100 case MAVLINK_MSG_ID_RANGEFINDER:
101 _handleRangefinder(message);
102 break;
103 default:
104 break;
105 }
106}
107
108void VehicleFactGroup::_handleAttitudeWorker(double rollRadians, double pitchRadians, double yawRadians)
109{
110 double rollDegrees = QGC::limitAngleToPMPIf(rollRadians);
111 double pitchDegrees = QGC::limitAngleToPMPIf(pitchRadians);
112 double yawDegrees = QGC::limitAngleToPMPIf(yawRadians);
113
114 rollDegrees = qRadiansToDegrees(rollDegrees);
115 pitchDegrees = qRadiansToDegrees(pitchDegrees);
116 yawDegrees = qRadiansToDegrees(yawDegrees);
117
118 if (yawDegrees < 0.0) {
119 yawDegrees += 360.0;
120 }
121 // truncate to integer so widget never displays 360
122 yawDegrees = trunc(yawDegrees);
123
124 roll()->setRawValue(rollDegrees);
125 pitch()->setRawValue(pitchDegrees);
126 heading()->setRawValue(yawDegrees);
127}
128
130{
131 if ((message.sysid != vehicle->id()) || (message.compid != vehicle->compId())) {
132 return;
133 }
134
135 if (_receivingAttitudeQuaternion) {
136 return;
137 }
138
139 mavlink_attitude_t attitude{};
140 mavlink_msg_attitude_decode(&message, &attitude);
141
142 _handleAttitudeWorker(attitude.roll, attitude.pitch, attitude.yaw);
143
145}
146
148{
149 mavlink_altitude_t altitude{};
150 mavlink_msg_altitude_decode(&message, &altitude);
151
152 // Data from ALTITUDE message takes precedence over gps messages
154 altitudeRelative()->setRawValue(altitude.altitude_relative);
155 altitudeAMSL()->setRawValue(altitude.altitude_amsl);
156
158}
159
161{
162 // only accept the attitude message from the vehicle's flight controller
163 if ((message.sysid != vehicle->id()) || (message.compid != vehicle->compId())) {
164 return;
165 }
166
167 _receivingAttitudeQuaternion = true;
168
169 mavlink_attitude_quaternion_t attitudeQuaternion{};
170 mavlink_msg_attitude_quaternion_decode(&message, &attitudeQuaternion);
171
172 QQuaternion quat(attitudeQuaternion.q1, attitudeQuaternion.q2, attitudeQuaternion.q3, attitudeQuaternion.q4);
173 QVector3D rates(attitudeQuaternion.rollspeed, attitudeQuaternion.pitchspeed, attitudeQuaternion.yawspeed);
174 QQuaternion repr_offset(attitudeQuaternion.repr_offset_q[0], attitudeQuaternion.repr_offset_q[1], attitudeQuaternion.repr_offset_q[2], attitudeQuaternion.repr_offset_q[3]);
175
176 // if repr_offset is valid, rotate attitude and rates
177 if (repr_offset.length() >= 0.5f) {
178 quat *= repr_offset;
179 rates = repr_offset * rates;
180 }
181
182 float attRoll, attPitch, attYaw;
183 float q[] = { quat.scalar(), quat.x(), quat.y(), quat.z() };
184 mavlink_quaternion_to_euler(q, &attRoll, &attPitch, &attYaw);
185
186 _handleAttitudeWorker(attRoll, attPitch, attYaw);
187
188 rollRate()->setRawValue(qRadiansToDegrees(rates[0]));
189 pitchRate()->setRawValue(qRadiansToDegrees(rates[1]));
190 yawRate()->setRawValue(qRadiansToDegrees(rates[2]));
191
193}
194
196{
197 mavlink_nav_controller_output_t navControllerOutput{};
198 mavlink_msg_nav_controller_output_decode(&message, &navControllerOutput);
199
200 altitudeTuningSetpoint()->setRawValue(_altitudeTuningFact.rawValue().toDouble() - navControllerOutput.alt_error);
201 xTrackError()->setRawValue(navControllerOutput.xtrack_error);
202 airSpeedSetpoint()->setRawValue(_airSpeedFact.rawValue().toDouble() - navControllerOutput.aspd_error);
203 distanceToNextWP()->setRawValue(navControllerOutput.wp_dist);
204
206}
207
209{
210 mavlink_vfr_hud_t vfrHud{};
211 mavlink_msg_vfr_hud_decode(&message, &vfrHud);
212
213 airSpeed()->setRawValue(qIsNaN(vfrHud.airspeed) ? 0 : vfrHud.airspeed);
214 groundSpeed()->setRawValue(qIsNaN(vfrHud.groundspeed) ? 0 : vfrHud.groundspeed);
215 climbRate()->setRawValue(qIsNaN(vfrHud.climb) ? 0 : vfrHud.climb);
216 throttlePct()->setRawValue(static_cast<int16_t>(vfrHud.throttle));
217 if (qIsNaN(_altitudeTuningOffset)) {
218 _altitudeTuningOffset = vfrHud.alt;
219 }
221 if (!qIsNaN(vfrHud.groundspeed) && !qIsNaN(_distanceToHomeFact.cookedValue().toDouble())) {
222 timeToHome()->setRawValue(_distanceToHomeFact.cookedValue().toDouble() / vfrHud.groundspeed);
223 }
224
226}
227
229{
230 mavlink_raw_imu_t imuRaw{};
231 mavlink_msg_raw_imu_decode(&message, &imuRaw);
232
233 imuTemp()->setRawValue((imuRaw.temperature == 0) ? 0 : (imuRaw.temperature * 0.01));
234
236}
237
239{
240 mavlink_rangefinder_t rangefinder{};
241 mavlink_msg_rangefinder_decode(&message, &rangefinder);
242
243 rangeFinderDist()->setRawValue(qIsNaN(rangefinder.distance) ? 0 : rangefinder.distance);
244
246}
struct __mavlink_message mavlink_message_t
Used to group Facts together into an object hierarachy.
Definition FactGroup.h:16
void _setTelemetryAvailable(bool telemetryAvailable)
Definition FactGroup.cc:175
void _addFact(Fact *fact, const QString &name)
Definition FactGroup.cc:116
QVariant cookedValue() const
Definition Fact.cc:226
void setRawValue(const QVariant &value)
Definition Fact.cc:134
QVariant rawValue() const
Definition Fact.h:90
void _handleAltitude(const mavlink_message_t &message)
VehicleFactGroup(QObject *parent=nullptr)
void handleMessage(Vehicle *vehicle, const mavlink_message_t &message) override
Allows a FactGroup to parse incoming messages and fill in values.
void _handleNavControllerOutput(const mavlink_message_t &message)
void _handleAttitude(Vehicle *vehicle, const mavlink_message_t &message)
void _handleRawImuTemp(const mavlink_message_t &message)
void updateRCRSSI(uint8_t rssi)
void _handleVfrHud(const mavlink_message_t &message)
void _handleAttitudeQuaternion(Vehicle *vehicle, const mavlink_message_t &message)
Fact * altitudeTuningSetpoint()
void _handleRangefinder(const mavlink_message_t &message)
int id() const
Definition Vehicle.h:429
int compId() const
Definition Vehicle.h:430
float limitAngleToPMPIf(double angle)
Definition QGCMath.cc:13