From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I avoid a sequence race condition

I'm very new to Labviews, so hopefully this is an easy question to answer and I'm just being dense.

Basically, I'm reading a data value, comparing it to the previous value read, then storing the new value as the old value so I can do it again with the next value read.

In pseudo-code that is:

read new
result = new - old
old = new
repeat

I'm using a local variable to store the old value. I created a sequence that takes the new value and does the compare in the first frame, passes the new value to the second frame as a sequence local, then stores the new value into the old value local variable.

This is creating a race condition and my result is always 0. What is happening here and what is the preferred way to do t
his simple calculation?

Thanks,
Jeff Vickers
NASA/KSC
0 Kudos
Message 1 of 5
(4,243 Views)
Jeff,
1) I preffer not to use Sequence with more than 1 frame.
2) I preffer also not to use local variables (because race condition)

It better to use while loop with shift register (this is local memory that remember previous values of something).

You can post your VI and i can change it to new structure with While Loop.
0 Kudos
Message 2 of 5
(4,243 Views)
Jeff,

It sounds like you have it wired right from your description, though sequences and locals are not the best way to accomplish this.

Are you running in a loop? If so, the loop may be running so fast that you aren't able to see the change in the result before it switches back to zero on the next iteration (when old=new). This depends on how fast your new value is changing.

Also check to make sure your frames are in the right order.

A better way is to use a while loop with shift registers. I've attached an example. Notice the delay in the loop. This is so you can see the change in result. If you take it out, the change will occur so fast you won't see.

Good Luck,
Scott
0 Kudos
Message 3 of 5
(4,243 Views)
ok use this demo vi (Labview 5.1 you can read on 6.X )

replace the random number vi by acquire vi


"Jeff Vickers" a ?crit dans le message de news:
50650000000800000065630000-1031838699000@exchange.ni.com...
> I'm very new to Labviews, so hopefully this is an easy question to
> answer and I'm just being dense.
>
> Basically, I'm reading a data value, comparing it to the previous
> value read, then storing the new value as the old value so I can do it
> again with the next value read.
>
> In pseudo-code that is:
>
> read new
> result = new - old
> old = new
> repeat
>
> I'm using a local variable to store the old value. I created a
> sequence that takes the new value and does the compare in the first
> frame, passes the new value to the s
econd frame as a sequence local,
> then stores the new value into the old value local variable.
>
> This is creating a race condition and my result is always 0. What is
> happening here and what is the preferred way to do this simple
> calculation?
>
> Thanks,
> Jeff Vickers
> NASA/KSC



[Attachment Demo of registre à décalage.vi, see below]
0 Kudos
Message 4 of 5
(4,243 Views)
I'll try to expand on my situation a little. I'm decoding a PCM stream to pull out some temperature data and display it for a demo of our system.

Here's the vi that I was working with as well as a couple of subvis. Don't laugh, I'm new to Labviews! 🙂 It's still a work in progress, so there is functionality that may be grouped into subvis in the future and certainly a lot of cleanup to do.

The company that made the PCM decoder card I am using sent me a vi to initialize the card and read the PCM frames into an array.

I modified the vi to pull out and decode the header informataion from the PCM frame, search the PCM frame for three different sensors, decode the temperature values, and then display them along with the frame count of the frame in which the sensor data was received.

Here's an attempt at describing the flow of the vi:

The vi that I received from the company was a sequence with 7 frames that went through the steps to initialize the card, set up my PCM information, return a handle to the card, and read one pcm frame into an array.

In the 7th frame of the sequence, I inserted a loop to continually read data from the card. The data is being received at a rate of approximately one frame per second.

As each frame is received, I take the array with the pcm data and pull out the header information and display it (including decoding the time value and displaying that).

I created a FIND MWIS (MWIS is our Micro Wireless Instrumentation Sensor) subvi. I pass the PCM array and a sensor ID to this vi and it will search the array for that sensor and return a true or false indicating whether it found the sensor and also return an 8-byte array containg the sensor data.

I use the true/false output from FIND MWIS as a selector on a case statement. If it's true, I decode and display the sensor data and I display the frame count that the data was received. If it's false, I do nothing.

If you've followed me so far, great!

Here's what I want to add. I want to calculate the sample rate of the sensor (which can be anywhere from once per second to once per hour). Since my frames are once per second, all I need to do is take the frame count of the last sensor reading and subtract that from the frame count of the new sensor reading. That will tell me how many seconds have passed between samples and thus, the sample rate.

I tried inserting a sequence into the TRUE frame of my case statemet, doing what I described in my original post, but wound up with a race condition and a sensor rate that would flash very breifly then change to 0. I guess I could pass each of the Frame counts from my 3 case statements out of the while loop through a shift register, but that seems kind of clunky.

Am I just approaching this whole problem from the wrong angle, or do I need to go pick up a Labviews book and start reading on the proper method of designing a vi? 🙂


PS - If you don't like local variables, don't even look at my MWIS Raw to Temp sub vi... It was my first shot at putting a moderately complex calculation into a vi. Doing it that way seemed to be easier than trying to figure out how to use formula nodes.

I plan on going back to play with it later, but it currently works and I'm not going to mess with it now 🙂
Download All
0 Kudos
Message 5 of 5
(4,243 Views)