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

STM - States and Transitions #23

Merged
merged 11 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
24 changes: 0 additions & 24 deletions lib/state_machine/message.hpp
TomLonergan03 marked this conversation as resolved.
Outdated
Show resolved Hide resolved

This file was deleted.

24 changes: 13 additions & 11 deletions lib/state_machine/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ namespace hyped::state_machine {

enum class State {
kIdle,
kCalibrating,
kCalibrate,
kPrecharge,
kReadyForLevitation,
kBeginLevitation,
kReady,
kAccelerating,
kCruising,
kMotorBraking,
kPreFrictionBraking,
kFrictionBraking,
kPreFrictionBrakingFail,
kFrictionBrakingFail,
kFailureBraking,
kAccelerate,
kLimBrake,
kFrictionBrake,
kStopLevitation,
kStopped,
kFailureStopped,
kOff,
kBatteryRecharge,
kCapacitorDischarge,
kFailureBrake,
kFailure,
kSafe,
};

} // namespace hyped::state_machine
18 changes: 14 additions & 4 deletions lib/state_machine/state_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,29 @@ StateMachine::StateMachine() : current_state_{State::kIdle}
{
}

Message StateMachine::stringToMessage(const std::string &message_name)
State StateMachine::stringToState(const std::string &state_name)
{
return string_to_message_.at(message_name);
return string_to_state_.at(state_name);
}

std::string StateMachine::messageToString(const Message &message)
std::string StateMachine::stateToString(const State &state)
{
return message_to_string_.at(message);
return state_to_string_.at(state);
}

State StateMachine::getCurrentState()
{
return current_state_;
}

core::Result StateMachine::handleTransition(const State &state)
{
const auto next_state = transition_to_state_.find({current_state_, state});
if (next_state != transition_to_state_.end()) {
current_state_ = next_state->second;
return core::Result::kSuccess;
}
return core::Result::kFailure;
}

} // namespace hyped::state_machine
111 changes: 59 additions & 52 deletions lib/state_machine/state_machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,69 +7,76 @@
#include <queue>
#include <unordered_map>

#include "core/types.hpp"
DylanCavers marked this conversation as resolved.
Show resolved Hide resolved
DH254 marked this conversation as resolved.
Show resolved Hide resolved
#include <boost/unordered_map.hpp>

