A LineFollower library for ASEE-2014

Dependents:   ASEE-2014

LineFollower.cpp

Committer:
blu12758
Date:
2014-02-21
Revision:
1:c319e24af8df
Parent:
0:2623af9e8ef3
Child:
2:7a4179249fa4

File content as of revision 1:c319e24af8df:

#include "LineFollower.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(DigitalIn ir1, DigitalIn ir2, DigitalIn ir3, DigitalIn ir4,
                DigitalIn ir5, DigitalIn ir6, DigitalIn ir7, DigitalIn ir8):
                _ir1(ir1), _ir2(ir2), _ir3(ir3), _ir4(ir4), _ir5(ir5), _ir6(ir6),
                _ir7(ir7), _ir8(ir8){
                    
        array[0] = _ir1;
        array[1] = _ir2;
        array[2] = _ir3;
        array[3] = _ir4;
        array[4] = _ir5;
        array[5] = _ir6;
        array[6] = _ir7;
        array[7] = _ir8;
    }
                
                
    /** Read the value of a LineFollower object
    * 
    * @return The value of the Sensor
    */                    
    uint8_t LineFollower::read(){
        uint8_t binary = 0;
        int multi = 1;
        for(int i=0; i<=8; i++){
               binary += array[i]*multi;
               multi = multi*2;
        }
        return binary;
    }
    
    /** Follow a line
    * 
    * @param    l left drive motor
    * @param    r  right drive motor
    */                    
    void followLine(Motor l, Motor r){
           int count = 0;
           for(int i = 0; i<8; i++){
                count += array[i];  
            }
            
            switch(count){
                
                case 1: if(this->read() == 0b10000000){
                            l = -(0.75 * MAXSPEED);
                            r = MAXSPEED;
                        }
                        else if(this->read() == 0b00000001){
                            l = MAXSPEED;
                            r = -(0.75 * MAXSPEED);
                        }
                        break;
                        
                case 2: if(this->read() == 0b00011000){
                            l = MAXSPEED;
                            r = MAXSPEED;
                        }
                        else if(this->read() == 0b11000000){
                            l = -(0.5 * MAXSPEED);
                            r = MAXSPEED;
                        }
                        else if(this->read() == 0b00000011){
                            l = MAXSPEED;
                            r = -(0.5 * MAXSPEED);
                        }
                        else if(this->read() == 0b01100000){
                            l = 0;
                            r = MAXSPEED;
                        }
                        else if(this->read() == 0b00000110){
                            l = MAXSPEED;
                            r = 0;
                        }
                        else if(this->read() == 0b00110000){
                            l = 0.5*MAXSPEED;
                            r = MAXSPEED;
                        }
                        else if(this->read() == 0b00001100){
                            l = MAXSPEED;
                            r = 0.5*MAXSPEED;
                        }
                        break;
                        
                case 3: if(this->read() == 0b11100000){
                            l = -(0.25*MAXSPEED);
                            r = MAXSPEED;
                        }
                        else if(this->read() == 0b00000111){
                            l = MAXSPEED;
                            r = -(0.25*MAXSPEED);
                        }
                        else if(this->read() == 0b01110000){
                            l = 0.25*MAXSPEED;
                            r = MAXSPEED;
                        }
                        else if(this->read() == 0b00001110){
                            l = MAXSPEED;
                            r = .25*MAXSPEED;
                        }
                        else if(this->read() == 0b00111000){
                            l = 0.5*MAXSPEED;
                            r = MAXSPEED;
                        }
                        else if(this->read() == 0b00011100){
                            l = MAXSPEED;
                            r = 0.5*MAXSPEED;
                        }
                        break
              default:  break;
            }
        
    }