Skip to content

Commit

Permalink
Merge pull request #106 from delft-hyperloop/demonstration
Browse files Browse the repository at this point in the history
prep
  • Loading branch information
Zakrok09 authored Jul 22, 2024
2 parents bbdc50b + 832b6b5 commit 7be4cd7
Show file tree
Hide file tree
Showing 32 changed files with 660 additions and 644 deletions.
8 changes: 7 additions & 1 deletion app/src/core/communication/comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ impl<I: HardwareLayer> CommunicationLayer for ExternalCommunicationHandler<I> {
debug!("[tcp] SetCurrentSpeed command received");
send_event(self.es, Event::SetCurrentSpeedCommand(x));
},
Command::EnablePropulsion(_) => {
send_event(self.es, Event::EnablePropulsionCommand);
},
Command::DisablePropulsion(_) => {
send_event(self.es, Event::DisablePropulsionCommand);
},
Command::StartRun(_) => {
debug!("[tcp] Start Run command received");
send_event(self.es, Event::RunStarting);
Expand All @@ -168,7 +174,7 @@ impl<I: HardwareLayer> CommunicationLayer for ExternalCommunicationHandler<I> {
},
Command::EndRun(_) => {
send_event(self.es, Event::ExitEvent);
}
},
Command::StartHV(_) => {
debug!("[tcp] StartHV command received");
send_event(self.es, Event::TurnOnHVCommand);
Expand Down
70 changes: 8 additions & 62 deletions app/src/core/communication/low/can.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
use defmt::*;
use defmt_rtt as _;
use embassy_executor::Spawner;
use embassy_stm32::can::CanRx;
use embassy_stm32::can::CanTx;
use embassy_stm32::can::Frame;
use embassy_stm32::can::Instance;
use embassy_time::Duration;
use embassy_time::Instant;
use embassy_time::Timer;
use panic_probe as _;

// use embedded_hal::can::Id;
use crate::core::communication::Datapoint;
use crate::core::controllers::battery_controller::ground_fault_detection_isolation_details;
use crate::core::controllers::battery_controller::ground_fault_detection_voltage_details;
use crate::core::controllers::can_controller::CanTwoUtils;
use crate::pconfig::bytes_to_u64;
use crate::pconfig::id_as_value;
use crate::pconfig::queue_event;
use crate::pconfig::send_event;
use crate::send_data;
use crate::try_spawn;
use crate::CanReceiver;
use crate::CanSender;
use crate::DataSender;
use crate::Datatype;
use crate::Event;
use crate::EventSender;
use crate::Info;
use crate::BATTERY_GFD_IDS;
use crate::DATA_IDS;
use crate::EVENT_IDS;
Expand All @@ -46,12 +38,9 @@ pub async fn can_transmitter(
}
}

static mut HV_LAST_RECEIVED: Instant = Instant::from_millis(0);
static mut LV_LAST_RECEIVED: Instant = Instant::from_millis(0);

