Page 366 - Hacking Roomba
P. 366

Chapter 15 — RoombaCam: Adding Eyes to Roomba                     347



                             The roombacmd application and all other code mentioned can be downloaded from http://
                             roombahacking.com/software/.





                               Listing 15-3: roombalib.c Basics

                               #define COMMANDPAUSE_MILLIS 100
                               #define DEFAULT_VELOCITY 200
                               Roomba* roomba_init(char* portname)
                               {
                                   int fd = roomba_init_serialport(portname,B57600);
                                   if( fd == -1 ) return NULL;
                                   char cmd[1];
                                   cmd[0] = 128;      // START
                                   int n = write(fd, cmd, 1);
                                   if( n!=1 ) {
                                        perror(“open_port: Unable to write to port “);
                                        return NULL;
                                   }
                                   roomba_delay(COMMANDPAUSE_MILLIS);
                                   cmd[0] = 130;     // CONTROL
                                   n = write(fd, cmd, 1);
                                   if( n!=1 ) {
                                        perror(“open_port: Unable to write to port “);
                                        return NULL;
                                   }
                                   roomba_delay(COMMANDPAUSE_MILLIS);
                                   Roomba* roomba = calloc(1, sizeof(Roomba));
                                   roomba->fd = fd;
                                   roomba->velocity = DEFAULT_VELOCITY;
                                   return roomba;
                               }
                               void roomba_drive(Roomba* roomba, int velocity, int radius)
                               {
                                   char vhi = velocity >> 8;
                                   char vlo = velocity & 0xff;
                                   char rhi = radius   >> 8;
                                   char rlo = radius   & 0xff;
                                   if(roombadebug) fprintf(stderr,
                                                     “roomba_drive: %.2hhx %.2hhx %.2hhx
                               %.2hhx\n”,
   361   362   363   364   365   366   367   368   369   370   371