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: 

passing value to-and-from timed loop

Solved!
Go to solution
Hello everybody,

I am working on a VI which consists of a timed loop that takes values out of a file every 6 ms and writes it in an output file. Inside this timed loop there is also a MATLAB block that calculates a parameter based on the value; based on this parameter a marker is set to either 0 or 1 (intended to work as a boolean marker).

The problems arises when the marker is set to 1. I need an additional structure of some sort that waits for 5 seconds (without stopping the iterations of the timed loop) and if a "stop button" is not pressed an additional string is placed in the output file. I wish for the marker to be set back to 0 if the "stop button" is pressed and for no string to be put in the file.

The main problem appears to be the use of a structure outside the timed loop. I cannot have a while loop or any structure that waits for 5 s inside the timed loop. And if I put it outside it does not pass the value of the marker to the second structure during execution of the timed loop.

+ Any ideas on what structure to use for the 5s waiting period? (I tried a while loop)
+ Should I use a local or global variable for the marker? (I briefly attempted this, but as a local variable I couldn't select a variable defined in the MATLAB block, and I don't understand global variables in LabVIEW)

I am attaching a simplified version of the VI along with a sample input file in case it may help to visualize it.

Thank you,
DAVA
Download All
0 Kudos
Message 1 of 4
(3,574 Views)
Solution
Accepted by topic author DAVA

There are a few issues with your solution so far, but first I'll try to help answer your original question.

 

You are correct that you can't have a structure inside the Timed Loop that waits for 5 seconds without blocking the execution of the Timed Loop for that same time period. You are also correct that you can't simply communicate via wires to an external structure. If you wire the output of one loop into another, that creates a dataflow dependency, which means that the loop receiving the data cannot run at all until the other loop terminates its execution entirely and sends it the data.

 

You have a couple options:

  • Don't actually use a Wait function to determine if 5 seconds has elapsed. Use the Elapsed Time Express VI (search the palette). It just keeps track of how much time has elapsed since it was last reset. It doesn't actually wait. So you're safe to use it inside your Timed Loop within a Case Structure that's triggered whenever the Marker value becomes 1. Also check the boolean control in this Case Structure to decide if you should output the data to the file.
  • Use an external loop like the example you posted, but use VIs from the Synchronization palette to send data between parallel loop. Consider a queue.

But overall I don't think there's much chance of this working very well. No way the Matlab Script Node executes fast enough to run at a period of 6ms. Also no way it runs deterministically at all. It has to communicate with an external program, so its execution time is really unbounded.
Jarrod S.
National Instruments
0 Kudos
Message 2 of 4
(3,570 Views)

Jarrod,

 

Thank you for your help and your observations. I will time my actual MATLAB code, you make a good point. What would happen if the time loop iteration (6ms) finishes before all the MATLAB code is run? Will this cause the iteration to go longer or will it cut short the MATLAB block?

 

I tried to implement your suggestion of using the elapsed time express VI. It circumvents the problem of having a different time scale within the timed loop, but I have not been able to get it to work properly. I am having issues with the reset input. I wish for the elapsed time VI to be reset everytime the marker is set to 1. Right now this only occurs the first time. Is there any way to pass this input a "1" only as soon as the marker is set? Presently, if I pass it the "1" from the marker itself, it is constantly reset and "time elapsed" stays at zero infinitely.

 

I am attaching the updated VI in case it may be of help.

 

Thank you,

Diego

0 Kudos
Message 3 of 4
(3,516 Views)

This is true of everything in LabVIEW. No structure (like a Timed Loop, for loop, case structure, etc.) can complete until everything inside it has completed executing. A Timed Loop won't abort code running inside it to maintain a certain speed. If you set a Timed Loop to have a period of 6ms, that only means the loop will try to schedule itself to run at 6ms intervals. If the code inside it takes 100ms to run, it won't be very successful.

 

The Matlab Script Node has a lot of overhead in communicating with Matlab, an external application that might be busy doing its own things, sending it the script and the data, and then receiving the output data. That wrapper code is not only going to be fairly slow, but undeterministic. That means one time it might take 4ms to finish, the next time 400ms. You never know. Your best bet would be to try to implement that code in LabVIEW in some way if possible to reduce this overhead and increase (hopefully) determinism. You could code this in LabVIEW, use a formula node, or perhaps look into the Mathscript Node, which uses the same m-file syntax, but compiles natively in LabVIEW. The Mathscript Node, however, is not very deterministic either.

 

As to the Reset problem: the construct you should look into is called a Shift Register. This attaches to loops and allows you to save values from the previous iteration of the loop. Use that wisely, and you'll be able to solve this problem of resetting the Elapsed Time counter every time Marker is 1. You only want to reset when Marker is 1 and its value has changed from something else.

Jarrod S.
National Instruments
0 Kudos
Message 4 of 4
(3,498 Views)