Skip to content

Commit

Permalink
Merge pull request #82 from delft-hyperloop/scrutineering
Browse files Browse the repository at this point in the history
Scrutineering Days
  • Loading branch information
andtsa authored Jul 11, 2024
2 parents 1eebde8 + ce10fc1 commit 9bf7123
Show file tree
Hide file tree
Showing 53 changed files with 1,045 additions and 317 deletions.
1 change: 1 addition & 0 deletions app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ features = ["defmt"]

# Only for build.rs
[build-dependencies]
anyhow = "1.0.86"
regex = "1.10.3"
serde = { version = "1.0.197", features = ["derive"] }
toml = "0.8.11"
Expand Down
22 changes: 12 additions & 10 deletions app/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extern crate serde;
use std::env;
use std::fs;
use std::path::Path;

use anyhow::Result;
use goose_utils::check_ids;
use goose_utils::ip::configure_gs_ip;
use serde::Deserialize;
Expand Down Expand Up @@ -68,31 +68,31 @@ pub const DATATYPES_PATH: &str = "../config/datatypes.toml";
pub const COMMANDS_PATH: &str = "../config/commands.toml";
pub const EVENTS_PATH: &str = "../config/events.toml";

fn main() {
fn main() -> Result<()> {
// if cfg!(debug_assertions) {
// env::set_var("DEFMT_LOG", "trace");
// } else {
// env::set_var("DEFMT_LOG", "off");
// }

let out_dir = env::var("OUT_DIR").unwrap();
let out_dir = env::var("OUT_DIR")?;
let dest_path = Path::new(&out_dir).join("config.rs");

let ip_file = fs::read_to_string(CONFIG_PATH).unwrap();
let config: Config = toml::from_str(&ip_file).unwrap();
let ip_file = fs::read_to_string(CONFIG_PATH)?;
let config: Config = toml::from_str(&ip_file)?;

let mut content = String::from("//@generated\n");

let _ = check_ids(DATATYPES_PATH, COMMANDS_PATH, EVENTS_PATH);

content.push_str(&configure_ip(&config));
content.push_str(&configure_gs_ip(config.gs.ip, config.gs.port, config.gs.force));
content.push_str(&configure_gs_ip(config.gs.ip, config.gs.port, config.gs.force)?);
content.push_str(&configure_pod(&config));
content.push_str(&configure_internal(&config));
content.push_str(&goose_utils::commands::generate_commands(COMMANDS_PATH, true));
content.push_str(&goose_utils::datatypes::generate_datatypes(DATATYPES_PATH, false));
content.push_str(&goose_utils::events::generate_events(EVENTS_PATH, true));
content.push_str(&goose_utils::info::generate_info(CONFIG_PATH, false));
content.push_str(&goose_utils::commands::generate_commands(COMMANDS_PATH, true)?);
content.push_str(&goose_utils::datatypes::generate_datatypes(DATATYPES_PATH, false)?);
content.push_str(&goose_utils::events::generate_events(EVENTS_PATH, true)?);
content.push_str(&goose_utils::info::generate_info(CONFIG_PATH, false)?);
// content.push_str(&*can::main(&id_list));

fs::write(dest_path.clone(), content).unwrap_or_else(|e| {
Expand All @@ -107,6 +107,8 @@ fn main() {
println!("cargo:rustc-link-arg-bins=--nmagic");
println!("cargo:rustc-link-arg-bins=-Tlink.x");
println!("cargo:rustc-link-arg-bins=-Tdefmt.x");

Ok(())
}

fn configure_ip(config: &Config) -> String {
Expand Down
10 changes: 8 additions & 2 deletions app/src/core/communication/can.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ pub async fn can_receiving_handler(
// frame.header().format();
let id = id_as_value(frame.id());
#[cfg(debug_assertions)]
data_sender.send(Datapoint::new(Datatype::ReceivedCan, id as u64, bytes_to_u64(frame.data()))).await;
data_sender
.send(Datapoint::new(
Datatype::ReceivedCan,
id as u64,
bytes_to_u64(frame.data()),
))
.await;
#[cfg(debug_assertions)]
info!("[CAN ({})] received frame: id={:?} data={:?}", bus_nr, id, frame.data());
if DATA_IDS.contains(&id) {
Expand Down Expand Up @@ -90,7 +96,7 @@ pub async fn can_receiving_handler(
timestamp.as_ticks(),
)
.await;
} else if gfd_counter > 2 {
} else if gfd_counter > 2 && frame.data()[2] == 0xFE {
gfd_counter = 0;
can_sender
.send(
Expand Down
30 changes: 30 additions & 0 deletions app/src/core/communication/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use embassy_net::Stack;
use embassy_stm32::eth::generic_smi::GenericSMI;
use embassy_stm32::eth::Ethernet;
use embassy_stm32::peripherals::ETH;
use embassy_time::Instant;
use embassy_time::Timer;
use embedded_io_async::Write;
use heapless::Deque;
Expand All @@ -15,9 +16,13 @@ use crate::core::communication::Datapoint;
use crate::pconfig::embassy_socket_from_config;
use crate::Command;
use crate::DataReceiver;
use crate::DataSender;
use crate::Datatype;
use crate::Event;
use crate::EventSender;
use crate::COMMAND_HASH;
use crate::DATA_HASH;
use crate::EVENTS_HASH;
use crate::GS_IP_ADDRESS;
use crate::IP_TIMEOUT;
use crate::NETWORK_BUFFER_SIZE;
Expand All @@ -32,6 +37,7 @@ pub async fn tcp_connection_handler(
stack: &'static Stack<Ethernet<'static, ETH, GenericSMI>>,
event_sender: EventSender,
data_receiver: DataReceiver,
data_sender: DataSender,
) -> ! {
let mut last_valid_timestamp = embassy_time::Instant::now().as_millis();
// info!("------------------------------------------------ TCP Connection Handler Started! ------------------------------------------");
Expand Down Expand Up @@ -69,6 +75,15 @@ pub async fn tcp_connection_handler(
},
}
event_sender.send(Event::ConnectionEstablishedEvent).await;
data_sender
.send(Datapoint::new(Datatype::CommandHash, COMMAND_HASH, Instant::now().as_ticks()))
.await;
data_sender
.send(Datapoint::new(Datatype::DataHash, DATA_HASH, Instant::now().as_ticks()))
.await;
data_sender
.send(Datapoint::new(Datatype::EventsHash, EVENTS_HASH, Instant::now().as_ticks()))
.await;
// let mut connection = client.connect(gs_addr).await.unwrap();
// info!("----------------------------------------------------------------Connected to ground station==========================");

Expand Down Expand Up @@ -203,6 +218,11 @@ pub async fn tcp_connection_handler(
info!("[tcp] Start Run command received");
event_sender.send(Event::RunStarting).await;
},
Command::ContinueRun(_) => {
#[cfg(debug_assertions)]
info!("[tcp] Start Run command received");
event_sender.send(Event::ContinueRunEvent).await;
},
Command::Shutdown(_) => {
#[cfg(debug_assertions)]
info!("[tcp] Shutdown command received");
Expand All @@ -219,6 +239,16 @@ pub async fn tcp_connection_handler(
event_sender.send(Event::TurnOffHVCommand).await;
// TODO: no turn off HV exists??
},
Command::DcOn(_) => {
#[cfg(debug_assertions)]
info!("[tcp] DcOn command received");
event_sender.send(Event::DcOn).await;
},
Command::DcOff(_) => {
#[cfg(debug_assertions)]
info!("[tcp] DcOff command received");
event_sender.send(Event::DcOff).await;
},
Command::EmitEvent(e) => {
#[cfg(debug_assertions)]
info!("[tcp] EmitEvent command received");
Expand Down
5 changes: 4 additions & 1 deletion app/src/core/controllers/breaking_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl BrakingController {
pf12: peripherals::PF12,
adc: Adc<'static, ADC1>,
_pb0: peripherals::PB0,
_pd5: peripherals::PD5,
pd5: peripherals::PD5,
_ptime: TIM16,
) -> Self {
info!("breaking controller started");
Expand Down Expand Up @@ -181,6 +181,9 @@ impl BrakingController {
let braking_signal = Output::new(pb8, Level::High, Speed::Low);
// pwm.enable(Channel::Ch1);

// VGA ground
let _ = Output::new(pd5, Level::Low, Speed::Low);

try_spawn!(
braking_sender,
x.spawn(control_braking_heartbeat(braking_sender, data_sender, braking_signal,))
Expand Down
7 changes: 6 additions & 1 deletion app/src/core/controllers/ethernet_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use static_cell::StaticCell;
use crate::core::communication::tcp::tcp_connection_handler;
use crate::try_spawn;
use crate::DataReceiver;
use crate::DataSender;
use crate::EventSender;
use crate::Irqs;
use crate::POD_MAC_ADDRESS;
Expand Down Expand Up @@ -47,6 +48,7 @@ impl EthernetController {
x: Spawner,
sender: EventSender,
receiver: DataReceiver,
data_sender: DataSender,
pins: EthernetPins,
) -> Self {
let mut rng = Rng::new(pins.p_rng, Irqs);
Expand Down Expand Up @@ -100,7 +102,10 @@ impl EthernetController {

try_spawn!(sender, x.spawn(net_task(stack)));

try_spawn!(sender, x.spawn(tcp_connection_handler(x, stack, sender, receiver)));
try_spawn!(
sender,
x.spawn(tcp_connection_handler(x, stack, sender, receiver, data_sender))
);
// unwrap!(x.spawn(udp_connection_handler(stack)));

ethernet_controller
Expand Down
8 changes: 6 additions & 2 deletions app/src/core/controllers/finite_state_machine_peripherals.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use defmt::debug;
use embassy_executor::Spawner;
use embassy_stm32::adc::Adc;
use embassy_stm32::gpio::Input;
use embassy_stm32::gpio::Level;
use embassy_stm32::gpio::Output;
use embassy_stm32::gpio::Pull;
use embassy_stm32::gpio::Speed;
use embassy_stm32::Peripherals;

Expand Down Expand Up @@ -33,8 +35,8 @@ pub struct FSMPeripherals {
impl FSMPeripherals {
// pub fn new(p : Peripherals, x: &Spawner, q : &PriorityChannel<NoopRawMutex, Event, Max, 16>) -> Self {
pub async fn new(p: Peripherals, x: &Spawner, i: InternalMessaging) -> Self {
// let mut init = PInit{p,x,q};
// let (braking_controller, init) = BrakingController::new(init);
// set to high impedance, since there's a 24V signal being given and this would fry the PCB
let _ = Input::new(p.PD4, Pull::None);

// The braking controller is responsible for rearming the braked
let braking_controller = BrakingController::new(
Expand Down Expand Up @@ -65,6 +67,7 @@ impl FSMPeripherals {
*x,
i.event_sender,
i.data_receiver,
i.data_sender,
EthernetPins {
p_rng: p.RNG,
eth_pin: p.ETH,
Expand Down Expand Up @@ -130,6 +133,7 @@ impl FSMPeripherals {
pin_4: Output::new(p.PD3, Level::Low, Speed::Low),
pin_6: Output::new(p.PG9, Level::Low, Speed::Low),
pin_7: Output::new(p.PG10, Level::Low, Speed::Low),
dc_dc: Output::new(p.PD2, Level::Low, Speed::Low),
},
red_led: Output::new(p.PB14, Level::Low, Speed::High),
propulsion_controller: PropulsionController::new(
Expand Down
1 change: 1 addition & 0 deletions app/src/core/controllers/hv_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub struct HVPeripherals {
pub pin_4: Output<'static>,
pub pin_6: Output<'static>,
pub pin_7: Output<'static>,
pub dc_dc: Output<'static>,
}

impl HVPeripherals {
Expand Down
32 changes: 28 additions & 4 deletions app/src/core/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
//! This module is the middle man between the data producers and the ground station.
mod batteries;

mod sources;
use crate::Datatype;
use crate::ValueCheckResult;
use crate::DataReceiver;
use crate::DataSender;
use crate::Event;
use crate::EventSender;

use crate::Datapoint;
/// ## Individual handling of datapoints
/// A lot of the subsystems on the pod use their own "encoding" for data.
/// In order to make a reasonable matching between semantic meaning of
Expand All @@ -24,11 +26,33 @@ pub async fn data_middle_step(
let data = incoming.receive().await;

// 1. check thresholds
if data.datatype.check_bounds(data.value) {
event_sender.send(Event::ValueOutOfBounds).await;
match data.datatype.check_bounds(data.value) {
ValueCheckResult::Fine => {},
ValueCheckResult::Warn => {
outgoing.send(value_warning(data.datatype, data.value)).await;
}
ValueCheckResult::Error => {
outgoing.send(value_error(data.datatype, data.value)).await;
}
ValueCheckResult::BrakeNow => {
event_sender.send(Event::ValueOutOfBounds).await;
outgoing.send(value_critical(data.datatype, data.value)).await;
}
}
// 2. check specific data types

outgoing.send(data).await;
}
}

fn value_warning(dt: Datatype, v: u64) -> Datapoint {
Datapoint::new(Datatype::ValueWarning, dt.to_id() as u64, v)
}

fn value_error(dt: Datatype, v: u64) -> Datapoint {
Datapoint::new(Datatype::ValueError, dt.to_id() as u64, v)
}

fn value_critical(dt: Datatype, v: u64) -> Datapoint {
Datapoint::new(Datatype::ValueCausedBraking, dt.to_id() as u64, v)
}
43 changes: 43 additions & 0 deletions app/src/core/data/sources.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#![allow(unused)]

use crate::Datatype;

pub enum Subsystems {
GroundStation,
SensorHub,
Propulsion,
Batteries,
Levitation,
}

pub const GROUND_STATION_DATA: [Datatype; 1] = [Datatype::ResponseHeartbeat];

pub const SENSOR_HUB_DATA: [Datatype; 9] = [
Datatype::Acceleration,
Datatype::Velocity,
Datatype::Localisation,
Datatype::AccelerationX,
Datatype::AccelerationY,
Datatype::AccelerationZ,
Datatype::GyroscopeX,
Datatype::GyroscopeY,
Datatype::GyroscopeZ,
];

pub const PROPULSION_DATA: [Datatype; 3] =
[Datatype::PropulsionSpeed, Datatype::PropulsionVoltage, Datatype::PropulsionCurrent];

pub const HV_BMS_DATA: [Datatype; 3] = [
Datatype::BatteryBalanceHigh,
Datatype::BatteryCurrentHigh,
Datatype::BatteryEstimatedChargeHigh,
];

pub const LV_BMS_DATA: [Datatype; 6] = [
Datatype::BatteryBalanceLow,
Datatype::BatteryCurrentLow,
Datatype::BatteryEstimatedChargeLow,
Datatype::BatteryVoltageLow,
Datatype::BatteryTemperatureLow,
Datatype::ChargeStateLow,
];
Loading

0 comments on commit 9bf7123

Please sign in to comment.