Introduction: Roomba With MATLAB
This project makes use of MATLAB and an iRobot Create2 programmable robot. By putting our knowledge of MATLAB to the test, we are able to program the Create2 to interpret images and detect signals. The functionality of the robot depends mainly on the MATLAB mobile app and a Raspberry Pi camera module.
Step 1: Parts and Materials
1. iRobot Create, Version 2
- This is a programmable robot that looks like a Roomba. Be aware that this product from iRobot is not a vacuum. It is intended for custom programming by the user.
2. MATLAB 2017a
- Most older versions will be compatible with the code we used below. MATLAB will identify a command that is not compatible with the version you have and will suggest a best fit command.
3. Raspberry Pi 3 Model B, Version 1.2
- Check to see which Raspberry Pi is compatible with your iRobot. See this link for further assistance: https://www.irobotweb.com/~/media/MainSite/PDFs/A... This instructable assumes you are working with a preprogrammed Raspberry Pi. Please be aware that you will need to work with a preprogrammed Pi in order for the following steps to work. Using a preprogrammed Pi will allow you to perform all your coding in MATLAB alone.
4. Camera Module V2 (for Raspberry Pi)
- You might be surprised; despite its size, the Raspberry Pi Camera Module is very good quality. It is the cheapest and most compatible option for this project.
Optional: 3D-printed stand. This is used stabilize the camera. It does not affect the functionality of the robot, but it will help assist your coding if you wish to use imaging data for color and/or object recognition.
Step 2: Configuration
1. Connecting Raspberry Pi and camera module (Hardware)
- To power the Raspberry Pi, you will need to run a male end micro USB to the female power port on the microcontroller. Optional: A voltage regulator may be used to ensure the voltage does not exceed 5V. After powering the Raspberry Pi, you can connect it to your robot by running male end USB a from the motherboard to USB port A on the microcontroller.
- After connecting the Pi to the Roomba, the camera is ready to be installed. The Camera Module will be much smaller than you expect. Note that the lens is mounted to a sensor, and a white ribbon extends from the camera. DO NOT remove or tear the ribbon! This is the cable you need to connect it to the Raspberry Pi. First, hold the end of the ribbon and find the silver connectors and the blue cable. These are on opposite sides. Next, find the slot between the ethernet and HDMI ports on your Raspberry Pi. Notice there is a small, white lock covering it. Slowly lift the lock, but do not remove it from the slot, as it will snap and be permanently damaged. Once you have lifted the lock, grab the ribbon and face the silver connectors to the HDMI port. The blue side will face the Ethernet port. Slowly slide the ribbon into the slot while it is still unlocked. You do not have to force it into the slot. After insertion, push the lock back down. If your camera is properly secured, you should be able to (gently) pull on the ribbon and feel tension. The ribbon should not be loose. After connecting your camera to the Pi, you may notice how loose it is. This is why we used a 3D-printed mount to steady it. It is your choice to determine what materials you would like to use to keep your camera still for high quality imaging.
2. Installing Proper Files and Connecting Roomba to your Computer After all of your hardware is configured, you can now move on to installing MATLAB along with the associated m-files that allow you to communicate with the robot. To do this, open MATLAB and create a new folder to keep all related files together. Use this script to install/update the required files: https://ef.engr.utk.edu/ef230-2017-08//projects/ro...
- All the files should now show up in your created folder. Right click in the Current Folder window and select ‘Add to Path’ to add that path to the list of directories where MATLAB looks for files. Ensure all of your files are in the correct path.
3. Once the files are installed, you can now begin to connect to your robot. Begin by turning your robot on and then hard resetting it directly after start up (do not forget to hard reset your robot every time before and after usage) . Second, connect both your robot and laptop to the same wifi network. After this, we will speak to the preprogrammed Raspberry Pi via MATLAB by calling the Roomba using its given name and the function roomba. For example, I would connect to robot 28 by using the following line: R = roomba(28).
- Notice how I assigned the object to a variable R. I can now access the associated Roomba functions from the install file by treating the variable R like a structure.
- R.turnAngle(90) If everything went well, a musical tone should playing, confirming the connection.
Step 3: MATLAB Logic
The PDF document at the bottom of this step is a detailed logic flow chart for our coding process in MATLAB. We activated the cliff, light, and light bump sensors in order to allow the robot to communicate with us when it detects an object in its immediate vicinity. For instance, when the robot moves forward, its light sensors scan for objects in its path according to the vector at which it is traveling. We picked a distance threshold for the robot so that when it approaches an object, it will reverse instead of colliding with it. Our robot is also configured with Twitter, which we specified in our coding process (this will be shown below).
To enhance the experience, we made use of the MATLAB application on our mobile devices so that we can control the robot's movements just by tilting our phones. This is an optional activity, as you can certainly have the robot move by using the moveDistance command in the MATLAB code segment instead. Keep in mind that using MATLAB commands to control your robot is preferred if your goal is to be precise. If you wish to aim you robot so that the camera takes a picture at a specific location, it may be better to code the robot's movements in MATLAB. While entertaining, using the MATLAB application to control your robot is not desirable for accuracy.
The Code commands the Roomba to perform a basic set up then continue through a continuous loop. Initially the laptop will set up a link with the Roomba using the Roomba() command. It also sets up the twitter connection using the webwrite() command in MATLAB. The loop contains five main logical flows depending on the environment surrounding the Roomba. First the Roomba checks for obstacles, and adjusts backwards if it finds it is impeded. Imbedded in that loop is the second path which alerts users if the Roomba is being carried away. An important utility in the harsh Martian Warzone. After the Roomba has determined its position is safe, it looks to the mobile device to determine its next movement. If the mobile device is tilted forward it will calculate a base velocity depending on the severity of the roll measurement than adjust the individual wheel speeds to turn based on the pitch degree. The phone can also move the Roomba in reverse. A neutral mobile device state gates the final two paths. A resting Roomba will search for an Alien flag and alert the user accordingly.
Below is our code (completed in MATLAB 2017a)
%inputs: Orientation data from a wifi connected
device, camera
%information, sensor data
%outputs: motion is controlled by the wifi connected device and the motion
% is safety checked by reading sensor data. If the camera detects
% an alien flag then the roomba responds by tweeting the enemy flag
% has been spotted.
%purpose: our device lives with no purpose except to protect those who
% created it, it serves its creator and does
% exactly what its told.
%Usage: essentially the program will run on its own.
clear all, close all, clc
%Initializing Objects and Variables
r=roomba(28);
m=mobiledev;
%use response = webwrite(hostname,data)
hostname = 'https://api.thingspeak.com/apps/thingtweet/1/statuses/update';
API='SGZCTNQXCWAHRCT5';
tweet='RoboCop is Operational...Awaiting Command';
data = strcat('api_key=',API,'&status=',tweet);
reponse=webwrite(hostname,data);
%constantly running loop
while 1==1
%Structures Containing Relavent Data
o=m.orientation; %orientation of Mobile Device
light=r.getLightBumpers(); %Light Bumper Values
a = r.getCliffSensors(); %CLiff Sensor Values
bump = r.getBumpers(); %Bumper Sensors
%check bumpers
if bump.right==1 || bump.left==1 ||bump.front==1
r.moveDistance(-.2,.2);
%check light Sensors
elseif light.left>60 || light.leftFront>60 || light.leftCenter>60 || light.right>60 || light.rightFront>60 || light.rightCenter>60
r.moveDistance(-.2,.2);
%check Cliff Sensors
%Anti-Theft Signal and notification
elseif a.left<300 && a.right<300 && a.leftFront<300 && a.rightFront<300
r.stop();
r.beep();
tweet='RoboCop has been lifted!'
data = strcat('api_key=',API,'&status=',tweet);
reponse=webwrite(hostname,data);
%Normal Cliff Avoidance Operation
elseif a.left<300 || a.right<300 || a.leftFront<300 || a.rightFront<300
r.moveDistance(-.2,.2);
%Roomba passed the checks and will now run with normal operation.
%Initially the roll of of the deviceis measured and becomes a base
%velocity wich is then used to calculate wheel speed
%Forward Motion
elseif o(3)>=0 && o(3)<=60
baseVel=(-.5/60)*(o(3)-60);
if o(2)>=-70 && o(2)<0
r.setDriveVelocity(baseVel+(.3/50)*abs(o(2)),baseVel-(.3/50)*abs(o(2)));
elseif o(2)<=70 && o(2)>=0
r.setDriveVelocity(baseVel-(.3/50)*abs(o(2)),baseVel+(.3/50)*abs(o(2)));
else r.stop
end
%Backwards Motion
elseif o(3)>100 && o(3)<150
r.setDriveVelocity(-.2,-.2)
r.beep();
r.beep();
%resting roomba will search for the Alien Flag marked as a flourescent
%green Piece of paper
else
r.stop
img=r.getImage(); %take image
threshold=graythresh(img(200:383,:,2))+.1; %calc green level
if threshold>.42
tweet='Enemy Spotted!!'
data = strcat('api_key=',API,'&status=',tweet);
reponse=webwrite(hostname,data);
else
r.stop
end
end
end
Attachments
Step 4: Conclusion
Remember, you may use the script we wrote above, but you can always alter it to fit your needs. It does not have to be controlled by your phone! (However, it does make it more fun.) Pick which method you prefer to use to control your robot. Drive around with your robot and enjoy!