namespace hyped::state_machine {

class StateMachine {
public:
StateMachine();
Message stringToMessage(const std::string &message_name);
std::string messageToString(const Message &message);
State stringToState(const std::string &state_name);
std::string stateToString(const State &state);
State getCurrentState();
core::Result handleTransition(const State &state);

private:
const std::unordered_map<std::string, Message> string_to_message_
= {{"kCalibrating", Message::kCalibrating},
{"kReady", Message::kReady},
{"kAccelerating", Message::kAccelerating},
{"kCruising", Message::kCruising},
{"kMotorBrake", Message::kMotorBrake},
{"kPreFrictionBrake", Message::kPreFrictionBrake},
{"kFrictionBrake", Message::kFrictionBrake},
{"kStopped", Message::kStopped},
{"kFailureBrake", Message::kFailureBrake},
{"kPreFrictionBrakeFail", Message::kPreFrictionBrakeFail},
{"kFrictionBrakeFail", Message::kFrictionBrakeFail},
{"kOff", Message::kOff},
{"kFailureStopped", Message::kFailureStopped},
{"kFailureOff", Message::kFailureOff}};
const std::unordered_map<Message, std::string> message_to_string_
= {{Message::kCalibrating, "kCalibrating"},
{Message::kReady, "kReady"},
{Message::kAccelerating, "kAccelerating"},
{Message::kCruising, "kCruising"},
{Message::kMotorBrake, "kMotorBrake"},
{Message::kPreFrictionBrake, "kPreFrictionBrake"},
{Message::kFrictionBrake, "kFrictionBrake"},
{Message::kStopped, "kStopped"},
{Message::kFailureBrake, "kFailureBrake"},
{Message::kPreFrictionBrakeFail, "kPreFrictionBrakeFail"},
{Message::kFrictionBrakeFail, "kFrictionBrakeFail"},
{Message::kOff, "kOff"},
{Message::kFailureStopped, "kFailureStopped"},
{Message::kFailureOff, "kFailureOff"}};
const std::unordered_map<std::string, State> string_to_state_
DH254 marked this conversation as resolved.
Show resolved Hide resolved
= {{"kCalibrate", State::kCalibrate},
{"kPrecharge", State::kPrecharge},
{"kReadyForLeviation", State::kReadyForLevitation},
{"kBeginLevitation", State::kBeginLevitation},
{"kReady", State::kReady},
{"kAccelerate", State::kAccelerate},
{"kLimBrake", State::kLimBrake},
{"kFrictionBrake", State::kFrictionBrake},
{"kStopLevitation", State::kStopLevitation},
{"kStopped", State::kStopped},
{"kBatteryRecharge", State::kBatteryRecharge},
{"kCapacitorDischarge", State::kCapacitorDischarge},
{"kFailureBrake", State::kFailureBrake},
{"kFailure", State::kFailure},
{"kSafe", State::kSafe}};
const std::unordered_map<State, std::string> state_to_string_
= {{State::kCalibrate, "kCalibrate"},
{State::kPrecharge, "kPrecharge"},
{State::kReadyForLevitation, "kReadyForLevitation"},
{State::kBeginLevitation, "kBeginLevitation"},
{State::kReady, "kReady"},
{State::kAccelerate, "kAccelerate"},
{State::kLimBrake, "kLimBrake"},
{State::kFrictionBrake, "kFrictionBrake"},
{State::kStopLevitation, "kStopLevitation"},
{State::kStopped, "kStopped"},
{State::kBatteryRecharge, "kBatteryRecharge"},
{State::kCapacitorDischarge, "kCapacitorDischarge"},
{State::kFailureBrake, "kFailureBrake"},
{State::kFailure, "kFailure"},
{State::kSafe, "kSafe"}};
const boost::unordered_map<SourceAndMessage, State, source_and_message_hash> transition_to_state_
= {{{State::kIdle, Message::kCalibrating}, State::kCalibrating},
{{State::kCalibrating, Message::kReady}, State::kReady},
{{State::kReady, Message::kAccelerating}, State::kAccelerating},
{{State::kAccelerating, Message::kCruising}, State::kCruising},
{{State::kCruising, Message::kMotorBrake}, State::kMotorBraking},
{{State::kMotorBraking, Message::kPreFrictionBrake}, State::kPreFrictionBraking},
{{State::kPreFrictionBraking, Message::kFrictionBrake}, State::kFrictionBraking},
{{State::kFrictionBraking, Message::kStopped}, State::kStopped},
{{State::kStopped, Message::kOff}, State::kOff},
{{State::kAccelerating, Message::kFailureBrake}, State::kFailureBraking},
{{State::kAccelerating, Message::kPreFrictionBrakeFail}, State::kPreFrictionBrakingFail},
{{State::kPreFrictionBrakingFail, Message::kFrictionBrakeFail}, State::kFrictionBrakingFail},
{{State::kCruising, Message::kFailureBrake}, State::kFailureBraking},
{{State::kCruising, Message::kPreFrictionBrakeFail}, State::kPreFrictionBrakingFail},
{{State::kMotorBraking, Message::kFailureBrake}, State::kFailureBraking},
{{State::kMotorBraking, Message::kPreFrictionBrakeFail}, State::kPreFrictionBrakingFail},
{{State::kFrictionBrakingFail, Message::kFailureBrake}, State::kFailureBraking},
{{State::kFrictionBrakingFail, Message::kFailureStopped}, State::kFailureStopped},
{{State::kFailureBraking, Message::kFailureStopped}, State::kFailureStopped},
{{State::kFailureStopped, Message::kFailureOff}, State::kOff}};
= {{{State::kIdle, State::kCalibrate}, State::kCalibrate},
{{State::kIdle, State::kFailure}, State::kFailure},
{{State::kCalibrate, State::kPrecharge}, State::kPrecharge},
{{State::kCalibrate, State::kFailure}, State::kFailure},
{{State::kPrecharge, State::kReadyForLevitation}, State::kReadyForLevitation},
{{State::kPrecharge, State::kFailure}, State::kFailure},
{{State::kReadyForLevitation, State::kBeginLevitation}, State::kBeginLevitation},
{{State::kReadyForLevitation, State::kFailure}, State::kFailure},
{{State::kBeginLevitation, State::kReady}, State::kReady},
{{State::kBeginLevitation, State::kFailure}, State::kFailure},
{{State::kReady, State::kAccelerate}, State::kAccelerate},
{{State::kAccelerate, State::kLimBrake}, State::kLimBrake},
{{State::kAccelerate, State::kFailureBrake}, State::kFailureBrake},
{{State::kLimBrake, State::kFrictionBrake}, State::kFrictionBrake},
{{State::kLimBrake, State::kFailureBrake}, State::kFailureBrake},
{{State::kFrictionBrake, State::kStopLevitation}, State::kStopLevitation},
{{State::kFrictionBrake, State::kFailureBrake}, State::kFailureBrake},
{{State::kStopLevitation, State::kStopped}, State::kStopped},
{{State::kStopped, State::kBatteryRecharge}, State::kBatteryRecharge},
{{State::kBatteryRecharge, State::kCapacitorDischarge}, State::kCapacitorDischarge},
{{State::kFailureBrake, State::kCapacitorDischarge}, State::kCapacitorDischarge},
{{State::kFailure, State::kCapacitorDischarge}, State::kCapacitorDischarge},
{{State::kCapacitorDischarge, State::kSafe}, State::kSafe}};

State current_state_;
};
Expand Down
3 changes: 1 addition & 2 deletions lib/state_machine/types.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include "message.hpp"
#include "state.hpp"

#include <cstdint>
Expand All @@ -10,7 +9,7 @@
namespace hyped::state_machine {
struct SourceAndMessage {
State source;
Message message;
State message;
DH254 marked this conversation as resolved.
Show resolved Hide resolved

bool operator==(const SourceAndMessage &key) const
{
Expand Down
Loading
Loading