FIRST Robotics Competition Blog

Community Browser
Showing results for 
Search instead for 
Did you mean: 

The Producer/Consumer design pattern is a great way to keep two processes in sync when they might run at different rates.  This is especially important when there is data transferred between the loops.  When programming your robot you can use the Producer/Consumer design to save data about motor speed, various voltages, accelerometer data, and a lot more!  This can come in handy as you're testing and fine-tuning your robot.

In this example the Producer loop acquires data from the LabVIEW Front Panel (the "Input Value" and "Scaling Factor" are Front Panel Controls) and adds it to a queue of data to be processed.  The Event Structure is where the data is actually added to the queue.  This part of the code runs when the number in "Input Value" changes (for more on Event Structures, see this blog post).  The Consumer loop then removes each element from the queue, displays it on the Front Panel ("Result Value"), and adds the data to a text file.  Because writing data to a text file is a time consuming activity, the Consumer loop takes longer to run than the Producer loop.

This White Paper talks about the Producer/Consumer pattern in more detail and has some additional example code snippets!

Producer Consumer.png