Difference between revisions of "R2D2 Tutorial"

From GcatWiki
Jump to: navigation, search
(Introduction)
(Control the iRobot)
Line 54: Line 54:
 
#Configure matlab script
 
#Configure matlab script
 
##Set the read image path for matlab to read in the file that python constantly writes to. This way, whenever the brain in matlab needs an image to process, it can simply read the file written by the python webcam controller.
 
##Set the read image path for matlab to read in the file that python constantly writes to. This way, whenever the brain in matlab needs an image to process, it can simply read the file written by the python webcam controller.
##An overview of the MatLab algorithms can be found [http://gcat.davidson.edu/mediawiki-1.15.0/images/5/50/The_Algorithm.docx here].
+
##A document detailing the MatLab algorithms is available: [http://gcat.davidson.edu/mediawiki-1.15.0/images/5/50/The_Algorithm.docx MatLab iRobot Algorithm].
 
#Run the robot
 
#Run the robot
 
##Begin webcam.py script in terminal <pre>python /path/to/webcam.py</pre>
 
##Begin webcam.py script in terminal <pre>python /path/to/webcam.py</pre>

Revision as of 15:45, 10 May 2012

Introduction

Part of Davidson College’s Climate Action Plan is the reduction of energy used in the heating and cooling of campus buildings. Our class has identified an innovative way of further increasing the energy efficiency of the Chambers academic building. In the warmer months of the year, students and professors frequently open classroom windows to let in fresh air. The problem is that they then neglect to close the windows, leaving them open all through the afternoon and night. Since the temperature and air filtration systems of the rooms and building in general are self-regulating, much energy is wasted when the systems tries to regulate a room with open windows. A large amount of energy could be saved if it was ensured that these windows were closed after the last class leaves the room each day.

Proposal

To this end, the CSC 382 Artificial Intelligence class, taught by Dr. Laurie Heyer, proposes a solution which will help reduce energy waste through these open windows and provide us with an opportunity for first-hand experience in robotics, an increasingly important subfield of Artificial Intelligence.

Our solution centers on configuring, programming and testing small robots to navigate Chambers at designated times, determine which classrooms have open windows, and report the room numbers (e.g., by email) to an agent capable of closing them. In this class, we have learned a variety of data and image processing methods that will enable us to implement this solution, but the college does not have the appropriate hardware.

We propose purchasing two iRobot Create robots, essentially the same hardware as the Roomba robotic vacuum cleaner. These commercially available robots are designed for educational projects such as ours, and include developer tools that enable us to program the robot directly, as well as wirelessly communicate with the robot from a standard laptop or desktop computer.

The full outline for this project is available: Davidson College iRobot Proposal.

Requirements

  • MacOS X computer
    • Needs bluetooth
    • Needs MatLab and Python 2.7.3
  • iRobot Create
  • BAM iRobot bluetooth adaptor
  • Wifi webcam that you can power
    • We used Foscam FI8918W because it ran off 5 volt power, allowing us to wire it to the iRobot for power.

Guide

Install software/packages/scripts on Mac

  • MatLab Setup
  1. Install Matlab Toolbox for the iRobot Create
  2. Download our iRobot Matlab Code
  • Python Setup
  1. Download and Install MacPorts
  2. Open terminal and type the following commands.
  3. Install open cv for python 2.7
    sudo port -v install opencv +python27
  4. Install pil for python 2.7
    sudo port install py27-pil
  5. Optional: Install selenium for python 2.7 (use this option if you want to control the webcam via a browser)
    sudo port install py27-selenium
  6. Set this MacPorts installed python as the default python interpreter:
    sudo port select --set python python27
  7. Download webcam.py to control the wifi webcam
  • Other Terminal Setup
  1. Install pkill command to terminal via MacPorts
    sudo port install proctools

Set up iRobot

  1. Determine how you want to power the webcam while it is attached to the robot (Is it going to feed off the robot power or have its own dedicated power supply?)
  2. Determine how you want to mount the camera to the iRobot. A document detailing our mount scheme is available: iRobot Mount Scheme/Dimensions.
  3. Connect the the BAM bluetooth adaptor to the iRobot by following the manual to connect it to the computer.
  4. Now connected to the iRobot, ensure the camera is turned on
  5. Connect to the iRobot using Matlab. A document detailing this is available: Matlab Serial Port and Bluetooth Setup Help (it is copied below for convenience).
    1. First you need to set up the Bluetooth, this is fairly easy, you can do it the same way you pair any normal Bluetooth device. The BAM will be named ElementSerial.
    2. For Mac OS X you need to find the address of the bluetooth serial port with the terminal, it will look something like “/dev/tty.ElementSerial-ElementSe”. This string will be your port, you need to hardcode this into the RoombaInit function so that it always connects to the appropriate port.
    3. It is useful to have an initialize function that will call RoombaInit and save the serial port in a conveniently named variable as you will need to the pass the serial port to pretty much every function. It is also nice to have it beep or show some sign of life so you know it went smoothly.
    4. Inside RoombaInit you will want to make sure the robot is starting in safe mode rather than full mode, so that it does not go off cliffs and will stop when lifted and such things (assuming that is what you want), you do this by assigning the correct value to the Contrl variable. (see IRobot Open Interface manual for more info/ specific opcodes) You also want to assign a value to the global variable td which is a time delay, 15ms is recommended. This creates a small delay between commands you send to the robot so it has time to process them (This is a MUST, your robot will almost always crash during the program unless you keep some delay).
    5. You will want a power cycling function that resets the robot remotely, this is very useful and can be done by sending the opcode ‘7’ to the robot. (This info is NOT in the manual and is very useful, because you will need to power cycle and initialize pretty much between every run of the robot).

Control the iRobot

  1. Configure webcam.py
    1. webcam.py has a method (write_img_stream) that will constantly write and update the same image jpeg file. Set the webcam object to write that file to a directory accessible by matlab.
    2. The webcam.py script should only set up the webcam object and call write_img_stream(). For more documentation see the python code.
  2. Configure matlab script
    1. Set the read image path for matlab to read in the file that python constantly writes to. This way, whenever the brain in matlab needs an image to process, it can simply read the file written by the python webcam controller.
    2. A document detailing the MatLab algorithms is available: MatLab iRobot Algorithm.
  3. Run the robot
    1. Begin webcam.py script in terminal
      python /path/to/webcam.py
    2. Run the matlab code
      1. The matlab code kills the python script (because it is in an infinite loop by this line
        unix('pkill -9 -f webcam.py')
      2. If for some reason this fails to kill the python script, you can manually kill it by typing in another terminal window
        pkill -9 -f webcam.py