LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Communication between two Consumer Loops in the Consumer Producer Architecture.

Solved!
Go to solution

Hi!

I am using the Consumer/Producer architecture to realize following Project: I have a sensor that continoiusly aquires data and a setup that moves an objects that needs to be measured in x,y,z. For the movment I have a consumer loop with a state machine (UI loop) which is getting a specified postion or stepsize from the producer loop. Now everytime I update the position I update the measured data and display it in the UI. My next step is to continously update and visualize the data while the User still can move the setup. For that added a second Consumer Loop (data aquiring loop) which is doing just that. My question is how do I establish communication between the two consumer loops. There are cases in the UI Loop where it needs the latest data, but only the latest. So my first thought was to give both queues to the data aquiring loop and let it que the latest data into the UI queue when told so by the producer. But that makes the acquiring loop a producer aswell. So far I cannot thing about any problems with this approach but since the project is just at it's beginning and there is a lot more to come, I want to make sure to have a clean base.

 

Regards,
Erik

0 Kudos
Message 1 of 9
(1,729 Views)

The situation you describe is quite common (at least, in my code).  Suppose I have an Acquisition loop that takes 16 channels of data at 1 kHz, and I want to both stream it to disk (so I don't lose any data) and also display it on a chart, perhaps showing a "decimated" (or "binned") plot that updates at 50 points/second.  I run a conventional "Producer/Consumer" loop that takes my 16 x 1000 (I collect 1000 points continuously) and sends them to the Consumer (I use a Stream Channel for this now, where I formerly would use a Queue).  In the Consumer, I not only write the data to disk, but I decimate (possibly by decimating, possibly by averaging) the data and send the 16 x 20 points (again, via a Stream Channel) to the "Charting" Consumer.  [Note I could also send the full data set, 16 x 1000 to the Charter, but it seems more "time-efficient" to decimate first, rather than later].

 

Sounds like that's what you are doing.  Nothing wrong (to my knowledge) with that!

 

Bob Schor

0 Kudos
Message 2 of 9
(1,690 Views)

Hi Bob!

Thank you for looking into my problem. It sounds like you are doing what I am trying to do but the right way. I added a simplified version of my code two better visulize what I've done. It looks like I have troubles to understand how to acquire data and catch user inputs at the same time in the producer loop. This is my understandment of what you are doing, please correct me if I am wrong.

0 Kudos
Message 3 of 9
(1,653 Views)

I've been further looking into the Producer/Consumer Structure and you can ignore the last post. If I understand correctly my User-Event Loop is not the Producer but my Data acquiring Loop is.

0 Kudos
Message 4 of 9
(1,618 Views)

Have you checked out the template project?

ZYOng_0-1674138225236.png

 

-------------------------------------------------------
Control Lead | Intelline Inc
Message 5 of 9
(1,629 Views)

No I haven't! It looks just like what I am trying to do. Thank you for that! 

0 Kudos
Message 6 of 9
(1,590 Views)
Solution
Accepted by topic author erikson_99

My problem with the Project Templates that ship with LabVIEW is that they are (to my mind) overly complex, trying to "do everything all at once" rather than demonstrating an example of a Simple Concept.

 

Try the following:

  • Open LabVIEW.
  • Go to the File menu, choose "New ..." (the second option), and in the DropDown, choose "From Templates\Frameworks\Design Patterns\Producer/Consumer Design Pattern (Events)".  You should get something "simple" that looks like your Event State Machine.  Look in the Consumer Loop -- this is where your "Do Something" code lives.  Suppose you put code in here that starts up DAQmx to acquire 1000 points at 1 kHz inside a While Loop.
  • Go back to the File Menu, choose "New ..." again, but this time choose "Producer/Consumer Design Pattern (Data)".  The top While Loop (where it says "Generate the Data Here"), which is the Producer in this VI, is pretty much what you called the "Consumer" in the previous P/C (Event) code, i.e. it is the While Loop (which needs its own, independent Queue) of the DAQmx Read function, with the Data-to-be-Enqueued the result of the Read function.
  • So you have the first Loop, the Event Handling Loop, calling the second Loop as its "Consumer", the second Loop having a DAQmx Read Loop as a Producer (of Data), passing it to a third Loop being a "Consumer" of Data and, say, graphing it, and/or saving it to disk.
  • All three loops run concurrently, in parallel, a (beautiful) "hidden feature" of LabVIEW.  To remind myself of this, I like to "stack" concurrent loop vertically.  To save space, I often times make them sub-VI that import and export their Queues (it is much easier to "stack" 32x32 pixel sub-VI icons than While Loops ...).
  • What about timing?  The Event Loop hardly ever runs -- unless there is an Event (keep your hands off the Front Panel!), it just sits there, taking no CPU cycles.  The DAQmx Loop runs when 1000 points at 1 kHz have been generated, i.e. once/second, and all it does is transfer 1000 points from the hardware buffer to LabVIEW, where you put it in a queue and send it on its way -- this maybe takes a few milliseconds.  This leaves the remaining part of the second available for the final Producer, which probably needs very little to plot a point or save a record.

Bob Schor

Message 7 of 9
(1,563 Views)

@Bob_Schor wrote:

My problem with the Project Templates that ship with LabVIEW is that they are (to my mind) overly complex, trying to "do everything all at once" rather than demonstrating an example of a Simple Concept.

 

  • Go to the File menu, choose "New ..." (the second option), and in the DropDown, choose "From Templates\Frameworks\Design Patterns\Producer/Consumer Design Pattern (Events)".  You should get something "simple" that looks like your Event State Machine.  Look in the Consumer Loop -- this is where your "Do Something" code lives.  Suppose you put code in here that starts up DAQmx to acquire 1000 points at 1 kHz inside a While Loop.
  • Go back to the File Menu, choose "New ..." again, but this time choose "Producer/Consumer Design Pattern (Data)".  The top While Loop (where it says "Generate the Data Here"), which is the Producer in this VI, is pretty much what you called the "Consumer" in the previous P/C (Event) code, i.e. it is the While Loop (which needs its own, independent Queue) of the DAQmx Read function, with the Data-to-be-Enqueued the result of the Read function.

I agree! Those examples are cleaner and i also prefer to start from those.

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 8 of 9
(1,491 Views)

Thank you very much. I think this will solve my problem very nicely!

0 Kudos
Message 9 of 9
(1,465 Views)