QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
SequenceState.cc
Go to the documentation of this file.
1#include "SequenceState.h"
3
4SequenceState::SequenceState(const QString& stateName, QState* parent)
5 : QGCState(stateName, parent)
6{
7}
8
9void SequenceState::addStep(const QString& name, StepAction action)
10{
11 _steps.append({name, std::move(action)});
12}
13
14void SequenceState::addSteps(const QList<Step>& steps)
15{
16 _steps.append(steps);
17}
18
20{
21 if (_currentStep >= 0 && _currentStep < _steps.size()) {
22 return _steps[_currentStep].name;
23 }
24 return QString();
25}
26
28{
29 _currentStep = -1;
30 _failedStep.clear();
31
32 // Start executing steps
33 QMetaObject::invokeMethod(this, &SequenceState::_executeNextStep, Qt::QueuedConnection);
34}
35
36void SequenceState::_executeNextStep()
37{
38 _currentStep++;
39
40 if (_currentStep >= _steps.size()) {
41 // All steps completed successfully
42 qCDebug(QGCStateMachineLog) << stateName() << "sequence completed successfully";
43 emit advance();
44 return;
45 }
46
47 const Step& step = _steps[_currentStep];
48 qCDebug(QGCStateMachineLog) << stateName() << "executing step" << _currentStep << ":" << step.name;
49
50 bool success = true;
51 if (step.action) {
52 success = step.action();
53 }
54
55 if (success) {
56 emit stepCompleted(step.name, _currentStep);
57 // Schedule next step
58 QMetaObject::invokeMethod(this, &SequenceState::_executeNextStep, Qt::QueuedConnection);
59 } else {
60 _failedStep = step.name;
61 qCDebug(QGCStateMachineLog) << stateName() << "step failed:" << step.name;
62 emit error();
63 }
64}
QString stateName() const
void stepCompleted(const QString &stepName, int index)
Emitted after each step completes successfully.
std::function< bool()> StepAction
Step action - returns true to continue, false to stop with error.
void addStep(const QString &name, StepAction action)
SequenceState(const QString &stateName, QState *parent)
QString currentStepName() const
Get the current step name.
void addSteps(const QList< Step > &steps)
Add multiple steps at once.
void onEnter() override
Override to perform actions on state entry.