mirror of
https://github.com/stevenhowes/MX5-Arduino.git
synced 2026-05-27 00:03:37 +01:00
Cleanup
This commit is contained in:
@@ -4,36 +4,33 @@
|
|||||||
#include "tables.h"
|
#include "tables.h"
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
debug_setup();
|
// Sensors
|
||||||
|
|
||||||
pinMode(pin_map, INPUT);
|
pinMode(pin_map, INPUT);
|
||||||
|
pinMode(pin_batt, INPUT);
|
||||||
|
|
||||||
|
// Dash / debug outputs
|
||||||
pinMode(pin_tach, OUTPUT);
|
pinMode(pin_tach, OUTPUT);
|
||||||
|
digitalWrite(pin_tach, LOW);
|
||||||
|
|
||||||
|
// Ignition outputs
|
||||||
pinMode(pin_coil1, OUTPUT);
|
pinMode(pin_coil1, OUTPUT);
|
||||||
pinMode(pin_coil2, OUTPUT);
|
pinMode(pin_coil2, OUTPUT);
|
||||||
pinMode(pin_coil3, OUTPUT);
|
|
||||||
pinMode(pin_coil4, OUTPUT);
|
|
||||||
|
|
||||||
digitalWrite(pin_tach, LOW);
|
|
||||||
digitalWrite(pin_coil1, LOW);
|
digitalWrite(pin_coil1, LOW);
|
||||||
digitalWrite(pin_coil2, 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
|
// Get an initial reading and output debug if it's not atmospheric
|
||||||
map_init();
|
map_init();
|
||||||
battery_init();
|
battery_init();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
// Always process the CAS
|
||||||
cas_process();
|
cas_process();
|
||||||
schedule_process();
|
|
||||||
|
|
||||||
// map_process();
|
|
||||||
// cas_process();
|
|
||||||
// tach_process();
|
|
||||||
// ignition_process();
|
|
||||||
|
|
||||||
|
|
||||||
|
// Run scheduler
|
||||||
|
schedule_process();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
void battery_init()
|
void battery_init()
|
||||||
{
|
{
|
||||||
|
// Run once to get start info
|
||||||
task_battery_run();
|
task_battery_run();
|
||||||
|
|
||||||
|
// Log if battery is exceptional in any way
|
||||||
if(battery_voltage_value < 12)
|
if(battery_voltage_value < 12)
|
||||||
{
|
{
|
||||||
Serial.println("ERR: Battery low");
|
Serial.println("ERR: Battery low");
|
||||||
@@ -11,12 +13,18 @@ void battery_init()
|
|||||||
Serial.println("ERR: Battery high");
|
Serial.println("ERR: Battery high");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
Serial.println("INF: Battery ok");
|
Serial.println("INF: Battery ok");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void task_battery_run()
|
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);
|
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;
|
battery_voltage_index = constrain(battery_voltage_value,7,16) - 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ void cas_process()
|
|||||||
byte sgt = digitalRead(pin_cas_sgt);
|
byte sgt = digitalRead(pin_cas_sgt);
|
||||||
unsigned long timestamp = micros();
|
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)
|
if(rpm_current_value > rpm_range_max)
|
||||||
{
|
{
|
||||||
rpm_limited_log = 1;
|
rpm_limited_log = 1;
|
||||||
@@ -21,6 +23,7 @@ void cas_process()
|
|||||||
// Handle tach
|
// Handle tach
|
||||||
task_tach_high = micros();
|
task_tach_high = micros();
|
||||||
|
|
||||||
|
// Log when it was
|
||||||
cas_sgt_lastrise = timestamp;
|
cas_sgt_lastrise = timestamp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -33,12 +36,14 @@ void cas_process()
|
|||||||
// Difference between last and current is 180 degrees
|
// Difference between last and current is 180 degrees
|
||||||
usec_per_degree = (timestamp - cas_sgt_lastfall) / 180;
|
usec_per_degree = (timestamp - cas_sgt_lastfall) / 180;
|
||||||
|
|
||||||
|
// Log when it was
|
||||||
cas_sgt_lastfall = timestamp;
|
cas_sgt_lastfall = timestamp;
|
||||||
|
|
||||||
// If we have crank pulse present then cyl1 is TDC
|
// If we have crank pulse present then cyl1 is TDC
|
||||||
if(digitalRead(pin_cas_sgc) == HIGH)
|
if(digitalRead(pin_cas_sgc) == HIGH)
|
||||||
{
|
{
|
||||||
// If we've arrived at cylinder 1 unexpectedly then we need to know!
|
// 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)
|
if(cylinder_next[cylinder_tdc] != 1)
|
||||||
{
|
{
|
||||||
cas_sync_fail=1;
|
cas_sync_fail=1;
|
||||||
@@ -46,6 +51,7 @@ void cas_process()
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
// Clear it if we're in sync, but don't clear log flag
|
||||||
cas_sync_fail = 0;
|
cas_sync_fail = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,7 +66,7 @@ void cas_process()
|
|||||||
// Schedule next cylinder, this one is already past TDC
|
// Schedule next cylinder, this one is already past TDC
|
||||||
cylinder_next_fire = cylinder_next[cylinder_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((cas_sync_fail == 0) && (rpm_limited == 0))
|
||||||
{
|
{
|
||||||
if(cylinder_next_fire == 1)
|
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_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]);
|
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;
|
cas_sgt_lastvalue = sgt;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ byte map_current_index = 0; // Index
|
|||||||
byte map_current_value = 0; // KPA value
|
byte map_current_value = 0; // KPA value
|
||||||
|
|
||||||
// Rotation
|
// Rotation
|
||||||
byte rpm_limited = 0;
|
byte rpm_limited = 0; // Are we currently running over RPM?
|
||||||
byte rpm_limited_log = 0;
|
byte rpm_limited_log = 0; // Are we due a warning message
|
||||||
const int rpm_range_min = 0; // Index 0
|
const int rpm_range_min = 0; // Index 0
|
||||||
const int rpm_range_max = 7200; // Index 15
|
const int rpm_range_max = 7200; // Index 15
|
||||||
byte rpm_current_index = 0; // Table index of current RPM
|
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_tdc = 0; // Most recent cylinder to hit TDC
|
||||||
byte cylinder_next_fire = 0; // Next cylinder due a spark
|
byte cylinder_next_fire = 0; // Next cylinder due a spark
|
||||||
//byte cylinder_next_inject = 0; // Next cylinder due fuel
|
//byte cylinder_next_inject = 0; // Next cylinder due fuel
|
||||||
// Sequential and wasted spark
|
byte cylinder_next[] = {0,2,1,2,1}; // CAn be 0,3,1,4,2 if we ever go sequential
|
||||||
//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
|
|
||||||
|
|
||||||
// Ignition
|
// Ignition
|
||||||
const int coil_dwell = 4000; // 5ms for stock coil
|
const int coil_dwell = 4000; // 4 read from scope on factory ECU
|
||||||
const int ignition_offset = 0;
|
const int ignition_offset = 0; // Difference between sensor and real world
|
||||||
|
|
||||||
// Tachometer
|
// 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)
|
||||||
@@ -50,8 +48,8 @@ const int tach_pulse_length = 5000; // 5ms equiv to dwell
|
|||||||
//int o2_current_value = 0; // Curent milivolts
|
//int o2_current_value = 0; // Curent milivolts
|
||||||
|
|
||||||
// Battery
|
// Battery
|
||||||
byte battery_voltage_value = 13;
|
byte battery_voltage_value = 13; // A default - is nuked quickly
|
||||||
byte battery_voltage_index = 7;
|
byte battery_voltage_index = 7; // The above as an index
|
||||||
const int battery_cal_7v = 252;
|
const int battery_cal_7v = 252; // Cal value for 7v using 10k/1k potential divider
|
||||||
const int battery_cal_16v = 579;
|
const int battery_cal_16v = 579; // The above for 16v
|
||||||
|
|
||||||
|
|||||||
@@ -39,12 +39,14 @@ void task_debug_run()
|
|||||||
Serial.print(battery_voltage_value);
|
Serial.print(battery_voltage_value);
|
||||||
Serial.println("");
|
Serial.println("");
|
||||||
|
|
||||||
|
// Log if we're out of sync timing-wise
|
||||||
if(cas_sync_fail_log > 0)
|
if(cas_sync_fail_log > 0)
|
||||||
{
|
{
|
||||||
Serial.println("ERR: CAS sync fail");
|
Serial.println("ERR: CAS sync fail");
|
||||||
cas_sync_fail_log = 0;
|
cas_sync_fail_log = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Log if we've gone over RPM
|
||||||
if(rpm_limited_log > 0)
|
if(rpm_limited_log > 0)
|
||||||
{
|
{
|
||||||
Serial.println("ERR: RPM limit");
|
Serial.println("ERR: RPM limit");
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ void map_init()
|
|||||||
{
|
{
|
||||||
task_map_run();
|
task_map_run();
|
||||||
|
|
||||||
|
// Highest and lowest kPa anywhere on earth
|
||||||
if(map_current_value < 87)
|
if(map_current_value < 87)
|
||||||
Serial.println("ERR: MAP low");
|
Serial.println("ERR: MAP low");
|
||||||
else if(map_current_value > 108)
|
else if(map_current_value > 108)
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ const int pin_batt = A4; // IN: Battery voltage
|
|||||||
|
|
||||||
const int pin_coil1 = 47;
|
const int pin_coil1 = 47;
|
||||||
const int pin_coil2 = 45;
|
const int pin_coil2 = 45;
|
||||||
const int pin_coil3 = 43; // TMP: SGT out
|
|
||||||
const int pin_coil4 = 41; // TMP: SGC out
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -17,10 +17,4 @@ unsigned long task_coil1_charge = 0; // Idle
|
|||||||
unsigned long task_coil1_fire = 0; // Idle
|
unsigned long task_coil1_fire = 0; // Idle
|
||||||
unsigned long task_coil2_charge = 0; // Idle
|
unsigned long task_coil2_charge = 0; // Idle
|
||||||
unsigned long task_coil2_fire = 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
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|||||||
@@ -29,33 +29,7 @@ void schedule_process()
|
|||||||
task_coil2_charge = 0;
|
task_coil2_charge = 0;
|
||||||
tasks++;
|
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
|
// Only carry on to the lower priority stuff if we did nothing else
|
||||||
if(tasks > 0)
|
if(tasks > 0)
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user