View Single Post
Old 07-19-2012, 01:44 AM   #18 (permalink)
thingstodo
Master EcoModder
 
Join Date: Sep 2010
Location: Saskatoon, canada
Posts: 1,488

Ford Prefect - '18 Ford F150 XLT XTR

Tess - '22 Tesla Y LR
Thanks: 746
Thanked 565 Times in 447 Posts
5.5 mAh/min

The code does not mention calibration for an amp sensor - is this what you mean?

Quote:
// interrupt to count amp hours used
ISR(TIMER1_COMPA_vect)
{
CurrentRead = analogRead(CurrentPin);
// Convert currentread to amps
CurrentRead = (CurrentRead - 512) * .78125;
// Convert to AH & add to total
AHused = AHused + (CurrentRead * .001);
}
If that's your manual calibration, I have one suggestion. See what you think:

Arduino floating point has 6 - 7 digits of precision. Your scale constant of 0.78125 uses up 5 of those digits.

1023 Max reading - 512 = 511. * 0.78125 and *.001 gives 0.39921875, which is already over the significant digits on the Arduino. Use another variable, let's call it AHusedLow, to keep track of some extra significant digits. Since your calculated number can't go over 0.4, we'll use 0.4 as the rollover

Quote:
CurrentRead = analogRead(CurrentPin);
// Convert currentread to amps
CurrentRead = (CurrentRead - 512) * .78125;
// Convert to AH & add to low partion of total
AHusedLow = AHusedLow + (CurrentRead * .001);
// Update AHused
if (AHusedLow > 0.4) {
AHused = AHused + 0.4;
AHusedLow = AHusedLow - 0.4;
}
I've used this technique of chaining together variables to fake double-precision floating point (and triple, and quad) before (and I likely will again) and it has served me well.

This needs to be well documented so that anyone changing the code knows about the method and how it works, to make sure that the rollover is high enough that it will not be exceeded in one call and as low as possible.

That's the only code issue that jumps out at me.

Depending on how many times per second your ISR is called (I could look it up from your code ... but I'm lazy) this could be a source of error ... but as far as I can see, this should not have anything to do with your observed .0055 Ah/minute drift.
  Reply With Quote