QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
MAVLinkStreamConfig.cc
Go to the documentation of this file.
2#include "MAVLinkLib.h"
4
5QGC_LOGGING_CATEGORY(MAVLinkStreamConfigLog, "MAVLink.MAVLinkStreamConfig")
6
8 : _messageIntervalCb(messageIntervalCb)
9{
10 // qCDebug(MAVLinkStreamConfigLog) << Q_FUNC_INFO << this;
11}
12
14{
15 // qCDebug(MAVLinkStreamConfigLog) << Q_FUNC_INFO << this;
16}
17
19{
20 static constexpr int requestedRate = static_cast<int>(1000000.0 / 100.0); // 100 Hz in usecs (better set this a bit higher than actually needed,
21 // to give it more priority in case of exceeding link bandwidth)
22
23 _nextDesiredRates = QList<DesiredStreamRate>{{
24 {MAVLINK_MSG_ID_ATTITUDE_QUATERNION, requestedRate},
25 {MAVLINK_MSG_ID_ATTITUDE_TARGET, requestedRate},
26 }};
27 // we could check if we're already configured as requested
28
29 setNextState(State::Configuring);
30}
31
33{
34 static constexpr int requestedRate = static_cast<int>(1000000.0 / 100.0);
35 _nextDesiredRates = QList<DesiredStreamRate>{{
36 {MAVLINK_MSG_ID_LOCAL_POSITION_NED, requestedRate},
37 {MAVLINK_MSG_ID_POSITION_TARGET_LOCAL_NED, requestedRate},
38 }};
39 setNextState(State::Configuring);
40}
41
43{
44 static constexpr int requestedRate = static_cast<int>(1000000.0 / 100.0);
45 _nextDesiredRates = QList<DesiredStreamRate>{{
46 {MAVLINK_MSG_ID_NAV_CONTROLLER_OUTPUT, requestedRate},
47 {MAVLINK_MSG_ID_VFR_HUD, requestedRate},
48 }};
49 setNextState(State::Configuring);
50}
51
53{
54 switch (_state) {
55 case State::Configuring:
56 nextDesiredRate();
57 break;
58 case State::RestoringDefaults:
59 restoreNextDefault();
60 break;
61 default:
62 break;
63 }
64}
65
66void MAVLinkStreamConfig::setNextState(State state)
67{
68 _nextState = state;
69 if (_state == State::Idle) {
70 // first restore defaults no matter what the next state is
71 _state = State::RestoringDefaults;
72 restoreNextDefault();
73 }
74}
75
76void MAVLinkStreamConfig::nextDesiredRate()
77{
78 if (_nextState != State::Idle) { // allow state to be interrupted by a new request
79 _desiredRates.clear();
80 _state = State::RestoringDefaults;
81 restoreNextDefault();
82 return;
83 }
84
85 if (_desiredRates.empty()) {
86 _state = State::Idle;
87 return;
88 }
89
90 const DesiredStreamRate &rate = _desiredRates.last();
91 _changedIds.push_back(rate.messageId);
92 _messageIntervalCb(rate.messageId, rate.rate);
93 _desiredRates.pop_back();
94}
95
97{
98 setNextState(State::RestoringDefaults);
99}
100
101void MAVLinkStreamConfig::restoreNextDefault()
102{
103 if (_changedIds.empty()) {
104 // do we have a pending request?
105 switch (_nextState) {
106 case State::Configuring:
107 _state = _nextState;
108 _desiredRates = _nextDesiredRates;
109 _nextDesiredRates.clear();
110 _nextState = State::Idle;
111 nextDesiredRate();
112 break;
113 case State::RestoringDefaults:
114 case State::Idle:
115 _nextState = State::Idle; // nothing to do, we just finished restoring
116 _state = State::Idle;
117 break;
118 }
119 return;
120 }
121
122 const int id = _changedIds.last();
123 _messageIntervalCb(id, 0); // restore the default rate
124 _changedIds.pop_back();
125}
#define QGC_LOGGING_CATEGORY(name, categoryStr)
std::function< void(int messageId, int rate)> SetMessageIntervalCb