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

INFRA - Inital State Machine Implementation (HYPE-22) #18

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions lib/state_machine/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "hyped_state_machine"
version = "0.1.0"
edition = "2021"

[features]
std = []

[dependencies]
heapless = "0.8.0"
hyped_core = { path = "../core" }
rust-mqtt = { version = "0.3.0", default-features = false, features = ["defmt"] }
embassy-net = { version = "0.4.0", default-features = false, features = ["defmt", "tcp", "proto-ipv4", "medium-ip"] }
DylanCavers marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions lib/state_machine/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub mod state_machine;
pub mod types;
63 changes: 63 additions & 0 deletions lib/state_machine/src/state_machine.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use crate::types::{SourceAndTarget, State};
use embassy_net::tcp::TcpSocket;
use heapless::FnvIndexMap;
use hyped_core::mqtt::HypedMqttClient;
use rust_mqtt::utils::rng_generator::CountingRng;

pub struct StateMachine<'a> {
pub(crate) current_state: State,
pub(crate) transition_map: FnvIndexMap<SourceAndTarget, State, 32>,
pub(crate) mqtt_client: HypedMqttClient<'a, TcpSocket<'a>, CountingRng>,
}

impl<'a> StateMachine<'a> {
pub fn new(mqtt_client: HypedMqttClient<'a, TcpSocket<'a>, CountingRng>) -> Self {
let mut transition_map = FnvIndexMap::<SourceAndTarget, State, 32>::new();

// this is not a very nice way to do this, but it's a start (move out into a function consruct_transition_map)
DylanCavers marked this conversation as resolved.
Show resolved Hide resolved
let _ = transition_map.insert(
SourceAndTarget {
source: State::Idle,
target: State::Calibrate,
},
State::Calibrate,
);

StateMachine {
current_state: State::Idle,
transition_map,
mqtt_client,
}
}

pub fn handle_transition(&mut self, to_state: &State) {
let to_from_state = SourceAndTarget {
source: self.current_state,
target: *to_state,
};
if let Some(&new_state) = self.transition_map.get(&to_from_state) {
self.current_state = new_state;
}
}

pub async fn run(&mut self) {
self.mqtt_client.subscribe("stm").await;

while self.current_state != State::Shutdown {
let state = self.current_state.to_string();
self.publish_state("stm", state.as_bytes()).await;

let new_state = self.consume_state().await;
self.handle_transition(&new_state);
}
}

pub async fn publish_state(&mut self, topic: &str, payload: &[u8]) {
self.mqtt_client.send_message(topic, payload, true).await;
davidbeechey marked this conversation as resolved.
Show resolved Hide resolved
}

pub async fn consume_state(&mut self) -> State {
let new_state = self.mqtt_client.receive_message().await.unwrap();
State::from_string(new_state.1).unwrap()
}
}
79 changes: 79 additions & 0 deletions lib/state_machine/src/types.rs
DylanCavers marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use core::str::FromStr;
use heapless::String;

#[derive(Hash, PartialEq, Eq, Clone, Copy)]
pub enum State {
Idle,
Calibrate,
Precharge,
ReadyForLevitation,
BeginLevitation,
Levitating,
Ready,
Accelerate,
LimBrake,
FrictionBrake,
StopLevitation,
Stopped,
BatteryRecharge,
CapacitorDischarge,
FailureBrake,
Failure,
Safe,
Shutdown,
}

impl State {
pub fn to_string(&self) -> String<20> {
match self {
State::Idle => String::<20>::from_str("idle").unwrap(),
State::Calibrate => String::<20>::from_str("calibrate").unwrap(),
State::Precharge => String::<20>::from_str("precharge").unwrap(),
State::ReadyForLevitation => String::<20>::from_str("ready_for_levitation").unwrap(),
State::BeginLevitation => String::<20>::from_str("begin_levitation").unwrap(),
State::Levitating => String::<20>::from_str("levitating").unwrap(),
State::Ready => String::<20>::from_str("ready").unwrap(),
State::Accelerate => String::<20>::from_str("accelerate").unwrap(),
State::LimBrake => String::<20>::from_str("lim_brake").unwrap(),
State::FrictionBrake => String::<20>::from_str("friction_brake").unwrap(),
State::StopLevitation => String::<20>::from_str("stop_levitation").unwrap(),
State::Stopped => String::<20>::from_str("stopped").unwrap(),
State::BatteryRecharge => String::<20>::from_str("battery_recharge").unwrap(),
State::CapacitorDischarge => String::<20>::from_str("capacitor_discharge").unwrap(),
State::FailureBrake => String::<20>::from_str("failure_brake").unwrap(),
State::Failure => String::<20>::from_str("failure").unwrap(),
State::Safe => String::<20>::from_str("safe").unwrap(),
State::Shutdown => String::<20>::from_str("shutdown").unwrap(),
}
}

pub fn from_string(state: &str) -> Option<State> {
match state {
"idle" => Some(State::Idle),
"calibrate" => Some(State::Calibrate),
"precharge" => Some(State::Precharge),
"ready_for_levitation" => Some(State::ReadyForLevitation),
"begin_levitation" => Some(State::BeginLevitation),
"levitating" => Some(State::Levitating),
"ready" => Some(State::Ready),
"accelerate" => Some(State::Accelerate),
"lim_brake" => Some(State::LimBrake),
"friction_brake" => Some(State::FrictionBrake),
"stop_levitation" => Some(State::StopLevitation),
"stopped" => Some(State::Stopped),
"battery_recharge" => Some(State::BatteryRecharge),
"capacitor_discharge" => Some(State::CapacitorDischarge),
"failure_brake" => Some(State::FailureBrake),
"failure" => Some(State::Failure),
"safe" => Some(State::Safe),
"shutdown" => Some(State::Shutdown),
_ => None,
}
}
}

#[derive(Hash, PartialEq, Eq)]
pub struct SourceAndTarget {
pub(crate) source: State,
pub(crate) target: State,
}