Skip to content

Commit

Permalink
Merge pull request #85 from delft-hyperloop/restyling
Browse files Browse the repository at this point in the history
FIX: Restyling Vitals
  • Loading branch information
andtsa authored Jul 14, 2024
2 parents 807df0e + cbb8bb4 commit 38c0da1
Show file tree
Hide file tree
Showing 71 changed files with 1,646 additions and 1,001 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ jobs:

steps:
- uses: actions/checkout@v3
<<<<<<< HEAD
- name: Build & Lint
run: rustup target add thumbv7em-none-eabihf && cd app && cargo clippy -- -D warnings

=======
- name: Build
run: rustup target add thumbv7em-none-eabihf && cd app && cargo build
>>>>>>> 441ec3870e99a4435b7c6742938ceacc8c9d3b10

station:

Expand All @@ -33,8 +38,13 @@ jobs:
sudo apt-get install -y libwebkit2gtk-4.0-dev libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf libgtk-3-dev
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
<<<<<<< HEAD
- name: Build & Lint
run: cd gs/station && cargo clippy --features full -- -D warnings
=======
- name: Build
run: cd gs/station && cargo build --features full
>>>>>>> 441ec3870e99a4435b7c6742938ceacc8c9d3b10
- name: Test
run: cd gs/station && cargo test --features full

2 changes: 1 addition & 1 deletion app/.cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU)

[env]
#DEFMT_LOG = "off"
DEFMT_LOG = "trace"
DEFMT_LOG = "error"
130 changes: 85 additions & 45 deletions app/build.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#![allow(non_snake_case)]
use anyhow::anyhow;

extern crate serde;

use std::collections::BTreeMap;
use std::env;
use std::fs;
use std::path::Path;

use anyhow::Result;
use goose_utils::check_ids;
use goose_utils::check_config;
use goose_utils::ip::configure_gs_ip;
use serde::Deserialize;

