Shop OBEX P1 Docs P2 Docs Learn Events
Math question for Tracy Allen... — Parallax Forums

Math question for Tracy Allen...

denodeno Posts: 242
edited 2007-08-08 22:01 in General Discussion
Been reading your articles on integer math from your web site and I have a question.

I am going to use one of Allegro's amp sensors to monitor current flow in and out of my house bank batterys on my boat.· The sensor in mind will sense 130-0-130 amps.
This·device from Allegro will provide a 0 to 5 volts output with 2.5 volts being equal to 0 amps. 0 volts out equals -130 amps (discharge) and 5 volts equals 130 amps. (Charging)

I already have a device built using the LTC1298 A/D converter which works well for reading battery voltage, and it does have the ability to monitor 2 different inputs or sensors.

As you probably already know the LTC1298 measures 0 to 5 volts and provides a 12bit resolution. (0 to 4096 or 1.22mVolt per 12bit unit count)


My question is: Would you use this math to calculate the amprage?

····· IF (LTC1298 = 2048) THEN· 'this would probably never happen...
······· amps = 0
····· ELSEIF (LTC1298 > 2048) THEN
······· amps = (4096 - LTC1298) */ $0010
····· ELSEIF (LTC1298 < 2048) THEN
······ ·amps = (2048 - LTC1298) */ $0010
····· ENDIF

The $0010 was derived from dividing 130 amps by 2048 and converting to HEX.
The 2048 is the number of 12bit units between 0 and +130 amps, and between 0 and -130 amps.

Thank you...Deno


·

