Page 54 - Designing Autonomous Mobile Robots : Inside the Mindo f an Intellegent Machine
P. 54
The Basics of Real-time Software (For Mere Mortals)
Also, it is important to notice that VB does not offer any ready way to prioritize
events. If a call to DoEvents is made by a thread, the next active event on the list
will be serviced first, regardless of its importance. This fact adds to the indetermi-
nate nature of event latency under VB. As a benchmark, I have written elaborate
8-channel PID algorithms that occurred on 16ms intervals in an environment
supporting modest graphical animation, and experienced manageable latency.
So when should a DoEvents call be placed in a thread? The answer comes in under-
standing the kind of things that bog down a CPU. In VB, these include loops,
handling of large arrays or strings, graphical operations, and screen-related functions
such as changing the background color of a text window. In each of these cases, the
function should call DoEvents immediately before and after such events, or in the
case of loops it should be done within the loop.
‘Post a message form with yes, no, and cancel buttons.
‘This form will return the operator’s selection through
‘the global variable Response.
YesNoCancel xYellow, “Are you sure it is safe to operate?”
Do While Unloading = False And Response = 0
DoEvents
Loop
If Response <> YesResponse Then
Exit Sub
End If
‘Now that we have permission, let’s start the system.
Figure 3.5. Using DoEvents in a loop
Figure 3.5 demonstrates several very important considerations required when using
VB as a real-time system. The first line calls the custom function YesNoCancel. The
function YesNoCancel launches a form which presents the question text on a col-
ored background and which has buttons for yes, no, and cancel. When the form is
launched, the public variable Response is set to zero to indicate that a response has
not occurred.
The program then enters a loop to wait for the operator to select one of these but-
tons. If a response occurs, the result is placed in the public variable Response, and
the query form closes. As a matter of interest, the query form also has a timer which
will enter a Cancel response and close the form if the operator does not respond in a
reasonable time.
37

