Page 367 - Hacking Roomba
P. 367

348       Part III — More Complex Interfacing





                               Listing 15-3 Continued

                                                      vhi,vlo,rhi,rlo);
                                   char cmd[5] = {137, vhi,vlo, rhi,rlo};  // DRIVE
                                   int n = write(roomba->fd, cmd, 5);
                                   if( n!=5 )
                                        perror(“roomba_drive: couldn’t write to roomba”);
                               }
                               int roomba_read_sensors(Roomba* roomba)
                               {
                                   char cmd[2] = {142, 0};    // SENSOR, get all sensor data
                                   int n = write(roomba->fd, cmd, 2);
                                   roomba_delay(COMMANDPAUSE_MILLIS);
                                   n = read(roomba->fd, roomba->sensor_bytes, 26);
                                   if( n!=26 ) {
                                        if(roombadebug) fprintf(stderr,
                                              “roomba_read_sensors: not enough read
                               (n=%d)\n”,n);
                                        return -1;
                                   }
                                   return 0;
                               }



                             On Mac OS X and Linux desktop systems, you can compile the program by simply typing
                             make in the directory, and the Makefile will build it for you:
                             demo% wget http://roombahacking.com/software/roombacmd/roombacmd-
                             1.0.tar.gz
                             demo% tar xzf roombacmd-1.0.tar.gz
                             demo% cd roombacmd
                             demo% make
                             gcc -Wall -I. roombacmd.c roombalib.c -o roombacmd
                             You can then run it with no arguments to see how to use it, as in Listing 15-4. This com-
                             piled version of roombacmd isn’t usable in OpenWrt, but you can test it before making an
                             OpenWrt version.


                               Listing 15-4: roombacmd Usage

                               demo% ./roombacmd
                               Usage: roombacmd -p <serialport> [OPTIONS]
                               Options:
                                 -h, -- help                   Print this help message
                                 -p, -- port=serialport        Serial port roomba is on
   362   363   364   365   366   367   368   369   370   371   372