feat(state-via-can): state transitions via can are forced to resync state across devices

This commit is contained in:
CarlWachter 2024-06-17 08:54:36 +02:00
parent e03f624c5b
commit d75bce6691
5 changed files with 31 additions and 10 deletions

View File

@ -42,10 +42,11 @@ namespace sta
* @param from The start we want to transition from. * @param from The start we want to transition from.
* @param to The state we want to transition to. * @param to The state we want to transition to.
* @param lockout An optional timer blocking state transition for a given time. * @param lockout An optional timer blocking state transition for a given time.
* @param force If true, the state transition will be executed regardless of the current state.
* *
* @ingroup tacos_api * @ingroup tacos_api
*/ */
void setState(uint32_t from, uint32_t to, uint32_t lockout = 0); void setState(uint32_t from, uint32_t to, uint32_t lockout = 0, bool force = false);
/** /**
* @brief Request a state transition after a given time has passed. Invalid state transitions will be dismissed. * @brief Request a state transition after a given time has passed. Invalid state transitions will be dismissed.

View File

@ -162,8 +162,9 @@ namespace sta
* @param from The state which we want to leave. This is used to filter out obsolete transitions. * @param from The state which we want to leave. This is used to filter out obsolete transitions.
* @param to The state to transition to. * @param to The state to transition to.
* @param lockout The minimum number of milliseconds we expect to stay in this state. This is used to block premature transitions. * @param lockout The minimum number of milliseconds we expect to stay in this state. This is used to block premature transitions.
* @param force If true, the state transition will be executed regardless of the current state.
*/ */
void requestStateTransition(uint32_t from, uint32_t to, uint32_t lockout = 0); void requestStateTransition(uint32_t from, uint32_t to, uint32_t lockout = 0, bool force = false);
/** /**
* @brief Request a state transition after a given time has passed. * @brief Request a state transition after a given time has passed.

View File

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

View File

@ -64,7 +64,7 @@ namespace sta
return currentState_; return currentState_;
} }
void Statemachine::requestStateTransition(uint32_t from, uint32_t to, uint32_t lockout /* = 0 */) void Statemachine::requestStateTransition(uint32_t from, uint32_t to, uint32_t lockout /* = 0 */, bool force /* = 0 */)
{ {
StateTransition transition; StateTransition transition;
transition.from = from; transition.from = from;
@ -72,9 +72,28 @@ namespace sta
transition.event = EventFlags::NORMAL; transition.event = EventFlags::NORMAL;
transition.lockout = lockout; transition.lockout = lockout;
// Try to add a state transition request to the queue. Don't wait if another if (force){
// thread is already requesting a state change. // Perform the transition and notify the threads. The event flags are set
queue_.put(transition, 0); // here in order to allow threads to react immediately.
currentState_ = transition.to;
Statemachine::stateChangeEvent.set(transition.event);
Statemachine::stateChangeEvent.clear(EventFlags::ALL);
if (failsafeTimer_.isRunning())
{
failsafeTimer_.stop();
}
// Start the lockout timer if requested.
if (transition.lockout != 0)
{
setLockoutTimer(transition.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);
}
} }
void Statemachine::requestTimedStateTransition(uint32_t from, uint32_t to, uint32_t millis, uint32_t lockout /* = 0 */) void Statemachine::requestTimedStateTransition(uint32_t from, uint32_t to, uint32_t millis, uint32_t lockout /* = 0 */)

View File

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