MultiTrack (Basic): Quick-start for Dummies
If you are in a real hurry, this section may be all you need to get started with using MultiTrack. This is especially so if you have been limiting yourself to the FastTrack instructions and now want to get SPLat to do several things at once. The MultiTrack mechanism greatly expands what can be achieved using little more that the 14 FastTrack instructions. You will need to learn a few more instructions (just 2 to get going), but it's very worthwhile!
Let's illustrate this with an example. As usual, we'll do some rather pointless light flashing, because right now I want you to focus on the mechanisms, not the logic of the application.
Suppose we want a simple FastTrack program which just flashes output 0, on for 430mS and off for 870mS, endlessly. This will do it:
(Click here for some tips for working around problems with copy and paste out of Internet Explorer and HTML-help (.chm)
files)
SlowFlash: On 0 Pause 43 Off 0 Pause 87 GoTo SlowFlash
Cool! The only snag is that this program will tie up the whole SPLat processor (a.k.a. SPLatty) just flashing a light. Suppose we want to really get bold and simultaneously have output 1 flashing at a quite different rate, say 110mS on and 50mS off. That could get messy! But thanks to MultiTrack, it's simple.
Here's how we do it, using MultiTrack:
Firstly we start to think of each of the two flashers as tasks. A task is like a program within a program. It sits there continuously performing a particular set of actions, independently of anything else in your program. A complete control program may consists of a number of tasks taking care of individual bits of the overall functionality, such as managing a pump or an operator interface.
In our simple flasher application the first task is already written. It's called SlowFlash
. Let's do the second one and call it FastFlash
:
FastFlash: On 1 Pause 11 Off 1 Pause 50 GoTo FastFlash
All we need now is something to tie the two tasks together. Here it is (I won't re-type the two tasks from above):
;Start of program LaunchTask SlowFlash LaunchTask FastFlash RunTasksForever ;The code for the two tasks goes in below here ...
What the LaunchTask
instruction does is to schedule the task that starts at the named line (SlowFlash
or FastFlash
in this case) for later execution. It does not actually execute any of the task code, it simply builds a queue (list) of tasks (which we call the task queue) that will be executed later.
The RunTasksForever
instruction then sets the task queue running. That means that each of the tasks in the queue is "run", in turn, in a round-bobbin sequence.
You can best understand what happens by running the program in SPLat/PC. Here's the whole program in one block you can copy and paste into SPLat/PC (you will need SPLat/PC V7.16.0 or later. You can download the latest SPLat/PC here).
;Start of Dual flasher program LaunchTask SlowFlash LaunchTask FastFlash RunTasksForever ;==== SlowFlash task ============== SlowFlash: On 0 Pause 4 Off 0 Pause 8 GoTo SlowFlash ;==== FastFlash task ============== FastFlash: On 1 Pause 1 Off 1 Pause 5 GoTo FastFlash
I have reduced the time intervals for you, so the program will give reasonably noticeable flashing in simulation mode.
Try single stepping the program and observe what happens. You will notice a lot of jumping between a Pause
in one task and a Pause
in the other task. What is happening is that when a task gets run, and is executing a Pause
that has not yet timed out, it immediately yields. What yielding means is that because the task has nothing to do, it frees up the processor (SPLatty) to do something else (switch to another task). Because one tasks yields as soon as it has nothing useful to do, and thereby lets SPLatty do something in another task, we get the illusion of SPLatty doing several things at once. If you look carefully you will see that the status bar at the bottom of the SPLat/PC editor window tells you when the program is in MultiTrack. The action of switching from one task to another is called task switching or task swapping.
All the FastTrack instructions that block the processor, i.e. make it wait for something to happen, become non-blocking with MultiTrack. That means you can now write several tasks using instructions like WaitOn
, Pause
and WaitOffT
, string them together using LaunchTask
and RunTasksForever
, and it will just work. Later you will learn how to make tasks communicate with each other via semaphores and other RAM variables so they can synchronize and coordinate their activities.
Exercise:
Add a third task to the sample program above. Call it Toggle
. Toggle
should monitor input 0. When input 0 comes on Toggle
should turn on output 3. Toggle
should then wait for input 0 to turn off, and then wait for it to turn on again a second time, then turn output 3 off. This should repeat indefinitely. If a push button switch is connected to input 0 it will act as a push on, push off switch for output 3. If your recognize this as the same tired old example I always trot out, don't worry. It's the simultaneous running of several tasks that counts, not what it does. Better still, if you don't like my example devise your own!