Shop OBEX P1 Docs P2 Docs Learn Events
Real-time power calculation — Parallax Forums

Real-time power calculation

mcstarmcstar Posts: 144
edited 2008-12-10 05:45 in Propeller 1
I'm building a battery discharge circuit using a propeller to log the discharge curve·of some NiMh batteries.· This weekend I setup the propeller circuit and connected it to an SD card for data logging and a 12-channel 8-bit ADC as well as a Mosfet for power control.· All is working well including an ADC driver I wrote based on the one in the obex.· I've got one channel monitoring the battery voltage (through a resistor dividor network) and another channel channel is measuring the the current, or actually the voltage across a .45Ohm shunt.· I then calculate the current through the shunt since I·know it's resistance.··Current is controlled by varying the PWM % sent to the mosfet gate.·In this manner, the current is kept near the target discharge value.

Both the current and voltage are being calculated as floating point numbers using FloatMath and stored in main memory as floats.

Now I need to figure out the real-time power calculations.· I'm wondering if anyone here has done real-time power calculations of this sort and if so, what does the algorithm look like?· At first I thought I could use one of the propeller's counters to accumulate the power over time, but I'm pretty sure the·counters won't work with floating point numbers right? ·I really want to accumulate the amount of power dissipated over time, so I suppose I could just calculate the Power (VxI) every so often (maybe every 1/10 of a second) in its own cog, but I'm not sure how the accumlation should work.· Would it be appropriate to divide the instantaneous power calculated by·P =V*I ·by the sample rate·over 360 to get watt hours?· How often does the calcuation need to be done to get accurate numbers?· Could I use an inline inductor to limit current transients and smooth out the fluctuations, thus reduce the need for a high sample frequency?

Post Edited (mcstar) : 12/8/2008 7:59:43 PM GMT

