Roomba Program 1

From GICL Wiki
Jump to: navigation, search

This program is a basic autonomous behavior for Roomba and can be used as a starting place. In essence, the Roomba turns right 45 degrees when the left bump sensor is triggered, and 45 degrees left when the right bump sensor is triggered.

The source file can be found here: http://gicl.cs.drexel.edu/wiki-data/images/0/03/ROOMBA_PROGRAM_1.zip

The Explanation

You do not have to know how this first section works.

   ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   // Program: ROOMBA_TEMPLATE
   // Description: - This a template for any Roomba program. In order to use this, simple place code in the 'while' loop in place of "// ((>>>>]]}- PLACE CODE HERE -{[[<<<<))"
   //              - As a Disclaimer, this loops much more complicated than it is...
   //
   // Drexelized By: Peter Thai - [pwt23@drexel.edu]
   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   
   // This imports the libraries necessary for communicating with Roomba (namely RoomabComm)
   import roombacomm.*;
   import roombacomm.net.*;
   
   
   //This stops Roomba and disconnects it from your computer.
   String roombacommPort = "COM3"; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<::::::::::::: ENTER YOUR ROOMBA PORT :::::::::::::
   //String roombacommPort = "/dev/cu.RooTooth-COM0-1";
   boolean hwhandshake = false; 
   RoombaCommSerial roombacomm = new RoombaCommSerial();
   roombacomm.waitForDSR = hwhandshake;
   
       
   // If Roomba fails to connect, it will print out an error and terminate the program 
   if ( ! roombacomm.connect(roombacommPort) )
       {
           println("Couldn't connect to "+roombacommPort);
           System.exit(1);  
       }

The only line that you need to worry about is this line:

 String roombacommPort = "COM3"; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<::::::::::::: ENTER YOUR ROOMBA PORT :::::::::::::

Here, the Roomba Port is COM3. You must replace this Roomba Port with your own Roomba Port. If you do not know your Roomba Port, please refer to the end of each installation section of each Connecting Roomba to Your Computer page.

The next section informs you that the Roomba is indeed connected to your computer. First, it connects to the Roomba to your computer and puts the Roomba in "Safe Mode," which is is a mode programmed by the 1337 h4ck0rz at MIT that allows a certain degree of control of the Roomba brain over the physical components of Roomba.

   // Once Roomba is connected, it will perform the following actions to indicate it has connected
   println("Roomba startup on port"+roombacommPort); // Prints out the Port on which Roomba is connected
   roombacomm.startup();
   roombacomm.control(); // Puts Roomba in "Safe Mode"

Now that the Roomba is connected, it plays a note, pauses so that the Roomba can finish playing the note, and then updates the state of all sensors on the Roomba so that it starts fresh. The state of the sensors can be used later with commands such as roombacomm.bumpLeft() and must be updated with the updateSensors().

   roombacomm.playNote(72,50); // Plays a note
   roombacomm.pause(100); // Pauses Roomba while note is being played
   roombacomm.updateSensors(); // Updates the state of all sensors 
   

This next section is the actual loop that drives the robot, the algorithm, if you will. It first creates a boolean variable (boolean refers to a variable of only True or False) called "done" that is set to false. This variable "done" is set into the "while" loop as a condition to terminate or reiterate the loop. The '!' in "(!done)" negates the done, which is initially set as false... so it is now true and will continue the while loop.

   // This is the main loop. It is executed continuously until the variable 'done' is set to true
   boolean done = false; // This instantiates the variable 'done' and sets it to false
   while (!done) 
       {
               ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
           // ::::::::::::: START CODE (Code goes between this thing and.... *look down*) :::::::::::::
               ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Now that we're actually in the while loop and we know that these next few lines of code will be reiterated continuously, we can finally start commanding Roomba. This first line, "roombacomm.goForward()," simply drives the Roomba forward. The next line, "roombacomm.pause(200)," stops the future execution of commands for 200 milliseconds so that the previous line of code can finish executing in the Roomba.

           roombacomm.goForward();
           roombacomm.pause(200);

Here is the "roombacomm.updateSensors()" command again... this is just to make sure that the sensors are updated again for the next few lines of code.

           roombacomm.updateSensors();      

Now comes the heart of the Roomba logic. The "if" statement here makes the Roomba turn right 45 degrees if the left bump sensor is triggered. The Roomba only has 2 sensors, right and a left sensor. When the "roombacom.bumpLeft()" command is called, it returns true if the left sensor is triggered and false if it is not.

            if (roombacomm.bumpLeft())            
                       {                
                           roombacomm.spinRight(45); 
                       }         

This next else if statement turns the Roomba left 45 degrees if the right bump sensor is triggered (the "roombacomm.bumpRight()" command returns a true).

            else if (roombacomm.bumpRight())             
                       {                
                           roombacomm.spinLeft(45);                      
                       }       
                   roombacomm.updateSensors();    
   
               ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
               // ::::::::::::: END CODE (Code goes between this thing and... *look up*) :::::::::::::
               ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       }
   

These last few lines of code simple stop your Roomba and then disconnect the Roomba from your computer... the chances that these few lines will ever be ran are rare.

   //This stops Roomba and disconnects it from your computer.
   roombacomm.stop(); 
   roombacomm.disconnect();