1 / 37

Lesson 2 Communicating Between Multiple Loops

Lesson 2 Communicating Between Multiple Loops. Variables Functional Global Variables Race Conditions Synchronizing Data. A. Variables. Cannot pass data between parallel loops with a wire

aguerin
Download Presentation

Lesson 2 Communicating Between Multiple Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lesson 2Communicating Between Multiple Loops • Variables • Functional Global Variables • Race Conditions • Synchronizing Data

  2. A. Variables • Cannot pass data between parallel loops with a wire • Variables allow you to circumvent normal dataflow by passing data from one place to another without connecting the two places with a wire

  3. A. Variables • Variables are block diagram elements that allow you to access or store data in another location • Variables can be of the following types: • Local: store data in front panel controls and indicators • Global: store data in special repositories that can be accessed from multiple VIs • Functional Global: store data in While Loop shift registers • Shared: transfers data between various distributed targets connected together over a network

  4. A. Variables – Using in a Single VI • Use local variables to pass data within a single VI

  5. Creating Local Variables • Create and use local variables.

  6. A. Variables – Using Between VIs • Use a global variable or a single process shared variable to share data between multiple VIs • Use a global variable to share data among VIs on the same computer, especially if you do not use a project file • Use a single process shared variable if you may need to share the variable information among VIs on multiple computers in the future

  7. Creating Global Variables • Create and use global variables.

  8. Creating Shared Variables • Create and use single process shared variables.

  9. A. Variables – Using Carefully

  10. A. Variables – Using Carefully

  11. A. Variables – Initializing • Verify that variables contain known data values before the VI runs • If you do not initialize the variable before the VI reads it for the first time, it contains the default value of the associated front panel object

  12. A. Variables – Initializing

  13. B. Functional Global Variables • The general form of a functional global variable includes an uninitialized shift register with a single iteration For or While Loop

  14. B. Functional Global Variables • A functional global variable usually has an action input parameter that specifies which task the VI performs • The VI uses an uninitialized shift register in a While Loop to hold the result of the operation

  15. B. Functional Global Variables – Timing • Very useful for performing customized elapsed time measurements

  16. Exercise 2-1: Variables VI • Use variables to write to and read from a control.

  17. C. Race Conditions • A race condition is a situation where the timing of events or the scheduling of tasks may unintentionally affect an output or data value • Race conditions are a common problem for programs that execute multiple tasks in parallel and share data between the tasks

  18. Race Conditions • Watch the instructor demonstrate race conditions. • C:/Exercises/LabVIEW Basics II/Demonstrations

  19. C. Race Conditions • Race conditions are very difficult to identify and debug • Often, code with a race condition can return the same result thousands of times in testing, but still be capable of returning a different result • Avoid race conditions by: • Controlling shared resources • Properly sequencing instructions • Identifying and protecting critical sections within your code • Reducing use of variables

  20. C. Race Conditions – Shared Resources

  21. C. Race Conditions – Critical Code • A critical section of code is code that may behave inconsistently if some shared resource is altered while it is running • If one loop interrupts another loop while it is executing critical code, then a race condition can occur • Eliminate race conditions by identifying and protecting critical code with: • Functional Global Variables • Semaphores

  22. C. Race Conditions – Critical Code

  23. C. Race Conditions – Critical Code • Functional Global Variable used to protect critical code:

  24. C. Race Conditions – Critical Code

  25. C. Race Conditions – Sequencing • What is the final value? • Four possible outcomes: • Value = (Value * 5) +2 • Value = (Value + 2) * 5 • Value = Value * 5 • Value = Value +2

  26. Exercise 2-2: Bank VI • Eliminate a race condition by protecting a critical section of code.

  27. D. Synchronizing the Transfer of Data • Variables are one method for passing data between parallel processes • Using variables breaks the LabVIEW dataflow paradigm, allows for race conditions, and incurs more overhead than passing the data by wire

  28. D. Synchronizing the Transfer of Data

  29. Notifiers • Open the Master/Slave Design Pattern template in LabVIEW and explore the operation of Notifiers.

  30. D. Synchronizing the Transfer of Data • The following benefits result from using Notifiers to transfer data between parallel loops: • Both loops are synchronized to the master loop—the slave loop only executes when the master loop sends a notification • You can use Notifiers to create globally available data, making it possible to send data with a notification • Using Notifiers creates efficient code—there is no need to poll to determine when data is available from the master loop

  31. D. Synchronizing the Transfer of Data Notifier disadvantages: • A notifier does not buffer data • If the master loop sends another piece of data before the first piece of data has been read by the slave loops, that data is overwritten and lost

  32. D. Synchronizing the Transfer of Data • Queues are similar to notifiers, except that a queue can store multiple pieces of data • By default, queues work in a FIFO (first in, first out) manner • Use a queue when you want to process all data placed in the queue • Use a notifier if you only want to process the current data

  33. D. Synchronizing the Transfer of Data

  34. Queues • Open the Producer/Consumer Design Pattern (data) template in LabVIEW and explore the operation of Queues.

  35. Weather Station • Demonstrate the use of queues for data transfer and synchronization of parallel loops. • C:/Exercises/LabVIEW Basics II/Demonstrations/Case Study - Queue

  36. Exercise 2-4: Global Data Project • OPTIONAL • Create a project containing multiple VIs that share data using a single process shared variable.

  37. You should use variables in your VI where ever possible. True False Which cannot transfer data? Semaphores Functional global variables Notifiers Queues Which can you use only within a project? Local variable Global variable Functional global variable Single process shared variable Which cannot be used to pass data between multiple VIs? Local variable Global variable Functional global variable Single process shared variable Summary—Quiz

More Related