Comments

  • kwinnkwinn Posts: 8,697
    edited 2008-12-08 22:11
    There are two ways to approach this. The first is to filter the pulse widh modulated signal to provide a smooth DC current and voltage, and sample them regularly ( say every 100ms). The second is to synchronize the voltage and current readings to the "on" time of the Mosfet and multiply by the duty cycle.
    for samples=1 to endsamples
    IP=V*I 'calculate the instantaneous power
    ( IP=IP*Dcycle 'correct instantaneous power for duty cycle if second approach is used)
    AP=AP+IP 'accumulate the instantaneous power
    samples=samples+1 'count number of samples
    next for
    T=samples/36000 'convert sample count to hours (based on 100ms/sample)
    WH=AP/T 'calculate watt hours
    The second approach is not how batteries are normally tested, so some comparison testing may be necessary.
  • mcstarmcstar Posts: 144
    edited 2008-12-08 22:32
    Great ideas kwinn. I think I'll try them both and compare the performance. I'm interested to see how accurate the pulse and measure method is. I'll take a look with my scope tonight to see how smooth the terminal voltage is while pulsing it raw. Thanks for the detailed answer.
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-12-09 04:37
    Using a good pi filter gives great results. I built a maximum power point tracker for some solar panels a while back, and I was doing something similiar. Basically, I was pulsing the current from the panels into a capacitor using a constantly adjusted PWM duty cycle. This was pretty high-frequency, so I need good filtering. On top of a good filter, you should be taking a reading more than 10 times a second. Probably about 100 times a second if your ADC is fast enough. Any ripple that is still present after the filter, will be filtered out again through software using this method. No sync required. If you want some schematics and code, I'd be happy to post later.
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-12-09 04:39
    By the way, no need for floating point math. It only slows things down in this case, and for best results, you need high speed filtering. Just go with regular integers and instead of representing the current in volts and amps, do it in millivolts and milliamps, or even MICRO amps and micro volts...
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-12-09 04:42
    Oh, one more thing... You're not using this battery as the power source for the circuitry are you? If so, that's going to skew your results...
  • mcstarmcstar Posts: 144
    edited 2008-12-09 14:56
    Thanks for the verification on the filter method Phill. I've been using FP mostly to keep the values in recognizable terms (for instance my R values are in ohms and my voltage divider factor is a ratio) and I was afraid I'd loose precision using only ints especially when I started doing division and multiplication on it. Now that I'm thinking about it the way you propose, I can see the value in keeping those raw numbers from the adc in integer form. This would allow the data acquisition cog to run at full speed and just collect data instead of doing all that fp math. I can always do fp conversions after the fact for display purposes if needed. My adc can collect up to 400K samples a second, so I should be able to sample 100x a second without a problem.

    Can you share the math you did for your power accumulator? That's the part I'm really interested in. Did you do yours like Kwinn proposed?

    As for the resolution, the ADC I have is 12-bits, so I'm limited to a resolution of 4095 spread out over my max current of about 10A and my max voltage of about 18V. That comes out to about 2.4milliamps/div and about 4millivolts/div of resolution. However, right now it's not the precision I'm struggling with, but the accuracy. I'm getting a variance of about 5% between adc samples even while monitoring a stable DC voltage as the input. Tonight I'll check the input voltage with a scope and add some capacitors to the circuit to see if I can clean up signal some.

    FYI, I have a separate battery pack for the propeller and adc circuit from the one I'm monitoring, they only share a ground, but thanks for mentioning that.
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-12-09 21:04
    That's basically what I did... Got a sample of the current, sample of the voltage, multiply them together to get power. Since energy is just power x time, just multiply the power by your delta-Time, and you get the amount of energy used during that time.

    Let's say you take 100 samples per second. Your first current reading is 3 Amps, and your voltage is 6V. That's 18 Watts, and at 100 samples per second, your delta-Time would be 0.01 seconds. So, multiply your 18W by 0.01, and you get 180 milliJoules or 180 Watt-Seconds. With every sample, just add the resulting energy to a variable called something like "TotalEnergy", and you will end up with an accumulation of energy used.

    I'm doing something like this again for a Pulse Width Modulation power supply I'm designing and plan on selling to the poor fools that think you can extract magic extra energy from water using electrolysis... I'm adding a bunch of fancy things to it - one of those things being an energy consumption tracker, which will be using this method.

    Oh, btw, if you REALLY want good results, and slightly more accurate, use the trapezoidal numerical integration. Instead of simply getting each sample, and multiplying by dt, take the average of two consecutive samples, and multiply by dt.

    Post Edited (Philldapill) : 12/9/2008 9:30:13 PM GMT
  • mcstarmcstar Posts: 144
    edited 2008-12-09 22:13
    Phildapill said...
    I'm doing something like this again for a Pulse Width Modulation power supply I'm designing and plan on selling to the poor fools that think you can extract magic extra energy from water using electrolysis... I'm adding a bunch of fancy things to it - one of those things being an energy consumption tracker, which will be using this method.

    OH, nice idea! It's about time someone comes up with a way to make those guys honest! I watched a video by an ex-Chrysler engineer that's actually running real vehicles on Hydrogen. He also does fuel mixing where you run like 90% gasoline, and inject a metered volume of gaseous hydrogen into the intake (his H2 is compressed off site using conventional means). Anyway, he knows exactly how much H2 is needed to increase the performance and mileage of different types of fuel (gasoline/ethanol/diesel fuels etc). According to his empirical data, he says you'd need about 5000watts of electrical power to produce enough hydrogen in real-time to increase a V-6's mpg by 20% assuming you had no extra load added to the engine. In reality that means you'd need 4-5 alternators drawing about 10-20 extra HP to create your H2; therefore all the "gains" you get from the H2 are eaten up by the extra load you put on the vehicle (plus a bit more in losses of course).
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-12-09 22:41
    Pssssh, what are you talking about mcstar... Everyone knows that using a homemade electrolizer the size of a small pitcher will get you and increase of 200% in gase mileage... LOL

    I'm not going to tell these people they are silly for thinking they've found a way to break the law of conservation of energy - all in their garage, but I AM going to provide them with a good way of tracking energy in(and maybe later, energy out). Even though I think it's ridiculous what they think they are doing, I hope to make a few bucks [noparse]:)[/noparse]
  • mcstarmcstar Posts: 144
    edited 2008-12-09 22:50
    Brilliant!
  • kwinnkwinn Posts: 8,697
    edited 2008-12-10 01:23
    Glad you liked my ideas. Please post the comparison between the steady current and pulse discharge measurements.

    Phildapill's comments are also a good idea. No point in doing all the calculations in floating point if you can use the binary numbers from the current and voltage directly and convert the end result to power. Also, there is no need for a high sample rate for battery discharge curves since the discharge time is usually measured in hours unless the current draw is very high for the battery under test.
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-12-10 05:45
    Agreed. Your change in battery voltage/current from second to second is going to be very small. I did a similiar thing on a large deep cycle battery. I set me sample rate to once a second and let it go for twelve hours. The resulting excel file was pretty big, to say the least. Besides, if you take 100 samples a second for something like this, the only real difference between samples will be noise. I could see from my graphs that there was a couple mV of noise throughout the whole thing, but overall, it was smooth.
Sign In or Register to comment.