STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
StateMachine.hpp
Go to the documentation of this file.
1
#pragma once
2
3
#include <cstdint>
4
#include <map>
5
#include <set>
6
#include <string>
7
#include <vector>
8
9
namespace
sequencer
{
10
11
/**
12
* System state IDs — must match the TypeScript SystemState enum in web-gui/shared/types.ts.
13
*/
14
enum class
State
: uint8_t {
15
DEBUG
= 0,
16
IDLE
= 1,
17
ARMED
= 2,
18
FUEL_FILL
= 3,
19
OX_FILL
= 4,
20
GN2_LOW_PRESS
= 5,
21
GN2_VENT
= 6,
22
FUEL_PRESS
= 7,
23
FUEL_VENT
= 8,
24
OX_PRESS
= 9,
25
OX_VENT
= 10,
26
GN2_HIGH_PRESS
= 11,
27
GN2_HIGH_VENT
= 12,
28
VENT
= 13,
29
CALIBRATE
= 14,
30
READY
= 15,
31
FIRE
= 16,
32
ENGINE_ABORT
= 17,
33
GSE_ABORT
= 18,
34
EMERGENCY_ABORT
= 19,
35
PRESS_STANDBY
= 20,
36
UNKNOWN
= 255,
37
};
38
39
/**
40
* Parses state_transitions.csv and validates state transitions.
41
* Thread-safe for concurrent reads after load().
42
*/
43
class
StateMachine
{
44
public
:
45
/**
46
* Load from a state_transitions.csv file.
47
* Tries the given path, then a set of fallback paths.
48
* @return true on success.
49
*/
50
bool
load
(
const
std::string& csv_path);
51
52
/**
53
* Returns true if transitioning from→to is allowed.
54
* In debug mode, always returns true.
55
*/
56
bool
isAllowed
(
State
from,
State
to)
const
;
57
58
/**
59
* Returns the list of states reachable from the given state.
60
* In debug mode, returns all known states.
61
*/
62
std::vector<State>
allowedFrom
(
State
state
)
const
;
63
64
/**
65
* Returns a bitmask where bit N is set if state N is reachable from the given state.
66
* Used for the [0x50, 0x00] Elodin VTable payload.
67
*/
68
uint32_t
allowedBitmask
(
State
state
)
const
;
69
70
/**
71
* Adopt the [[states]] entries from a config as the source of truth for state names and
72
* semantics.
73
*
74
* This is an OVERRIDE LAYER, deliberately: anything the config does not mention keeps its
75
* built-in name and behaviour. The state list was duplicated in eight places across C++, the
76
* shared TS types, the backend and six frontend label maps, several already disagreeing (the
77
* top bar rendered GSE Abort and Emergency Abort as "STATE 18"/"STATE 19"). Config becomes the
78
* one place to edit, without a partial or missing [[states]] being able to erase the machine's
79
* knowledge of its own states.
80
*
81
* `id` is a stable key, not an ordering: it is written into Elodin run history as a raw u8
82
* with no name table beside it, so an id must keep meaning the same state. Reordering,
83
* renaming, adding and removing are all fine; reusing an id for a different state is not.
84
*/
85
static
void
loadStatesFromConfig
(
const
std::string& config_content);
86
87
/** True if the state carries the abort semantics (config `is_abort`, else the built-in trio).
88
*/
89
static
bool
isAbort
(
State
s);
90
91
/** The state to boot into (config `is_boot`, else Idle). */
92
static
State
bootState
();
93
94
/** Translate a CSV name (e.g. "Emergency Abort") to a State. */
95
static
State
fromName
(
const
std::string&
name
);
96
97
/** Human-readable name for a State (e.g. State::FIRE → "Fire"). */
98
static
std::string
name
(
State
s);
99
/** Numeric id for a state name, or 255 if unknown. Lets a service that only needs the id
100
* (e.g. the controller's PWM gate) avoid carrying its own copy of the enum. */
101
static
uint8_t
stateId
(
const
std::string&
name
) {
102
State
s =
fromName
(
name
);
103
return
static_cast<
uint8_t
>
(s);
104
}
105
106
bool
isLoaded
()
const
{
107
return
loaded_
;
108
}
109
110
private
:
111
std::map<State, std::set<State>>
transitions_
;
112
bool
loaded_
{
false
};
113
114
static
const
std::map<std::string, State>&
csvStateMap
();
115
};
116
117
}
// namespace sequencer
sequencer::StateMachine
Parses state_transitions.csv and validates state transitions.
Definition
StateMachine.hpp:43
sequencer::StateMachine::load
bool load(const std::string &csv_path)
Load from a state_transitions.csv file.
Definition
StateMachine.cpp:222
sequencer::StateMachine::allowedFrom
std::vector< State > allowedFrom(State state) const
Returns the list of states reachable from the given state.
Definition
StateMachine.cpp:315
sequencer::StateMachine::loaded_
bool loaded_
Definition
StateMachine.hpp:112
sequencer::StateMachine::stateId
static uint8_t stateId(const std::string &name)
Numeric id for a state name, or 255 if unknown.
Definition
StateMachine.hpp:101
sequencer::StateMachine::fromName
static State fromName(const std::string &name)
Translate a CSV name (e.g.
Definition
StateMachine.cpp:184
sequencer::StateMachine::name
static std::string name(State s)
Human-readable name for a State (e.g.
Definition
StateMachine.cpp:128
sequencer::StateMachine::csvStateMap
static const std::map< std::string, State > & csvStateMap()
Definition
StateMachine.cpp:18
sequencer::StateMachine::allowedBitmask
uint32_t allowedBitmask(State state) const
Returns a bitmask where bit N is set if state N is reachable from the given state.
Definition
StateMachine.cpp:325
sequencer::StateMachine::isAllowed
bool isAllowed(State from, State to) const
Returns true if transitioning from→to is allowed.
Definition
StateMachine.cpp:308
sequencer::StateMachine::isLoaded
bool isLoaded() const
Definition
StateMachine.hpp:106
sequencer::StateMachine::bootState
static State bootState()
The state to boot into (config is_boot, else Idle).
Definition
StateMachine.cpp:109
sequencer::StateMachine::loadStatesFromConfig
static void loadStatesFromConfig(const std::string &config_content)
Adopt the [[states]] entries from a config as the source of truth for state names and semantics.
Definition
StateMachine.cpp:72
sequencer::StateMachine::isAbort
static bool isAbort(State s)
True if the state carries the abort semantics (config is_abort, else the built-in trio).
Definition
StateMachine.cpp:99
sequencer::StateMachine::transitions_
std::map< State, std::set< State > > transitions_
Definition
StateMachine.hpp:111
state
static ActuatorControllerState state
Definition
main.cpp:57
IDLE
@ IDLE
Definition
gnss_frame.c:6
sequencer
Definition
AbortBroadcaster.hpp:11
sequencer::State
State
System state IDs — must match the TypeScript SystemState enum in web-gui/shared/types....
Definition
StateMachine.hpp:14
sequencer::State::PRESS_STANDBY
@ PRESS_STANDBY
Definition
StateMachine.hpp:35
sequencer::State::READY
@ READY
Definition
StateMachine.hpp:30
sequencer::State::EMERGENCY_ABORT
@ EMERGENCY_ABORT
Definition
StateMachine.hpp:34
sequencer::State::OX_PRESS
@ OX_PRESS
Definition
StateMachine.hpp:24
sequencer::State::OX_VENT
@ OX_VENT
Definition
StateMachine.hpp:25
sequencer::State::ENGINE_ABORT
@ ENGINE_ABORT
Definition
StateMachine.hpp:32
sequencer::State::UNKNOWN
@ UNKNOWN
Definition
StateMachine.hpp:36
sequencer::State::VENT
@ VENT
Definition
StateMachine.hpp:28
sequencer::State::GN2_HIGH_PRESS
@ GN2_HIGH_PRESS
Definition
StateMachine.hpp:26
sequencer::State::FUEL_VENT
@ FUEL_VENT
Definition
StateMachine.hpp:23
sequencer::State::CALIBRATE
@ CALIBRATE
Definition
StateMachine.hpp:29
sequencer::State::GN2_LOW_PRESS
@ GN2_LOW_PRESS
Definition
StateMachine.hpp:20
sequencer::State::GSE_ABORT
@ GSE_ABORT
Definition
StateMachine.hpp:33
sequencer::State::FIRE
@ FIRE
Definition
StateMachine.hpp:31
sequencer::State::OX_FILL
@ OX_FILL
Definition
StateMachine.hpp:19
sequencer::State::FUEL_PRESS
@ FUEL_PRESS
Definition
StateMachine.hpp:22
sequencer::State::GN2_VENT
@ GN2_VENT
Definition
StateMachine.hpp:21
sequencer::State::FUEL_FILL
@ FUEL_FILL
Definition
StateMachine.hpp:18
sequencer::State::DEBUG
@ DEBUG
Definition
StateMachine.hpp:15
sequencer::State::GN2_HIGH_VENT
@ GN2_HIGH_VENT
Definition
StateMachine.hpp:27
sequencer::State::ARMED
@ ARMED
Definition
StateMachine.hpp:17
daq-server
diablo_server
lib
include
control
StateMachine.hpp
Generated by
1.15.0