Page 166 - Hacking Roomba
P. 166

Chapter 7 — Making RoombaView                 147



                             The Processing commands translate() and rotate() act to move and rotate all subsequent
                             drawing that is done. For simple sketches, using those commands by themselves is fine. When
                             drawing multiple things that use translate() and rotate(), you need a way to save and
                             restore the current drawing position. The pushMatrix() and popMatrix() commands save
                             and store, respectively, the current drawing position and orientation.
                             For example, the draw() method in Listing 7-1 could be only slightly re-written so that it’s a
                             blue diamond that follows the mouse instead:
                             void draw() {
                               background(51);
                               fill(0,0,255);
                               rectMode(CENTER);
                               pushMatrix();
                               translate( mouseX, mousey );
                               rotate(PI/4);
                               rect(0,0, width/5, height/5);
                               popMatrix();
                             }

                             Notice that a square is still being drawn, but what turns it into a diamond is the rotate(PI/4)
                             method, which rotates all subsequent drawing by 45 degrees. (In Processing rotation is measured
                             in radians. 2 × PI radians == 360 degrees, thus PI/4 radians = 45 degrees. PI is π= ~3.14.) This
                             is the exact technique used to rotate and move the Roomba icon without needing to parameterize
                             all the Roomba drawing code.

                             Computing Position
                             The three values needed to specify the location of the virtual Roomba icon are rx, ry, and
                             rangle. These are the three arguments to the drawRoombaStatus() method in Listing 7-5.
                             From the current location and new distance and angle data from the Roomba, the task is to
                             compute the next on-screen location. Listing 7-6 is the algorithm used every time the sensors
                             are read to determine the new location.
                             The first thing it does once it gets the new sensor data is to determine the new rangle by
                             subtracting it from the current angle. It’s a subtraction because Roomba measures angles
                             counter-clockwise, while Processing measures angles clockwise. Given the new angle, the x
                             and y components (dx,dy) of the updated position vector can be computed. With a new dx
                             and dy, the values of rx and ry can be calculated by adding dx,dy to rx,ry. The sign of dy is
                             flipped because on-screen graphics are flipped top-to-bottom from normal coordinate systems.
                             Both dx and dy are scaled by experimentally determined scalex and scaley values. This
                             scaling effectively converts the sensor data in units of millimeters to units of pixels. Finally, if
                             rx or ry extend beyond the boundaries of the screen, they are wrapped around, just like in
                             Asteroids.
                             Experiment with changing scalex and scaley to see how it affects the speed of the on-screen
                             version of Roomba.
   161   162   163   164   165   166   167   168   169   170   171