Working with running sums

Recommended Posts


I have just started with DAQFactory and I am trying to accumulate voltage readings over time and would like to know how best to do it. I have succeeded with one process using the following script as a sequence:

global z=0





I can monitor z as a variable just fine. While this is running in the background there is a channel monitoring the same voltage every 10 seconds so that I can plot and log it.

But this brings up several questions:

1. Is the voltage[0] in my sequence coming from the channel conversion, or is it doing its own conversion? I suspect the latter but I would prefer they be the same so that I would only have to set the sample time in one place and also have the same voltages logged that are being used in the summation.

2. Could this expression, or some equivalent, somehow be used in a channel or conversion so that I could plot or log the summation?

3. Exactly where can I declare and use variables? So far, the sequence is the only place I have found.

Thank You Very Much,

Link to comment
Share on other sites

First, this is actually not the best way to do this. It would be better to put the sum line in the event for the "voltage" channel. The event is script that executes every time the channel gets a new value. This, however, presumes that you are reading "voltage" every 10 seconds. If not, and you want to count values even if they aren't new, then your method would work. I will say, however, that you although you typically want to avoid wait(), this is a perfectly reasonable use for it to keep things happening exactly on the 10 second mark. Since the rest of the loop will always be fast and take the same amount of time, there is no real issues. But on to your questions:

1) if you have a conversion applied to "voltage" then voltage[0] will hold the converted value. In fact there is no way to access the unconverted value, except in a logging set

2) yes. Instead of using a global variable for "z", make it a test channel, the easiest way would be a test "D to A" channel. The script remains the same except you can drop the word "global" so its just z = 0, and the summation becomes: z = z[0] + voltage[0] because now z has a history too.

3) you can declare in any script. Its a statement, not an expression though, so it has to be script, be it a sequence, channel event, component event, etc. You can use global variables anywhere you can reference a channel, so all script as well as expressions in components, in conversions, etc.

Link to comment
Share on other sites

First of all, thank you Guru for the speedy response!

I made a test channel called "z" as you suggested. I put z=z[0]+voltage[0] in the event for the "voltage" channel, again as you suggested. This works well but I have to set z=0 in the command window or as part of a sequence to make it function. It seems that the test channel z is not viable until a value is assigned to it. Is this correct, or am I missing something?

Thanks Again

Link to comment
Share on other sites

Yes, sorry, I forgot to mention that. z[0] doesn't equal anything unless you initialize it to something so you can't add voltage[0] to it. This is true for variables, channels and V channels. Just put the initialization in a sequence that is marked Auto-Start.

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.