When you write a MultiTrack program containing several tasks, each task will normally be designed to take care of some fairly well defined aspect of the program. However, you will frequently need the various tasks to interact with each other, to coordinate their activities.
A simple but powerful way of coordinating the activities of multiple task is to use semaphores. Semaphores are simple on/off flags that can be set, cleared and tested by your program. Think of the semaphore signals used along railroad tracks, coordinating the activities of trains running on multiple tracks. Just as railroad semaphores synchronize the activities of the various trains to produce good outcomes rather than bad ones, semaphores can be used to coordinate the activities of tasks in a MultiTrack system.
Example
Imagine you are writing a program to control a batch processing vessel in the food industry. The vessel has an inlet valve, a heater and a pump to pump out the processed liquid. The heater is controlled by a simple thermostat but must only be turned on while the vessel is full. The fluid level in the vessel is controlled by low and high level probes. In addition there is a dispensing pump that must run for 10 seconds to dose the liquid with an additive during filling. The process is started by an input called iStartButton.
The program will have 3 tasks:
The complete program file is located in the directory path Examples>MultTrk under SPLatPC in Program Files. The file name is MulTrk01.SPT.
Apart from the MultiTrack instructions that have been introduced earlier in this tutorial, it uses one other new MultiTrack-related instruction, WaitForST.
WaitForST (Wait For Semaphore True) will simply sit and wait for the nominated semaphore to become True. It will continually yield until the condition is met. Once the condition is met is lets control pass to the next instruction in the task.There is also a complimentary instruction, WaitForSF, which waits for the semaphore to become false.
Let's dissect the main features of the program, one task at a time...
| ||||
| ||||
| ||||
Exercise 1:
If the start button is pressed in the middle of a cycle, when the cycle completes a new cycle will start immediately. Work out why this is so, and change the program so the button must be pressed after the end of the cycle to have any effect.
Exercise 2:
The code in the heating loop in the heater control could be a little smaller. The loop contains one conditional GoTo and one unconditional GoTo. Rewrite the loop to eliminate the unconditional GoTo.
Exercise 3:
Run the program in the SPLat/PC simulator. You can get the complete program in the directory path described above. If it's not there you may need to download and install the latest SPLat/PC. You will probably discover that testing the complete program in toto is quite impractical. Break the program into individual tasks and test each task independently, as best you can. You will have to write some special code to aid testing.