Roomba
COMP 1010 Home > Back to Extra Labs
IntroductionWelcome 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
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.
Hacking Roomba |
Regular Roomba |
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
After I compiled my code, the roomba does nothing
There are two things that could be wrong:
- If you are using the hacking roomba, than set the hackingModel variable to true in the connectToRoomba function. If you are not using the hacking roomba, change it to false.
- Change the port number. The connectToRoomba function will print out a list of available ports. Select the next available one from the list.
My first program ran on the roomba, but now it does not do anything
This is because when you run your first program, Eclipse will put it in the background. It will still be running and it will maintain its connection to the roomba. You need to stop it before you can send another command.
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 some code that will flash the LEDs.
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.
Note | Octave | Value |
---|---|---|
C | 0 | 0 |
C# | 0 | 1 |
D | 0 | 2 |
D# | 0 | 3 |
E | 0 | 4 |
F | 0 | 5 |
F# | 0 | 6 |
G | 0 | 7 |
G# | 0 | 8 |
A | 0 | 9 |
A# | 0 | 10 |
B | 0 | 11 |
C | 4 | 48 |
C# | 4 | 49 |
D | 4 | 50 |
D# | 4 | 51 |
E | 4 | 52 |
F | 4 | 53 |
F# | 4 | 54 |
G | 4 | 55 |
G# | 4 | 56 |
A | 4 | 57 |
A# | 4 | 58 |
B | 4 | 59 |
C | 5 | 60 |
C# | 5 | 70 |
D | 5 | 71 |
D# | 5 | 72 |
E | 5 | 73 |
F | 5 | 74 |
F# | 5 | 75 |
G | 5 | 76 |
G# | 5 | 77 |
A | 5 | 78 |
A# | 5 | 79 |
B | 5 | 80 |
Remember to pause between every note.
ProTip: When setting the note and the time use a constant so that it will be easy to change later on.
final int NOTE = 35; final int DURATION = 25; final int PAUSE_TIME = 100; roombacomm.playNote(NOTE,DURATION); roombacomm.pause(PAUSE_TIME);
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
Make the roomba go forward until it hits something, than make it go back, wait a couple of seconds than spin and go forward again.