avr: adapt scaling factor in power calc to increased sampling freq [667Hz]

This commit is contained in:
Bart Van Der Meerssche 2010-12-29 15:28:43 +01:00
parent d80377ee19
commit 9c5842f262
2 changed files with 11 additions and 8 deletions

View File

@ -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) {

View File

@ -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