Excellence in Automation by Design

Regarding Real-time & Multi-tasking

  • The subject of "real-time" and "multi-taking" in regards to operating systems has been at the heart of many discussions when it comes to automation. It seems that in school, the subject of real-time is covered but then the comment that 'Microsoft Windows is not a real-time, multi-tasking system' is what is remembered. But how are real-time and multi-tasking defined? In truth, there is no such thing as they are strictly defined by the names themselves. In preemptive multi-tasking systems, there is such a thing as latency which never approaches zero. The time it takes to service an interrupt by pushing all of the CPU register and adder data onto the stack takes some finite time. Multi-tasking is something of a misnomer since a CPU can only be working on one routine at a time. By rapidly switching between tasks, the appearance of doing several things at a time is developed. But in regards to mechanical automation, just how important are these subjects and how narrowly must they be defined?
  • In PC-controlled equipment automation, the computer runs thousands of times faster than the mechanics of a machine. Any process that requires attention more often than a few milliseconds is normally handled by a hardware plug-in such as a Motion Controller card. Modern motion control modules have update times of 1 millisecond or less. This is necessary to provide frequent updates to the motors' command signals to give smooth performance. However, the time it takes to make the actual motion is usually much longer and may often be measured in seconds. No problem for the computer to handle.
  • Another form of multi-tasking, called cooperative multi-tasking, is better suited to the task of machine control, especially on a Windows-based platform. In a typical motion application, the motion controller is commanded to go to a certain position at a certain velocity and a certain acceleration. After the command is issued, you normally must keep checking the motion controller to see when the move has been completed. When put into a loop, this checking can occur many, many times. The actual time to ask the motion controller if it is finished and to get a response is typically in the microsecond range or less. If you have several such motions moving at once, there is plenty of computer time available to be checking all of them. So how is this accomplished?
  • By breaking up the routine into "states" and using a switch to arrive at the current state upon re-entry into the routine, the computer can keep track of many such movements concurrently. Using a task manager routine to check if each motion task needs servicing, the actual time spent within a single task is measured in microseconds. The routine is either starting a motion, checking the status of the motion or looking at a task flag from another task to see if it is time to initiate the next sequence. It is actually faster to use cooperative multitasking since the computer is not constantly pushing a lot of data onto the stack and then pulling it off to go to the next task. You also have complete control of when and where in your routine the computer will switch tasks.
  • The following example, written in Visual Basic, shows how this is accomplished. The task moves an object on a carrier along the X-axis to position1000. The Pickup Head moves down and picks the object off of the carrier. By putting a DoEvents() statement within the loop, the CPU is able to process other events generated by other processes running on the computer including screen painting.
Public Sub Task1()
Dim Static state As Integer
Dim startTime As Single
'
Select Case state
   Case 0
      'Start a move to position 1000
      XAxis.StartMove 1000
      state = state + 1
      Task1Flag = True   'Tell Task Manager service is required here
   Case 1
      'Check to see if the move is finished
      If Not XAxis.Busy Then
         state = state +1
      End If
   Case 2
      'X move finished, now move X to 200
      ZAxis.StartMove 200
      state = state + 1
   Case 3
      'Check to see if ZAxis done
      If Not XAxis.Busy Then
         ZHeadVacuum.TurnOn
         startTime = Timer
         state = state + 1
      End If
   Case 4
      If Timer > startTime + 1 Then
         ZAxis.StartMove 2000
         state = state + 1
      End If
   Case 5
      If Not ZAxis.Busy Then
         state = 0
         Task1Flag = False
      End If
End Select
End Sub
'
'The Task Manager
Public Sub TaskManager()
   Do
   DoEvents()
   If Task1Flag = True Then
      Task1
   End If
   '
   If Task2Flag = True Then
      Task2
   End If
   .
   .
   .
   If TasknFlag = True Then
      Taskn
   End If
   Loop
End Sub