From 9c5842f2622e97b30c84e70d0d999e4a4a9aa085 Mon Sep 17 00:00:00 2001 From: Bart Van Der Meerssche Date: Wed, 29 Dec 2010 15:28:43 +0100 Subject: [PATCH] avr: adapt scaling factor in power calc to increased sampling freq [667Hz] --- mote/v2/avr/main.c | 17 ++++++++++------- mote/v2/avr/main.h | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/mote/v2/avr/main.c b/mote/v2/avr/main.c index 208b7aa..ff9e79a 100644 --- a/mote/v2/avr/main.c +++ b/mote/v2/avr/main.c @@ -280,14 +280,17 @@ void calculate_power(struct state_struct *pstate) // Since the AVR has no dedicated floating-point hardware, we need // to resort to fixed-point calculations for converting nWh/s to W. // 1W = 10^6/3.6 nWh/s - // value[watt] = 3.6/10^6 * rest[nWh/s] - // value[watt] = 3.6/10^6 * 65536 * (rest[nWh/s] / 65536) - // value[watt] = 3.6/10^6 * 65536 * 262144 / 262144 * (rest[nWh/s] / 65536) - // value[watt] = 61847.53 / 262144 * (rest[nWh/s] / 65536) - // We round the constant down to 61847 to prevent 'underflow' in the + // power[watt] = 3.6/10^6 * rest[nWh/s] + // power[watt] = 3.6/10^6 * 65536 * (rest[nWh/s] / 65536) + // power[watt] = 3.6/10^6 * 65536 * 262144 / 262144 * (rest[nWh/s] / 65536) + // power[watt] = 61847.53 / 262144 * (rest[nWh/s] / 65536) + // We have to correct for only using 666 samples iso 2000/3, so: + // power[watt] = 61847.53 * 1/666 * 2000/3 / 262144 * (rest[nWh/s] / 65536) + // power[watt] = 61909.44 / 262144 * (rest[nWh/s] / 65536) + // We round the constant down to 61909 to prevent 'underflow' in the // consecutive else statement. - // The error introduced in the fixed-point rounding equals 8.6*10^-6. - MacU16X16to32(power, (uint16_t)(labs(rest)/65536), 61847); + // The error introduced in the fixed-point rounding equals 7.1*10^-6. + MacU16X16to32(power, (uint16_t)(labs(rest)/65536), 61909); power /= 262144; if (rest >= 0) { diff --git a/mote/v2/avr/main.h b/mote/v2/avr/main.h index dfd0f56..62bca43 100644 --- a/mote/v2/avr/main.h +++ b/mote/v2/avr/main.h @@ -22,7 +22,7 @@ struct sensor_struct { }; # define WATT 1000000000 -# define SECOND 666 // 667Hz - 1 +# define SECOND 665 // 666Hz - 1 #define STATE_PULSE 1 #define STATE_SKIP 2