Measuring values over time - let the “brain” do the math!
In this OptoForum post we looked at digital totalizing, where we let the brain do the math to measure the On Time of a pump.
But recently I got a “programming” question from a PAC/groov user measuring watts from a small solar system, who wanted to know wh (watt-hours). In particular, he wanted to know how much power his solar panels generated in a day.
In the picture below, notice the yellow line; and also the totalized value on the left. This is a 2-day trend, so the previous day’s data is in the middle there, and you see a break in the yellow line off to the right when he downloaded his revised strategy halfway through the day.
The total value he needed was essentially the area under that yellow curve. (The “integrated” value.)
While we could write some code to add up values and whatnot (yay! [URL=“https://en.wikipedia.org/wiki/Integral#Interpretations_of_the_integral”]calculus! fun!); I’m generally lazy err… I mean efficient… and prefer to use the right tool for the job.
In this case, the value he wanted to “totalize” was coming from an analog input - a SNAP-AIPM power module. It just so happens, in addition to the digital On-Time Totalizing function, our super-smart I/O Units (aka “brains” for good reason) also have an analog totalizing function built in. No code calculations needed!
Use this built-in brain feature via PAC Control with just a few simple commands: one to set the “rate” (on PAC brains, this is essentially the units you want – the analog scanner on the brain updates the total as fast as possible for you, to get the best accuracy possible); and one or two more commands to either just read or read & clear the current total.
First: The PAC Control command Set Analog Totalizer Rate requires units of seconds. We only need to call this command once to start the totalizing, with the correct units. In the solar power example, he needed to know watt hours. So, in this case, he set the “rate” to 3600 seconds or one hour. If you wanted to know gallons per minute (and your analog input was scaled for gallons), the rate would be 60 seconds. Widgets per second, the rate would be 1.0 and so on.
Next: In a loop (with a delay, of course), use the command Get Analog Totalizer Value to “watch” the number grow. In this case, once a second was enough to just watch it change in that groov page pictured above.
Eventually, we need to use Get & Clear Analog Totalizer Value before it maxes out and can’t totalize anymore. For this application, I recommended doing this once a day (so that number on the left would be a daily total), for example, after the sun has set. He already had some code checking and clearing other values when the day rolled over, so that’s a perfect place for the Get & Clear.
WARNING: Before adding this new daily total to, say, a monthly total, do a sanity check. Since we’re totalizing an analog input, it’s possible for that value to become invalid: for example, you’d see a -nan (or -qnan, #QNAN, -1.#QNAN depending on the software you’re using) if the module is unplugged or not receiving enough power. If you add this to a good value, the good value will be clobbered, becoming a NAN itself.
So in order to avoid propagating that invalid number into your good values, use the PAC Control command Float Valid? first before adding.
In the case of this solar application, the programmer/owner plans to add up each day’s total and eventually display a monthly total (he’s hoping by then it will be kilowatt hours, not just watt hours!) since he gets his power bill each month and the total amount billed is in kilowatt hours. He’ll then see how much power the solar system has saved him.
Totalizing for the win; no need to do that integral yourself!