ASEE-2014 / LineFollower

Dependents:   ASEE-2014

Fork of LineFollower by Christopher Bradford

Committer:
blu12758
Date:
Fri Feb 28 04:21:26 2014 +0000
Revision:
4:2abe380c57f1
Parent:
3:bc54728ca6c8
Child:
5:1c4c12596ad2
Fixed the followLine() method as well as some compile-time errors

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 */
blu12758 4:2abe380c57f1 36 void 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 4:2abe380c57f1 39 count += (array&(1<<0))?1:0;
blu12758 1:c319e24af8df 40 }
blu12758 1:c319e24af8df 41
blu12758 1:c319e24af8df 42 switch(count){
blu12758 1:c319e24af8df 43
blu12758 4:2abe380c57f1 44 case 1: if(this->read() == 0x80){
blu12758 2:7a4179249fa4 45 l.speed(-(0.75 * MAXSPEED));
blu12758 2:7a4179249fa4 46 r.speed(MAXSPEED);
blu12758 1:c319e24af8df 47 }
blu12758 4:2abe380c57f1 48 else if(this->read() == 0x01){
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 4:2abe380c57f1 54 case 2: if(this->read() == 0x18){
blu12758 2:7a4179249fa4 55 l.speed(MAXSPEED);
blu12758 2:7a4179249fa4 56 r.speed(MAXSPEED);
blu12758 1:c319e24af8df 57 }
blu12758 4:2abe380c57f1 58 else if(this->read() == 0xC0){
blu12758 2:7a4179249fa4 59 l.speed(-(0.5 * MAXSPEED));
blu12758 2:7a4179249fa4 60 r.speed(MAXSPEED);
blu12758 1:c319e24af8df 61 }
blu12758 4:2abe380c57f1 62 else if(this->read() == 0x03){
blu12758 2:7a4179249fa4 63 l.speed(MAXSPEED);
blu12758 2:7a4179249fa4 64 r.speed(-(0.5 * MAXSPEED));
blu12758 1:c319e24af8df 65 }
blu12758 4:2abe380c57f1 66 else if(this->read() == 0x60){
blu12758 2:7a4179249fa4 67 l.speed(0);
blu12758 2:7a4179249fa4 68 r.speed(MAXSPEED);
blu12758 1:c319e24af8df 69 }
blu12758 4:2abe380c57f1 70 else if(this->read() == 0x06){
blu12758 2:7a4179249fa4 71 l.speed(MAXSPEED);
blu12758 2:7a4179249fa4 72 r.speed(0);
blu12758 1:c319e24af8df 73 }
blu12758 4:2abe380c57f1 74 else if(this->read() == 0x30){
blu12758 2:7a4179249fa4 75 l.speed(0.5*MAXSPEED);
blu12758 2:7a4179249fa4 76 r.speed(MAXSPEED);
blu12758 1:c319e24af8df 77 }
blu12758 4:2abe380c57f1 78 else if(this->read() == 0x0C){
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 4:2abe380c57f1 84 case 3: if(this->read() == 0xE0){
blu12758 2:7a4179249fa4 85 l.speed(-(0.25*MAXSPEED));
blu12758 2:7a4179249fa4 86 r.speed(MAXSPEED);
blu12758 1:c319e24af8df 87 }
blu12758 4:2abe380c57f1 88 else if(this->read() == 0x07){
blu12758 2:7a4179249fa4 89 l.speed(MAXSPEED);
blu12758 2:7a4179249fa4 90 r.speed(-(0.25*MAXSPEED));
blu12758 1:c319e24af8df 91 }
blu12758 4:2abe380c57f1 92 else if(this->read() == 0x70){
blu12758 2:7a4179249fa4 93 l.speed(0.25*MAXSPEED);
blu12758 2:7a4179249fa4 94 r.speed(MAXSPEED);
blu12758 1:c319e24af8df 95 }
blu12758 4:2abe380c57f1 96 else if(this->read() == 0x0E){
blu12758 2:7a4179249fa4 97 l.speed(MAXSPEED);
blu12758 2:7a4179249fa4 98 r.speed(0.25*MAXSPEED);
blu12758 1:c319e24af8df 99 }
blu12758 4:2abe380c57f1 100 else if(this->read() == 0x38){
blu12758 2:7a4179249fa4 101 l.speed(0.5*MAXSPEED);
blu12758 2:7a4179249fa4 102 r.speed(MAXSPEED);
blu12758 1:c319e24af8df 103 }
blu12758 4:2abe380c57f1 104 else if(this->read() == 0x1C){
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;
blu12758 1:c319e24af8df 110 }
blu12758 0:2623af9e8ef3 111
blu12758 0:2623af9e8ef3 112 }