Previous Topic

Next Topic

Book Contents

Book Index

MultiTrack (Intermediate): Synchronizing tasks

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:

  1. The main sequencer. It controls the inlet valve, the processing time (3 minutes, timed from when the temperature reaches the setpoint) and the pump-out action. It also uses semaphores to "master control" the heater and the additive pump.
  2. The heater control. It controls the heater using the thermostat input and a semaphore from the main sequencer. It signals back to the main sequencer when the temperature gets to the setpoint, using a different semaphore.
  3. The additive pump control. It runs the additive pump for 10 seconds when commanded by the main sequencer via a semaphore.

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...

The main sequencer

Roll your mouse over each line of code in turn to display an explanation.

MainSequence:
        WaitOnK         iStartButton
        On              oFillValve
        WaitOn          iFullProbe
        Off             oFillValve
        SetS            sHeaterEnable
        SetS            sAdditiveEnable
        WaitForST       sAtTempSetPoint
        Pause           18000
        ClrS            sHeaterEnable
        On              oDrainPump 
        WaitOff         iEmptyProbe
        Off             oDrainPump
        GoTo            MainSequence


Heater control

The heater control task is best explained in terms of small blocks of code, each corresponding to a different phase in the life and times of a cycle.

sHeaterEnable           defSEM
sAtTempSetPoint         defSEM

Heater: WaitForST       sHeaterEnable 

        On              oHeater 
        WaitOff         iThermostat 
        Off             oHeater 
        SetS            sAtTempSetPoint 

HeatLoop:
        YieldTask
        GoIfSF          sHeaterEnable,HeaterDone
        Input           iThermostat
        Output          oHeater
        GoTo            HeatLoop

HeaterDone:
        Off             oHeater
        ClrS            sAtTempSetPoint 
        GoTo            Heater 

Additive pump


sAdditiveEnable         defSEM

AdditivePump:   
        WaitForST       sAdditiveEnable
        On              oAdditivePump
        Pause           1000
        Off             oAdditivePump
        ClrS            sAdditiveEnable
        GoTo            AdditivePump

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.

Previous Topic

Next Topic