Page 57 - Designing Autonomous Mobile Robots : Inside the Mindo f an Intellegent Machine
P. 57
Chapter 3
To correct this problem, simply add the line “exit sub” immediately after the “Un-
load me” and before the “End if.”
Modal controls
If you have programmed in VB before, you probably had another question about the
code in Figure 3.5—namely, why go to all the trouble to create a query form like that
launched by YesNoCancel? After all, VB has a standard control called MsgBox whose
style can be set to YesNo to perform exactly this function. Well, not quite…
Many VB controls that require input are programmed to intentionally block their
thread (and thus all other events) until they get that input. In other words, they are
not polite and do not call DoEvents while waiting for their input. They are said to be
modal and cannot be used in a real-time application, because they will block all
threads and background tasks. Exactly why they were not given a property that could
be changed from modal to non-modal is a bit of a mystery, as many very nice controls
are rendered useless to real-time programmers.
Some other tips on using VB for real-time applications
Many VB controls like text boxes are not quite as smart as one would expect. For
example, if the text in the box is “System On,” and a line of code again sets the text
to “System On,” the text will be repainted despite the fact that nothing has
changed. The same is true if the background color is set to the same value that it
already is. This will cause these controls to flicker and it will waste CPU cycles.
When a large number of readings, bar-graphs, and other visible controls need to be
refreshed regularly, this can cause a very significant waste of computer resources. For
this reason one should include code that tests to see if the new text or property is
different from the old one before attempting to set it.
Setting up a structure
As we discussed in Chapter 1, the most important decisions you will make are those
regarding the structure of your system. Here are some pointers to remember.
1. List all of the services your program will need to provide. These might in-
clude optional and transient services as well as full-time support services such
as communications interfaces.
40

