From c5451712719d1b3d9a0191a057816f3325e4d5f0 Mon Sep 17 00:00:00 2001 From: Paul Goeser Date: Sun, 10 Jul 2011 22:34:10 +0200 Subject: [PATCH] hacked a lot of postprocessing --- firmware/shared/spi_proto.c | 4 +--- firmware/slavechip/filter.c | 45 ++++++++++++++++++++++++++++++++++--- firmware/slavechip/filter.h | 18 ++++++++++++++- firmware/slavechip/main.c | 20 ++++++++++++----- firmware/slavechip/muxer.c | 7 ++++++ 5 files changed, 82 insertions(+), 12 deletions(-) diff --git a/firmware/shared/spi_proto.c b/firmware/shared/spi_proto.c index 4dda941..e55fc05 100644 --- a/firmware/shared/spi_proto.c +++ b/firmware/shared/spi_proto.c @@ -95,9 +95,7 @@ uint16_t spi_proto_slaveaction(uint8_t opcode, uint8_t addr, uint16_t data) { break; case 7: #ifndef SPI_MASTER - if(addr < 4) { - retval = temperatures[addr]; - } else return 0; + retval = get_temperature(addr); #endif break; default: diff --git a/firmware/slavechip/filter.c b/firmware/slavechip/filter.c index 5f19a3b..0c1afa4 100644 --- a/firmware/slavechip/filter.c +++ b/firmware/slavechip/filter.c @@ -2,13 +2,52 @@ #include "filter.h" #include -#define CHANNELCOUNT 8 +Sensordata sensordata[4]; +float ambient_temp=25.; + float avg_cumul[CHANNELCOUNT]; float avg_lastval[CHANNELCOUNT]; uint8_t avg_count[CHANNELCOUNT]; float avg_noise[CHANNELCOUNT]; + +void process_thermocouple_value(int16_t raw_data, uint8_t channel){ + // This function does everything that needs to be done to raw adc values + float a = raw_data; + a -= sensordata[channel].offset; + a = a*0.000625; // adc count to voltage (mV) + a = filter_voltage_to_temp(a); // voltage to temp + // TODO: first ambient->voltage, add that to a, then voltage->temp + a += ambient_temp; // compensate ambient temperature + // TODO: lowpass + + uint16_t result = filter_float_to_fixpoint(a); + sensordata[channel].temperature = result; +} + +void process_offset_value(int16_t raw_data, uint8_t channel){ + sensordata[channel].offset = raw_data; +} + +void process_ambient_value(int16_t raw_data){ + // 9.625mV after amp per °C + float a = raw_data; + a = a * 0.03125; //adc to mV after amp + a = a / 9.625; + ambient_temp = a; + dbgLog("ambient: %3.3f°C (lastval %i)\n",a, raw_data); +} + + +uint16_t get_temperature(uint8_t channel){ + if(channel>=4){ + return 0; + } + return sensordata[channel].temperature; +} + + void filter_average_input(uint8_t channel, float value) { avg_cumul[channel] += value; float noisetmp = avg_lastval[channel] - value; @@ -44,8 +83,8 @@ float filter_average_noise(uint8_t channel){ return noise; } -uint16_t filter_float_to_decimal8(float f){ - return( (uint16_t)(f / 256.) ); +uint16_t filter_float_to_fixpoint(float f){ + return( (uint16_t)(f * 256.) ); } //uint16_t calc_temp(int16_t val, uint8_t channel) { diff --git a/firmware/slavechip/filter.h b/firmware/slavechip/filter.h index 2bbf978..273123b 100644 --- a/firmware/slavechip/filter.h +++ b/firmware/slavechip/filter.h @@ -1,6 +1,22 @@ #include +#define CHANNELCOUNT 8 +typedef struct { + int16_t offset; // in adc-counts TODO:float? + // TODO fir-data + uint16_t temperature; +} Sensordata; + +extern Sensordata sensordata[4]; +extern float ambient_temp; + +void process_thermocouple_value(int16_t raw_data, uint8_t channel); +void process_offset_value(int16_t raw_data, uint8_t channel); +void process_ambient_value(int16_t raw_data); + +uint16_t get_temperature(uint8_t channel); + void filter_average_input(uint8_t channel, float value); uint8_t filter_average_done(uint8_t channel, uint8_t samples); @@ -11,7 +27,7 @@ float filter_average_result(uint8_t channel); float filter_average_noise(uint8_t channel); -uint16_t filter_float_to_decimal8(float f); +uint16_t filter_float_to_fixpoint(float f); float filter_voltage_to_temp(float mV); diff --git a/firmware/slavechip/main.c b/firmware/slavechip/main.c index a9b407d..fc07e0e 100644 --- a/firmware/slavechip/main.c +++ b/firmware/slavechip/main.c @@ -54,19 +54,19 @@ int __attribute__((noreturn)) main(void) { muxer_set(1); - uint8_t active_sensor = 0; int16_t data; for(;;){ // measure temps 5*2 times for(int i=0; i<5; i++) { - for(int active_sensor=0; active_sensor<2; active_sensor++) { // only measuring two probes atm + for(uint8_t active_sensor=0; active_sensor<2; active_sensor++) { // only measuring two probes atm muxer_set(active_sensor); while(!mcpadc_has_new_data()) _delay_ms(1); mcpadc_get_data(); // first data after switch to trash while(!mcpadc_has_new_data()) _delay_ms(1); data = mcpadc_get_data(); + process_thermocouple_value(data,active_sensor); float f = filter_voltage_to_temp(((float)data) * 0.000625 ); filter_average_input(active_sensor, f); if(filter_average_done(active_sensor,16)){ @@ -77,7 +77,7 @@ int __attribute__((noreturn)) main(void) { } // measure 2 offsets - for(int active_sensor=0; active_sensor<2; active_sensor++) { // only measuring two offsets atm + for(uint8_t active_sensor=0; active_sensor<2; active_sensor++) { // only measuring two offsets atm muxer_set(active_sensor); offset_measure_start(active_sensor); while(!mcpadc_has_new_data()) _delay_ms(1); @@ -85,12 +85,22 @@ int __attribute__((noreturn)) main(void) { while(!mcpadc_has_new_data()) _delay_ms(1); data = mcpadc_get_data(); + process_ambient_value(data); // TODO: what to do with the offset? offset_measure_stop(); } + + // measure ambient + muxer_set(5); + while(!mcpadc_has_new_data()) _delay_ms(1); + mcpadc_get_data(); // first data after switch to trash + while(!mcpadc_has_new_data()) _delay_ms(1); + data = mcpadc_get_data(); + process_ambient_value(data); - SOFTTIMER(1,8000) { // maybe measure coldjunction comp + +/* SOFTTIMER(1,8000) { // maybe measure coldjunction comp muxer_set(23); // TODO: channel for ntc?! // first measure ntc offset?! maybe? @@ -108,7 +118,7 @@ int __attribute__((noreturn)) main(void) { while(!mcpadc_has_new_data()) _delay_ms(1); data = mcpadc_get_data(); - } + }*/ diff --git a/firmware/slavechip/muxer.c b/firmware/slavechip/muxer.c index bdcdf83..8671753 100644 --- a/firmware/slavechip/muxer.c +++ b/firmware/slavechip/muxer.c @@ -1,6 +1,13 @@ #include "muxer.h" #include + +/***************** + * themor-sensors are channels 0-3 + * + * pt1000 is on channel 5 + */ + void muxer_init(){ DDRC |= (_BV(0)|_BV(1)|_BV(2)|_BV(3)); // conf as outputs PORTC |= _BV(3); // inhibit