LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

General strategies on producer/ consumer loop sampling, when consumer loop controls producer loop

Solved!
Go to solution

Does anyone have any advice on how to control a producer loop from a consumer loop? This application is sampling a sensor output in the producer loop, and when it reaches conditions which are determined in the consumer loop the sampling stops. The producer loop needs to be stopped at this point by the consumer loop, otherwise it would run indefinitely (unless the operator stopped it manually of course). However, when I try this I don't get functional code because the loop doesn't seem to execute. I can post a code snippet if requested, but I have kind of gone back to the drawing board with this and I'm just rethinking my overall approach so would be interested to know if anyone out there has had any experience with this type of functionality.

0 Kudos
Message 1 of 12
(4,895 Views)
Solution
Accepted by DG76

Hello DLTD,

 

Yes please, post your snippet.

Here is my solution with a queue. To stop de producer, I use a local variable.

If someone has a better solution, I'm curious

Capture3.PNG

 

0 Kudos
Message 2 of 12
(4,860 Views)

Thanks Yddet, I'll keep that in mind. I think what I will do next is start again from the beginning, as I was adapting some pre-existing code for a producer/ consumer loop structure and perhaps that wasn't the best approach. Here is the code snippet - it wasn't the original design but I added a conditional so that the default value of the bool controlling the producer loop end would theoretically be false, rather than undefined in case that was causing the loop not to execute. As it turned out, that made no difference

0 Kudos
Message 3 of 12
(4,846 Views)

You could use a queue or notifier.  But based on what I see, I would just put it all in a single loop.  Make a VI for just processing the data.  It should just accept that cluster your Read Sensor VI is already putting out and output the position and any other state information that is needed.

 

As a general rule, if the consumer has to command the producer you are likely doing something wrong.



There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 4 of 12
(4,839 Views)

Hi DLDT,

 

perhaps that wasn't the best approach

You have two loops, which should run in parallel, but you programmed them to run sequential - due to THINK DATAFLOW!

You CANNOT use wires to send values from one loop to the other while they run in parallel! (Well, there are channels nowadays which look like wires making it more tricky for LabVIEW beginners to understand the basic THINK DATAFLOW.)

 

Solution: use local variables - or notifiers…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 5 of 12
(4,837 Views)

The original version was all in a single loop, however this basically wasn't fast enough. Decoupling the processing from the sampling is a way of trying to optimise for speed. I'm just looking to see if it is possible, and what sort of performance improvement it would bring if so.

0 Kudos
Message 6 of 12
(4,821 Views)

I understand that they are parallel loops. The mismatch is essentially temporal. I don't see this as a reason in itself for a solution to be impossible, however. Thanks for the suggestion on local variables or notifiers.

0 Kudos
Message 7 of 12
(4,818 Views)

Yddet, I have developed a version of the producer/ consumer loop which uses a local variable to stop the producer loop. I needed to move the flush and release to after the producer loop and implement my own conditional logic etc. but your example was a good starting point so I'm marking it as the solution. Many thanks!

0 Kudos
Message 8 of 12
(4,804 Views)

1. I agree with crossrulz that the very minimal processing that's visible doesn't seem demanding enough to need to be broken out into a parallel loop.

 

2. If you stick with the 2 loops, I agree with crossrulz again that a queue would be preferred over a local variable, though a local variable can work if used very carefully.  

   I'd have the producer loop perform a Dequeue with a 0 timeout.  It'll almost always timeout immediately, but if a message is sent to "Stop" or "Start", you'll receive it and be able to act on it.

   The nice thing about a queue is that the message doesn't linger around after you've already responded to it once.  That "lingering" is one of the possible gotchas that come with local or global variables.  It's also a potential gotcha with Notifiers, which is why I'd prefer a Queue for this particular purpose.  (Notifiers have their place, but here I think a Queue is preferable).

 

 

-Kevin P

 

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
0 Kudos
Message 9 of 12
(4,776 Views)

@DLTD wrote:

The original version was all in a single loop, however this basically wasn't fast enough.


Seriously?  How much other processing did you have?  The processing in the code you gave us was next to nothing to a CPU.  Packaging into the queue would possibly take as much time as the processing.

 

Another option would be to use a pipeline architecture.  The idea is to use a single loop, but let the processing still happen in parallel with the read.  You basically just use that shift register you already have and let the processing of that data happen on the next iteration.  No need for queues or local variables.  This is a very common architecture in FPGA systems.



There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 10 of 12
(4,773 Views)