Page 134 - Hacking Roomba
P. 134

Chapter 6 — Reading the Roomba Sensors                 115



                     ROI SENSORS Command


                             The ROI presents the sensor data in a lightly processed form available through the SENSORS
                             command. This command can retrieve subsets of the entire sensor payload, but for all the code
                             presented here, you will fetch the entire set.

                             Sending the SENSORS Command
                             Sending the SENSORS command is easy; it’s the receiving of the data and parsing it that can
                             be problematic. To send the SENSORS command that retrieves all of the sensor data using
                             RoombaComm, any of the following are valid:
                             // using send() and literal bytes in an array
                             byte cmd = {(byte)0x8e,(byte)0x00};
                             roombacomm.send(cmd);
                             // using two sends and RoombaComm defines
                             roombacomm.send((byte)RoombaComm.SENSORS);
                             roombacomm.send((byte)RoombaComm.SENSORS_ALL);
                             // using high-level command
                             roombacomm.sensors(RoombaComm.SENSORS_ALL);

                             // using high-level command default
                             roombacomm.sensors();

                             The last one is what’s normally used. When this command is sent, Roomba responds by send-
                             ing the sensor packet in usually less than 100 milliseconds. The actual time it takes depends on
                             the serial interface used (Bluetooth adapters have a higher latency at times) and other factors
                             that aren’t entirely clear.

                             Receiving the Sensor Data

                             The SENSORS command is the only one that returns data. All the other commands are one-
                             way. Receiving data can be tricky because the only way to know if you’ve read all the data is to
                             wait for all the bytes to come back. An initial implementation of getting sensor data might be
                             (in pseudo-code):
                             send(SENSORS, SENSORS_ALL);
                             byte data[] = receive(26); // waits for 26 bytes of data

                             Unfortunately if this hypothetical receive() function never gets all 26 bytes, then it either
                             waits forever or times out. Even if it works every time, every time you get data, you block the
                             rest of your program until all the data is obtained.
                             A common way to deal with this problem in Java is to spawn another thread and tell it to go
                             deal with the waiting around. It performs its task and then notifies the main code, returning
                             the result. This callback or event pattern is used a lot when dealing with the real world. In Java,
                             you’ll use the RXTX library’s ability to call back to you when the Roomba sends data.
   129   130   131   132   133   134   135   136   137   138   139