Page 158 - Hacking Roomba
P. 158

Chapter 7 — Making RoombaView                 139




                               Listing 7-3: SimpleTest in Processing

                               String roombacommPort = “/dev/cu.KeySerial1”;  // or COM3, etc.
                               boolean hwhandshake = false;     // true for Windows bluetooth
                               RoombaCommSerial roombacomm = new RoombaCommSerial();
                               roombacomm.waitForDSR = hwhandshake;
                               if( !roombacomm.connect(roombacommPort) ) {
                                 println(“Couldn’t connect to “+roombacommPort);
                                 System.exit(1);
                               }
                               println(“Roomba startup on port”+roombacommPort);
                               roombacomm.startup();
                               roombacomm.control();
                               roombacomm.pause(30);

                               println(“Playing a note”);
                               roombacomm.playNote(72,10);  // C
                               roombacomm.pause(200);

                               println(“Spinning left, then right”);
                               roombacomm.spinLeft();
                               roombacomm.pause(1000);
                               roombacomm.spinRight();
                               roombacomm.pause(1000);
                               roombacomm.stop();
                               roombacomm.disconnect();



                             The main difference from the regular Java SimpleTest is the lack of command-line argument
                             parsing. For now, just enter in the serial port name directly into the sketch by setting the
                             roombacommPort variable. Also different from regular Java is all the normal Java class infra-
                             structure code. With Processing you just start writing the commands you want.

                             Dealing with setup() and draw()

                             You probably noticed when comparing Listing 7-1 and Listing 7-3 that the latter doesn’t
                             have the setup() and draw() functions defined. In Processing you can either operate in a
                             single-pass mode or in animation mode. Listing 7-3 is an example of the single-pass mode.
                             The code runs from top to bottom once and then exits. Listing 7-1 is an example of animation
                             mode: setup() is called once, then draw() is called several times a second, determined by
                             the framerate().
                             Since draw() is called periodically, it is expected to complete before the next occasion of
                             draw() is to be called. For example, when framerate(15) is specified in setup(), draw()
                             will be called 15 times per second, which is every 67 milliseconds. Thus waiting around doing
                             nothing for 1000 milliseconds with roombacomm.pause(1000) is entirely inappropriate
                             when using animation mode.
   153   154   155   156   157   158   159   160   161   162   163