When programming is first taught, the concept that a program follows the “input—
processing—output” model is used (Fig. 13-4) in which data inputs to the program are
passed to a processing block and then passed out of the program in the form of outputs.
This model is good for initial programs on a PC in which some number crunching is done to help you learn how the different programming statements work, but they have little relevance when programming robots.
Instead, a programming model like the one shown in Fig. 13-5 should be used. In this
model, the program is continually looping (never ending) and reading (or polling) inputs, passing them to a block of code for processing into outputs and then delaying some amount of time before repeating.
The delay block is critical for robot programming as it allows the code to be tuned for the operation of the robot and its parts. If the delay is too short, the robot may vibrate more than move because one set of commands from some input conditions are countermanded by the next set of input conditions, resulting in the original input conditions are true again . . .
Specifying the appropriate amount of delay is something that will have to be found by
trial and error. The total loop time should be less than 1 s, but as to the “best” time, that is something that you will have to find by experimenting with your robot. As a rule of thumb, try to keep the total loop time at least 100 ms (0.1 s) so that the motors have time to start up and initiate action before stopping and a new set of inputs polled.
You may also want to leave the motors running during the delay based on the assumption that chances are the processed output will not change radically from loop to loop. This will help make your robot move more smoothly, which is always desirable.
Graphical Programming
In this chapter, there is a lot of material on text-based programming that probably seems very difficult and complex, especially when you consider that there are many graphical robot programming environments available that could reduce a reasonably complex function like programming a light-seeking robot into the simple chore of drawing a picture like the one in Fig. 13-6.
This program will cause a robot to move toward the brightest point in the room. This is a lot easier to understand than the text-based version, which would look something like
while (1) ................. Loop Forever
If (Left > Right) ............Turn Left
Right Motor = On
Left Motor = Off
else
if (Left < Right) ....................Turn Right
Right Motor = Off
Left Motor = On
else ........................... Left = Right, Go Straight
Right Motor = On
Left Motor = On
endif
endif
Dlay(100) .............Delay 100 ms
endwhile
which would probably take you a lot longer to program than moving some blocks and lines around in a graphic editor. While being more difficult to understand and longer to develop, the text-based program gives you a great deal of flexibility that you don’t have with most graphical programming environments. Both programs perform the same operations and work the same way, but what happens if you discover that rather than turning as soon as the left and right light sensors are different values, the robot could run straight while they were within two values of
each other.
In the graphical programming environment, this would be impossible, but in the text-based program the two if statements could be changed to
if (Left > (Right + 2)
if ((Left + 2) < Right)
which would cause the robot to move forward if Left and Right were +/−2 of each other
rather than turning if they weren’t exactly equal. There are a lot of other cases where you will find that you need to tweak the program to run optimally, but you find that it is impossible to modify the graphic program for these needs.
Another advantage of text-based programming over graphic programming is the ability to add more complex logic to the operation of the robot. For example, if you wanted the robot to turn right if it has turned left five times in a row you can add the code quite easily to the text program whereas the graphic program does not have that capability (and indeed, very few graphical programming environments even have variables).
It is important to not generalize. While most beginner and hobbyist graphical programming environments and languages do have the limitations listed here, more professional ones do not. For example, National Instruments’ LabView programming environment offers the same capabilities and flexibility as a text programming language despite being based on just graphic symbols for operators, variables, and decision structures.
Hopefully you realize that these capabilities do not come free; creating a complex graphic program will require at least the same amount of study and practice as developing a text-based one.
Thursday, July 24, 2008
Robotics Programming
Wednesday, July 23, 2008
LINEAR PROGRAM EXECUTION
Modern computer systems, regardless of their sophistication, are really nothing more than electronic circuits that read instructions from their memory in order and execute them as they are received. This may fly in the face of your perception of how a computer program works; especially when you are familiar with working on a PC in which different dialog boxes can be brought up at different times and different buttons or controls can be accessed randomly and not in any predefined sequence.
By saying that a computer program is a sequence of instructions that are read and executed may seem to belittle the amount of work that goes into them along with the sophistication of the operations they perform, but this is really all they are.
A sequence of instructions to latch data from a storage location into an external register using two I/O registers (one for data and one for the register clock line) could be:
Address Instruction
1 I/O Port 1 = All Output
2 I/O Port 2 = 1 Clock Output Bit
3 I/O Port 2 Clock Output Bit = Low
4 Holding Register = Storage Location "A"
5 I/O Port 1 = Holding Register
6 I/O Port 2 Clock Output Bit = High
7 I/O Port 2 Clock Output Bit = Low
In this sequence of instructions, the address each instruction is stored in has been included to show that each incrementing address holds the next instruction in sequence for the program. The first computers could only execute the sequence of instructions as-is and not modify the execution in any way. Modern computers have been given the ability to change which section of instruction sequence is to be executed, either always or conditionally.
The following sequence of instructions will latch the values of one to five into the external register by conditionally changing the section of the instruction sequence to be executed, based on the current value.
Address Instruction
1........................................... Holding Register = 1
2........................................... Storage Location "A" = Holding Register
3........................................... I/O Port 1 = All Output
4........................................... I/O Port 2 = 1 Clock Output Bit
5........................................... I/O Port 2 Clock Output Bit = Low
6........................................... Holding Register = Storage Location "A"
7........................................... I/O Port 1 = Holding Register
8........................................... I/O Port 2 Clock Output Bit = High
9........................................... I/O Port 2 Clock Output Bit = Low
10........................................... Holding Register = Storage Location "A"
11........................................... Holding Register = Holding Register + 1
12........................................... Storage Location "A" = Holding Register
13........................................... Holding Register = Holding Register - 5
14 If Holding Register Does Not Equal 0, Next Execution Step is 6
In the second program, after the contents of ‘Holding Register “A” ’ have 1 added to them, the value has 5 subtracted from it and if the result is not equal to zero, execution continues at address 6 rather than ending after address 14. Note that data from a “Storage Location” cannot pass directly to an output port nor can mathematical operations be performed on it without storing the contents in the “Holding Register” first.
In this simple program, you can see many of the programming concepts listed in operation, and while they will seem unfamiliar to you, the operation of the program should be reasonably easy to understand. The sequential execution of different instructions is known as linear program execution and is the basis for program execution in computers.
To simplify the creation of instruction sequences, you will be writing your software in what is known as a high-level language or, more colloquially, a programming language. The programming language is designed for
you to write your software (or code) in a format similar to the English language, and a computer program known as a compiler will convert it into the sequence of instructions needed to carry out the program.