Comments

  • Sparks-R-FunSparks-R-Fun Posts: 388
    edited 2007-08-04 01:09
    I hope Tracy will still answer this as the question was addressed to him. However, I have a very similar project in mind and could not help reviewing the math. (I actually plan to use the 150 Amp Allegro sensor.) Here is what I see:

    If you stick with signed numbers (as I will probably do) the math can be handled by this simple equation:

    amps = (LTC1298 - 2048) / Conversion_Factor


    If you wish to avoid signed numbers this logic should suffice:
          IF (LTC1298 > 2048) THEN
            amps = (LTC1298 - 2048) / Conversion_Factor
          ELSE 
            amps = (2048 - LTC1298) / Conversion_Factor
          ENDIF
    


    Either way, if LTC1298 = 2048 a zero value is returned. So no special zero case is required.

    Your conversion factor is on the right track and will work… yet some additional math will yield more accurate conversion results. The choice is up to you.


    Conversion Accuracy

    You took 2048 ADC increments and divided them by 130 Amps to yield approximately 15.75 Increments/Amp that you then rounded to the next nearest integer, 16. This will yield an approximate answer because of the rounding.

    A slight concern is that this introduces a percent error of approximately (16 – 15.75) / 15.75 * 100 = 1.587%. At the top and bottom (negative end) of the scale your conversions will be off by about (130 Amps * 1.57%) = 2.063 Amps. This may be fine for you application. If so, do nothing further.

    However, you can obtain greater conversion accuracy at the expense of some additional math. The example I will show should work if you are using 16-bit numbers.

    Since the ADC is returning only twelve bits its maximum value of 4096 leaves four bits of a 16-bit number unused. Furthermore, since 2048 is the midline value and gets subtracted out an extra bit becomes available. Now if signed numbers are used one bit must be used for the sign. So only four extra bits are available in that case. If unsigned numbers are used, there will be five extra bits that can be used to increase the conversion accuracy.

    Let’s assume signed numbers are being used and only four extra bits are available. Each bit represents a power of two. Since 2^4 = 16 we could multiply even our maximum ADC value by sixteen and still fit it into a 16-bit number without overflow.


    Why bother to do this?

    It will be useful for improving our conversion accuracy!

    Now when we divide the midline-adjusted ADC value by our conversion factor we must use a conversion factor sixteen times larger than before since we made our adjusted ADC value sixteen times larger. So we will use a conversion factor of 15.75 * 16 = 252.

    There. Did you catch that? The *magic* just happened!

    Previously our conversion factor was rounded up to 16 from the much closer value of 15.75. The real value, complete to two decimal places, was not retained. However, by scaling everything up by a factor of sixteen the value, to two decimal places, is much better retained! This results in greater accuracy.


    How much greater?

    Well, if the real conversion factor were exactly 15.75 then the conversion would be exact. The real number is actually closer to 15.7538461… something. I calculate the percent error to be around -0.024%. So at maximum amps it would be off by about –0.0317 amps. That is not too shabby!

    Here is what you would need to do to gain that extra precision:

    amps = (LTC1298 - 2048) * 16
    amps = amps / 252



    Or if you wish to avoid signed numbers this logic should suffice:
          IF (LTC1298 > 2048) THEN
            amps = (LTC1298 - 2048)
          ELSE 
            amps = (2048 - LTC1298) 
          ENDIF
    
          amps = amps / 252
    

    Note: For maximal precision in the unsigned version the last statement could be replaced with this:
    amps = amps / 504
    

    This takes advantage of the extra bit that is available when using unsigned numbers. 2^5 = 32 and 15.75 * 32 = 504.


    It is possible I may not have explained this very well, but I believe the logic and mathematics are sound.

    Additional comments are welcome. Let us know how it turns out!


    - Sparks
  • denodeno Posts: 242
    edited 2007-08-04 02:33
    Thank you, Sparks-R-Fun...you have made yourself understood perfectly.· Sort of like finding a commom denominator in fractions...

    I will use your idea, to improve the accuracy.

    Have you used the Allegro device?·

    I have been using a shunt (100mVolt = 100 amp) for the current sensor.·The problem with using a shunt· is when the current drain from the house bank is low (lets say under 15·amps which equals 15·mVolts from the shunt) and the sensor wire to the LTC1298 (and the Stamp) is 10 feet long, I get inaccurate readings.·I hope·with the Allegro device, I start out at 2.5 volts, which is easier to "transmit" thru 10 feet of wire to the LTC1298.· Most of the time, my house bank loads are around 15 to 20 amps, which puts me in the middle of the curve, and not on the low end.

    My display device is a dot matrix that uses 5 panels of a 5X8 matrix.· If I am in a discharge state, below 2.5 volts, I will simply display a "down arrow" in the left hand panel, and the opposite for a charging situation.·

    Thanks again...deno


    Post Edited (deno) : 8/4/2007 2:38:43 AM GMT
  • Tracy AllenTracy Allen Posts: 6,656
    edited 2007-08-04 04:39
    Deno,

    I'd do it like this, coming in with the ltc1298 result from 0 to 4095 from the ADC :

    amps = ltc1298 ** 4160 - 130
    DEBUG SDEC amps
    ' for a result from -130 to +130

    or


    amps = ltc1298 ** 41600 - 1300
    DEBUG REP "-"\amps.bit15, DEC ABS amps / 10, ".", DEC1 ABS amps
    ' for a result from -130.0 to +130.0

    The factor 41600 comes from 260 amps / 4096 counts = 0.0634765625. The stamp approximates that fraction as 4160/65536 (actually, it is exact, not an approximation). The division by 65536 is implied in the Stamp ** operator. Save the subtraction for last, because ** doesn't work on negative numbers on the Stamp. The higher resolution version just shifts the decimal point one place

    Also, you were on a good track with */, but you don't need to convert anything to hex to use it.

    amps = ltc1298 */ 1625 - 13000
    DEBUG REP "-"\amps.bit15, DEC ABS amps / 100, ".", DEC2 ABS amps

    There is that factor 16.25, and it gives you a resolution of 0.01 amp! The factor is 260 amps/4096 counts * 256 * 100 = 1625 exactly.

    Spark, division doesn't work on negative numbers, so there would be a problem with {(ltc1298 - 2048)/factor} when ltc1298 < 2048. But I think your method is valid, to make the numerator as large as possible before the division by a denominator that is also scaled up to get more precision.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • denodeno Posts: 242
    edited 2007-08-05 03:28
    Thanks to Tracy and Sparks for your input.· Very helpful and in use as we speak.

    Deno
  • Sparks-R-FunSparks-R-Fun Posts: 388
    edited 2007-08-07 17:20
    I am glad you asked your question, deno. Like I said, I am about to do something very similar and it forced me to think about how I planned to do it. It gave me an opportunity to air my thoughts and solicit feedback. I am glad I did!

    I am still trying to figure out how Tracy realized an exact conversion could be made. Yes I see it now but I did not see it earlier.

    I have run some signed number division tests under SX/B since I did not recall and have since looked for but not found any warnings regarding division using signed numbers producing undesired results. Sure enough, the numbers are not what I expected.

    Thanks Tracy for your answer and thanks deno for asking about the math!

    - Sparks
  • denodeno Posts: 242
    edited 2007-08-07 20:47
    I have received at 130 amp and a 50 amp EV board/device from Allegro and there shipping was really fast.· It's always nice to get something right away.

    What a nice device.· Hooked it up on the bench, ran a big battery charger thru it to a car battery, and several loads of different sizes for testing.· Readout was right on.· What an improvement over the simple 100mV/100amp shunt device.· I'll bet the shunt makers are getting a little nervious.

    Kind of like the slide rule makers, when the electronic calculator came on the market.

    Anyway, thanks to all for the good advice.

    Deno
  • Sparks-R-FunSparks-R-Fun Posts: 388
    edited 2007-08-08 22:01
    Deno,

    You mentioned having trouble with inaccurate reading using a shunt after the signal travels through ten feet of wire on your boat. I am interested in knowing if you still see inaccuracies from the Allegro sensor under these same conditions.

    - Sparks
Sign In or Register to comment.