Expand All @@ -28,7 +30,6 @@ struct GS {
ip: [u8; 4],
force: bool,
port: u16,
// udp_port: u16,
buffer_size: usize,
timeout: u64,
heartbeat: u64,
Expand All @@ -38,21 +39,22 @@ struct GS {
struct Pod {
net: NetConfig,
internal: InternalConfig,
bms: Bms,
comm: Comm,
heartbeats: BTreeMap<String, u64>,
}

#[derive(Debug, Deserialize)]
struct Bms {
lv_ids: Vec<u16>,
hv_ids: Vec<u16>,
struct Comm {
bms_lv_ids: Vec<u16>,
bms_hv_ids: Vec<u16>,
gfd_ids: Vec<u16>,
}

#[derive(Debug, Deserialize)]
struct NetConfig {
ip: [u8; 4],
port: u16,
udp_port: u16,
// ip: [u8; 4],
// port: u16,
// udp_port: u16,
mac_addr: [u8; 6],
keep_alive: u64,
}
Expand All @@ -70,12 +72,6 @@ pub const COMMANDS_PATH: &str = "../config/commands.toml";
pub const EVENTS_PATH: &str = "../config/events.toml";

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")?;
let dest_path = Path::new(&out_dir).join("config.rs");

Expand All @@ -84,16 +80,18 @@ fn main() -> Result<()> {

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

let _ = check_ids(DATATYPES_PATH, COMMANDS_PATH, EVENTS_PATH);
content.push_str(&check_config(DATATYPES_PATH, COMMANDS_PATH, EVENTS_PATH, CONFIG_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_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)?);
let dt = goose_utils::datatypes::generate_datatypes(DATATYPES_PATH, false)?;
content.push_str(&dt);
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(&configure_heartbeats(&config, &dt)?);
// content.push_str(&*can::main(&id_list));

fs::write(dest_path.clone(), content).unwrap_or_else(|e| {
Expand All @@ -112,36 +110,50 @@ fn main() -> Result<()> {
Ok(())
}

fn configure_heartbeats(config: &Config, dt: &str) -> Result<String> {
let mut x = format!("\npub const HEARTBEATS_LEN: usize = {};\npub const HEARTBEATS: [(Datatype, u64); HEARTBEATS_LEN] = [", config.pod.heartbeats.len());
for (key, val) in &config.pod.heartbeats {
if !dt.contains(key) {
return Err(anyhow!("\n\nFound heartbeat for non-existing datatype: {:?}\nYou can only add a timeout for datatypes present in /config/datatypes.toml (check your spelling)\n", key));
}
x.push_str(&format!("(Datatype::{}, {}), ", key, val));
}
x.push_str("];\n");
Ok(x)
}

fn configure_ip(config: &Config) -> String {
format!("pub const NETWORK_BUFFER_SIZE: usize = {};\n", config.gs.buffer_size)
+ &*format!("pub const IP_TIMEOUT: u64 = {};\n", config.gs.timeout)
}

fn configure_pod(config: &Config) -> String {
// format!(
// "pub static POD_IP_ADDRESS: ([u8;4],u16) = ([{},{},{},{}],{});\n",
// config.pod.net.ip[0],
// config.pod.net.ip[1],
// config.pod.net.ip[2],
// config.pod.net.ip[3],
// config.pod.net.port
// )
// + &*format!(
// "pub static POD_UDP_IP_ADDRESS: ([u8;4],u16) = ([{},{},{},{}],{});\n",
// config.pod.net.ip[0],
// config.pod.net.ip[1],
// config.pod.net.ip[2],
// config.pod.net.ip[3],
// config.pod.net.udp_port
// ) +
format!(
"pub static POD_IP_ADDRESS: ([u8;4],u16) = ([{},{},{},{}],{});\n",
config.pod.net.ip[0],
config.pod.net.ip[1],
config.pod.net.ip[2],
config.pod.net.ip[3],
config.pod.net.port
) + &*format!(
"pub static POD_UDP_IP_ADDRESS: ([u8;4],u16) = ([{},{},{},{}],{});\n",
config.pod.net.ip[0],
config.pod.net.ip[1],
config.pod.net.ip[2],
config.pod.net.ip[3],
config.pod.net.udp_port
) + &*format!(
"pub static POD_MAC_ADDRESS: [u8;6] = [{},{},{},{},{},{}];\n",
config.pod.net.mac_addr[0],
config.pod.net.mac_addr[1],
config.pod.net.mac_addr[2],
config.pod.net.mac_addr[3],
config.pod.net.mac_addr[4],
config.pod.net.mac_addr[5]
) + &*format!("pub const KEEP_ALIVE: u64 = {};\n", config.pod.net.keep_alive)
+ &*format!("pub const HEARTBEAT: u64 = {};\n", config.gs.heartbeat)
) + &format!("pub const KEEP_ALIVE: u64 = {};\n", config.pod.net.keep_alive)
+ &format!("pub const HEARTBEAT: u64 = {};\n", config.gs.heartbeat)
}

fn configure_internal(config: &Config) -> String {
Expand All @@ -150,20 +162,34 @@ fn configure_internal(config: &Config) -> String {
+ &*format!("pub const CAN_QUEUE_SIZE: usize = {};\n", config.pod.internal.can_queue_size)
+ &*format!(
"pub const LV_IDS: [u16;{}] = [{}];\n",
config.pod.bms.lv_ids.len(),
config.pod.bms.lv_ids.iter().map(|x| x.to_string()).collect::<Vec<String>>().join(", ")
config.pod.comm.bms_lv_ids.len(),
config
.pod
.comm
.bms_lv_ids
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>()
.join(", ")
)
+ &*format!(
"pub const HV_IDS: [u16;{}] = [{}];\n",
config.pod.bms.hv_ids.len(),
config.pod.bms.hv_ids.iter().map(|x| x.to_string()).collect::<Vec<String>>().join(", ")
config.pod.comm.bms_hv_ids.len(),
config
.pod
.comm
.bms_hv_ids
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>()
.join(", ")
)
+ &*format!(
"pub const GFD_IDS: [u16;{}] = [{}];\n",
config.pod.bms.gfd_ids.len(),
config.pod.comm.gfd_ids.len(),
config
.pod
.bms
.comm
.gfd_ids
.iter()
.map(|x| x.to_string())
Expand All @@ -172,14 +198,28 @@ fn configure_internal(config: &Config) -> String {
)
+ &*format!(
"pub const BATTERY_GFD_IDS: [u16;{}] = [{},{},{}];\n",
config.pod.bms.lv_ids.len()
+ config.pod.bms.hv_ids.len()
+ config.pod.bms.gfd_ids.len(),
config.pod.bms.lv_ids.iter().map(|x| x.to_string()).collect::<Vec<String>>().join(", "),
config.pod.bms.hv_ids.iter().map(|x| x.to_string()).collect::<Vec<String>>().join(", "),
config.pod.comm.bms_lv_ids.len()
+ config.pod.comm.bms_hv_ids.len()
+ config.pod.comm.gfd_ids.len(),
config
.pod
.comm
.bms_lv_ids
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>()
.join(", "),
config
.pod
.comm
.bms_hv_ids
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>()
.join(", "),
config
.pod
.bms
.comm
.gfd_ids
.iter()
.map(|x| x.to_string())
Expand Down
33 changes: 16 additions & 17 deletions app/src/core/communication/can.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use crate::core::controllers::battery_controller::ground_fault_detection_voltage
use crate::core::controllers::can_controller::CanTwoUtils;
use crate::pconfig::bytes_to_u64;
use crate::pconfig::id_as_value;
use crate::pconfig::send_event;
use crate::send_data;
use crate::CanReceiver;
use crate::CanSender;
use crate::DataSender;
Expand Down Expand Up @@ -53,21 +55,18 @@ pub async fn can_receiving_handler(
let mut error_counter = 0u64;
let mut gfd_counter = 0u64;
loop {
// #[cfg(debug_assertions)]
match bus.read().await {
Ok(envelope) => {
error_counter = 0;
let (frame, timestamp) = envelope.parts();
// 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;
send_data!(
data_sender,
Datatype::ReceivedCan,
id as u64,
bytes_to_u64(frame.data())
);
#[cfg(debug_assertions)]
info!("[CAN ({})] received frame: id={:?} data={:?}", bus_nr, id, frame.data());
if DATA_IDS.contains(&id) {
Expand Down Expand Up @@ -125,17 +124,17 @@ pub async fn can_receiving_handler(
.await;
}
} else if EVENT_IDS.contains(&id) {
event_sender.send(Event::from_id(id, Some(69420))).await; // 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
// 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 {
#[cfg(debug_assertions)]
info!("[CAN ({})] unknown ID: {:?}", bus_nr, id);
data_sender
.send(Datapoint::new(
Datatype::UnknownCanId,
id as u64,
bytes_to_u64(frame.data()),
))
.await;
send_data!(
data_sender,
Datatype::UnknownCanId,
id as u64,
bytes_to_u64(frame.data())
);
}
},
Err(e) => {
Expand Down
14 changes: 14 additions & 0 deletions app/src/core/communication/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use defmt::Formatter;

use crate::Datatype;

pub mod can;
Expand Down Expand Up @@ -33,3 +35,15 @@ impl Datapoint {
bytes
}
}

impl defmt::Format for Datapoint {
fn format(&self, fmt: Formatter) {
defmt::write!(
fmt,
"Datapoint {{ datatype: {:?}, value: {:?}, timestamp: {:?} }}",
self.datatype,
self.value,
self.timestamp
)
}
}
Loading

0 comments on commit 38c0da1

Please sign in to comment.