Difference between revisions of "Roomba"

From CompSciWiki
Jump to: navigation, search
Line 81: Line 81:
 
The reason this code does not work is because the roombacomm object sends and recieves commands at real time. This means that you have to give the roomba some time to complete an instruction before moving on to the next one.
 
The reason this code does not work is because the roombacomm object sends and recieves commands at real time. This means that you have to give the roomba some time to complete an instruction before moving on to the next one.
  
Now try this:
+
Now try this:<br>
 
'''WARNING: Never leave a robot on a table unattended.'''
 
'''WARNING: Never leave a robot on a table unattended.'''
 
{{CodeBlock
 
{{CodeBlock

Revision as of 00:44, 5 April 2012

COMP 1010 Home > Back to Extra Labs

Introduction

Welcome to the Comp 1010 extra labs. In this lab you will learn the basics about the roomba. We will cover basic movements, how to use the lights, how to make sound, play a simple melody, and how to read sensor data with Roomba.

Getting started

Setting up Eclipse

If you are not familiar with eclipse please see the Introduction to Eclipse extra lab.

Run your first program

Startup.jpg
Importing your project
  • Import the Roomba project placed on your desktop.
    • Click File->import
    • Select General->Existing Project
    • Click Browse and find the RoombaTest project
    • Click Finish
  • Connect your computer to your roomba using the connection cable
  • Compile the project by clicking the Green arrow
  • It should make a noise.

connectToRoomba

This function is given to you as part of the Rommba.java source code. Its purpose is to create a connection between your computer and the roomba.

 public static RoombaCommSerial connectToRoomba(boolean hackingModel, int port) 

The input parameters are:

  • boolean hackingModel: There are two types of roombas that are available to you in this lab. One is called a hacking model and the other is the regular model.

The only differance between the two is that the hacking model does not have a vacuum. Set this value to true if you are using the hacking model, and false otherwise.

HackingRoomba.JPG
Hacking Roomba
Roomba.JPG
Regular Roomba
RoombaCable.JPG
Connection cable
  • int port: This is the usb port your roomba will connect to. By default your roomba will be on port 0. If it is not do not worry, the function will print a list of available ports to the console.

Output:

  • RoombaCommSerial: A RoombaCommSerial object will allow you to control the roomba. More on this later.

Troubleshooting

Problem tab

Ports

The roombacom object

The connectToRoomba function will return a RoombaCommSerial object which will be assigned to the variable roombacom. This object will maintain the state and connection to the roomba. With it you can send commands to the roomba and receive information back from the roomba. Now lets see how it works.

Wait a sec

Try running this code

 roombacomm.goForward();
    roombacomm.stop(); 

This code will not work. Please take a moment to figure out why before you continue.

The reason this code does not work is because the roombacomm object sends and recieves commands at real time. This means that you have to give the roomba some time to complete an instruction before moving on to the next one.

Now try this:
WARNING: Never leave a robot on a table unattended.

 roombacomm.goForward();
    roombacomm.pause(1000);
    roombacomm.stop(); 

The pause function takes the number of milliseconds as its input and will make your program wait that set amount of time before continuing.

Basic Movement

Here is a list of functions that will make to roomba move around.

Lights

The roomba has an array of LEDs which you can use. They can be used by accessing the setLEDs function.

 setLEDs(
    boolean status_green,
    boolean status_red,
    boolean spot,
    boolean clean,
    boolean max,
    boolean dirt,
    int power_color,
    int power_intensity); 

Here is the code that is used to flash the LEDs in the video.

 while (stillRunning) 
    {
        roombacomm.setLEDs(true, true, false, true, true, false, 200, 200);
        roombacomm.setLEDs(false, false, true, false, false, true, 20, 20);
    } 


Sound

The roomba can make beeping sounds and you can control those beeps to make the roomba play a song.

 void playNote( int note, int duration ); 

Input:

  • int note: this is an integer note from the table below.
  • int duration: This is the number of 1/64th seconds of the note.

caption

Sensor Data

You can read the roombas sensor data and get it to react to objects in its environment. In order to read the sensor data you need to understand a concept called polling. Polling is the act of constantly sampling an external event, in this case if the roomba has bumped into something.

In order to get sensor data you need to poll the function:

 void updateSensors(void); 

Only after this function has been run can you read the other sensors. Here is an example of how to use it.

 boolean stillRunning = true;
    while( stillRunning )
    {
        roombacomm.updateSensors();
        if( roombacomm.bump() )
        { 
              System.out.println( "bump" );
        }
    } 

Here is a list of sensor commands:

 boolean bump();            // Middle sensor bumped
    boolean bumpLeft();        // Left sensor bumped
    boolean bumpRight();       // Right sensor bumped
    boolean wheelDropLeft();   // Left wheel droped
    boolean wheelDropRight();  // Right wheel droped
    boolean cliffFrontLeft();  // Front left of roomba is over an edge
    boolean cliffFrontRight(); // Front right of roomba is over an edge
    boolean cliffLeft();       // Left side of the roomba is over an edge
    boolean cliffRight();      // Right side of the roomba is over an edge
    boolean powerButton();     // Power button is pressed
    boolean wall();            // Roomba is next to a wall 

Exercise

And Beyond ...