#[embassy_executor::task(pool_size = 2)]
pub async fn can_receiving_handler(
x: Spawner,
// x: Spawner,
event_sender: EventSender,
can_sender: CanSender,
data_sender: DataSender,
Expand All @@ -62,9 +51,6 @@ pub async fn can_receiving_handler(
info!("[CAN] Ready for bus {:?}", bus_nr);
let mut error_counter = 0u64;
let mut gfd_counter = 0u64;
// if bus_nr == 2 {
// try_spawn!(event_sender, x.spawn(can_two_watchdog(event_sender, data_sender)));
// }
loop {
match bus.read().await {
Ok(envelope) => {
Expand All @@ -79,16 +65,10 @@ pub async fn can_receiving_handler(
ut.hv_controller
.bms_can_handle(id, frame.data(), data_sender, timestamp.as_ticks())
.await;
unsafe {
HV_LAST_RECEIVED = Instant::now();
}
} else if LV_IDS.contains(&id) {
ut.lv_controller
.bms_can_handle(id, frame.data(), data_sender, timestamp.as_ticks())
.await;
unsafe {
LV_LAST_RECEIVED = Instant::now();
}
} else if GFD_IDS.contains(&id) {
if id == Datatype::IMDVoltageDetails.to_id() {
ground_fault_detection_isolation_details(
Expand Down Expand Up @@ -136,12 +116,13 @@ pub async fn can_receiving_handler(
// since we are never supposed to change the speed through the can bus (and run config is the only event with an actual value), i want a magic number that i can filter out from the run config handler just to make sure the pod doesn't do something stupid
send_event(event_sender, Event::from_id(id, Some(69420)));
} else {
send_data!(
data_sender,
Datatype::UnknownCanId,
id as u64,
bytes_to_u64(frame.data())
);
Timer::after_micros(1).await;
// send_data!(
// data_sender,
// Datatype::UnknownCanId,
// id as u64,
// bytes_to_u64(frame.data())
// );
}
},
Err(e) => {
Expand All @@ -162,38 +143,3 @@ pub async fn can_receiving_handler(
Timer::after_micros(500).await;
}
}

#[embassy_executor::task]
pub async fn can_two_watchdog(event_sender: EventSender, data_sender: DataSender) {
Timer::after_secs(5).await;

send_data!(data_sender, Datatype::Info, Info::StartingCanWatchdog as u64);
warn!("Started Can Watchdog");
// let mut lv_trigger = true;
// let mut hv_trigger = true;
loop {
info!("test");
if unsafe { HV_LAST_RECEIVED.elapsed().as_ticks() > Duration::from_millis(2500).as_ticks() } {
// if lv_trigger {
queue_event(event_sender, Event::EmergencyBraking).await;
// lv_trigger = false;
send_data!(data_sender, Datatype::Info, Info::HvBmsTimedOut as u64);
warn!("HV BMS timed out");
}
// } else {
// lv_trigger = true;

if unsafe { LV_LAST_RECEIVED.elapsed() > Duration::from_millis(2500) } {
// if hv_trigger {
queue_event(event_sender, Event::EmergencyBraking).await;
// hv_trigger = false;
send_data!(data_sender, Datatype::Info, Info::LvBmsTimedOut as u64);
warn!("LV BMS timed out");
// }
} else {
// hv_trigger = true;
}

Timer::after_millis(500).await;
}
}
38 changes: 1 addition & 37 deletions app/src/core/controllers/battery_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,12 @@ use crate::pconfig::bytes_to_u64;
use crate::pconfig::queue_dp;
use crate::DataSender;
use crate::Datatype;
use crate::EventSender;
use crate::Info;

// #[allow(dead_code)]
pub struct BatteryController {
// sender: EventSender,
data_sender: DataSender,
// id: u16,
// temperature_threshold: u8,
// voltage_threshold: u16,
// current_threshold: u16,
// number_of_groups: u8,
high_voltage: bool,
single_cell_id: u16,
// receive_single_cell_id: bool,
// current_number_of_cells: usize,
module_buffer: [u64; 14],
temp_buffer: [u64; 112],
voltage_buffer: [u64; 112],
Expand All @@ -29,33 +19,12 @@ pub struct BatteryController {
}

impl BatteryController {
/// parameters needed:
/// - x: Spawner, to create the tasks that actually do stuff
/// - sender: EventSender, to send events to the FSM
/// - ?
pub fn new(
_sender: EventSender,
// temperature: u8,
// voltage: u16,
// current: u16,
// id: u16,
// number_of_groups: u8,
data_sender: DataSender,
high_voltage: bool,
) -> Self {
pub fn new(data_sender: DataSender, high_voltage: bool) -> Self {
// Initialise anything needed by the battery controller here
Self {
// sender,
// id,
// temperature_threshold: temperature,
// voltage_threshold: voltage,
// current_threshold: current,
// number_of_groups,
data_sender,
high_voltage,
single_cell_id: 0,
// receive_single_cell_id: true,
// current_number_of_cells: 0,
module_buffer: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
temp_buffer: [0; 112],
voltage_buffer: [0; 112],
Expand Down Expand Up @@ -136,11 +105,9 @@ impl BatteryController {
while i < 8 {
self.single_cell_id = i;
let a = &self.temp_buffer[(i * 14) as usize..((i + 1) * 14) as usize];
// Initialize a new fixed-size array
let mut temp: [u64; 14] = [0; 14];

temp.copy_from_slice(a);
// Copy the elements from the slice to the fixed-size array
self.module_buffer = temp;
self.send_module_temp(timestamp).await;
i += 1;
Expand Down Expand Up @@ -169,11 +136,9 @@ impl BatteryController {
while i < 8 {
self.single_cell_id = i;
let a = &self.voltage_buffer[(i * 14) as usize..((i + 1) * 14) as usize];
// Initialize a new fixed-size array
let mut temp: [u64; 14] = [0; 14];

temp.copy_from_slice(a);
// Copy the elements from the slice to the fixed-size array
self.module_buffer = temp;
self.send_module_voltage(timestamp).await;
i += 1;
Expand Down Expand Up @@ -204,7 +169,6 @@ impl BatteryController {
}

pub async fn event_bms(&mut self, data: &[u8], timestamp: u64) {
// let mut msg: u64 = 0;
let dt =
if self.high_voltage { Datatype::BatteryEventHigh } else { Datatype::BatteryEventLow };
queue_dp(self.data_sender, dt, bytes_to_u64(data), timestamp).await;
Expand Down
13 changes: 2 additions & 11 deletions app/src/core/controllers/can_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use embassy_stm32::peripherals::PD0;
use embassy_stm32::peripherals::PD1;
use panic_probe as _;

use crate::core::communication::low::can::{can_receiving_handler, can_two_watchdog};
use crate::core::communication::low::can::can_receiving_handler;
use crate::core::communication::low::can::can_transmitter;
use crate::core::controllers::battery_controller::BatteryController;
use crate::core::controllers::battery_controller::GroundFaultDetection;
Expand Down Expand Up @@ -76,19 +76,11 @@ impl CanController {
c1_tx.write(&can::frame::Frame::new_standard(0x123, &[1, 2, 3, 4]).unwrap()).await;
try_spawn!(
event_sender,
x.spawn(can_receiving_handler(
x,
event_sender,
_can_one_sender,
data_sender,
c1_rx,
None
))
x.spawn(can_receiving_handler(event_sender, _can_one_sender, data_sender, c1_rx, None))
);
try_spawn!(
event_sender,
x.spawn(can_receiving_handler(
x,
event_sender,
can_two_sender,
data_sender,
Expand All @@ -101,7 +93,6 @@ impl CanController {
})
))
);
try_spawn!(event_sender, x.spawn(can_two_watchdog(event_sender, data_sender)));
try_spawn!(event_sender, x.spawn(can_transmitter(can_one_receiver, c1_tx)));
try_spawn!(event_sender, x.spawn(can_transmitter(can_two_receiver, c2_tx)));

Expand Down
5 changes: 2 additions & 3 deletions app/src/core/controllers/finite_state_machine_peripherals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ 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 {
// 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);
Expand All @@ -57,9 +56,9 @@ impl FSMPeripherals {

debug!("creating hv controller");
// the battery controllers contain functions related to interpreting the `BMS CAN` messages
let hv_controller = BatteryController::new(i.event_sender, i.data_sender, true);
let hv_controller = BatteryController::new(i.data_sender, true);
debug!("creating lv controller");
let lv_controller = BatteryController::new(i.event_sender, i.data_sender, false);
let lv_controller = BatteryController::new(i.data_sender, false);

debug!("creating ethernet controller");
// The ethernet controller configures IP and then spawns the ethernet task
Expand Down
34 changes: 13 additions & 21 deletions app/src/core/controllers/hv_controller.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use defmt::info;
use embassy_stm32::gpio::Output;
use embassy_time::Duration;
use embassy_time::Instant;
Expand All @@ -14,30 +13,23 @@ pub struct HVPeripherals {
pub dc_dc: Output<'static>,
pub pre_charge_start: Instant,
pub pre_charge_min: Duration,
// pub pre_charge_timeout: Duration,
// pub event_sender: EventSender,
pub pre_charge_successful: bool,
}

impl HVPeripherals {
pub async fn power_on_hv_procedure(&mut self) {
self.pin_6.set_high();
#[cfg(debug_assertions)]
info!("just set pin 6 high");
self.pin_7.set_high();
#[cfg(debug_assertions)]
info!("just set pin 7 high");
Timer::after_millis(5000).await;
self.pin_7.set_low();
#[cfg(debug_assertions)]
info!("just set pin 7 low");
self.pin_4.set_high();
#[cfg(debug_assertions)]
info!("just set pin 4 high");
#[cfg(debug_assertions)]
info!("HV Powered on");
info!("HV Powered on");
}
// pub async fn power_on_hv_procedure(&mut self) {
// self.pin_6.set_high();
// info!("just set pin 6 high");
// self.pin_7.set_high();
// info!("just set pin 7 high");
// Timer::after_millis(5000).await;
// self.pin_7.set_low();
// info!("just set pin 7 low");
// self.pin_4.set_high();
// info!("just set pin 4 high");
// info!("HV Powered on");
// info!("HV Powered on");
// }

pub fn power_hv_off(&mut self) {
self.pin_4.set_low();
Expand Down
12 changes: 6 additions & 6 deletions app/src/core/controllers/propulsion_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,18 @@ pub async fn read_prop_adc(
) {
Timer::after_millis(1000).await;
loop {
Timer::after_millis(250).await;
Timer::after_millis(50).await;
if !CONNECTED.load(Ordering::Relaxed) {
continue;
}
for _ in 0..3 {
let i = adc.read(&mut pa6) as u64;
send_data!(data_sender, Datatype::PropulsionCurrent, i; 5000);
Timer::after_millis(100).await;
}
let v_ref_int = adc.read_internal(&mut v_ref_int_channel);
let v = adc.read(&mut pa5) as u64;
let i = adc.read(&mut pa6) as u64;
send_data!(data_sender, Datatype::PropulsionVoltage, v; 5000);
send_data!(data_sender, Datatype::PropulsionCurrent, i; 5000);
send_data!(data_sender, Datatype::PropulsionVRefInt, v_ref_int as u64; 5000);
// queue_data(data_sender, Datatype::PropulsionVoltage, v).await;
// queue_data(data_sender, Datatype::PropulsionCurrent, i).await;
// queue_data(data_sender, Datatype::PropulsionVRefInt, v_ref_int as u64).await;
}
}
Loading

0 comments on commit 7be4cd7

Please sign in to comment.