Skip to content

Commit

Permalink
Version 1.0.6 (Dev)
Browse files Browse the repository at this point in the history
  • Loading branch information
rnd-ash committed Apr 25, 2023
1 parent c63b79f commit fd99ed2
Show file tree
Hide file tree
Showing 23 changed files with 678 additions and 158 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# 1.0.6 (In development)
* Move diagnostic executor to seperate thread
* Migrate to diag_server_unified ecu_diagnostics branch
* Show diagnostic mode on status bar
* Chart rendering at 60fps - Along with linear interpolation!
* Initial SCN Configuration wizard:
1. Save/Load from YML files
2. Write/Read program settings
3. Wiki integration
* Add support for ResponsePending ECU response (useful when flashing)


# 1.0.3 (27/2/23)
* Removed "UNDER DEVELOPMENT" watermark for V1.3 PCB

Expand Down
4 changes: 3 additions & 1 deletion backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ ecu_diagnostics = { git = "https://github.com/rnd-ash/ecu_diagnostics", branch="
#ecu_diagnostics = { path = "../../ecu_diagnostics" }
serial-rs = {git="https://github.com/rnd-ash/serial-rs"}
packed_struct="0.10.0"
static_assertions = "1.1.0"
static_assertions = "1.1.0"
serde_yaml="0.9.21"
serde = { version = "1.0", features = ["derive"] }
6 changes: 4 additions & 2 deletions backend/src/diag/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ use crate::hw::{
usb::{EspLogMessage, Nag52USB},
usb_scanner::Nag52UsbScanner,
};

pub mod ident;
pub mod flash;
pub mod settings;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum AdapterType {
Expand Down Expand Up @@ -173,8 +175,8 @@ impl Nag52Diag {
send_id: 0x07E1,
recv_id: 0x07E9,
timeout_cfg: TimeoutConfig {
read_timeout_ms: 2500,
write_timeout_ms: 2500,
read_timeout_ms: 5000,
write_timeout_ms: 5000,
}
};

Expand Down
120 changes: 120 additions & 0 deletions backend/src/diag/settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
use std::{ptr::slice_from_raw_parts};

use serde::{Deserialize, Serialize};

pub type UnpackResult<T> = std::result::Result<T, UnPackError>;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum UnPackError {
WrongId {
wanted: u8,
real: u8
},
InvalidLen {
wanted: usize,
len: usize
}
}

impl ToString for UnPackError {
fn to_string(&self) -> String {
match self {
UnPackError::WrongId { wanted, real } => format!("Wrong setting ID. Wanted 0x{:02X?}, got 0x{:02X?}", wanted, real),
UnPackError::InvalidLen { wanted, len } => format!("Wrong response length. Wanted {} bytes, got {} bytes. Maybe a config app/firmware mismatch?", wanted, len),
}
}
}

pub fn unpack_settings<T>(settings_id: u8, raw: &[u8]) -> UnpackResult<T>
where T: Copy {
if settings_id != raw[0] {
Err(UnPackError::WrongId { wanted: settings_id, real: raw[0] })
} else if raw.len()-1 != std::mem::size_of::<T>() {
Err(UnPackError::InvalidLen { wanted: std::mem::size_of::<T>(), len: raw.len()-1 })
} else {
let ptr: *const T = raw[1..].as_ptr() as *const T;
Ok(unsafe { *ptr })
}
}

pub fn pack_settings<T>(settings_id: u8, settings: T) -> Vec<u8>
where T: Copy {
let mut ret = vec![settings_id];
let ptr= slice_from_raw_parts((&settings as *const T) as *const u8, std::mem::size_of::<T>());
ret.extend_from_slice(unsafe { &*ptr });
ret
}

pub trait TcuSettings<'de> : Serialize + Deserialize<'de> where Self: Sized {
fn wiki_url(&self) -> Option<&'static str>;
fn setting_name(&self) -> &'static str;
fn get_revision_name(&self) -> &'static str;
fn get_scn_id(&self) -> u8;
}


#[derive(Default, Debug, Copy, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
#[repr(C, packed)]
pub struct LinearInterpSettings {
pub new_min: f32,
pub new_max: f32,
pub raw_min: f32,
pub raw_max: f32
}

impl LinearInterpSettings {
// Copied from TCU source lib/core/tcu_maths.cpp - Function scale_number()
pub fn calc_with_value(&self, input: f32) -> f32 {
let raw_limited = self.raw_min.max(input.min( self.raw_max));
return (((self.new_max - self.new_min) * (raw_limited - self.raw_min)) / (self.raw_max - self.raw_min)) + self.new_min;
}
}

#[derive(Default, Debug, Copy, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
#[repr(C, packed)]
pub struct TccSettings {
pub adapt_enable: bool,
pub enable_d1: bool,
pub enable_d2: bool,
pub enable_d3: bool,
pub enable_d4: bool,
pub enable_d5: bool,
pub prefill_pressure: u16,
pub lock_rpm_threshold: u16,
pub min_locking_rpm: u16,
pub adjust_interval_ms: u16,
pub tcc_stall_speed: u16,
pub min_torque_adapt: u16,
pub max_torque_adapt: u16,
pub prefill_min_engine_rpm: u16,
pub base_pressure_offset_start_ramp: u16,
pub pressure_increase_ramp_settings: LinearInterpSettings,
pub adapt_pressure_inc: u8,
pub adapt_lock_detect_time: u16,
pub pulling_slip_rpm_low_threshold: u16,
pub pulling_slip_rpm_high_threhold: u16,
pub reaction_torque_multiplier: f32,
pub trq_consider_coasting: u16,
pub load_dampening: LinearInterpSettings,
pub pressure_multiplier_output_rpm: LinearInterpSettings,
pub max_allowed_bite_pressure: u16,
pub max_allowed_pressure_longterm: u16,
}

impl TcuSettings<'_> for TccSettings {
fn wiki_url(&self) -> Option<&'static str> {
Some("https://docs.ultimate-nag52.net/en/gettingstarted/configuration/settings/tcc#revision-a2-240423")
}

fn setting_name(&self) -> &'static str {
"TCC Settings"
}

