LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Event-Driven Structure with two loops

Solved!
Go to solution

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?

0 Kudos
Message 1 of 10
(3,380 Views)

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…)

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 10
(3,367 Views)

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)

0 Kudos
Message 3 of 10
(3,324 Views)

If 1 loop waits for another loop, it means neither of them is independent

Kudos and Marked as Solution == Thanks
0 Kudos
Message 4 of 10
(3,309 Views)

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?

0 Kudos
Message 5 of 10
(3,306 Views)

Hi kaenzign,

 

you could delete (one of) the queues after exiting loop A. In loop B you handle the error from QueueRead to stop this loop…

 

I agree with Christian to create a better code architecture!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 6 of 10
(3,300 Views)

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.

0 Kudos
Message 7 of 10
(3,294 Views)
Solution
Accepted by topic author kaenzign

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!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 8 of 10
(3,285 Views)

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?

 

0 Kudos
Message 9 of 10
(3,262 Views)
Solution
Accepted by topic author kaenzign

Hi,

 

surely there is a way: wire the shift registers through all cases!

This is very basic LabVIEW stuff: did you take the free lessons?

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 10 of 10
(3,253 Views)