Page 211 - The Ultimate Palm Robot
P. 211

Color profile: Generic CMYK printer profile
           Composite  Default screen
                                                      Bots / The Ultimate Palm Robot/ Mukhar & Johnson / 222880-6 / Chapter 7






                    194     The Ultimate Palm Robot



                                      readSensor();
                                      if (rv > 0) {
                                        rangeStr = ctostr(range);
                                        rVal = rangeStr; //converts string to int
                                        if (rVal > 55) doJig();
                                      }
                                      sleep(1000);
                                      ev = event(0);
                                      if (ev == 3) {
                                        x = penx();
                                        y = peny();
                                        if (x>85 && x<120 && y>50 && y<65) done = 1;
                                      }
                                   }
                                   text(40, 70, "Stopping");
                                   serclose();
                                 }
                               }

                               Simple Jig Explained
                               The program starts by declaring a set of character arrays. These character ar-
                               rays hold the servo commands that will be sent to the BrainStem. We named the
                               character arrays using the string cmd followed by the servo number and the
                               servo position. So, looking above, you can see that the arrays that hold the com-
                               mands to set servo 1 to positions 1, 128, and 255 are cmd11, cmd1128, and
                               cmd1255. The arrays for the commands for servos 2 and 3 are named similarly.
                               We also declare two other variables at the top of the program. The variable
                               range[10] is a character array that will hold the sensor readings from the SV203.
                               The variable rv will hold the number of characters waiting on the serial port.

                                 You may have noticed that the last character in the command arrays was
                               not set when the character array was declared. The final character of each
                               command needs to be the ASCII code 13. We use the initCmds( ) function to
                               set the final character of the array. This function will be one of the first things
                               called by the main( ) function.

                                 The next function is the readSensor( ) function. This function contains the
                               code to get a sensor reading from the SV203 controller. The code starts by de-
                               claring an eight-character array named c to hold the command to read a sen-
                               sor. It also declares a temporary variable named x that will be used for some
                               debugging output. The code calls the strtoc( ) function to put the sensor reading









           P:\010Comp\Bots\880-6\ch07.vp
           Monday, May 12, 2003 4:29:28 PM
   206   207   208   209   210   211   212   213   214   215   216