diff --git a/lambda-test/command-test.c b/lambda-test/command-test.c index 8474463..48fac5a 100644 --- a/lambda-test/command-test.c +++ b/lambda-test/command-test.c @@ -21,17 +21,17 @@ static bool testIsSimulation(void) { setupPorts(); - setHeatingOn(false); + setHeaterOn(false); assertFalse(isSimulation()); runCommand("se"); assertTrue(isSimulation()); assertTrue(getTime() == 0); - assertTrue(isHeatingOn()); + assertTrue(isHeaterOn()); runCommand("sd"); assertFalse(isSimulation()); assertTrue(getTime() == 0); - // assertFalse(isHeatingOn()); + // assertFalse(isHeaterOn()); return true; } @@ -46,15 +46,15 @@ return true; } -static bool testHeating(void) { +static bool testHeater(void) { setupPorts(); - setHeatingOn(false); + setHeaterOn(false); - assertFalse(isHeatingOn()); + assertFalse(isHeaterOn()); runCommand("he"); - assertTrue(isHeatingOn()); + assertTrue(isHeaterOn()); runCommand("hd"); - assertFalse(isHeatingOn()); + assertFalse(isHeaterOn()); return true; } @@ -75,14 +75,14 @@ /* Test names */ static const char testIsSimulation_P[] PROGMEM = "testIsSimulation"; static const char testIsLogging_P[] PROGMEM = "testIsLogging"; -static const char testHeating_P[] PROGMEM = "testHeating"; +static const char testHeater_P[] PROGMEM = "testHeater"; static const char testCycleDisplay_P[] PROGMEM = "testCycleDisplay"; /* Tests */ static TestCase const tests[] = { {class, testIsSimulation_P, testIsSimulation}, {class, testIsLogging_P, testIsLogging}, - {class, testHeating_P, testHeating}, + {class, testHeater_P, testHeater}, {class, testCycleDisplay_P, testCycleDisplay} }; diff --git a/lambda-test/display-test.c b/lambda-test/display-test.c index 24fe5d2..1d62d72 100644 --- a/lambda-test/display-test.c +++ b/lambda-test/display-test.c @@ -27,32 +27,32 @@ static bool testCycle(void) { cancelAlert(true); - position = 0; + position = displayPosCurrent;; updatePending = false; - assertTrue(position == 0); + assertTrue(position == displayPosCurrent); assertFalse(updatePending); cycleDisplay(); - assertTrue(position == 1); + assertTrue(position == displayPosMax); assertTrue(updatePending); assertTrue(beepCount == 1); assertTrue(beepLength == 2); cycleDisplay(); - assertTrue(position == 2); + assertTrue(position == displayPosHeater); cycleDisplay(); - assertTrue(position == 3); + assertTrue(position == displayPosLastText); cycleDisplay(); - assertTrue(position == 4); + assertTrue(position == displayPosTime); cycleDisplay(); // roll over to 0 - assertTrue(position == 0); + assertTrue(position == displayPosCurrent); return true; } static bool testCycleCancelAlert(void) { - position = 0; + position = displayPosCurrent;; updatePending = false; alert(1, 1, 31, "", "", false); @@ -60,7 +60,7 @@ cycleDisplay(); assertFalse(isAlertActive()); - assertTrue(position == 0); + assertTrue(position == displayPosCurrent); assertTrue(updatePending); return true; diff --git a/lambda-test/interrupts-test.c b/lambda-test/interrupts-test.c index 8e7902e..76c54c3 100644 --- a/lambda-test/interrupts-test.c +++ b/lambda-test/interrupts-test.c @@ -24,7 +24,7 @@ // test that the beep output pin is enabled assertTrue(bit_is_set(DDRB, PB1)); - // test that the oxygen sensor heating control output pin is enabled + // test that the oxygen sensor heater control output pin is enabled assertTrue(bit_is_set(DDRB, PB1)); return true; diff --git a/lambda-test/rules-test.c b/lambda-test/rules-test.c index b9880ea..147660d 100644 --- a/lambda-test/rules-test.c +++ b/lambda-test/rules-test.c @@ -25,34 +25,34 @@ Measurement meas = {0, 0, 0}; resetRules(); - dir = -1; + dir = burning_down; reason(meas); assertFalse(rules[0].fired); resetRules(); - dir = 0; + dir = none; reason(meas); assertFalse(rules[0].fired); resetRules(); - dir = 1; + dir = firing_up; reason(meas); assertFalse(rules[0].fired); meas.tempI = 500; resetRules(); - dir = -1; + dir = burning_down; reason(meas); assertFalse(rules[0].fired); resetRules(); - dir = 0; + dir = none; reason(meas); assertFalse(rules[0].fired); resetRules(); - dir = 1; + dir = firing_up; reason(meas); assertTrue(rules[0].fired); assertTrue(50 == airgate); @@ -67,34 +67,34 @@ Measurement meas = {999, 0, 0}; resetRules(); - dir = 1; + dir = firing_up; reason(meas); assertFalse(rules[1].fired); resetRules(); - dir = 0; + dir = none; reason(meas); assertFalse(rules[1].fired); resetRules(); - dir = -1; + dir = burning_down; reason(meas); assertFalse(rules[1].fired); meas.tempI = 799; resetRules(); - dir = 1; + dir = firing_up; reason(meas); assertFalse(rules[1].fired); resetRules(); - dir = 0; + dir = none; reason(meas); assertFalse(rules[1].fired); resetRules(); - dir = -1; + dir = burning_down; reason(meas); assertTrue(rules[1].fired); assertTrue(25 == airgate); @@ -109,34 +109,34 @@ Measurement meas = {999, 0, 0}; resetRules(); - dir = 1; + dir = firing_up; reason(meas); assertFalse(rules[2].fired); resetRules(); - dir = 0; + dir = none; reason(meas); assertFalse(rules[2].fired); resetRules(); - dir = -1; + dir = burning_down; reason(meas); assertFalse(rules[2].fired); meas.tempI = 399; resetRules(); - dir = 1; + dir = firing_up; reason(meas); assertFalse(rules[2].fired); resetRules(); - dir = 0; + dir = none; reason(meas); assertFalse(rules[2].fired); resetRules(); - dir = -1; + dir = burning_down; reason(meas); assertTrue(rules[2].fired); assertTrue(0 == airgate); @@ -149,9 +149,9 @@ static bool testTooRich(void) { Measurement meas = {0, 0, 0}; - dir = 0; + dir = none; - setHeatingState(HEATING_READY); + setHeaterState(heaterStateReady); meas.tempI = 100; meas.lambda = 2000; @@ -200,9 +200,9 @@ static bool testTooLean(void) { Measurement meas = {0, 0, 0}; - dir = 0; + dir = none; - setHeatingState(HEATING_READY); + setHeaterState(heaterStateReady); meas.tempI = 500; meas.lambda = 1601; @@ -246,7 +246,7 @@ resetRules(); Measurement meas = {0, 0, 0}; - dir = 1; + dir = firing_up; meas.tempI = 50; age = 0; @@ -278,96 +278,96 @@ return true; } -static bool testHeatingReady(void) { +static bool testHeaterReady(void) { resetRules(); resetTime(); Measurement meas = {0, 0, 0}; - dir = 1; + dir = firing_up; - setHeatingOn(false); + setHeaterOn(false); meas.current = 1300; reason(meas); - assertTrue(HEATING_OFF == getHeatingState()); + assertTrue(heaterStateOff == getHeaterState()); - setHeatingOn(true); + setHeaterOn(true); meas.current = 5000; reason(meas); - assertTrue(HEATING_UP == getHeatingState()); + assertTrue(heaterStateUp == getHeaterState()); - setHeatingOn(true); + setHeaterOn(true); meas.current = 1300; reason(meas); - assertTrue(HEATING_READY == getHeatingState()); + assertTrue(heaterStateReady == getHeaterState()); cancelAlert(false); return true; } -static bool testHeatingFaultNoconn(void) { +static bool testHeaterFaultNoconn(void) { resetRules(); resetTime(); Measurement meas = {0, 0, 0}; - dir = 1; + dir = firing_up; - setHeatingOn(true); + setHeaterOn(true); meas.current = 0; reason(meas); - assertTrue(HEATING_FAULT == getHeatingState()); + assertTrue(heaterStateFault == getHeaterState()); cancelAlert(false); return true; } -static bool testHeatingFaultShort(void) { +static bool testHeaterFaultShort(void) { resetRules(); resetTime(); Measurement meas = {0, 0, 0}; - dir = 1; + dir = firing_up; - setHeatingOn(true); + setHeaterOn(true); meas.current = 8000; reason(meas); - assertTrue(HEATING_FAULT == getHeatingState()); + assertTrue(heaterStateFault == getHeaterState()); cancelAlert(false); return true; } -static bool testHeatingFaultNoheat(void) { +static bool testHeaterFaultNoheat(void) { resetRules(); resetTime(); Measurement meas = {0, 0, 0}; - dir = 1; + dir = firing_up; - setHeatingOn(true); + setHeaterOn(true); // more than 3 mins addInts(INTS_PER_SEC * 181); meas.current = 5000; reason(meas); - assertTrue(HEATING_FAULT == getHeatingState()); + assertTrue(heaterStateFault == getHeaterState()); cancelAlert(false); return true; } -static bool testHeatingTimeout(void) { +static bool testHeaterTimeout(void) { resetRules(); resetTime(); Measurement meas = {0, 0, 0}; - dir = 1; + dir = firing_up; - setHeatingOn(true); + setHeaterOn(true); // more than 3 hours below 400°C addInts(INTS_PER_SEC * 10800UL); @@ -375,7 +375,7 @@ meas.tempI = 300; meas.current = 1300; reason(meas); - assertTrue(HEATING_OFF == getHeatingState()); + assertTrue(heaterStateOff == getHeaterState()); cancelAlert(false); @@ -498,11 +498,11 @@ static const char testTooRich_P[] PROGMEM = "testTooRich"; static const char testTooLean_P[] PROGMEM = "testTooLean"; static const char testFireOut_P[] PROGMEM = "testFireOut"; -static const char testHeatingReady_P[] PROGMEM = "testHeatingReady"; -static const char testHeatingFaultNoconn_P[] PROGMEM = "testHeatingFaultNoconn"; -static const char testHeatingFaultShort_P[] PROGMEM = "testHeatingFaultShort"; -static const char testHeatingFaultNoheat_P[] PROGMEM = "testHeatingFaultNoheat"; -static const char testHeatingTimeout_P[] PROGMEM = "testHeatingTimeout"; +static const char testHeaterReady_P[] PROGMEM = "testHeaterReady"; +static const char testHeaterFaultNoconn_P[] PROGMEM = "testHeaterFaultNoconn"; +static const char testHeaterFaultShort_P[] PROGMEM = "testHeaterFaultShort"; +static const char testHeaterFaultNoheat_P[] PROGMEM = "testHeaterFaultNoheat"; +static const char testHeaterTimeout_P[] PROGMEM = "testHeaterTimeout"; static const char testReasonDirBurnUp_P[] PROGMEM = "testReasonDirBurnUp"; static const char testReasonDirBurnDown_P[] PROGMEM = "testReasonDirBurnDown"; @@ -514,11 +514,11 @@ {class, testTooRich_P, testTooRich}, {class, testTooLean_P, testTooLean}, {class, testFireOut_P, testFireOut}, - {class, testHeatingReady_P, testHeatingReady}, - {class, testHeatingFaultNoconn_P, testHeatingFaultNoconn}, - {class, testHeatingFaultShort_P, testHeatingFaultShort}, - {class, testHeatingFaultNoheat_P, testHeatingFaultNoheat}, - {class, testHeatingTimeout_P, testHeatingTimeout}, + {class, testHeaterReady_P, testHeaterReady}, + {class, testHeaterFaultNoconn_P, testHeaterFaultNoconn}, + {class, testHeaterFaultShort_P, testHeaterFaultShort}, + {class, testHeaterFaultNoheat_P, testHeaterFaultNoheat}, + {class, testHeaterTimeout_P, testHeaterTimeout}, {class, testReasonDirBurnUp_P, testReasonDirBurnUp}, {class, testReasonDirBurnDown_P, testReasonDirBurnDown} }; diff --git a/lambda-test/sensors-test.c b/lambda-test/sensors-test.c index 95679e1..e8e55f8 100644 --- a/lambda-test/sensors-test.c +++ b/lambda-test/sensors-test.c @@ -34,9 +34,9 @@ // enable pull-up resistor so the measured voltage // should be close to AREF PORTC |= ((1 << ADC_TEMPI) | (1 << ADC_TEMPO) | (1 << ADC_LAMBDA) | - (1 << ADC_HEATING)); + (1 << ADC_HEATER)); - setHeatingState(HEATING_READY); + setHeaterState(heaterStateReady); // do many measurements so the averaged voltages are near the measured // voltages (close to AREF) @@ -185,25 +185,25 @@ return ! strcmp(info, MSG_RICH); } -static bool testSetHeatingOn(void) { - setHeatingOn(true); - assertTrue(isHeatingOn()); - assertTrue(HEATING_UP == getHeatingState()); +static bool testSetHeaterOn(void) { + setHeaterOn(true); + assertTrue(isHeaterOn()); + assertTrue(heaterStateUp == getHeaterState()); - setHeatingOn(false); - assertFalse(isHeatingOn()); - assertTrue(HEATING_OFF == getHeatingState()); + setHeaterOn(false); + assertFalse(isHeaterOn()); + assertTrue(heaterStateOff == getHeaterState()); return true; } -static bool testSetHeatingState(void) { - setHeatingOn(false); - assertFalse(isHeatingOn()); - assertTrue(HEATING_OFF == getHeatingState()); +static bool testSetHeaterState(void) { + setHeaterOn(false); + assertFalse(isHeaterOn()); + assertTrue(heaterStateOff == getHeaterState()); - setHeatingState(HEATING_FAULT); - assertTrue(HEATING_FAULT == getHeatingState()); + setHeaterState(heaterStateFault); + assertTrue(heaterStateFault == getHeaterState()); return true; } @@ -232,8 +232,8 @@ static const char testToInfoOkay_P[] PROGMEM = "testToInfoOkay"; static const char testToInfoIdeal_P[] PROGMEM = "testToInfoIdeal"; static const char testToInfoRich_P[] PROGMEM = "testToInfoRich"; -static const char testSetHeatingOn_P[] PROGMEM = "testSetHeatingOn"; -static const char testSetHeatingState_P[] PROGMEM = "testSetHeatingState"; +static const char testSetHeaterOn_P[] PROGMEM = "testSetHeaterOn"; +static const char testSetHeaterState_P[] PROGMEM = "testSetHeaterState"; /* Tests */ static TestCase const tests[] = { @@ -257,8 +257,8 @@ {class, testToInfoOkay_P, testToInfoOkay}, {class, testToInfoIdeal_P, testToInfoIdeal}, {class, testToInfoRich_P, testToInfoRich}, - {class, testSetHeatingOn_P, testSetHeatingOn}, - {class, testSetHeatingState_P, testSetHeatingState} + {class, testSetHeaterOn_P, testSetHeaterOn}, + {class, testSetHeaterState_P, testSetHeaterState} }; TestClass sensorsClass = {tests, sizeof(tests) / sizeof(tests[0])}; diff --git a/lambda/TODO b/lambda/TODO index ea873a1..9594df8 100644 --- a/lambda/TODO +++ b/lambda/TODO @@ -6,7 +6,6 @@ - Take in account F_CPU everywhere CPU speed matters * Functionality -- Show fire dir and (recommended) airgate position? * Circuit - AVCC should be connected to VCC via an LC network @@ -16,4 +15,4 @@ - pins.h LCD pins - interrupts.h: INTS_PER_SEC 61 - sensors.h: SHUNT_MILLIOHMS 100 -- rules.c: heatingTimeout(): getTime() >= 10800 \ No newline at end of file +- rules.c: heaterTimeout(): getTime() >= 10800 \ No newline at end of file diff --git a/lambda/command.c b/lambda/command.c index 14b8216..bc2eab3 100644 --- a/lambda/command.c +++ b/lambda/command.c @@ -43,7 +43,7 @@ resetTime(); resetDisplay(); resetRules(); - setHeatingOn(true); + setHeaterOn(true); beep(1, 2, 31); } else if (strcmp_P(fields[0], PSTR("sd")) == 0) { @@ -65,13 +65,13 @@ beep(1, 2, 31); } else if (strcmp_P(fields[0], PSTR("he")) == 0) { - // oxygen sensor heating enable - setHeatingOn(true); + // oxygen sensor heater enable + setHeaterOn(true); beep(1, 2, 31); } else if (strcmp_P(fields[0], PSTR("hd")) == 0) { - // oxygen sensor heating disable - setHeatingOn(false); + // oxygen sensor heater disable + setHeaterOn(false); beep(1, 2, 31); } else if (strcmp_P(fields[0], PSTR("cm")) == 0) { @@ -101,8 +101,8 @@ // assuming one measurement was logged per second addInts(INTS_PER_SEC); Measurement meas = readMeas(fields, fieldCount); - if (getHeatingState() == HEATING_OFF || - getHeatingState() == HEATING_READY) { + if (getHeaterState() == heaterStateOff || + getHeaterState() == heaterStateReady) { updateMeas(meas); } reason(meas); diff --git a/lambda/display.c b/lambda/display.c index b31bf76..b932ad2 100644 --- a/lambda/display.c +++ b/lambda/display.c @@ -23,13 +23,7 @@ #include "messages.h" #include "rules.h" -#define MENU_OFF 0 -#define MENU_MAX_VALUES 1 -#define MENU_CURRENT 2 -#define MENU_LAST_TEXT 3 -#define MENU_TIME 4 - -uint8_t position = MENU_OFF; +DisplayPos position = displayPosCurrent; bool updatePending = false; Measurement measLatest = {0, 0, 2000, 0}; Measurement measMax = {0, 0, 2000, 0}; @@ -63,7 +57,7 @@ char line0[17]; char line1[17]; snprintf(line0, sizeof(line0), "Ti %3dC To %3dC ", meas.tempI, meas.tempO); - if (getHeatingState() == HEATING_READY || position == MENU_MAX_VALUES) { + if (getHeaterState() == heaterStateReady || position == displayPosMax) { snprintf(line1, sizeof(line1), "L %d.%02d %s %s", lambdaT.quot, abs(lambdaT.rem), toInfo(lambdax100), hint); } else { @@ -81,7 +75,7 @@ char line1[17]; snprintf(line1, sizeof(line1), "%d.%02d Amp", ampsT.quot, abs(ampsT.rem)); - setText(MSG_HEATING_CURRENT, line1); + setText(MSG_HEATER_CURRENT, line1); } /** @@ -101,8 +95,8 @@ return; } position++; - if (position > MENU_TIME) { - position = MENU_OFF; + if (position > displayPosTime) { + position = displayPosCurrent; } updatePending = true; beep(1, 2, 31); @@ -130,19 +124,19 @@ if (updatePending && ! isAlertActive()) { updatePending = false; - if (position == MENU_MAX_VALUES) { + if (position == displayPosMax) { displayMeas(measMax, " ^"); - } else if (position == MENU_LAST_TEXT) { + } else if (position == displayPosLastText) { setText(lastLine0, lastLine1); - } else if (position == MENU_CURRENT) { + } else if (position == displayPosHeater) { displayCurrent(measLatest.current); - } else if (position == MENU_TIME) { + } else if (position == displayPosTime) { displayTime(); } else { char* hint; switch (getDir()) { - case DIR_BURN_UP: hint = " >"; break; - case DIR_BURN_DOWN: hint = " <"; break; + case firing_up: hint = " >"; break; + case burning_down: hint = " <"; break; default: hint = " -"; break; } displayMeas(measLatest, hint); diff --git a/lambda/display.h b/lambda/display.h index 0d1ff09..efaf506 100644 --- a/lambda/display.h +++ b/lambda/display.h @@ -13,6 +13,17 @@ #include "sensors.h" +/** + * Display positions. + */ +typedef enum { + displayPosCurrent = 0, + displayPosMax = 1, + displayPosHeater = 2, + displayPosLastText = 3, + displayPosTime = 4 +} DisplayPos; + uint8_t getPosition(void); /** diff --git a/lambda/lambda.c b/lambda/lambda.c index 70b6768..2b38ce3 100644 --- a/lambda/lambda.c +++ b/lambda/lambda.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "usart.h" #include "lcdroutines.h" #include "adc.h" @@ -39,6 +40,9 @@ * runs commands sent via USART. */ int main(void) { + + // clock_prescale_set(clock_div_1); + initUSART(); lcd_init(); setupPorts(); @@ -50,7 +54,7 @@ alert_P(1, 2, 31, PSTR(MSG_WELCOME), PSTR(""), false); // spend some time on being polite while (getTime() < 3) {} - setHeatingOn(true); + setHeaterOn(true); uint32_t ints = 0; Measurement meas; diff --git a/lambda/messages.h b/lambda/messages.h index fcd2cd8..7c16f89 100644 --- a/lambda/messages.h +++ b/lambda/messages.h @@ -30,15 +30,15 @@ #define MSG_TOO_RICH_0 "Too rich, open" #define MSG_TOO_RICH_1 "air gate" #define MSG_FIRE_OUT_0 "Fire out?" - #define MSG_HEATING_UP_0 "Oxygen sensor" - #define MSG_HEATING_UP_1 "heating up..." - #define MSG_HEATING_READY_0 "Oxygen sensor" - #define MSG_HEATING_READY_1 "ready" - #define MSG_HEATING_FAULT_0 "Oxygen sensor" - #define MSG_HEATING_FAULT_1 "heating fault!" - #define MSG_HEATING_OFF_0 "Oxygen sensor" - #define MSG_HEATING_OFF_1 "off" - #define MSG_HEATING_CURRENT "Heater current" + #define MSG_HEATER_UP_0 "Oxygen sensor" + #define MSG_HEATER_UP_1 "heating up..." + #define MSG_HEATER_READY_0 "Oxygen sensor" + #define MSG_HEATER_READY_1 "ready" + #define MSG_HEATER_FAULT_0 "Oxygen sensor" + #define MSG_HEATER_FAULT_1 "heater fault!" + #define MSG_HEATER_OFF_0 "Oxygen sensor" + #define MSG_HEATER_OFF_1 "off" + #define MSG_HEATER_CURRENT "Heater current" #define MSG_TIME_SINCE_START "Time since start" #elif LANG == 1 @@ -59,15 +59,15 @@ #define MSG_TOO_RICH_0 "Zu fett, Luft-" #define MSG_TOO_RICH_1 "schieber oeffnen" #define MSG_FIRE_OUT_0 "Feuer aus?" - #define MSG_HEATING_UP_0 "Lambdasonde" - #define MSG_HEATING_UP_1 "waermt auf..." - #define MSG_HEATING_READY_0 "Lambdasonde" - #define MSG_HEATING_READY_1 "bereit" - #define MSG_HEATING_FAULT_0 "Lambdasonden-" - #define MSG_HEATING_FAULT_1 "heizung Fehler!" - #define MSG_HEATING_OFF_0 "Lambdasonde" - #define MSG_HEATING_OFF_1 "aus" - #define MSG_HEATING_CURRENT "Heizstrom" + #define MSG_HEATER_UP_0 "Lambdasonde" + #define MSG_HEATER_UP_1 "waermt auf..." + #define MSG_HEATER_READY_0 "Lambdasonde" + #define MSG_HEATER_READY_1 "bereit" + #define MSG_HEATER_FAULT_0 "Lambdasonden-" + #define MSG_HEATER_FAULT_1 "heizung Fehler!" + #define MSG_HEATER_OFF_0 "Lambdasonde" + #define MSG_HEATER_OFF_1 "aus" + #define MSG_HEATER_CURRENT "Heizstrom" #define MSG_TIME_SINCE_START "Zeit seit Start" #endif diff --git a/lambda/pins.h b/lambda/pins.h index 931b6cd..f307075 100644 --- a/lambda/pins.h +++ b/lambda/pins.h @@ -18,8 +18,8 @@ #define ADC_TEMPO PC0 /** ADC pin for the LSM 11 oxygen sensor signal */ #define ADC_LAMBDA PC1 -/** ADC pin for the LSM 11 oxygen sensor heating current */ -#define ADC_HEATING PC3 +/** ADC pin for the LSM 11 oxygen sensor heater current */ +#define ADC_HEATER PC3 /** Port for menu button, beeper and oxygen sensor heater */ #define PORT PORTB diff --git a/lambda/rules.c b/lambda/rules.c index 9d801be..2fa7f47 100644 --- a/lambda/rules.c +++ b/lambda/rules.c @@ -17,7 +17,7 @@ #define TONE 31 uint8_t age = 0; -int8_t dir = 0; +FireDir dir = none; uint8_t airgate = 100; static Measurement rulesMeasMax = {0, 0, 2000, 0}; @@ -33,7 +33,7 @@ */ static void airgate50(bool* const fired, int8_t const dir, Measurement const meas) { - if (! *fired && dir == DIR_BURN_UP && meas.tempI >= 500) { + if (! *fired && dir == firing_up && meas.tempI >= 500) { airgate = 50; alert_P(BEEPS, LENGTH, TONE, PSTR(MSG_AIRGATE_50_0), PSTR(""), false); *fired = true; @@ -46,7 +46,7 @@ */ static void airgate25(bool* const fired, int8_t const dir, Measurement const meas) { - if (! *fired && dir == DIR_BURN_DOWN && meas.tempI < 800) { + if (! *fired && dir == burning_down && meas.tempI < 800) { airgate = 25; alert_P(BEEPS, LENGTH, TONE, PSTR(MSG_AIRGATE_25_0), PSTR(""), false); *fired = true; @@ -59,8 +59,8 @@ */ static void airgateClose(bool* const fired, int8_t const dir, Measurement const meas) { - if (! *fired && dir == DIR_BURN_DOWN && meas.tempI < 450) { - setHeatingOn(false); + if (! *fired && dir == burning_down && meas.tempI < 450) { + setHeaterOn(false); airgate = 0; alert_P(BEEPS, LENGTH, TONE, PSTR(MSG_AIRGATE_CLOSE_0), PSTR(""), false); @@ -74,7 +74,7 @@ static void tooRich(bool* const fired, int8_t const dir, Measurement const meas) { if (! *fired && meas.tempI > 100 && meas.lambda < 1200 && - getHeatingState() == HEATING_READY && airgate < 100) { + getHeaterState() == heaterStateReady && airgate < 100) { airgate = 100; alert_P(BEEPS, LENGTH, TONE, PSTR(MSG_TOO_RICH_0), PSTR(MSG_TOO_RICH_1), false); @@ -91,7 +91,7 @@ static void tooLean(bool* const fired, int8_t const dir, Measurement const meas) { if (! *fired && meas.tempI > 500 && meas.lambda > 1600 && - getHeatingState() == HEATING_READY && airgate > 50) { + getHeaterState() == heaterStateReady && airgate > 50) { airgate = 50; alert_P(BEEPS, LENGTH, TONE, PSTR(MSG_AIRGATE_50_0), PSTR(""), false); *fired = true; @@ -106,7 +106,7 @@ */ static void fireOut(bool* const fired, int8_t const dir, Measurement const meas) { - if (! *fired && dir == DIR_BURN_UP && meas.tempI < 100 && + if (! *fired && dir == firing_up && meas.tempI < 100 && rulesMeasMax.tempI - meas.tempI > 25) { alert_P(BEEPS, LENGTH, TONE, PSTR(MSG_FIRE_OUT_0), PSTR(""), false); *fired = true; @@ -117,49 +117,49 @@ } /** - * Notifies that the heating is ready and sets the corresponding state. + * Notifies that the heater is ready and sets the corresponding state. */ -static void heatingReady(bool* const fired, int8_t const dir, +static void heaterReady(bool* const fired, int8_t const dir, Measurement const meas) { - if (! isHeatingOn() || getHeatingState() == HEATING_READY) { + if (! isHeaterOn() || getHeaterState() == heaterStateReady) { return; } - if (meas.current <= HEATING_READY_MA && meas.current > HEATING_DISCONN_MA) { - setHeatingState(HEATING_READY); - alert_P(3, 10, TONE, PSTR(MSG_HEATING_READY_0), - PSTR(MSG_HEATING_READY_1), false); + if (meas.current <= milliAmpsReady && meas.current > milliAmpsDisconn) { + setHeaterState(heaterStateReady); + alert_P(3, 10, TONE, PSTR(MSG_HEATER_READY_0), + PSTR(MSG_HEATER_READY_1), false); } } /** - * Notifies that the heating or its connection is faulty and sets the + * Notifies that the heater or its connection is faulty and sets the * corresponding state. */ -static void heatingFault(bool* const fired, int8_t const dir, +static void heaterFault(bool* const fired, int8_t const dir, Measurement const meas) { - if (! isHeatingOn() || getHeatingState() == HEATING_FAULT) { + if (! isHeaterOn() || getHeaterState() == heaterStateFault) { return; } - if (meas.current > HEATING_SHORT_MA || meas.current < HEATING_DISCONN_MA || - (getTime() >= 180 && meas.current > HEATING_READY_MA)) { + if (meas.current > milliAmpsShort || meas.current < milliAmpsDisconn || + (getTime() >= 180 && meas.current > milliAmpsReady)) { // short circuit or disconnected or did not warm up within 3 minutes - setHeatingOn(false); - setHeatingState(HEATING_FAULT); - alert_P(BEEPS, LENGTH, TONE, PSTR(MSG_HEATING_FAULT_0), - PSTR(MSG_HEATING_FAULT_1), true); + setHeaterOn(false); + setHeaterState(heaterStateFault); + alert_P(BEEPS, LENGTH, TONE, PSTR(MSG_HEATER_FAULT_0), + PSTR(MSG_HEATER_FAULT_1), true); } } /** - * Switches the heating off if it is still on after 3 hours and there does + * Switches the heater off if it is still on after 3 hours and there does * not seem to be a fire. */ -static void heatingTimeout(bool* const fired, int8_t const dir, +static void heaterTimeout(bool* const fired, int8_t const dir, Measurement const meas) { - if (isHeatingOn() && getTime() >= 10800 && meas.tempI < 400) { - setHeatingOn(false); - alert_P(3, 10, TONE, PSTR(MSG_HEATING_OFF_0), - PSTR(MSG_HEATING_OFF_1), false); + if (isHeaterOn() && getTime() >= 10800 && meas.tempI < 400) { + setHeaterOn(false); + alert_P(3, 10, TONE, PSTR(MSG_HEATER_OFF_0), + PSTR(MSG_HEATER_OFF_1), false); } } @@ -181,9 +181,9 @@ * Heater rules applied to each not averaged measured heater current value */ Rule heaterRules[] = { - {false, heatingReady}, - {false, heatingFault}, - {false, heatingTimeout} + {false, heaterReady}, + {false, heaterFault}, + {false, heaterTimeout} }; @@ -209,13 +209,13 @@ // try to figure out if the fire is building up or burning down by // comparing current measurements with ones that are 3 minutes old. if (age >= 180) { - dir = DIR_NONE; + dir = none; if ((meas.tempI - rulesMeasPrev.tempI) >= 10 && rulesMeasMax.tempI < 800 && meas.lambda >= 2000) { - dir = DIR_BURN_UP; + dir = firing_up; } else if ((rulesMeasPrev.tempI - meas.tempI) >= 1 && rulesMeasMax.tempI > 800 && meas.lambda >= 2000) { - dir = DIR_BURN_DOWN; + dir = burning_down; } rulesMeasPrev = meas; @@ -234,7 +234,7 @@ rulesMeasMax.tempI = 0; age = 0; - dir = DIR_NONE; + dir = none; airgate = 100; size_t rulesSize = sizeof(rules) / sizeof(rules[0]); diff --git a/lambda/rules.h b/lambda/rules.h index a3db885..0df4ed5 100644 --- a/lambda/rules.h +++ b/lambda/rules.h @@ -11,9 +11,11 @@ #include #include "sensors.h" -#define DIR_NONE 0 -#define DIR_BURN_UP 1 -#define DIR_BURN_DOWN -1 +typedef enum { + none = 0, + firing_up = 1, + burning_down = -1 +} FireDir; /** * An attempt to create some sort of rule "object". diff --git a/lambda/sensors.c b/lambda/sensors.c index b2ad47d..dee5abd 100644 --- a/lambda/sensors.c +++ b/lambda/sensors.c @@ -21,7 +21,7 @@ #include "pins.h" #include "messages.h" -static int8_t heatingState = HEATING_OFF; +static HeaterState heaterState = heaterStateOff; /** * Table used to look up the lambda value at 12 V heater voltage @@ -91,7 +91,7 @@ tempOVoltageAvg = tempOVoltage + tempOVoltageAvg - ((tempOVoltageAvg - 4) >> 3); - if (heatingState == HEATING_READY) { + if (heaterState == heaterStateReady) { uint32_t lambdaVoltage = linADC(getVoltage(ADC_LAMBDA)); lambdaVoltageAvg = lambdaVoltage + lambdaVoltageAvg - ((lambdaVoltageAvg - 4) >> 3); @@ -99,13 +99,13 @@ lambdaVoltageAvg = 44 << 3; } - uint16_t heatingVoltage = linADC(getVoltage(ADC_HEATING)); + uint16_t heaterVoltage = linADC(getVoltage(ADC_HEATER)); Measurement meas; meas.tempI = toTempI(tempIVoltageAvg >> 3); meas.tempO = toTempO(tempOVoltageAvg >> 3); meas.lambda = toLambda(lambdaVoltageAvg >> 3); - meas.current = toCurrent(heatingVoltage); + meas.current = toCurrent(heaterVoltage); return meas; } @@ -191,25 +191,25 @@ } } -void setHeatingOn(bool const on) { +void setHeaterOn(bool const on) { if (on) { PORTB |= (1 << PB2); - heatingState = HEATING_UP; - alert_P(1, 2, 31, PSTR(MSG_HEATING_UP_0), PSTR(MSG_HEATING_UP_1), true); + heaterState = heaterStateUp; + alert_P(1, 2, 31, PSTR(MSG_HEATER_UP_0), PSTR(MSG_HEATER_UP_1), true); } else { PORTB &= ~(1 << PB2); - heatingState = HEATING_OFF; + heaterState = heaterStateOff; } } -bool isHeatingOn(void) { +bool isHeaterOn(void) { return bit_is_set(PORTB, PB2); } -void setHeatingState(int8_t const state) { - heatingState = state; +void setHeaterState(int8_t const state) { + heaterState = state; } -int8_t getHeatingState(void) { - return heatingState; +int8_t getHeaterState(void) { + return heaterState; } diff --git a/lambda/sensors.h b/lambda/sensors.h index ba77c5e..17b02f6 100644 --- a/lambda/sensors.h +++ b/lambda/sensors.h @@ -14,14 +14,25 @@ #include #define SHUNT_MILLIOHMS 100 -#define HEATING_READY_MA 1400 -#define HEATING_SHORT_MA 7500 -#define HEATING_DISCONN_MA 100 -#define HEATING_FAULT -1 -#define HEATING_OFF 0 -#define HEATING_UP 1 -#define HEATING_READY 2 +/** + * Oxygen sensor heater current limits at certain states in milliamps. + */ +typedef enum { + milliAmpsReady = 1400, + milliAmpsShort = 7500, + milliAmpsDisconn = 100 +} HeaterMilliAmps; + +/** + * Oxygen sensor heater states. + */ +typedef enum { + heaterStateFault = -1, + heaterStateOff = 0, + heaterStateUp = 1, + heaterStateReady = 2 +} HeaterState; /** * Entry for the lookup tables. @@ -80,7 +91,7 @@ uint16_t toLambda(uint16_t mV); /** - * Returns the current of the oxygen sensor heating in mA. + * Returns the current of the oxygen sensor heater in mA. */ uint16_t toCurrent(uint16_t mv); @@ -105,24 +116,24 @@ char* toInfo(uint16_t lambda); /** - * Turns the heating of the oxygen sensor on or off. + * Turns the heater of the oxygen sensor on or off. */ -void setHeatingOn(bool on); +void setHeaterOn(bool on); /** - * Returns true if the heating of the oxygen sensor is turned on, + * Returns true if the heater of the oxygen sensor is turned on, * false otherwise. */ -bool isHeatingOn(void); +bool isHeaterOn(void); /** - * Sets the state of the heating of the oxygen sensor to the given value. + * Sets the state of the heater of the oxygen sensor to the given value. */ -void setHeatingState(int8_t state); +void setHeaterState(int8_t state); /** - * Returns the state of the heating of the oxygen sensor. + * Returns the state of the heater of the oxygen sensor. */ -int8_t getHeatingState(void); +int8_t getHeaterState(void); #endif /* SENSORS_H_ */