Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct StateMachine Inheritance #12250

Merged
merged 1 commit into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 33 additions & 29 deletions src/Utilities/StateMachine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,58 @@
****************************************************************************/

#include "StateMachine.h"
#include "QGCLoggingCategory.h"

StateMachine::StateMachine(void)
QGC_LOGGING_CATEGORY(StateMachineLog, "qgc.utilities.statemachine");

StateMachine::StateMachine(QObject *parent)
: QObject(parent)
{
// qCDebug(StateMachineLog) << Q_FUNC_INFO << this;
}

StateMachine::~StateMachine()
{
// qCDebug(StateMachineLog) << Q_FUNC_INFO << this;
}

void StateMachine::start(void)
void StateMachine::start()
{
_active = true;
advance();
}

void StateMachine::advance(void)
void StateMachine::advance()
{
if (_active) {
_stateIndex++;
if (_stateIndex < stateCount()) {
(*rgStates()[_stateIndex])(this);
} else {
_active = false;
statesCompleted();
}
if (!_active) {
return;
}
}

void StateMachine::move(StateFn stateFn)
{
if (_active) {
for (int i=0; i<stateCount(); i++) {
if (rgStates()[i] == stateFn) {
_stateIndex = i;
(*rgStates()[_stateIndex])(this);
break;
}
}
_stateIndex++;
if (_stateIndex < stateCount()) {
(*rgStates()[_stateIndex])(this);
} else {
_active = false;
statesCompleted();
}
}

void StateMachine::statesCompleted(void) const
void StateMachine::move(StateFn stateFn)
{
if (!_active) {
return;
}

for (int i = 0; i < stateCount(); i++) {
if (rgStates()[i] == stateFn) {
_stateIndex = i;
(*rgStates()[_stateIndex])(this);
break;
}
}
}

StateMachine::StateFn StateMachine::currentState(void) const
StateMachine::StateFn StateMachine::currentState() const
{
if (_active) {
return rgStates()[_stateIndex];
} else {
return nullptr;
}
return (_active ? rgStates()[_stateIndex] : nullptr);
}
24 changes: 14 additions & 10 deletions src/Utilities/StateMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,44 @@

#pragma once

#include <QtCore/QLoggingCategory>
#include <QtCore/QObject>

Q_DECLARE_LOGGING_CATEGORY(StateMachineLog)

class StateMachine : public QObject
{
Q_OBJECT

public:
typedef void (*StateFn)(StateMachine* stateMachine);
StateMachine(QObject *parent = nullptr);
~StateMachine();

StateMachine(void);
typedef void (*StateFn)(StateMachine *stateMachine);

/// Start the state machine with the first step
void start(void);
void start();

/// Advance the state machine to the next state and call the state function
virtual void advance(void);
virtual void advance();

/// Move the state machine to the specified state and call the state function
void move(StateFn stateFn);

StateFn currentState(void) const;
StateFn currentState() const;

/// @return The number of states in the rgStates array
virtual int stateCount(void) const = 0;
virtual int stateCount() const = 0;

/// @return Array of states to execute
virtual const StateFn* rgStates(void) const = 0;
virtual const StateFn *rgStates() const = 0;

/// Called when all states have completed
virtual void statesCompleted(void) const;
virtual void statesCompleted() const {};

bool active() const { return _active; }

protected:
bool _active = false;
int _stateIndex = -1;
bool _active = false;
int _stateIndex = -1;
};
26 changes: 20 additions & 6 deletions src/Vehicle/Components/ComponentInformationManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,29 @@

#include <QtCore/QStandardPaths>

QGC_LOGGING_CATEGORY(ComponentInformationManagerLog, "ComponentInformationManagerLog")
QGC_LOGGING_CATEGORY(ComponentInformationManagerLog, "qgc.vehicle.components.componentinformationmanager")

ComponentInformationManager::ComponentInformationManager(Vehicle* vehicle)
: _vehicle (vehicle)
, _requestTypeStateMachine (this)
ComponentInformationManager::ComponentInformationManager(Vehicle *vehicle, QObject *parent)
: StateMachine(parent)
, _vehicle(vehicle)
, _requestTypeStateMachine(this, this)
, _cachedFileDownload(new QGCCachedFileDownload(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QLatin1String("/QGCCompInfoFileDownloadCache"), this))
, _fileCache(ComponentInformationCache::defaultInstance())
, _translation(new ComponentInformationTranslation(this, _cachedFileDownload))
{
// qCDebug(ComponentInformationManagerLog) << Q_FUNC_INFO << this;

_compInfoMap[MAV_COMP_ID_AUTOPILOT1][COMP_METADATA_TYPE_GENERAL] = new CompInfoGeneral (MAV_COMP_ID_AUTOPILOT1, vehicle, this);
_compInfoMap[MAV_COMP_ID_AUTOPILOT1][COMP_METADATA_TYPE_PARAMETER] = new CompInfoParam (MAV_COMP_ID_AUTOPILOT1, vehicle, this);
_compInfoMap[MAV_COMP_ID_AUTOPILOT1][COMP_METADATA_TYPE_EVENTS] = new CompInfoEvents (MAV_COMP_ID_AUTOPILOT1, vehicle, this);
_compInfoMap[MAV_COMP_ID_AUTOPILOT1][COMP_METADATA_TYPE_ACTUATORS] = new CompInfoActuators (MAV_COMP_ID_AUTOPILOT1, vehicle, this);
}

ComponentInformationManager::~ComponentInformationManager()
{
// qCDebug(ComponentInformationManagerLog) << Q_FUNC_INFO << this;
}

int ComponentInformationManager::stateCount(void) const
{
return _cStates;
Expand Down Expand Up @@ -165,10 +173,16 @@ QString ComponentInformationManager::_getFileCacheTag(int compInfoType, uint32_t
}


RequestMetaDataTypeStateMachine::RequestMetaDataTypeStateMachine(ComponentInformationManager* compMgr)
: _compMgr(compMgr)
RequestMetaDataTypeStateMachine::RequestMetaDataTypeStateMachine(ComponentInformationManager *compMgr, QObject *parent)
: StateMachine(parent)
, _compMgr(compMgr)
{
// qCDebug(RequestMetaDataTypeStateMachineLog) << Q_FUNC_INFO << this;
}

RequestMetaDataTypeStateMachine::~RequestMetaDataTypeStateMachine()
{
// qCDebug(RequestMetaDataTypeStateMachineLog) << Q_FUNC_INFO << this;
}

void RequestMetaDataTypeStateMachine::request(CompInfo* compInfo)
Expand Down
29 changes: 16 additions & 13 deletions src/Vehicle/Components/ComponentInformationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <QtCore/QElapsedTimer>
#include <QtCore/QLoggingCategory>

Q_DECLARE_LOGGING_CATEGORY(RequestMetaDataTypeStateMachineLog)
Q_DECLARE_LOGGING_CATEGORY(ComponentInformationManagerLog)

class Vehicle;
Expand All @@ -31,7 +32,8 @@ class RequestMetaDataTypeStateMachine : public StateMachine
Q_OBJECT

public:
RequestMetaDataTypeStateMachine(ComponentInformationManager* compMgr);
RequestMetaDataTypeStateMachine(ComponentInformationManager *compMgr, QObject *parent = nullptr);
~RequestMetaDataTypeStateMachine();

void request (CompInfo* compInfo);
QString typeToString(void);
Expand Down Expand Up @@ -93,28 +95,29 @@ class ComponentInformationManager : public StateMachine
Q_OBJECT

public:
static constexpr int cachedFileMaxAgeSec = 3 * 24 * 3600; ///< 3 days

ComponentInformationManager(Vehicle* vehicle);
ComponentInformationManager(Vehicle *vehicle, QObject *parent = nullptr);
~ComponentInformationManager();

typedef void (*RequestAllCompleteFn)(void* requestAllCompleteFnData);
typedef void (*RequestAllCompleteFn)(void *requestAllCompleteFnData);

void requestAllComponentInformation (RequestAllCompleteFn requestAllCompletFn, void * requestAllCompleteFnData);
Vehicle* vehicle (void) { return _vehicle; }
CompInfoParam* compInfoParam (uint8_t compId);
CompInfoGeneral* compInfoGeneral (uint8_t compId);
void requestAllComponentInformation(RequestAllCompleteFn requestAllCompletFn, void * requestAllCompleteFnData);
Vehicle *vehicle() { return _vehicle; }
CompInfoParam *compInfoParam(uint8_t compId);
CompInfoGeneral *compInfoGeneral(uint8_t compId);

// Overrides from StateMachine
int stateCount (void) const final;
const StateFn* rgStates (void) const final;
int stateCount() const final;
const StateFn *rgStates() const final;

ComponentInformationCache& fileCache() { return _fileCache; }
ComponentInformationTranslation* translation() { return _translation; }
ComponentInformationCache &fileCache() { return _fileCache; }
ComponentInformationTranslation *translation() { return _translation; }

float progress() const;

void advance() override;

static constexpr int cachedFileMaxAgeSec = 3 * 24 * 3600; ///< 3 days

signals:
void progressUpdate(float progress);

Expand Down
17 changes: 12 additions & 5 deletions src/Vehicle/InitialConnectStateMachine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,25 @@
#include "RallyPointManager.h"
#include "QGCLoggingCategory.h"

QGC_LOGGING_CATEGORY(InitialConnectStateMachineLog, "InitialConnectStateMachineLog")
QGC_LOGGING_CATEGORY(InitialConnectStateMachineLog, "qgc.vehicle.initialconnectstatemachine")

InitialConnectStateMachine::InitialConnectStateMachine(Vehicle* vehicle)
: _vehicle(vehicle)
InitialConnectStateMachine::InitialConnectStateMachine(Vehicle *vehicle, QObject *parent)
: StateMachine(parent)
, _vehicle(vehicle)
{
static_assert(sizeof(_rgStates)/sizeof(_rgStates[0]) == sizeof(_rgProgressWeights)/sizeof(_rgProgressWeights[0]),
"array size mismatch");
static_assert(std::size(_rgStates) == std::size(_rgProgressWeights), "array size mismatch");

_progressWeightTotal = 0;
for (int i = 0; i < _cStates; ++i) {
_progressWeightTotal += _rgProgressWeights[i];
}

// qCDebug(InitialConnectStateMachineLog) << Q_FUNC_INFO << this;
}

InitialConnectStateMachine::~InitialConnectStateMachine()
{
// qCDebug(InitialConnectStateMachineLog) << Q_FUNC_INFO << this;
}

int InitialConnectStateMachine::stateCount(void) const
Expand Down
5 changes: 2 additions & 3 deletions src/Vehicle/InitialConnectStateMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@

Q_DECLARE_LOGGING_CATEGORY(InitialConnectStateMachineLog)

class Vehicle;

class InitialConnectStateMachine : public StateMachine
{
Q_OBJECT

public:
InitialConnectStateMachine(Vehicle* vehicle);
InitialConnectStateMachine(Vehicle *vehicle, QObject *parent = nullptr);
~InitialConnectStateMachine();

// Overrides from StateMachine
int stateCount (void) const final;
Expand Down
4 changes: 2 additions & 2 deletions src/Vehicle/Vehicle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ void Vehicle::_commonInit()
connect(_missionManager, &MissionManager::newMissionItemsAvailable, _trajectoryPoints, &TrajectoryPoints::clear);

_standardModes = new StandardModes (this, this);
_componentInformationManager = new ComponentInformationManager (this);
_initialConnectStateMachine = new InitialConnectStateMachine (this);
_componentInformationManager = new ComponentInformationManager (this, this);
_initialConnectStateMachine = new InitialConnectStateMachine (this, this);
_ftpManager = new FTPManager (this);

_vehicleLinkManager = new VehicleLinkManager (this);
Expand Down
1 change: 1 addition & 0 deletions test/qgcunittest/UnitTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ void UnitTest::init()
{
_initCalled = true;

MultiVehicleManager::instance()->init();
LinkManager::instance()->setConnectionsAllowed();

// Force offline vehicle back to defaults
Expand Down
Loading