diff --git a/lambda-test/lambda-test.c b/lambda-test/lambda-test.c index 8131aad..4de9c68 100644 --- a/lambda-test/lambda-test.c +++ b/lambda-test/lambda-test.c @@ -5,7 +5,7 @@ * Author: dode@luniks.net * */ -#include + #include // #include #include "USART.h" @@ -17,6 +17,26 @@ {10, 10} }; +uint8_t testAverage(void) { + int16_t avg = 0; + avg = average(8, avg, 4); + if (avg != 2) { return 0; } + avg = average(8, avg, 4); + if (avg != 4) { return 0; } + avg = average(8, avg, 4); + if (avg != 5) { return 0; } + avg = average(8, avg, 4); + if (avg != 6) { return 0; } + avg = average(8, avg, 4); + if (avg != 7) { return 0; } + avg = average(8, avg, 4); + if (avg != 8) { return 0; } + avg = average(8, avg, 4); + if (avg != 8) { return 0; } + + return 1; +} + uint8_t testToLambdaValue(void) { int16_t lambda = toLambda(12); @@ -72,6 +92,7 @@ } test tests[] = { + {"testAverage", testAverage}, {"testToLambdaValue", testToLambdaValue}, {"testToLambdaInter", testToLambdaInter}, {"testToTempI", testToTempI}, diff --git a/lambda/lambda.c b/lambda/lambda.c index b388128..6be140b 100644 --- a/lambda/lambda.c +++ b/lambda/lambda.c @@ -7,10 +7,7 @@ * TODO attribution * TODO have a look at string.h */ -#include #include -#include -#include #include #include #include @@ -18,13 +15,6 @@ #include "sensors.h" #include "integers.h" -/** - * Global variables holding averaged voltages. - */ -int16_t lambdaVoltageAvg = 0; -int16_t tempIVoltageAvg = 0; -int16_t tempOVoltageAvg = 0; - EMPTY_INTERRUPT(ADC_vect); /** @@ -47,55 +37,6 @@ } /** - * Formats the given values, displays them on an 16x2 LCD - * and prints them over USART. - */ -void display( - int16_t tempIVoltage, int16_t tempI, - int16_t tempOVoltage, int16_t tempO, - int16_t lambdaVoltage, int16_t lambda) { - div_t lambdaT = div(lambda, 1000); - - // TODO chars per line 16 - char line0[40]; - char line1[40]; - snprintf(line0, sizeof(line0), "Ti %3d C %d To %3d C %d\r\n", tempI, tempIVoltage, tempO, tempOVoltage); - snprintf(line1, sizeof(line1), "L %d.%03d %d\r\n", lambdaT.quot, abs(lambdaT.rem), lambdaVoltage); - printString(line0); - printString(line1); -} - -/** - * Creates an exponential moving average of the given value and - * average weighted by the given weight. - */ -int16_t average(int16_t value, int16_t average, uint8_t weight) { - return roundUp(value + (average * weight), weight + 1); -} - -/** - * Measures the "input" and "output" temperatures and the lambda value - * and displays the measured values. - */ -void measure(void) { - int16_t tempIVoltage = getVoltage(PC5); - tempIVoltageAvg = average(tempIVoltage, tempIVoltageAvg, 4); - - int16_t tempOVoltage = getVoltage(PC0); - tempOVoltageAvg = average(tempOVoltage, tempOVoltageAvg, 4); - - // OP factor is 11 - int16_t lambdaVoltage = (getVoltage(PC2) + 5) / 11; - lambdaVoltageAvg = average(lambdaVoltage, lambdaVoltageAvg, 4); - - int16_t tempI = toTempI(tempIVoltageAvg); - int16_t tempO = toTempO(tempOVoltageAvg); - int16_t lambda = toLambda(lambdaVoltageAvg); - - display(tempIVoltageAvg, tempI, tempOVoltageAvg, tempO, lambdaVoltageAvg, lambda); -} - -/** * Initializes the USART transmitter and receiver, sets up the ADC * and sleep mode and then infinitely measures with a 1 second delay * in between. diff --git a/lambda/sensors.c b/lambda/sensors.c index 6476996..33b1932 100644 --- a/lambda/sensors.c +++ b/lambda/sensors.c @@ -6,10 +6,12 @@ * * TODO put defines in makefile */ -#include +#include +#include #include #include #include +#include "USART.h" #include "sensors.h" #include "integers.h" @@ -52,6 +54,54 @@ { 3762, 400 } }; +/** + * Global variables holding averaged voltages. + */ +int16_t lambdaVoltageAvg = 0; +int16_t tempIVoltageAvg = 0; +int16_t tempOVoltageAvg = 0; + +/** + * Measures the "input" and "output" temperatures and the lambda value + * and displays the measured values. + */ +void measure(void) { + int16_t tempIVoltage = getVoltage(PC5); + tempIVoltageAvg = average(tempIVoltage, tempIVoltageAvg, 4); + + int16_t tempOVoltage = getVoltage(PC0); + tempOVoltageAvg = average(tempOVoltage, tempOVoltageAvg, 4); + + // OP factor is 11 + int16_t lambdaVoltage = (getVoltage(PC2) + 5) / 11; + lambdaVoltageAvg = average(lambdaVoltage, lambdaVoltageAvg, 4); + + int16_t tempI = toTempI(tempIVoltageAvg); + int16_t tempO = toTempO(tempOVoltageAvg); + int16_t lambda = toLambda(lambdaVoltageAvg); + + display(tempIVoltageAvg, tempI, tempOVoltageAvg, tempO, lambdaVoltageAvg, lambda); +} + +int16_t average(int16_t value, int16_t average, uint8_t weight) { + return roundUp(value + (average * weight), weight + 1); +} + +void display( + int16_t tempIVoltage, int16_t tempI, + int16_t tempOVoltage, int16_t tempO, + int16_t lambdaVoltage, int16_t lambda) { + div_t lambdaT = div(lambda, 1000); + + // TODO chars per line 16 + char line0[40]; + char line1[40]; + snprintf(line0, sizeof(line0), "Ti %3d C %d To %3d C %d\r\n", tempI, tempIVoltage, tempO, tempOVoltage); + snprintf(line1, sizeof(line1), "L %d.%03d %d\r\n", lambdaT.quot, abs(lambdaT.rem), lambdaVoltage); + printString(line0); + printString(line1); +} + int16_t getVoltage(uint8_t port) { ADMUX = (0b11110000 & ADMUX) | port; diff --git a/lambda/sensors.h b/lambda/sensors.h index ff9f704..618482a 100644 --- a/lambda/sensors.h +++ b/lambda/sensors.h @@ -25,6 +25,27 @@ } tableEntry; /** + * Measures the "input" and "output" temperatures and the lambda value + * and displays the measured values. + */ +void measure(void); + +/** + * Creates an exponential moving average of the given value and + * average weighted by the given weight. + */ +int16_t average(int16_t value, int16_t average, uint8_t weight); + +/** + * Formats the given values, displays them on an 16x2 LCD + * and prints them over USART. + */ +void display( + int16_t tempIVoltage, int16_t tempI, + int16_t tempOVoltage, int16_t tempO, + int16_t lambdaVoltage, int16_t lambda); + +/** * Returns the voltage sampled at the given ADC input port doing * 16x oversampling and taking in account the calibrated AREF and * ADC offset voltages.