Skip to content

Commit

Permalink
STM - States and Transitions (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
DH254 authored and licornes-fluos committed Jan 27, 2024
1 parent 69ae888 commit 5f12b42
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 102 deletions.
24 changes: 0 additions & 24 deletions lib/state_machine/message.hpp

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
113 changes: 60 additions & 53 deletions lib/state_machine/state_machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,68 +8,75 @@
#include <unordered_map>

#include <boost/unordered_map.hpp>
#include <core/types.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 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}};
const std::unordered_map<std::string, State> string_to_state_
= {{"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<SourceAndTarget, State, source_and_target_hash> transition_to_state_
= {{{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
15 changes: 7 additions & 8 deletions lib/state_machine/types.hpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
#pragma once

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

#include <cstdint>

#include <boost/functional/hash/hash.hpp>

namespace hyped::state_machine {
struct SourceAndMessage {
struct SourceAndTarget {
State source;
Message message;
State target;

bool operator==(const SourceAndMessage &key) const
bool operator==(const SourceAndTarget &key) const
{
return key.source == source && key.message == message;
return key.source == source && key.target == target;
}
};

struct source_and_message_hash {
std::size_t operator()(SourceAndMessage const &key) const
struct source_and_target_hash {
std::size_t operator()(SourceAndTarget const &key) const
{
std::size_t seed = 0;
boost::hash_combine(seed, key.source);
boost::hash_combine(seed, key.message);
boost::hash_combine(seed, key.target);
return seed;
}
};
Expand Down
Loading

0 comments on commit 5f12b42

Please sign in to comment.