Page 184 - The Ultimate Palm Robot
P. 184
Color profile: Generic CMYK printer profile
Composite Default screen
Bots /The Ultimate Palm Robot/ Mukhar & Johnson / 222880-6 / Chapter 7
Chapter 7 Essential Robot Programming Strategies 167
Simple Jig Explained
The program starts at line 10 by opening the serial port. The serial port will be
closed if the program reaches line 90, or if the program is terminated. HotPaw
automatically closes the serial port if a program is terminated. In general, you
will want to open the serial port as the first action your program takes, and
close the serial port only when the program ends. In line 30, the program uses
the form btn command to create a form button with the label Start. In HotPaw
Basic, each button is assigned a key code. The first button to be created is as-
signed key code 14 by default, the next gets 15, and so on. HotPaw does pro-
vide a command to change these key codes within the program, if you need to.
Next, it uses the input$(1) function to wait for the user to tap the Start button.
When the user taps the Start button, the program prints the string “Run-
ning…” and begins its main loop. It begins in line 65 by calling the subroutine to
read sensor 1. If the value returned is greater than 55, the program calls the sub-
routine to do the jig. (The board will return a digital value between 0 and 255 for
the sensor reading. The closer the detected object to the sensor, the higher the
returned value.) The program waits for one second, then starts the loop again.
The first subroutine, which starts at line 1000, moves the robot in its little
jig. It does this by commanding the servos. With the SV203, you send a text
command for each servo. The text command looks like this: SVnMn. The first
n is the servo number; the second is the servo position. The position can be any
number from 1 to 255. The value 128 is center or stop, 1 is full reverse, and 255
is full forward. So to command servo 1 to go forward, the command is
SV1M255. The jig subroutine sets various values for three strings that corre-
spond to the three servos, and calls the subroutine at line 1100 to actually send
the command strings.
The final subroutine, at line 1200, commands the SV203 to read sensor 1,
reads the sensor value if one exists, and stores it in the range variable. The com-
mand for reading an analog port is AD1. The program sends the command
BD1 AD1 to tell SV203 board 1 to read analog port 1. (The BD command is
used when one has multiple SV203s connected together, in which case each
board has its own ID number. Thus, it’s not really needed every time we send
the AD1 command.) The fn serial(5) function returns the number of bytes
waiting at the serial port. If the function returns a value greater than zero, the
bytes are read as a string with the get$(#5,n) function, where n is the number
of bytes to read. If the port was read, the bytes are converted into a value and
the function returns.
P:\010Comp\Bots\880-6\ch07.vp
Monday, May 12, 2003 4:29:22 PM