04-20-2017 03:27 AM
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.
Solved! Go to Solution.
04-20-2017 04:45 AM
04-20-2017 05:32 AM - edited 04-20-2017 05:33 AM
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
04-20-2017 05:38 AM
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.
04-20-2017 05:39 AM
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…
04-20-2017 07:03 AM
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.
04-20-2017 07:07 AM - edited 04-20-2017 07:08 AM
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.
04-20-2017 07:36 AM
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!
04-20-2017 11:55 AM
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
04-20-2017 12:13 PM
@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.