12-26-2017 12:03 PM
Hi there,
I'm struggling with the LabView event structures.
What I want to do is to implement two independent loops. When the first loop completes an iteration it should fire an event that triggers the second loop to execute and the first loop pauses. When the second loop the completes its iteration it fires an event that triggers the first loop again while the second loop pauses. So the loops execute alternately but never at the same time.
However I have some problems with implementing this. Currently I use a Value(signaling) properties to trigger the events, however I don't know how to pause the loops after the events are fired.
Can you give me a hint on how to implement this?
Also it would be nice to be able to delay these events, so when the first loop fires a event the second loop shall not start immediately but after a certain time. Is this somehow possible?
Solved! Go to Solution.
12-26-2017 12:18 PM
Hi kaenzign,
why do you want to use event structures here?
Why not use 2 queues? (First loop puts an element in first queue, second loop wait for element in first queue. After receiving the element second loop can run and send an element in 2nd queue. First loop wait for element in 2nd queue…)
But be warned: all those attempts have a (significant) risk of deadlocks! (Both loops might wait for their trigger…)
12-27-2017 12:18 AM
Your architecture makes no sense. Since only one loop runs at any given time, why not just use a single loop with a case structure containing a case for each code alternative?
(take it a little bit further, and you'll end up with a standard state machine. :D. Go there!)
(Event's are mostly for handling user interaction. Don't abuse them for internal program control)
12-27-2017 03:49 AM
If 1 loop waits for another loop, it means neither of them is independent
12-27-2017 04:57 AM
Thanks a lot for the quick replies.
I implemented the syncing of the two loops now using queues as you suggested.
It seems to work, however I'm not sure how to close the loops after the program has executed.
Loop A is a for-loop with N iterations and Loop B is a while loop. So after the N iteration loop A stops, however as B is a while loop it continues.
How can I close loop B when A terminates?
12-27-2017 05:06 AM
12-27-2017 05:40 AM
You're right that the architecture is not too elegant as is.
Maybe it makes sense to explain at this point what I'm trying to do:
I have a signal generator and a measurement device I use to measure voltage (U) and current (I).
I want to measure U & I at different frequencies in an automated way. So what my current implementation does is the following:
Loop A: for-loop that iterates through an array of frequencies and sets in each iteration the frequency of the output signal of the signal generator to the frequency of the current iteration.
Loop B: This loop is activated after the frequency of the signal generator was changed in Loop A, and a measurement of U & I is performed at the new frequency.
Implementing a state machine would be more elegant I think, using a 'set-frequency' and a 'measurement' state. However I'm not sure how to implement this. I'd have to make one for-loop and put a case structure into it with the cases 'set-frequency' and 'measurement' right? However, I'm not sure then how to tackle e.g. the following problem:
- I want to get a new frequency from my frequency array only in 'set-frequency' iterations, how do I do that? Currently I iterate over the frequency array in the for-loop so I'd also get a new frequency in 'measurement' iterations.
Am I on the right track here or do you know a more elegant way to do this?
I attach my code in the following, which currently uses queues.. maybe this helps to better understand what I'm doing.
It's the first time I work with LabView, I really appreciate your help.
12-27-2017 06:24 AM
Hi kaenzign,
Implementing a state machine would be more elegant I think, using a 'set-frequency' and a 'measurement' state.
You got it - that's the right way! 😄
However I'm not sure how to implement this.
Start with the example statemachine project coming with LabVIEW!
To do so select "New…" from file menu and create a new statemachine project!
12-27-2017 09:47 AM
Thank you so much. I used now the state machine template as basis and implemented it from scratch. Everything seems to work fine now.
The only thing I'm not sure how to do now is how to collect the measured U & I values and store them into an array or e.g. plot them.
I tried to do it store them into arrays using auto-indexed tunnels through the while loop in the 'Measurement' state. However, if I introduce these tunnels into the case structure I get the Error 'Tunnel: Missing assignment to tunnel', as it seems to be required to connect the tunnels for ALL cases of the case structure. But I want to use these tunnels only in the 'measurement' state. I could connect '0's to the tunnels in the other cases, but then my arrays containing the measurement would contain these bogus zero values.
Is there an elegant way to do this?
12-27-2017 10:24 AM