-
Notifications
You must be signed in to change notification settings - Fork 2
Feat/state machine callbacks #91
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5f22c25
feat: add function callbacks to state machine and always use changeSt…
Elan456 8013aca
feat: replace vector with a array of fixed size
Elan456 0c9cd9d
ref: rename fn to functionptr
Elan456 20add44
Merge branch 'main' of https://github.com/CURocketEngineering/Avionic…
Elan456 60d0d47
fix: add a default state handle to StateMachine and BurnoutStateMachine
Elan456 6044c65
fix: remove extra descent branch and just use a default branch
Elan456 a08403a
test: add base state machine unit tests
Elan456 8a340d5
fix: add cstdint header inclusion, also use comments to indicate 'sof…
Elan456 99e4326
fix: check LD before FLD to remove double state change in one update …
Elan456 7745011
docs: fix spelling mistake for necessarily
Elan456 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,30 @@ | ||
| #ifndef FLIGHT_STATES_H | ||
| #define FLIGHT_STATES_H | ||
|
|
||
| #include <cstdint> // For uint8_t | ||
|
|
||
| // Not all states are used by all state machines | ||
| // Order matters, systems rely on comparisons like `current_state` > STATE_ASCENT to know if the | ||
| // flight has passed the ascent state and is now falling. So states are ordered from earliest to latest. | ||
| // The literal value of these states should never be used because a new state can be added | ||
| // in between existing states, shifting their values. | ||
| // in between existing states, shifting their values. B/c we tag the version of Avionics used for each flight, | ||
| // we can come back here to see the numerical values of each state when decoding the logs. The ground station | ||
| // keeps YAMLs of these state definitions every time they are changed. To avoid having a bunch of YAMLs we try | ||
| // to keep these values semi-stable, which is why it includes states that aren't currently used to be forward-compatible. | ||
| // Keep in mind that there is a difference between states and events. For example, | ||
| // apogee is an event that causes a transition from STATE_ASCENT to STATE_DESCENT, but it is not a state itself. | ||
| // The state machines are in charge of detecting these transitions and updating the states. | ||
| enum FlightState { | ||
| STATE_UNARMED, // 0x00 | ||
| STATE_ARMED, // 0x01 | ||
| STATE_SOFT_ASCENT, // 0x02 | ||
| STATE_ASCENT, // 0x03 Don't use the ascent state if you are already using powered ascent and coast ascent | ||
| STATE_POWERED_ASCENT, // 0x04 | ||
| STATE_COAST_ASCENT, // 0x05 | ||
| STATE_DESCENT, // 0x06 | ||
| STATE_DROGUE_DEPLOYED, // 0x07 | ||
| STATE_MAIN_DEPLOYED, // 0x08 | ||
| STATE_LANDED, // 0x09 | ||
| enum FlightState : uint8_t { | ||
| STATE_UNARMED = 0, // 0 This may not necessarily always be the 0th state, or the first state. A state could be added before it. | ||
| STATE_ARMED, // 1 | ||
| STATE_SOFT_ASCENT, // 2 | ||
| STATE_ASCENT, // 3 Don't use the ascent state if you are already using powered ascent and coast ascent | ||
| STATE_POWERED_ASCENT, // 4 | ||
| STATE_COAST_ASCENT, // 5 | ||
| STATE_DESCENT, // 6 | ||
| STATE_DROGUE_DEPLOYED, // 7 | ||
| STATE_MAIN_DEPLOYED, // 8 | ||
| STATE_LANDED, // 9 | ||
| }; | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #include "state_estimation/BaseStateMachine.h" | ||
|
|
||
| BaseStateMachine::BaseStateMachine(FlightState initialState) : state_(initialState) {} | ||
|
|
||
| uint8_t BaseStateMachine::getState() const { | ||
| return static_cast<uint8_t>(state_); | ||
| } | ||
|
|
||
| bool BaseStateMachine::registerOnStateEntry(FlightState targetState, StateEntryCallback functionPtr) { | ||
| if (functionPtr == nullptr) { | ||
| return false; | ||
| } | ||
|
|
||
| // Searching for a duplicate registration | ||
| for (std::size_t i = 0; i < callbackCount_; i++) { | ||
| const StateCallbackRegistration& registration = onStateEntryCallbacks_[i]; | ||
| if (registration.state == targetState && registration.functionPtr == functionPtr) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| // Checking if we have room for another callback | ||
| if (callbackCount_ >= kMaxStateEntryCallbacks) { | ||
| return false; | ||
| } | ||
|
|
||
| // Register the new callback | ||
| onStateEntryCallbacks_[callbackCount_] = {targetState, functionPtr}; | ||
| callbackCount_++; | ||
| return true; | ||
| } | ||
|
|
||
| bool BaseStateMachine::changeState(FlightState newState) { | ||
| if (state_ == newState) { | ||
| return false; | ||
| } | ||
|
|
||
|
Elan456 marked this conversation as resolved.
|
||
| state_ = newState; | ||
|
|
||
| // Calling the registered callbacks for the new state | ||
| for (std::size_t i = 0; i < callbackCount_; i++) { | ||
| const StateCallbackRegistration& registration = onStateEntryCallbacks_[i]; | ||
| if (registration.state == state_) { | ||
| registration.functionPtr(); | ||
| } | ||
| } | ||
|
Elan456 marked this conversation as resolved.
|
||
|
|
||
| return true; | ||
| } | ||
|
|
||
| FlightState BaseStateMachine::getFlightState() const { | ||
| return state_; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.