Page 168 - Hacking Roomba
P. 168

Chapter 7 — Making RoombaView                 149




                               Listing 7-7 Continued

                                    status = “connected. safety fault. reposition roomba and
                               reset.”;
                                 else
                                   status = “connected. roomba detected. ok.”;
                                 if( roombacomm != null ) {
                                   batt_mA     = roombacomm.current();
                                   batt_volts  = roombacomm.voltage();
                                   batt_charge = roombacomm.charge();
                                   batt_cap    = roombacomm.capacity();
                                   batt_percent = (batt_cap == 0) ? 0 :
                               (batt_charge*100)/batt_cap;
                                 }
                                 text(“ status: “+status, 8,12);
                                 text(“   mode: “+((roombacomm==null)?”no roomba” :

                               roombacomm.modeAsString()), 8,24);
                                 text(“battery: “+batt_percent+”%
                               (“+batt_charge+”/”+batt_cap+” mAh) “+
                                                 “  voltage: “+batt_volts+” mV @ “+batt_mA+”
                               mA”, 8,36);
                               }




                             Wrapping It All Up

                             With all the important functionality wrapped up in methods, the draw() method becomes
                             very simple. Listing 7-8 shows what it looks like. The only methods not covered were
                             drawGridlines(), which draws a simple grid on the background, and updateRoombaState(),
                             which only calls roombacomm.sensors(). The finished and running application is shown in
                             Figure 7-6. If you’d like to save the trouble of typing in all the code, the full sketch with already
                             exported applications can be found in the examples directory of the RoombaComm software
                             distribution. This is a fairly complex Processing application, but even so is only approximately
                             400 lines of code.


                               Listing 7-8: The draw() Method of RoombaView
                               void draw() {
                                 background(cFlor);
                                 drawGridlines();
                                 drawStatus();
                                 computeRoombaLocation();
                                 drawRoombaStatus(rx,ry,rangle);
                                 updateRoombaState();
                               }
   163   164   165   166   167   168   169   170   171   172   173