ASEE-2014 / LineFollower

Dependents:   ASEE-2014

Fork of LineFollower by Christopher Bradford

LineFollower.cpp

Committer:
blu12758
Date:
2014-02-28
Revision:
4:2abe380c57f1
Parent:
3:bc54728ca6c8
Child:
5:1c4c12596ad2

File content as of revision 4:2abe380c57f1:

#include "LineFollower.h"
#include "Motor.h"
#include "mbed.h"
#include <stdint.h>

    /** Create a Line Follower interface for an IR Sensor Array
    *
    * @param ir1  IR Sensor 1
    * @param ir2  IR Sensor 2
    * @param ir3  IR Sensor 3
    * @param ir4  IR Sensor 4
    * @param ir5  IR Sensor 5
    * @param ir6  IR Sensor 6
    * @param ir7  IR Sensor 7
    * @param ir8  IR Sensor 8
    */   
    LineFollower::LineFollower(PinName ir1, PinName ir2, PinName ir3, PinName ir4,
                PinName ir5, PinName ir6, PinName ir7, PinName ir8):
                array(ir1, ir2, ir3, ir4, ir5, ir6, ir7, ir8)
                {}
                
                
    /** Read the value of a LineFollower object
    * 
    * @return The value of the Sensor
    */                    
    uint8_t LineFollower::read(){
        return array;
    }
    
    /** Follow a line
    * 
    * @param    l left drive motor
    * @param    r  right drive motor
    */                    
    void LineFollower::followLine(Motor l, Motor r){
           int count = 0;
           for(int i = 0; i<8; i++){
                count += (array&(1<<0))?1:0;  
            }
            
            switch(count){
                
                case 1: if(this->read() == 0x80){
                            l.speed(-(0.75 * MAXSPEED));
                            r.speed(MAXSPEED);
                        }
                        else if(this->read() == 0x01){
                            l.speed(MAXSPEED);
                            r.speed(-(0.75 * MAXSPEED));
                        }
                        break;
                        
                case 2: if(this->read() == 0x18){
                            l.speed(MAXSPEED);
                            r.speed(MAXSPEED);
                        }
                        else if(this->read() == 0xC0){
                            l.speed(-(0.5 * MAXSPEED));
                            r.speed(MAXSPEED);
                        }
                        else if(this->read() == 0x03){
                            l.speed(MAXSPEED);
                            r.speed(-(0.5 * MAXSPEED));
                        }
                        else if(this->read() == 0x60){
                            l.speed(0);
                            r.speed(MAXSPEED);
                        }
                        else if(this->read() == 0x06){
                            l.speed(MAXSPEED);
                            r.speed(0);
                        }
                        else if(this->read() == 0x30){
                            l.speed(0.5*MAXSPEED);
                            r.speed(MAXSPEED);
                        }
                        else if(this->read() == 0x0C){
                            l.speed(MAXSPEED);
                            r.speed(0.5*MAXSPEED);
                        }
                        break;
                        
                case 3: if(this->read() == 0xE0){
                            l.speed(-(0.25*MAXSPEED));
                            r.speed(MAXSPEED);
                        }
                        else if(this->read() == 0x07){
                            l.speed(MAXSPEED);
                            r.speed(-(0.25*MAXSPEED));
                        }
                        else if(this->read() == 0x70){
                            l.speed(0.25*MAXSPEED);
                            r.speed(MAXSPEED);
                        }
                        else if(this->read() == 0x0E){
                            l.speed(MAXSPEED);
                            r.speed(0.25*MAXSPEED);
                        }
                        else if(this->read() == 0x38){
                            l.speed(0.5*MAXSPEED);
                            r.speed(MAXSPEED);
                        }
                        else if(this->read() == 0x1C){
                            l.speed(MAXSPEED);
                            r.speed(0.5*MAXSPEED);
                        }
                        break;
              default:  break;
            }
        
    }