QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
FallbackChainState.cc
Go to the documentation of this file.
3
4FallbackChainState::FallbackChainState(const QString& stateName, QState* parent)
5 : QGCState(stateName, parent)
6{
7}
8
9void FallbackChainState::addStrategy(const QString& name, Strategy action)
10{
11 _strategies.append({name, std::move(action)});
12}
13
15{
16 _currentIndex = -1;
17 _successfulStrategy.clear();
18
19 // Start trying strategies
20 QMetaObject::invokeMethod(this, &FallbackChainState::_tryNextStrategy, Qt::QueuedConnection);
21}
22
23void FallbackChainState::_tryNextStrategy()
24{
25 _currentIndex++;
26
27 if (_currentIndex >= _strategies.size()) {
28 // All strategies failed
29 qCDebug(QGCStateMachineLog) << stateName() << "all strategies exhausted";
30 emit error();
31 return;
32 }
33
34 const StrategyEntry& entry = _strategies[_currentIndex];
35
36 qCDebug(QGCStateMachineLog) << stateName() << "trying strategy" << entry.name
37 << "(" << (_currentIndex + 1) << "of" << _strategies.size() << ")";
38 emit tryingStrategy(entry.name, _currentIndex, _strategies.size());
39
40 bool success = false;
41 if (entry.action) {
42 success = entry.action();
43 }
44
45 if (success) {
46 qCDebug(QGCStateMachineLog) << stateName() << "strategy succeeded:" << entry.name;
47 _successfulStrategy = entry.name;
48 emit strategySucceeded(entry.name);
49 emit advance();
50 } else {
51 qCDebug(QGCStateMachineLog) << stateName() << "strategy failed:" << entry.name;
52 emit strategyFailed(entry.name);
53 // Try next strategy (deferred to allow signal processing)
54 QMetaObject::invokeMethod(this, &FallbackChainState::_tryNextStrategy, Qt::QueuedConnection);
55 }
56}
std::function< bool()> Strategy
Strategy action - returns true on success.
void onEnter() override
Override to perform actions on state entry.
FallbackChainState(const QString &stateName, QState *parent)
void strategyFailed(const QString &name)
Emitted when a strategy fails and moving to next.
void tryingStrategy(const QString &name, int index, int total)
Emitted when trying a strategy.
void strategySucceeded(const QString &name)
Emitted when a strategy succeeds.
void addStrategy(const QString &name, Strategy action)
QString stateName() const