cleanup: separate functions for requesting and forcing state transitions, fix publishing behavior

This commit is contained in:
CarlWachter
2024-11-03 13:42:25 +01:00
parent 9f4d99191d
commit ca3f303293
5 changed files with 62 additions and 22 deletions

View File

@@ -160,7 +160,7 @@ namespace sta
STA_ASSERT(header.payloadLength == 2);
// First byte of payload is the origin state, second byte is the destination state. Transition is forced
tacos::setState(payload[0], payload[1], 0, true);
tacos::forceState(payload[0], payload[1], 0, true);
return true;
}

View File

@@ -44,8 +44,11 @@ namespace sta
STA_ASSERT(transition.to < STA_TACOS_NUM_STATES);
#ifdef STA_TACOS_CAN_BUS_ENABLED
// Publish the state via CAN bus.
tacos::publishState(transition.from, transition.to, 0);
if (transition.publish)
{
// Publish the state via CAN bus.
tacos::publishState(transition.from, transition.to, 0);
}
#endif // STA_TACOS_CAN_BUS_ENABLED
// Perform the transition and notify the threads. The event flags are set
@@ -84,7 +87,7 @@ namespace sta
return currentState_;
}
void Statemachine::requestStateTransition(uint32_t from, uint32_t to, uint32_t lockout /* = 0 */, bool force /* = 0 */, bool publish /* = true */)
void Statemachine::requestStateTransition(uint32_t from, uint32_t to, uint32_t lockout /* = 0 */, bool publish /* = true */)
{
StateTransition transition;
transition.from = from;
@@ -93,17 +96,28 @@ namespace sta
transition.lockout = lockout;
transition.publish = publish;
// Force the transition if requested, but only if the requested state is different from the current one.
if (force && transition.to != currentState_){
// Try to add a state transition request to the queue. Don't wait if another
// thread is already requesting a state change.
queue_.put(transition, 0);
}
void Statemachine::forceStateTransition(uint32_t from, uint32_t to, uint32_t lockout /* = 0 */, bool publish /* = true */)
{
// Force the transition, but only if the requested state is different from the current one.
if (to != currentState_){
// Perform the transition and notify the threads. The event flags are set
// here in order to allow threads to react immediately.
currentState_ = transition.to;
currentState_ = to;
#ifdef STA_TACOS_CAN_BUS_ENABLED
tacos::publishState(transition.from, transition.to, transition.lockout);
if (publish)
{
// Publish the state via CAN bus.
tacos::publishState(from, to, 0);
}
#endif // STA_TACOS_CAN_BUS_ENABLED
Statemachine::stateChangeEvent.set(transition.event);
Statemachine::stateChangeEvent.set(EventFlags::NORMAL);
Statemachine::stateChangeEvent.clear(EventFlags::ALL);
if (failsafeTimer_.isRunning())
@@ -112,14 +126,10 @@ namespace sta
}
// Start the lockout timer if requested.
if (transition.lockout != 0)
if (lockout != 0)
{
setLockoutTimer(transition.lockout);
setLockoutTimer(lockout);
}
} else {
// Try to add a state transition request to the queue. Don't wait if another
// thread is already requesting a state change.
queue_.put(transition, 0);
}
}
@@ -128,7 +138,7 @@ namespace sta
STA_ASSERT(to < STA_TACOS_NUM_STATES);
failsafeTimer_.setCallback([from, to, lockout, publish](void* arg) {
Statemachine::instance()->requestStateTransition(from, to, lockout, false, publish);
Statemachine::instance()->requestStateTransition(from, to, lockout, publish);
}, NULL);
failsafeTimer_.start(millis);

View File

@@ -16,9 +16,14 @@ namespace sta
return Statemachine::instance()->getCurrentState();
}
void setState(uint32_t from, uint32_t to, uint32_t lockout /* = 0 */, bool force /* = false */, bool publish /* = false */)
void requestState(uint32_t from, uint32_t to, uint32_t lockout /* = 0 */, bool publish /* = true */)
{
Statemachine::instance()->requestStateTransition(from, to, lockout, force, publish);
Statemachine::instance()->requestStateTransition(from, to, lockout, publish);
}
void forceState(uint32_t from, uint32_t to, uint32_t lockout /* = 0 */, bool publish /* = true */)
{
Statemachine::instance()->forceStateTransition(from, to, lockout, publish);
}
void setStateTimed(uint32_t from, uint32_t to, uint32_t millis, uint32_t lockout /* = 0 */, bool publish /* = false */)