ASEE-2014 / LineFollower

Dependents:   ASEE-2014

Fork of LineFollower by Christopher Bradford

Committer:
blu12758
Date:
Mon Mar 10 00:03:42 2014 +0000
Revision:
8:12751953ad8e
Parent:
7:de9902f316aa
Child:
9:039a74519b89
fixed bitshifting;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
blu12758 0:2623af9e8ef3 1 #include "LineFollower.h"
blu12758 4:2abe380c57f1 2 #include "Motor.h"
blu12758 0:2623af9e8ef3 3 #include "mbed.h"
blu12758 0:2623af9e8ef3 4 #include <stdint.h>
blu12758 0:2623af9e8ef3 5
blu12758 0:2623af9e8ef3 6 /** Create a Line Follower interface for an IR Sensor Array
blu12758 0:2623af9e8ef3 7 *
blu12758 0:2623af9e8ef3 8 * @param ir1 IR Sensor 1
blu12758 0:2623af9e8ef3 9 * @param ir2 IR Sensor 2
blu12758 0:2623af9e8ef3 10 * @param ir3 IR Sensor 3
blu12758 0:2623af9e8ef3 11 * @param ir4 IR Sensor 4
blu12758 0:2623af9e8ef3 12 * @param ir5 IR Sensor 5
blu12758 0:2623af9e8ef3 13 * @param ir6 IR Sensor 6
blu12758 0:2623af9e8ef3 14 * @param ir7 IR Sensor 7
blu12758 0:2623af9e8ef3 15 * @param ir8 IR Sensor 8
blu12758 0:2623af9e8ef3 16 */
blu12758 4:2abe380c57f1 17 LineFollower::LineFollower(PinName ir1, PinName ir2, PinName ir3, PinName ir4,
blu12758 4:2abe380c57f1 18 PinName ir5, PinName ir6, PinName ir7, PinName ir8):
blu12758 4:2abe380c57f1 19 array(ir1, ir2, ir3, ir4, ir5, ir6, ir7, ir8)
blu12758 4:2abe380c57f1 20 {}
blu12758 0:2623af9e8ef3 21
blu12758 0:2623af9e8ef3 22
blu12758 0:2623af9e8ef3 23 /** Read the value of a LineFollower object
blu12758 0:2623af9e8ef3 24 *
blu12758 0:2623af9e8ef3 25 * @return The value of the Sensor
blu12758 0:2623af9e8ef3 26 */
blu12758 0:2623af9e8ef3 27 uint8_t LineFollower::read(){
blu12758 4:2abe380c57f1 28 return array;
blu12758 1:c319e24af8df 29 }
blu12758 1:c319e24af8df 30
blu12758 1:c319e24af8df 31 /** Follow a line
blu12758 1:c319e24af8df 32 *
blu12758 1:c319e24af8df 33 * @param l left drive motor
blu12758 1:c319e24af8df 34 * @param r right drive motor
blu12758 1:c319e24af8df 35 */
cbradford 6:3482f3f93988 36 int LineFollower::followLine(Motor l, Motor r){
blu12758 1:c319e24af8df 37 int count = 0;
blu12758 1:c319e24af8df 38 for(int i = 0; i<8; i++){
blu12758 8:12751953ad8e 39 count += (array&(1<<i))?0:1;
blu12758 1:c319e24af8df 40 }
blu12758 1:c319e24af8df 41
blu12758 1:c319e24af8df 42 switch(count){
blu12758 1:c319e24af8df 43
blu12758 8:12751953ad8e 44 case 1: if(this->read() == 0x7F){
blu12758 2:7a4179249fa4 45 l.speed(-(0.75 * MAXSPEED));
blu12758 2:7a4179249fa4 46 r.speed(MAXSPEED);
blu12758 1:c319e24af8df 47 }
cbradford 5:1c4c12596ad2 48 else if(this->read() == 0xFE){
blu12758 2:7a4179249fa4 49 l.speed(MAXSPEED);
blu12758 2:7a4179249fa4 50 r.speed(-(0.75 * MAXSPEED));
blu12758 1:c319e24af8df 51 }
blu12758 1:c319e24af8df 52 break;
blu12758 1:c319e24af8df 53
blu12758 8:12751953ad8e 54 case 2: if(this->read() == 0xE7){
blu12758 2:7a4179249fa4 55 l.speed(MAXSPEED);
blu12758 2:7a4179249fa4 56 r.speed(MAXSPEED);
blu12758 1:c319e24af8df 57 }
cbradford 5:1c4c12596ad2 58 else if(this->read() == 0x3F){
blu12758 2:7a4179249fa4 59 l.speed(-(0.5 * MAXSPEED));
blu12758 2:7a4179249fa4 60 r.speed(MAXSPEED);
blu12758 1:c319e24af8df 61 }
cbradford 5:1c4c12596ad2 62 else if(this->read() == 0xFC){
blu12758 2:7a4179249fa4 63 l.speed(MAXSPEED);
blu12758 2:7a4179249fa4 64 r.speed(-(0.5 * MAXSPEED));
blu12758 1:c319e24af8df 65 }
cbradford 5:1c4c12596ad2 66 else if(this->read() == 0x9F){
blu12758 2:7a4179249fa4 67 l.speed(0);
blu12758 2:7a4179249fa4 68 r.speed(MAXSPEED);
blu12758 1:c319e24af8df 69 }
cbradford 5:1c4c12596ad2 70 else if(this->read() == 0xF9){
blu12758 2:7a4179249fa4 71 l.speed(MAXSPEED);
blu12758 2:7a4179249fa4 72 r.speed(0);
blu12758 1:c319e24af8df 73 }
cbradford 5:1c4c12596ad2 74 else if(this->read() == 0xCF){
blu12758 2:7a4179249fa4 75 l.speed(0.5*MAXSPEED);
blu12758 2:7a4179249fa4 76 r.speed(MAXSPEED);
blu12758 1:c319e24af8df 77 }
cbradford 5:1c4c12596ad2 78 else if(this->read() == 0xF3){
blu12758 2:7a4179249fa4 79 l.speed(MAXSPEED);
blu12758 2:7a4179249fa4 80 r.speed(0.5*MAXSPEED);
blu12758 1:c319e24af8df 81 }
blu12758 1:c319e24af8df 82 break;
blu12758 1:c319e24af8df 83
blu12758 8:12751953ad8e 84 case 3: if(this->read() == 0x1F){
blu12758 2:7a4179249fa4 85 l.speed(-(0.25*MAXSPEED));
blu12758 2:7a4179249fa4 86 r.speed(MAXSPEED);
blu12758 1:c319e24af8df 87 }
cbradford 5:1c4c12596ad2 88 else if(this->read() == 0xF8){
blu12758 2:7a4179249fa4 89 l.speed(MAXSPEED);
blu12758 2:7a4179249fa4 90 r.speed(-(0.25*MAXSPEED));
blu12758 1:c319e24af8df 91 }
cbradford 5:1c4c12596ad2 92 else if(this->read() == 0x8F){
blu12758 2:7a4179249fa4 93 l.speed(0.25*MAXSPEED);
blu12758 2:7a4179249fa4 94 r.speed(MAXSPEED);
blu12758 1:c319e24af8df 95 }
cbradford 5:1c4c12596ad2 96 else if(this->read() == 0xF1){
blu12758 2:7a4179249fa4 97 l.speed(MAXSPEED);
blu12758 2:7a4179249fa4 98 r.speed(0.25*MAXSPEED);
blu12758 1:c319e24af8df 99 }
cbradford 5:1c4c12596ad2 100 else if(this->read() == 0xC7){
blu12758 2:7a4179249fa4 101 l.speed(0.5*MAXSPEED);
blu12758 2:7a4179249fa4 102 r.speed(MAXSPEED);
blu12758 1:c319e24af8df 103 }
cbradford 5:1c4c12596ad2 104 else if(this->read() == 0xE3){
blu12758 2:7a4179249fa4 105 l.speed(MAXSPEED);
blu12758 2:7a4179249fa4 106 r.speed(0.5*MAXSPEED);
blu12758 1:c319e24af8df 107 }
blu12758 4:2abe380c57f1 108 break;
blu12758 1:c319e24af8df 109 default: break;
cbradford 6:3482f3f93988 110
blu12758 1:c319e24af8df 111 }
cbradford 6:3482f3f93988 112 return count;
blu12758 0:2623af9e8ef3 113 }