Page 138 - Hacking Roomba
P. 138

Chapter 6 — Reading the Roomba Sensors                 119



                             The ROI has a variety of ways of representing numbers because it has a variety of different types
                             of data to represent. Some sensors produce simple on/off data that can be represented as a single
                             bit. Other types of sensors (like the current sensor) have a high resolution and thus need many
                             bits to represent its value.The variety of data sizes allows Roomba to store its data compactly,
                             since it has limited space.The data you receive over the ROI is broken up into byte-sized chunks
                             that then must be teased apart or combined together to reconstruct the original sensor data.

                             Word Values

                             Word (16-bit) values are sometimes thought of as two bytes concatenated together, but it’s
                             truly a single 16-bit entity. The 16-bits of a word can be used to represent positive numbers
                             from 0 to 65535, in which case it’s called an unsigned word. Alternatively, those bits can repre-
                             sent a positive and negative number range from -32768 to 32767. That’s called a signed word.
                             You use words when you can’t fit the values you want to represent in a single byte (range 0 to
                             255 or -128 to 127). You choose signed or unsigned depending on whether you need to repre-
                             sent negative numbers.

                             The general method of converting two bytes to a 16-bit (word) values is:
                             int word = (high_byte << 8) | low_byte
                             In Java, things aren’t so simple because technically bytes are signed (-127 to 127) and doing the
                             preceding sort of combination of two bytes yields very strange and incorrect results. Also, the
                             preceding pattern doesn’t quite work for the signed values of distance and angle. Instead of
                             having one mechanism for dealing with word values, you need two. Listing 6-3 shows the Java
                             version of these two. A Java short is a 16-bit signed integer, thus capable of holding a 16-bit
                             signed word. To hold an unsigned 16-bit word, you have to use a Java int.


                               Listing 6-3: RoombaComm.toShort() and .toUnsignedShort()

                               static public final short toShort(byte hi, byte lo) {
                                 return (short)((hi << 8) | (lo & 0xff));
                               }
                               static public final int toUnsignedShort(byte hi, byte lo) {
                                 return (int)(hi & 0xff) << 8 | lo & 0xff;
                               }



                             They are defined as static public final because they are just utility methods usable by
                             anyone.
                             Word values were first mentioned in Chapter 2, and in Chapter 5 you saw how to decompose a
                             16-bit word value into two bytes for use with the DRIVE command.
   133   134   135   136   137   138   139   140   141   142   143