Real-time power calculation
mcstar
Posts: 144
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
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
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.
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.
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
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).
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]
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.