Skip to content

Commit

Permalink
change SPI speed, add sensors to efi_status, add get of ve values for…
Browse files Browse the repository at this point in the history
… alphaN
  • Loading branch information
FDSoftware committed Aug 13, 2023
1 parent a385ea9 commit b0f08d9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 21 deletions.
4 changes: 4 additions & 0 deletions src/app/engine/engine_status.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::app::engine::sensors::SensorValues;

#[allow(non_camel_case_types)]
#[repr(u8)]
#[derive(Debug)]
Expand Down Expand Up @@ -38,10 +40,12 @@ pub struct EngineStatus {
pub cycle_duration: f32,
pub cycle_status: __rpm_status,
pub rpm: i32,
pub sensors: SensorValues,
}

pub fn get_default_engine_status() -> EngineStatus {
let status = EngineStatus {
sensors: SensorValues::new(),
injection: InjectionInfo {
targetAFR: 0.0,
injection_bank_1_time: 0.0,
Expand Down
4 changes: 4 additions & 0 deletions src/app/engine/sensors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ impl SensorValues {
SensorTypes::TPS => {
self.raw_tps = EMA_LP_ALPHA * (raw_value as f32)
+ (1.0 - EMA_LP_ALPHA) * (self.raw_tps as f32);

// SDUBTUD: tan fácil iba a ser?
let result = (24.6914 * self.raw_tps - 11.11f32).clamp(0f32,100f32);
self.tps = result;
}
SensorTypes::BatteryVoltage => {
self.raw_batt = EMA_LP_ALPHA * (raw_value as f32)
Expand Down
24 changes: 18 additions & 6 deletions src/app/injection/alpha_n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
use crate::app::engine::{
efi_cfg::EngineConfig, engine_status::EngineStatus, get_engine_cycle_duration,
};
use crate::app::memory::tables::{DataT, Tables};

use super::injectors::fuel_mass_to_time;

const STD_AIR_PRES: f32 = 101.325f32;
const STD_AIR_TEMP: f32 = 293.15f32;
const AIR_R: f32 = 8.31446261815324f32 / 28.9647f32;
const AIR_R: f32 = 8.31445f32 / 28.965f32;

pub fn calculate_injection_fuel(es: &mut EngineStatus, ecfg: &EngineConfig) -> f32 {
if es.rpm == 0 {
pub fn calculate_injection_fuel(es: &mut EngineStatus, ecfg: &EngineConfig, tables: &mut Tables) -> f32 {
if es.rpm == 0 || !ecfg.ready {
return 0.0;
}

let ve = get_ve();
let ve = get_ve(es, tables.tps_rpm_ve);
let air_mass = get_air_mass(ve, ecfg.engine.cilinder_count, ecfg.engine.displacement);

let lambda = ecfg.injection.target_lambda;
Expand Down Expand Up @@ -44,13 +45,24 @@ pub fn calculate_correction_time() -> f32 {
0.0
}

// esta funcion es solo para alphaN, cuando implemente speed-density o maf se puede migrar modificando las constantes
pub fn get_air_mass(ve: f32, engine_cilinders: u8, engine_displacement: u32) -> f32 {
let full_cycle: f32 =
(ve / 10000.0) * (engine_displacement as f32 * STD_AIR_PRES / (AIR_R * STD_AIR_TEMP));

full_cycle / engine_cilinders as f32
}

pub fn get_ve() -> f32 {
4.4
// en este caso se revisa RPM y % del TPS
pub fn get_ve(es: &EngineStatus, ve_table: Option<DataT>) -> f32 {
if es.rpm == 0 || ve_table.is_none() {
return 0.0;
}

//data[0].into_iter().position(|x| x <= 307200)
let table = ve_table.unwrap();
let rpm_index = table[0].into_iter().position(|x| x <= es.rpm);
let load_index = table.into_iter().position(|y| y[0] <= es.sensors.tps as i32);

return table[load_index.unwrap_or(0)][rpm_index.unwrap_or(0)] as f32 / 100.0;
}
32 changes: 17 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#![feature(exclusive_range_pattern)]
#![feature(proc_macro_hygiene)]
#![feature(int_roundings)]
#![feature(is_some_and)]
#![feature(stdsimd)]

#![no_main]
#![no_std]
#![feature(stdsimd)]
#![feature(int_roundings)]
#![feature(exclusive_range_pattern)]

#![allow(stable_features)]
#![feature(is_some_and)]
#![allow(unused_mut)]

use panic_halt as _;
Expand Down Expand Up @@ -47,7 +49,6 @@ mod app {
memory::tables::{SpiT, Tables},
webserial::{handle_tables, send_message, SerialMessage, SerialStatus},
};

use crate::app::engine::pmic::{PMIC, PmicT};
use crate::app::webserial::{handle_engine, handle_pmic, handle_realtime_data};

Expand Down Expand Up @@ -81,8 +82,6 @@ mod app {
usb_web: WebUsb<UsbBusType>,

// core:
// el implement de "shared_bus_resources" anda para el culo;
// asi que hago el lock a mano
spi_lock: bool,
crc: Crc32,

Expand Down Expand Up @@ -213,7 +212,7 @@ mod app {
gpio_config.spi_mosi,
),
mode,
(3).MHz(),
(30).MHz(),
&clocks,
);

Expand All @@ -225,12 +224,10 @@ mod app {

let mut flash = w25q::series25::Flash::init(spi_bus.acquire(), gpio_config.memory_cs).unwrap();

let id = flash.read_jedec_id().unwrap();
// let id = flash.read_jedec_id().unwrap();

let flash_info = flash.get_device_info().unwrap();

host::debug!("FLASH: {:?}", id);

// EFI Setup:
let mut table = Tables {
tps_rpm_ve: None,
Expand All @@ -253,8 +250,8 @@ mod app {

host::debug!("AF {:?}", _efi_status.injection.air_flow);

gpio_config.led.led_check.toggle();
gpio_config.led.led_mil.toggle();
// gpio_config.led.led_check.toggle();
// gpio_config.led.led_mil.toggle();

let mut sensors = SensorValues::new();

Expand Down Expand Up @@ -491,9 +488,10 @@ mod app {
)
}

#[task(priority = 8, local = [analog_pins, adc], shared = [sensors])]
#[task(priority = 8, local = [analog_pins, adc], shared = [sensors, efi_status])]
fn sensors_callback(mut ctx: sensors_callback::Context) {
let mut sensors = ctx.shared.sensors;
let mut engine_status = ctx.shared.efi_status;
let adc_pins = ctx.local.analog_pins;
let adc = ctx.local.adc;

Expand Down Expand Up @@ -522,7 +520,11 @@ mod app {
get_sensor_raw(SensorTypes::BatteryVoltage, adc_pins, adc),
SensorTypes::CooltanTemp,
);
})
});

(sensors, engine_status).lock(|sensors, engine_status| {
engine_status.sensors = *sensors;
});
}

// prioridad? si; task para manejar el pwm de los inyectores; exportar luego a cpwm.rs
Expand Down

0 comments on commit b0f08d9

Please sign in to comment.