View Single Post
Old 07-19-2012, 02:25 PM   #22 (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: 749
Thanked 565 Times in 447 Posts
Floating point

Quote:
Originally Posted by Daox View Post
Thanks a lot for taking the time to look through this thingstodo! I appreciate it very much.
I'm on holidays - this is the kind of thing that I enjoy.

Since your CurrentRead can now be somewhere between 511 and 1023, 1023 - 0 (potentially worst case) = 1023 * 0.78125 *.0000002777 gives 0.000221943046875 so I've changed the 0.4 rollover to 0.0003.
I chose a single digit of precision to maximize the total A-h you are calculating.

After you get your code updated, you may want to re-run one or two tests and see if your AHused is still tracking.

This
Code:
    // Convert currentread to amps
    CurrentRead = (RawCurrentRead - CurrentSensorCalib) * .78125;
    
    // Convert to AH used and add to total
    AHused = AHused + (CurrentRead * 0.0000002777);
would become something like this
Code:
    // Convert currentread to amps
    CurrentRead = (RawCurrentRead - CurrentSensorCalib) * .78125;
    
   // Convert to AH & add to low portion of total
   AHusedLow = AHusedLow + (CurrentRead * 0.0000002777);

   // Update AHused
   if (AHusedLow > 0.0003) {
       AHused = AHused + 0.0003;
       AHusedLow = AHusedLow - 0.0003;
   }
This is the simplest and fastest code change - I hesitate to put too much extra code in the ISR.

With 6 digits of precision, adding .0003 would give you good accuracy up to 99.9999 A-h for sure. Likely a bit higher but I've lost a lot of the details on floating point accuracy ... I'd have to re-learn some of that.
  Reply With Quote