Mars Roomba Project UTK


DISCLAIMER: THIS WILL ONLY WORK IF THE ROOMBA IS SET UP IN A
VERY SPECIFIC WAY, THIS INSTRUCTABLE WAS CREATED FOR AND INTENDED TO BE USED BY UNIVERSITY OF TENNESSEE STUDENTS AND FACULTY
This code is used to set up a Roomba to run locally written and saved code in MATLAB. This will not work if you are unable to get the necessary libraries from the University of Tennessee website. If you have the libraries then you are able to use them to program your own Roomba using the functions in the library. This Instructable teaches you how to install the libraries, create a folder for all the code, and how to code and use the program we have provided down below.
Required Materials:
· Roomba
· MATLAB
· Raspberry Pi and Pi Camera
Getting the Libraries
On the engineering website there is a toolbox/library provided, download it and place it into a new folder. This folder must contain all project work files as, any function used in a program you make will need to refer to the library. After you have done this you can begin working on your programs
Writing the Programs
There are quite a few functions that can be used in the program, these functions can be accessed by using the command "doc roomba". Using these functions, you can control your Roomba in a lot of different ways. The code given below uses the bump sensors, light bar sensors, camera, and cliff sensors in different ways to create a mars rover. We used the bump sensors to detect when the Roomba hits an object, when this happens the robot will reverse, turn around, and continue to move. Before the Roomba hits an object, the light bar will detect the object and slow down the Roomba so that when it does bump into the object to activate the bump sensor the Roomba will be less damaged/affected by the impact. The camera searches for water or lava on the surface, if there is no liquid found then the robot will continue to search, if there is some water found then the robot will message the operators. The cliff sensors are designed to stop the robot if it approaches a cliff. If the robot senses a cliff it will reverse and turn around to avoid falling.
Code
Copy and paste this into a MATLAB file that is located in the same folder as the libraries
function
MainRoombaFile(r)
r.setDriveVelocity(0.1,0.1)
while true % Infinte while loop to keep code running
dontFall = cliffCheck(r) % Assigns variable 'dontFall' to the function 'cliffCheck'
if dontFall % if statement to proceed in code after 'cliffCheck' is complete
r.setDriveVelocity(0.1,0.1) % Keeps Roomba moving after 'cliffCheck' is complete
end % ends 'dontFall' if statement
bumper=bumpcheck(r) % Assigns variable 'bumper' to the function 'bumpcheck'
if bumper % if statement to proceed in code after 'bumpcheck' is complete
r.setDriveVelocity(0.1,0.1) % Keeps Roomba moving after 'bumpcheck' is complete
end % ends 'bumper' if statement
liquids = LiquidCheck(r) % Assigns variable 'liquids' to the function 'LiquidCheck'
if liquids % if statement to proceed in code after 'LiquidCheck' is complete
r.setDriveVelocity(0.1,0.1) % Keeps Roomba moving after 'LiquidCheck' is complete
end % ends 'liquids' if statement
lightbumper=lightcheck(r) % Assigns variable 'lightbumper' to the function 'lightcheck'
pause(0.1) % Briefly pause to avoid continuous loop iteration
end % ends infinite while loop
end % ends function
function bumper=bumpcheck(r) % Creates 'bumpcheck' function
bumpdata= r.getBumpers % Assigns all data from the bumper to variable 'bumpdata'
bumper = bumpdata.right || bumpdata.left || bumpdata.front % Creates a stored variable, 'bumper', for the different bumpers
if bumpdata.right>0 % If statement to cause different functions of the roomba to happen if Bumper is bumped
r.stop % Stops Roomba
r.moveDistance(-0.3, 0.2) % Reverses Roomba 0.3m
r.turnAngle(90,0.5) % Rotates Roomba 90 degrees as fast as possible
end
if bumpdata.front>0
r.stop
r.moveDistance(-0.3, 0.2)
r.turnAngle(randi(270),0.5) % Rotates Roomba at a random interval between 0 and 270 degrees as fast as possible
end
if bumpdata.left>0
r.stop
r.moveDistance(-0.3, 0.2)
r.turnAngle(-90,0.5) % Rotates Roomba -90 degrees as fast as possible
end
end
function lightbumper=lightcheck(r) % Creates 'lightcheck' function
lightdata= r.getLightBumpers % Assigns all data from the light bump sensor to variable 'lightdata'
lightbumper = lightdata.left || lightdata.right || lightdata.rightCenter || lightdata.leftCenter % Creates a stored variable, 'lightbumper', for the different light bumpers
if lightbumper % If statement to call the lightbumper data from above
if lightdata.left>10 % If statement to cause different functions of the roomba to happen if the light bumper senses greater than 10 values
r.setDriveVelocity(0.05,0.05) % Slows down roomba to prepare for bump
end % ends initial if statement
if lightdata.rightCenter>10
r.setDriveVelocity(0.05,0.05)
end
if lightdata.right>10
r.setDriveVelocity(0.05,0.05)
end
if lightdata.leftCenter>10
r.setDriveVelocity(0.05,0.05)
end
end % ends 'lightbumper' if statement
end %ends lightcheck function
function dontFall = cliffCheck(r) % Creates 'cliffCheck' function
data = r.getCliffSensors; % Assigns all data from the cliff sensor to variable 'data'
dontFall = data.left<1020 || data.leftFront<1020 || data.rightFront<1020 || data.right<1020 % Creates a stored variable, 'dontFall', for the different cliff sensors
if dontFall % If statement to call the cliff sensor data from above
if data.left < 1010 % If statement to cause different functions of the roomba to happen if the cliff sensor senses less than 1010 values
r.stop
r.moveDistance(-0.2, 0.2) % Reverses Roomba 0.2m
r.turnAngle(-90,0.5) % Rotates Roomba -90 degrees as fast as possible
elseif data.leftFront < 1010
r.stop
r.moveDistance(-0.3, 0.2)
r.turnAngle(90,0.5) % Rotates Roomba 90 degrees as fast as possible
elseif data.rightFront < 1010
r.stop
r.moveDistance(-0.3, 0.2)
r.turnAngle(90,0.5) % Rotates Roomba 90 degrees as fast as possible
elseif data.right < 1010
r.stop
r.moveDistance(-0.3, 0.2)
r.turnAngle(90,0.5) % Rotates Roomba 90 degrees as fast as possible
end
end
end
function liquids = LiquidCheck(r) % Creates 'LiquidCheck' function
while true %start infinite loop for calibrating
img = r.getImage; % reads the camera off of the robot
image(img) % shows the image in a figure window
red_mean = mean(mean(img(200,150,1)))% reads the mean amount of red pixels
blue_mean = mean(mean(img(200,150,3)))% reads the mean amount of blue pixels
liquids = red_mean || blue_mean % Creates a stored variable, 'liquids', for the different color variables
if liquids % If statement to call the image data from above
if red_mean>170 % If statement to cause different functions of the roomba to happen if the camera sees a mean red color of greater than 170
r.stop % stops roomba
r.setLEDCenterColor(255) % sets circle to color red
r.setLEDDigits(); % clear the display
f = waitbar(0,'*INCOMING MESSAGE*'); % creates a waitbar for a loading message
r.setLEDDigits('HOT'); % sets LED display to output 'HOT'
pause(0.5) %Brief Pause to read information dispayed
r.setLEDDigits('LAVA'); % sets LED display to output 'LAVA'
pause(0.5)
waitbar(.33,f,'*INCOMING MESSAGE*'); %creates an increase in the waitbar
r.setLEDDigits('HOT');
pause(0.5)
r.setLEDDigits('LAVA');
pause(0.5)
waitbar(.67,f,'*INCOMING MESSAGE*'); % creates an increase in the waitbar
r.setLEDDigits('HOT');
pause(0.5)
r.setLEDDigits('LAVA');
waitbar(1,f,'*INCOMING MESSAGE*'); %completes the waitbar
pause(1)
close(f) %closes the waitbar
r.setLEDDigits(); % clears the LED display
close all %Closes all previous windows
axes('Color','none','XColor','none','YColor','none') % Clears the plot window of the axes and chart
y=0.5; % sets the y-position of the text in the plotting window
x=0.06; % sets the x-position of the text in the plotting window
title('FROM MARS ROOMBA','fontsize',32) % Adds a title to the plotting window
quadeqtxt = 'DANGER LAVA'; % Sets the variable 'quadeqtxt' to output 0
text(x,y,quadeqtxt,'interpreter','latex','fontsize',36); % displays the quadeq text in the plotting window
r.moveDistance(-0.2, 0.2) %reverses the roomba 0.2m
r.turnAngle(180,0.5) %turns the roomba 180 degrees as fast as possible
r.setLEDCenterColor(128,128); % sets the roomba center LED to orange
close all %closes remaining open windows
elseif blue_mean>175 % If statement to cause different functions of the roomba to happen if the camera sees a mean blue color of greater than 175
r.stop % stops roomba
r.setLEDCenterColor(255) % sets circle to color red
r.setLEDDigits(); % clear the display
f = waitbar(0,'*INCOMING MESSAGE*'); % creates a waitbar for a loading message
r.setLEDDigits('LOOK'); % sets LED display to output 'LOOK'
pause(0.5) %Brief Pause to read information dispayed
r.setLEDDigits('WATR'); % sets LED display to output 'WATR'
pause(0.5)
waitbar(.33,f,'*INCOMING MESSAGE*'); %creates an increase in the waitbar
r.setLEDDigits('LOOK');
pause(0.5)
r.setLEDDigits('WATR');
pause(0.5)
waitbar(.67,f,'*INCOMING MESSAGE*'); % creates an increase in the waitbar
r.setLEDDigits('LOOK');
pause(0.5)
r.setLEDDigits('WATR');
waitbar(1,f,'*INCOMING MESSAGE*'); %completes the waitbar
pause(1)
close(f) %closes the waitbar
r.setLEDDigits(); % clears the LED display
close all %Closes all previous windows
axes('Color','none','XColor','none','YColor','none') % Clears the plot window of the axes and chart
y=0.5; % sets the y-position of the text in the plotting window
x=0.06; % sets the x-position of the text in the plotting window
title('FROM MARS ROOMBA','fontsize',32) % Adds a title to the plotting window
quadeqtxt = 'FOUND WATER'; % Sets the variable 'quadeqtxt' to output 0
text(x,y,quadeqtxt,'interpreter','latex','fontsize',36); % displays the quadeq text in the plotting window
r.moveDistance(-0.2, 0.2) %reverses the roomba 0.2m
r.turnAngle(180,0.5) %turns the roomba 180 degrees as fast as possible
r.setLEDCenterColor(128,128); % sets the roomba center LED to orange
close all %closes remaining open windows
end %ends 'red_mean' if statement
end %ends 'liquids' if statement
end % closes infinite while loop
end % ends function 'LiquidCheck'
Running the Code
After you have copied and pasted the code into MATLAB you must connect to the Roomba. Once the Roomba is connected you must name the variable r. The functions use the variable r when referring to the Roomba, so the Roomba must be defined as the variable r. After running the code the Roomba should run as instructed.