fn get_revision_name(&self) -> &'static str {
"A2 (24/03/23)"
}

fn get_scn_id(&self) -> u8 {
0x01
}
}
2 changes: 2 additions & 0 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ pub mod diag;
pub mod hw;

pub use ecu_diagnostics;
pub use serde;
pub use serde_yaml;
2 changes: 1 addition & 1 deletion config_app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "config_app"
version = "0.1.4"
version = "0.1.6"
edition = "2021"
resolver = "2"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
12 changes: 5 additions & 7 deletions config_app/src/ui/configuration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ use self::cfg_structs::{
TcmCoreConfig, TcmEfuseConfig,
};

use super::{status_bar::MainStatusBar, StatusText};
use super::{StatusText};

pub mod cfg_structs;

pub struct ConfigPage {
nag: Nag52Diag,
bar: MainStatusBar,
status: StatusText,
scn: Option<TcmCoreConfig>,
efuse: Option<TcmEfuseConfig>,
Expand All @@ -47,7 +46,7 @@ fn load_image(image: DynamicImage, name: &str) -> RetainedImage {
}

impl ConfigPage {
pub fn new(nag: Nag52Diag, bar: MainStatusBar) -> Self {
pub fn new(nag: Nag52Diag) -> Self {
let red_img = image::load_from_memory_with_format(
include_bytes!("../../../res/pcb_11.jpg"),
ImageFormat::Jpeg,
Expand All @@ -69,7 +68,6 @@ impl ConfigPage {
let pcb_13_img = load_image(bet_img, "V13-PCB");
Self {
nag,
bar,
status: StatusText::Ok("".into()),
scn: None,
efuse: None,
Expand Down Expand Up @@ -427,7 +425,7 @@ impl crate::window::InterfacePage for ConfigPage {
})
});
if reload {
*self = Self::new(self.nag.clone(), self.bar.clone());
*self = Self::new(self.nag.clone());
}
self.show_final_warning = tmp;

Expand All @@ -439,7 +437,7 @@ impl crate::window::InterfacePage for ConfigPage {
"Configuration"
}

fn get_status_bar(&self) -> Option<Box<dyn crate::window::StatusBar>> {
Some(Box::new(self.bar.clone()))
fn should_show_statusbar(&self) -> bool {
true
}
}
65 changes: 36 additions & 29 deletions config_app/src/ui/diagnostics/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::ui::status_bar::MainStatusBar;
use crate::window::{PageAction, StatusBar};
use backend::diag::Nag52Diag;
use backend::ecu_diagnostics::kwp2000::{KwpSessionTypeByte, KwpSessionType};
Expand Down Expand Up @@ -27,7 +26,6 @@ pub enum CommandStatus {
}

pub struct DiagnosticsPage {
bar: MainStatusBar,
query_ecu: Arc<AtomicBool>,
last_update_time: Arc<AtomicU64>,
curr_values: Arc<RwLock<Option<LocalRecordData>>>,
Expand All @@ -39,7 +37,7 @@ pub struct DiagnosticsPage {
}

impl DiagnosticsPage {
pub fn new(nag: Nag52Diag, bar: MainStatusBar) -> Self {
pub fn new(nag: Nag52Diag) -> Self {

let run = Arc::new(AtomicBool::new(true));
let run_t = run.clone();
Expand All @@ -48,26 +46,32 @@ impl DiagnosticsPage {
let store_t = store.clone();

let store_old = Arc::new(RwLock::new(None));
let store_old_t = store.clone();
let store_old_t = store_old.clone();

let to_query: Arc<RwLock<Option<RecordIdents>>> = Arc::new(RwLock::new(None));
let to_query_t = to_query.clone();
let last_update = Arc::new(AtomicU64::new(0));
let last_update_t = last_update.clone();

let launch_time = Instant::now();
let launch_time_t = launch_time.clone();

let _ = thread::spawn(move || {
nag.with_kwp(|server| {
server.kwp_set_session(KwpSessionTypeByte::Standard(KwpSessionType::Normal))
});
let launch_time = Instant::now();
while run_t.load(Ordering::Relaxed) {
let start = Instant::now();
if let Some(to_query) = *to_query_t.read().unwrap() {
let prev = store_t.read().unwrap().clone();
match nag.with_kwp(|server| to_query.query_ecu(server)) {
Ok(r) => {
last_update_t.store(launch_time.elapsed().as_millis() as u64, Ordering::Relaxed);
*store_old_t.write().unwrap() = prev;
let curr = store_t.read().unwrap().clone();
*store_old_t.write().unwrap() = curr;
*store_t.write().unwrap() = Some(r);
last_update_t.store(
launch_time_t.elapsed().as_millis() as u64,
Ordering::Relaxed,
);
},
Err(e) => {
eprintln!("Could not query {}", e);
Expand All @@ -83,14 +87,13 @@ impl DiagnosticsPage {

Self {
query_ecu: run,
bar,
last_update_time: last_update,
prev_values: store_old,
curr_values: store,
record_to_query: to_query,
charting_data: VecDeque::new(),
chart_idx: 0,
time_since_launch: Instant::now()
time_since_launch: launch_time_t
}
}
}
Expand Down Expand Up @@ -152,28 +155,32 @@ impl crate::window::InterfacePage for DiagnosticsPage {

if !c.is_empty() {
let mut d = c[0].clone();
/*
// Compare to
let mut prev = d.clone();
if let Some(p) = prev_value.get_chart_data().get(0).cloned() {
if p.bounds == d.bounds && p.data.len() == d.data.len() {
prev = p;
}
prev = p;
}
// Linear interp
let now = self.time_since_launch.elapsed().as_millis() as u64;
let last = self.last_update_time.load(Ordering::Relaxed);
let time_since = now - last;
let p_new = time_since as f32/(RLI_QUERY_INTERVAL as f32);// Proportion of old data
let p_old = 1.0 - p_new; // Proportion of new data
for idx in 0..d.data.len() {
let interpolated = (prev.data[idx].1 * p_old) + (d.data[idx].1 * p_new);
d.data[idx].1 = interpolated;
if prev != d {
// Linear interp when values differ
let ms_since_update = std::cmp::min(
RLI_QUERY_INTERVAL,
self.time_since_launch.elapsed().as_millis() as u64
- self.last_update_time.load(Ordering::Relaxed),
);
let mut proportion_curr: f32 = (ms_since_update as f32) / RLI_QUERY_INTERVAL as f32; // Percentage of old value to use
let mut proportion_prev: f32 = 1.0 - proportion_curr; // Percentage of curr value to use
if ms_since_update == 0 {
proportion_prev = 1.0;
proportion_curr = 0.0;
} else if ms_since_update == RLI_QUERY_INTERVAL {
proportion_prev = 0.0;
proportion_curr = 1.0;
}
for idx in 0..d.data.len() {
let interpolated = (prev.data[idx].1 * proportion_prev) + (d.data[idx].1 * proportion_curr);
d.data[idx].1 = interpolated;
}
}
*/

self.charting_data.push_back((self.chart_idx, d.clone()));
self.chart_idx+=1;
if self.charting_data.len() > (50000 / 100) {
Expand Down Expand Up @@ -230,8 +237,8 @@ impl crate::window::InterfacePage for DiagnosticsPage {
"Ultimate-NAG52 diagnostics"
}

fn get_status_bar(&self) -> Option<Box<dyn StatusBar>> {
Some(Box::new(self.bar.clone()))
fn should_show_statusbar(&self) -> bool {
true
}
}

Expand Down
5 changes: 2 additions & 3 deletions config_app/src/ui/diagnostics/rli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl DataPressures {
("MPC pressure", self.mpc_pressure as f32, None),
("TCC pressure", self.tcc_pressure as f32, None),
],
Some((0.0, 7000.0)),
None
)]
}
}
Expand Down Expand Up @@ -625,7 +625,7 @@ impl DataCanDump {
("Driver", drv, None),
("EGS Request", egs, None)
],
Some((-100.0, self.max_torque_ms as f32)),
None,
)]
}
}
Expand All @@ -644,7 +644,6 @@ pub struct DataSysUsage {

impl DataSysUsage {
pub fn to_table(&self, ui: &mut Ui) -> InnerResponse<()> {
println!("{:#?}", self);
let r_f = self.free_ram as f32;
let r_t = self.total_ram as f32;
let p_f = self.free_psram as f32;
Expand Down
Loading

0 comments on commit fd99ed2

Please sign in to comment.