Page 288 - Hacking Roomba
P. 288

Chapter 13 — Giving Roomba a New Brain and Senses                    269



                             Sending Serial Data with the BS2
                             In PBASIC, to send serial data out on a pin, use the SEROUT command with a special
                             baudmode value that is defined in the Stamp reference manual. To set up the ROI as is
                             standard, but at a 2400 bps speed, the PBASIC commands are:
                             ‘ baudmode 32 == 19200 8N1, not inverted
                             BAUDFAST    CON 32
                             ‘ baudmode 396 == 2400 8N1, not inverted
                             BAUDSLOW    CON 396
                             SEROUT RXD, BAUDFAST, [R_START]
                             PAUSE 100
                             SEROUT RXD, BAUDFAST, [R_BAUD, 3] ‘ 3 == 2400 bps
                             PAUSE 100
                             SEROUT RXD, BAUDSLOW, [R_START]
                             PAUSE 100
                             SEROUT RXD, BAUDSLOW, [R_CONTROL]
                             PAUSE 100

                             At this point you can send commands to Roomba like normal, but at a low level like in
                             Chapter 3. For example, to tell Roomba to drive forward, the PBASIC command is:
                             SEROUT RXD,BAUDSLOW, [R_DRIVE,$00,$c8,$80,$00]

                             The R_DRIVE, R_START, R_CONTROL, and so on are constants similar to what’s in RoombaComm.
                             In general, creating constants like that are great mnemonics and make typos easier to catch.
                             Otherwise you’d have to keep remembering that 137 meant the DRIVE command.

                             PBASIC uses the dollar sign ($) to indicate hexadecimal characters. A single quote (‘) indicates
                             the start of a comment.




                             Receiving Serial Data with the BS2
                             The Basic Stamp 2 has no serial input buffer, so as soon as the SENSORS command is sent,
                             your code must wait around until all the data is received. Instead of getting all 26 bytes of
                             sensor data, only data packet 1 (containing 10 bytes) will be requested. The SERIN PBASIC
                             command takes a list of variables to fill out with serial data as its final arguments, making the
                             code to get sensor data look like:
                             SEROUT RXD, BAUDSLOW, [R_SENSORS, 1]
                             SERIN  TXD, BAUDSLOW, 2000, No_Data,
                                     [SENS_BUMPWHEEL,SENS_WALL,

                                     SENS_CLIFF_L,SENS_CLIFF_FL,SENS_CLIFF_FR,SENS_CLIFF_R,
                                     SENS_VWALL,SENS_MOTOROVER,SENS_DIRT_L,SENS_DIRT_R]

                             The SERIN command optionally takes a timeout and GOTO label in case not all the data can be
                             fetched. In the above code snippet, SERIN is to wait two seconds and then jump to No_Data if
                             all the variables can’t be filled out.
   283   284   285   286   287   288   289   290   291   292   293