diff --git a/ECU.ino b/ECU.ino index a43d6d3..d51dfee 100644 --- a/ECU.ino +++ b/ECU.ino @@ -7,7 +7,8 @@ void setup() { // Sensors pinMode(pin_map, INPUT); pinMode(pin_batt, INPUT); - + pinMode(pin_throttle, INPUT); + // Dash / debug outputs pinMode(pin_tach, OUTPUT); digitalWrite(pin_tach, LOW); @@ -21,9 +22,10 @@ void setup() { // Set up serial speed etc and do intro output debug_setup(); - // Get an initial reading and output debug if it's not atmospheric + // Get initial values and check they are sane map_init(); battery_init(); + throttle_init(); } void loop() { diff --git a/data.h b/data.h index c89985b..3d26605 100644 --- a/data.h +++ b/data.h @@ -1,55 +1,61 @@ // MAP Sensor -const int map_sensor_min_kpa = 20; // Physical minimum of the hardware -const int map_sensor_max_kpa = 250; // Physical maximum of the hardware -const int map_range_min = 20; // Index 0 (min we care about) -const int map_range_max = 100; // Index 15 (max we care about, anything >100 can be atmospheric as far as we care) -byte map_current_index = 0; // Index -byte map_current_value = 0; // KPA value +const int map_sensor_min_kpa = 20; // Physical minimum of the hardware +const int map_sensor_max_kpa = 250; // Physical maximum of the hardware +const int map_range_min = 20; // Index 0 (min we care about) +const int map_range_max = 100; // Index 15 (max we care about, anything >100 can be atmospheric as far as we care) +byte map_current_index = 0; // Index +byte map_current_value = 0; // KPA value // Rotation -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 -int rpm_current_value = 0; // Current engine RPM -int usec_per_degree = 0; // uSec per degree of rotation at current RPM -//unsigned long usec_per_degree = 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 +int rpm_current_value = 0; // Current engine RPM +int usec_per_degree = 0; // uSec per degree of rotation at current RPM +//unsigned long usec_per_degree = 0; // Cam Angle Sensor -unsigned long cas_sgt_lastrise = 0; // micros() of the last rise -unsigned long cas_sgt_lastfall = 0; // "" of the last fall -byte cas_sgt_lastvalue = 0; // The state of SGT on previous cycle +unsigned long cas_sgt_lastrise = 0; // micros() of the last rise +unsigned long cas_sgt_lastfall = 0; // "" of the last fall +byte cas_sgt_lastvalue = 0; // The state of SGT on previous cycle -unsigned long cas_sgc_lastrise = 0; // micros() of the last rise -unsigned long cas_sgc_lastfall = 0; // "" of the last fall -byte cas_sgc_lastvalue = 0; // The state of SGC on the previous cycle +unsigned long cas_sgc_lastrise = 0; // micros() of the last rise +unsigned long cas_sgc_lastfall = 0; // "" of the last fall +byte cas_sgc_lastvalue = 0; // The state of SGC on the previous cycle -byte cas_sync_fail = 0; -byte cas_sync_fail_log = 0; +byte cas_sync_fail = 0; // We lost track of a cylinder +byte cas_sync_fail_log = 0; // We need to output this in debug // Cylinder sequencing -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 -byte cylinder_next[] = {0,2,1,2,1}; // CAn be 0,3,1,4,2 if we ever go sequential +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 +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; // 4 read from scope on factory ECU -const int ignition_offset = 0; // Difference between sensor and real world +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) +const int tach_pulse_length = 5000; // 5ms equiv to dwell time on old coil at sensible RPM (tach pulse length) // O2 sensor -//const int o2_min = 200; // 200mV is lean -//const int o2_target = 450; // 450mV is 14.7:1 apparently... -//const int o2_max = 800; // 800mv is rich -//int o2_current_value = 0; // Curent milivolts +//const int o2_min = 200; // 200mV is lean +//const int o2_target = 450; // 450mV is 14.7:1 apparently... +//const int o2_max = 800; // 800mv is rich +//int o2_current_value = 0; // Curent milivolts // Battery -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 +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 + +// TPS +byte throttle_current_value = 0; // Percentage +byte throttle_previous_1_value = 0; // Percentage +byte throttle_previous_2_value = 0; // Percentage +byte throttle_delta = 0; // Difference between current and delta diff --git a/debug.ino b/debug.ino index 815853b..3586627 100644 --- a/debug.ino +++ b/debug.ino @@ -17,6 +17,8 @@ void debug_setup() Serial.print("rpm_current_value"); Serial.print(","); Serial.print("battery_voltage_value"); + Serial.print(","); + Serial.print("throttle_current_value"); Serial.println(""); // Now we have init we can do real loop @@ -37,6 +39,8 @@ void task_debug_run() Serial.print(rpm_current_value); Serial.print(","); Serial.print(battery_voltage_value); + Serial.print(","); + Serial.print(throttle_current_value); Serial.println(""); // Log if we're out of sync timing-wise diff --git a/pins.h b/pins.h index d6ae40d..6be5472 100644 --- a/pins.h +++ b/pins.h @@ -7,7 +7,7 @@ const int pin_cas_sgt = 18; // IN: ignition pulse -white const int pin_tach = 49; // OUT: pulse for tach //const int pin_intake_temp = A0; // IN: Intake temperature //const int pin_coolant_temp = A1; // IN: Coolant temperature -//const int pin_throttle_position = A2; // IN: Throttle position +const int pin_throttle = A2; // IN: Throttle position const int pin_map = A3; // IN: Manifold absolute pressure const int pin_batt = A4; // IN: Battery voltage //const int pin_o2 = A8; // IN: O2 sensor diff --git a/schedule.h b/schedule.h index 44d8099..c9b592f 100644 --- a/schedule.h +++ b/schedule.h @@ -1,20 +1,23 @@ -unsigned long task_map = 1; // Get initial figure -const unsigned long task_map_interval = 93000; // 93ms +unsigned long task_map = 1; // Get initial figure +const unsigned long task_map_interval = 93000; // 93ms -unsigned long task_rpm = 1; // Get initial figure -const unsigned long task_rpm_interval = 199000; // 199ms +unsigned long task_throttle = 1; // Get initial figure +const unsigned long task_throttle_interval = 88000; // 88ms -unsigned long task_battery = 1; // Get initial figure -const unsigned long task_battery_interval = 265000; // 265ms +unsigned long task_rpm = 1; // Get initial figure +const unsigned long task_rpm_interval = 199000; // 199ms + +unsigned long task_battery = 1; // Get initial figure +const unsigned long task_battery_interval = 265000; // 265ms unsigned long task_debug = 0; // Triggers after init const unsigned long task_debug_interval = 387000; // 2s (eh?) -unsigned long task_tach_high = 0; // Ilde -unsigned long task_tach_low = 0; // Idle +unsigned long task_tach_high = 0; // Ilde +unsigned long task_tach_low = 0; // Idle -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_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 diff --git a/schedule.ino b/schedule.ino index a41ee1e..a1cb90e 100644 --- a/schedule.ino +++ b/schedule.ino @@ -45,6 +45,11 @@ void schedule_process() task_rpm = micros() + task_rpm_interval; task_rpm_run(); tasks++; + }else if(micros() > task_throttle) + { + task_throttle = micros() + task_throttle_interval; + task_throttle_run(); + tasks++; }else if(micros() > task_battery) { task_battery = micros() + task_battery_interval; diff --git a/throttle.ino b/throttle.ino new file mode 100644 index 0000000..e0fe538 --- /dev/null +++ b/throttle.ino @@ -0,0 +1,13 @@ +void throttle_init() +{ + task_throttle_run(); +} + +void task_throttle_run() +{ + throttle_previous_2_value = throttle_previous_1_value; + throttle_previous_1_value = throttle_current_value; + throttle_current_value = map(analogRead(pin_throttle), 0, 1023, 0, 100); +} + +