QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
MAVLinkInspectorController.cc
Go to the documentation of this file.
3#include "MAVLinkMessage.h"
4#include "MAVLinkProtocol.h"
5#include "MAVLinkSystem.h"
7#include "QGCApplication.h"
10#include "Vehicle.h"
11
12#include <QtQml/QQmlEngine>
13
14QGC_LOGGING_CATEGORY(MAVLinkInspectorControllerLog, "AnalyzeView.MAVLinkInspectorController")
15
16MAVLinkInspectorController::TimeScale_st::TimeScale_st(const QString &label_, uint32_t timeScale_)
17 : label(label_)
18 , timeScale(timeScale_)
19{
20
21}
22
23/*===========================================================================*/
24
25MAVLinkInspectorController::Range_st::Range_st(const QString &label_, qreal range_)
26 : label(label_)
27 , range(range_)
28{
29
30}
31
32/*===========================================================================*/
33
35 : QObject(parent)
36 , _updateFrequencyTimer(new QTimer(this))
37 , _systems(new QmlObjectListModel(this))
38{
39 // qCDebug(MAVLinkInspectorControllerLog) << Q_FUNC_INFO << this;
40
41 MultiVehicleManager *const multiVehicleManager = MultiVehicleManager::instance();
42 (void) connect(multiVehicleManager, &MultiVehicleManager::vehicleAdded, this, &MAVLinkInspectorController::_vehicleAdded);
43 (void) connect(multiVehicleManager, &MultiVehicleManager::vehicleRemoved, this, &MAVLinkInspectorController::_vehicleRemoved);
44 (void) connect(multiVehicleManager, &MultiVehicleManager::activeVehicleChanged, this, &MAVLinkInspectorController::_setActiveVehicle);
45
46 MAVLinkProtocol *const mavlinkProtocol = MAVLinkProtocol::instance();
47 (void) connect(mavlinkProtocol, &MAVLinkProtocol::messageReceived, this, &MAVLinkInspectorController::_receiveMessage);
48 (void) connect(_updateFrequencyTimer, &QTimer::timeout, this, &MAVLinkInspectorController::_refreshFrequency);
49
50 _updateFrequencyTimer->setInterval(1000);
51 _updateFrequencyTimer->setSingleShot(false);
52 _updateFrequencyTimer->start();
53
54 _timeScaleSt.append(new TimeScale_st(tr("5 Sec"), 5 * 1000));
55 _timeScaleSt.append(new TimeScale_st(tr("10 Sec"), 10 * 1000));
56 _timeScaleSt.append(new TimeScale_st(tr("30 Sec"), 30 * 1000));
57 _timeScaleSt.append(new TimeScale_st(tr("60 Sec"), 60 * 1000));
58 emit timeScalesChanged();
59
60 _rangeSt.append(new Range_st(tr("Auto"), 0));
61 _rangeSt.append(new Range_st(tr("10,000"), 10000));
62 _rangeSt.append(new Range_st(tr("1,000"), 1000));
63 _rangeSt.append(new Range_st(tr("100"), 100));
64 _rangeSt.append(new Range_st(tr("10"), 10));
65 _rangeSt.append(new Range_st(tr("1"), 1));
66 _rangeSt.append(new Range_st(tr("0.1"), 0.1));
67 _rangeSt.append(new Range_st(tr("0.01"), 0.01));
68 _rangeSt.append(new Range_st(tr("0.001"), 0.001));
69 _rangeSt.append(new Range_st(tr("0.0001"), 0.0001));
70 emit rangeListChanged();
71}
72
74{
75 qDeleteAll(_timeScaleSt);
76 qDeleteAll(_rangeSt);
77 _systems->clearAndDeleteContents();
78
79 // qCDebug(MAVLinkInspectorControllerLog) << Q_FUNC_INFO << this;
80}
81
83{
84 if (_timeScales.isEmpty()) {
85 for (const TimeScale_st *timeScale : _timeScaleSt) {
86 _timeScales << timeScale->label;
87 }
88 }
89
90 return _timeScales;
91}
92
94{
95 if (_rangeList.isEmpty()) {
96 for (const Range_st *range : _rangeSt) {
97 _rangeList << range->label;
98 }
99 }
100
101 return _rangeList;
102}
103
104void MAVLinkInspectorController::_setActiveVehicle(Vehicle *vehicle)
105{
106 if (vehicle) {
107 QGCMAVLinkSystem *const system = _findVehicle(static_cast<uint8_t>(vehicle->id()));
108 if (system) {
109 _activeSystem = system;
110 } else {
111 _activeSystem = nullptr;
112 }
113 } else {
114 _activeSystem = nullptr;
115 }
116
117 emit activeSystemChanged();
118}
119
120QGCMAVLinkSystem *MAVLinkInspectorController::_findVehicle(uint8_t id)
121{
122 for (int i = 0; i < _systems->count(); i++) {
123 QGCMAVLinkSystem *const system = qobject_cast<QGCMAVLinkSystem*>(_systems->get(i));
124 if (system && (system->id() == id)) {
125 return system;
126 }
127 }
128
129 return nullptr;
130}
131
132void MAVLinkInspectorController::_refreshFrequency()
133{
134 for (int i = 0; i < _systems->count(); i++) {
135 QGCMAVLinkSystem *const system = qobject_cast<QGCMAVLinkSystem*>(_systems->get(i));
136 if (!system) {
137 continue;
138 }
139
140 for (int messageIndex = 0; messageIndex < system->messages()->count(); messageIndex++) {
141 QGCMAVLinkMessage *const msg = qobject_cast<QGCMAVLinkMessage*>(system->messages()->get(messageIndex));
142 if (msg) {
143 msg->updateFreq();
144 }
145 }
146 }
147}
148
149void MAVLinkInspectorController::_vehicleAdded(Vehicle *vehicle)
150{
151 QGCMAVLinkSystem *sys = _findVehicle(static_cast<uint8_t>(vehicle->id()));
152
153 if (sys) {
154 sys->messages()->clearAndDeleteContents();
155 } else {
156 sys = new QGCMAVLinkSystem(static_cast<uint8_t>(vehicle->id()), this);
157 _systems->append(sys);
158 _systemNames.append(tr("System %1").arg(vehicle->id()));
159
160 (void) connect(vehicle, &Vehicle::mavlinkMsgIntervalsChanged, sys, [sys](uint8_t compid, uint16_t msgId, int32_t rate) {
161 for (int i = 0; i < sys->messages()->count(); i++) {
162 QGCMAVLinkMessage *const msg = qobject_cast<QGCMAVLinkMessage*>(sys->messages()->get(i));
163 if ((msg->compId() == compid) && (msg->id() == msgId)) {
164 msg->setTargetRateHz(rate);
165 break;
166 }
167 }
168 });
169 }
170
171 emit systemsChanged();
172}
173
174void MAVLinkInspectorController::_vehicleRemoved(const Vehicle *vehicle)
175{
176 QGCMAVLinkSystem *const system = _findVehicle(static_cast<uint8_t>(vehicle->id()));
177 if (!system) {
178 return;
179 }
180
181 system->deleteLater();
182 (void) _systems->removeOne(system);
183
184 const QString systemName = tr("System %1").arg(vehicle->id());
185 (void) _systemNames.removeOne(systemName);
186
187 emit systemsChanged();
188}
189
190void MAVLinkInspectorController::_receiveMessage(LinkInterface *link, const mavlink_message_t &message)
191{
192 Q_UNUSED(link);
193
194 QGCMAVLinkMessage *msg = nullptr;
195 QGCMAVLinkSystem *system = _findVehicle(message.sysid);
196
197 if (!system) {
198 system = new QGCMAVLinkSystem(message.sysid, this);
199 _systems->append(system);
200 _systemNames.append(tr("System %1").arg(message.sysid));
201 emit systemsChanged();
202
203 if (!_activeSystem) {
204 _activeSystem = system;
205 emit activeSystemChanged();
206 }
207 } else {
208 msg = system->findMessage(message.msgid, message.compid);
209 }
210
211 if (!msg) {
212 msg = new QGCMAVLinkMessage(message, this);
213 system->append(msg);
214 } else {
215 msg->update(message);
216 }
217}
218
220{
221 QGCMAVLinkSystem *const system = _findVehicle(systemId);
222 if (system != _activeSystem) {
223 _activeSystem = system;
224 emit activeSystemChanged();
225 }
226}
227
229{
230 if (!_activeSystem) {
231 return;
232 }
233
234 MultiVehicleManager *const multiVehicleManager = MultiVehicleManager::instance();
235 if (!multiVehicleManager) {
236 return;
237 }
238
239 const uint8_t sysId = _selectedSystemID();
240 if (sysId == 0) {
241 return;
242 }
243
244 Vehicle *const vehicle = multiVehicleManager->getVehicleById(sysId);
245 if (!vehicle) {
246 return;
247 }
248
249 const QGCMAVLinkMessage *const msg = _activeSystem->selectedMsg();
250 if (!msg) {
251 return;
252 }
253
254 const uint8_t compId = _selectedComponentID();
255 if (compId == 0) {
256 return;
257 }
258
259 // TODO: Make QGCMAVLinkMessage a part of comm and use signals/slots for msg rate changes
260 vehicle->setMessageRate(compId, msg->id(), rate);
261}
262
263uint8_t MAVLinkInspectorController::_selectedSystemID() const
264{
265 return (_activeSystem ? _activeSystem->id() : 0);
266}
267
268uint8_t MAVLinkInspectorController::_selectedComponentID() const
269{
270 const QGCMAVLinkMessage *const msg = _activeSystem ? _activeSystem->selectedMsg() : nullptr;
271 return (msg ? msg->compId() : 0);
272}
struct __mavlink_message mavlink_message_t
#define QGC_LOGGING_CATEGORY(name, categoryStr)
The link interface defines the interface for all links used to communicate with the ground station ap...
MAVLink message inspector controller (provides the logic for UI display)
void setMessageInterval(int32_t rate) const
MAVLinkInspectorController(QObject *parent=nullptr)
void messageReceived(LinkInterface *link, const mavlink_message_t &message)
Message received and directly copied via signal.
static MAVLinkProtocol * instance()
void vehicleAdded(Vehicle *vehicle)
void vehicleRemoved(Vehicle *vehicle)
void activeVehicleChanged(Vehicle *activeVehicle)
void append(QObject *object)
Caller maintains responsibility for object ownership and deletion.
QObject * get(int index)
QObject * removeOne(const QObject *object) override final
int count() const override final
void clearAndDeleteContents() override final
Clears the list and calls deleteLater on each entry.
int id() const
Definition Vehicle.h:425
void setMessageRate(uint8_t compId, uint16_t msgId, int32_t rate)
Definition Vehicle.cc:4319
void mavlinkMsgIntervalsChanged(uint8_t compid, uint16_t msgId, int32_t rate)