diff --git a/ECU.ino b/ECU.ino index 18989fc..a43d6d3 100644 --- a/ECU.ino +++ b/ECU.ino @@ -4,36 +4,33 @@ #include "tables.h" void setup() { - debug_setup(); - + // Sensors pinMode(pin_map, INPUT); + pinMode(pin_batt, INPUT); + // Dash / debug outputs pinMode(pin_tach, OUTPUT); + digitalWrite(pin_tach, LOW); + + // Ignition outputs pinMode(pin_coil1, OUTPUT); pinMode(pin_coil2, OUTPUT); - pinMode(pin_coil3, OUTPUT); - pinMode(pin_coil4, OUTPUT); - - digitalWrite(pin_tach, LOW); digitalWrite(pin_coil1, LOW); digitalWrite(pin_coil2, LOW); - digitalWrite(pin_coil3, LOW); - digitalWrite(pin_coil4, LOW); + // Set up serial speed etc and do intro output + debug_setup(); + // Get an initial reading and output debug if it's not atmospheric map_init(); battery_init(); } void loop() { + // Always process the CAS cas_process(); - schedule_process(); - -// map_process(); -// cas_process(); -// tach_process(); -// ignition_process(); - + // Run scheduler + schedule_process(); } diff --git a/battery.ino b/battery.ino index be6190c..622d97c 100644 --- a/battery.ino +++ b/battery.ino @@ -1,7 +1,9 @@ void battery_init() { + // Run once to get start info task_battery_run(); + // Log if battery is exceptional in any way if(battery_voltage_value < 12) { Serial.println("ERR: Battery low"); @@ -11,12 +13,18 @@ void battery_init() Serial.println("ERR: Battery high"); } else + { Serial.println("INF: Battery ok"); + } } void task_battery_run() { + // Min/Max we care about are 7 and 16. Anything that low or high is bad so outside of that + // doesn't need any special consideration battery_voltage_value = map(analogRead(pin_batt), battery_cal_7v, battery_cal_16v, 7, 16); + + // Nudge it along so we have an index starting at 0 for tables etc battery_voltage_index = constrain(battery_voltage_value,7,16) - 7; } diff --git a/cas.ino b/cas.ino index 9e39e65..3430cc9 100644 --- a/cas.ino +++ b/cas.ino @@ -5,6 +5,8 @@ void cas_process() byte sgt = digitalRead(pin_cas_sgt); unsigned long timestamp = micros(); + // Simple rev limiter, drop the spark until we're slow again and set a flag so we log it + // at the next chance we get if(rpm_current_value > rpm_range_max) { rpm_limited_log = 1; @@ -21,6 +23,7 @@ void cas_process() // Handle tach task_tach_high = micros(); + // Log when it was cas_sgt_lastrise = timestamp; } } @@ -33,12 +36,14 @@ void cas_process() // Difference between last and current is 180 degrees usec_per_degree = (timestamp - cas_sgt_lastfall) / 180; + // Log when it was cas_sgt_lastfall = timestamp; // If we have crank pulse present then cyl1 is TDC if(digitalRead(pin_cas_sgc) == HIGH) { // If we've arrived at cylinder 1 unexpectedly then we need to know! + // We'll drop sparks until we're back and log it if(cylinder_next[cylinder_tdc] != 1) { cas_sync_fail=1; @@ -46,6 +51,7 @@ void cas_process() } else { + // Clear it if we're in sync, but don't clear log flag cas_sync_fail = 0; } @@ -60,7 +66,7 @@ void cas_process() // Schedule next cylinder, this one is already past TDC cylinder_next_fire = cylinder_next[cylinder_tdc]; - // If we're out of sync, we kill ignition for a bit for now + // If we're out of sync, we kill ignition for a bit for now. Same if we're at crazy RPM if((cas_sync_fail == 0) && (rpm_limited == 0)) { if(cylinder_next_fire == 1) @@ -71,18 +77,12 @@ void cas_process() { task_coil2_fire = micros() + (usec_per_degree * (180 + ignition_offset - table_ignition[rpm_current_index + (map_current_index << 4)])); task_coil2_charge = task_coil2_fire - (coil_dwell + table_dwell[battery_voltage_index]); - }/*else if(cylinder_next_fire == 3) - { - task_coil3_fire = micros() + (usec_per_degree * 180); - task_coil3_charge = task_coil3_fire - (coil_dwell + table_dwell[battery_voltage_index]); - }else if(cylinder_next_fire == 4) - { - task_coil4_fire = micros() + (usec_per_degree * 180); - task_coil4_charge = task_coil4_fire - (coil_dwell + table_dwell[battery_voltage_index]); - }*/ + } } } } + + // Save current state cas_sgt_lastvalue = sgt; diff --git a/data.h b/data.h index be4af98..c89985b 100644 --- a/data.h +++ b/data.h @@ -7,8 +7,8 @@ byte map_current_index = 0; // Index byte map_current_value = 0; // KPA value // Rotation -byte rpm_limited = 0; -byte rpm_limited_log = 0; +byte rpm_limited = 0; // Are we currently running over RPM? +byte rpm_limited_log = 0; // Are we due a warning message const int rpm_range_min = 0; // Index 0 const int rpm_range_max = 7200; // Index 15 byte rpm_current_index = 0; // Table index of current RPM @@ -32,13 +32,11 @@ byte cas_sync_fail_log = 0; byte cylinder_tdc = 0; // Most recent cylinder to hit TDC byte cylinder_next_fire = 0; // Next cylinder due a spark //byte cylinder_next_inject = 0; // Next cylinder due fuel -// Sequential and wasted spark -//byte cylinder_next[] = {0,3,1,4,2}; // For 1-3-4-2 Current cylinder as index returns next cylinder -byte cylinder_next[] = {0,2,1,2,1}; // For 1-3-4-2 Current cylinder as index returns next cylinder +byte cylinder_next[] = {0,2,1,2,1}; // CAn be 0,3,1,4,2 if we ever go sequential // Ignition -const int coil_dwell = 4000; // 5ms for stock coil -const int ignition_offset = 0; +const int coil_dwell = 4000; // 4 read from scope on factory ECU +const int ignition_offset = 0; // Difference between sensor and real world // Tachometer const int tach_pulse_length = 5000; // 5ms equiv to dwell time on old coil at sensible RPM (tach pulse length) @@ -50,8 +48,8 @@ const int tach_pulse_length = 5000; // 5ms equiv to dwell //int o2_current_value = 0; // Curent milivolts // Battery -byte battery_voltage_value = 13; -byte battery_voltage_index = 7; -const int battery_cal_7v = 252; -const int battery_cal_16v = 579; +byte battery_voltage_value = 13; // A default - is nuked quickly +byte battery_voltage_index = 7; // The above as an index +const int battery_cal_7v = 252; // Cal value for 7v using 10k/1k potential divider +const int battery_cal_16v = 579; // The above for 16v diff --git a/debug.ino b/debug.ino index 9123c7b..815853b 100644 --- a/debug.ino +++ b/debug.ino @@ -39,12 +39,14 @@ void task_debug_run() Serial.print(battery_voltage_value); Serial.println(""); + // Log if we're out of sync timing-wise if(cas_sync_fail_log > 0) { Serial.println("ERR: CAS sync fail"); cas_sync_fail_log = 0; } + // Log if we've gone over RPM if(rpm_limited_log > 0) { Serial.println("ERR: RPM limit"); diff --git a/map.ino b/map.ino index db3a6ab..3c9565e 100644 --- a/map.ino +++ b/map.ino @@ -2,6 +2,7 @@ void map_init() { task_map_run(); + // Highest and lowest kPa anywhere on earth if(map_current_value < 87) Serial.println("ERR: MAP low"); else if(map_current_value > 108) diff --git a/pins.h b/pins.h index d91a151..d6ae40d 100644 --- a/pins.h +++ b/pins.h @@ -19,7 +19,6 @@ const int pin_batt = A4; // IN: Battery voltage const int pin_coil1 = 47; const int pin_coil2 = 45; -const int pin_coil3 = 43; // TMP: SGT out -const int pin_coil4 = 41; // TMP: SGC out + diff --git a/schedule.h b/schedule.h index 07f6f1e..44d8099 100644 --- a/schedule.h +++ b/schedule.h @@ -17,10 +17,4 @@ unsigned long task_coil1_charge = 0; // Idle unsigned long task_coil1_fire = 0; // Idle unsigned long task_coil2_charge = 0; // Idle unsigned long task_coil2_fire = 0; // Idle -/* -unsigned long task_coil3_charge = 0; // Idle -unsigned long task_coil3_fire = 0; // Idle -unsigned long task_coil4_charge = 0; // Idle -unsigned long task_coil4_fire = 0; // Idle -*/ diff --git a/schedule.ino b/schedule.ino index 4cf0183..a41ee1e 100644 --- a/schedule.ino +++ b/schedule.ino @@ -29,33 +29,7 @@ void schedule_process() task_coil2_charge = 0; tasks++; } -/* - // Coil 3 - if((task_coil3_fire > 0) && (micros() > task_coil3_fire)) - { - task_coilx_fire_run(pin_coil3); - task_coil3_fire = 0; - tasks++; - }else if((task_coil3_charge > 0) && (micros() > task_coil3_charge)) - { - task_coilx_charge_run(pin_coil3); - task_coil3_charge = 0; - tasks++; - } - // Coil 4 - if((task_coil4_fire > 0) && (micros() > task_coil4_fire)) - { - task_coilx_fire_run(pin_coil4); - task_coil4_fire = 0; - tasks++; - }else if((task_coil4_charge > 0) && (micros() > task_coil4_charge)) - { - task_coilx_charge_run(pin_coil4); - task_coil4_charge = 0; - tasks++; - } -*/ // Only carry on to the lower priority stuff if we did nothing else if(tasks > 0) return;