Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of LineFollower by
Diff: LineFollower.cpp
- Revision:
- 4:2abe380c57f1
- Parent:
- 3:bc54728ca6c8
- Child:
- 5:1c4c12596ad2
--- a/LineFollower.cpp Thu Feb 27 01:50:13 2014 +0000
+++ b/LineFollower.cpp Fri Feb 28 04:21:26 2014 +0000
@@ -1,4 +1,5 @@
#include "LineFollower.h"
+#include "Motor.h"
#include "mbed.h"
#include <stdint.h>
@@ -13,20 +14,10 @@
* @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;
- }
+ 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
@@ -34,13 +25,7 @@
* @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;
+ return array;
}
/** Follow a line
@@ -48,79 +33,79 @@
* @param l left drive motor
* @param r right drive motor
*/
- void followLine(Motor l, Motor r){
+ void LineFollower::followLine(Motor l, Motor r){
int count = 0;
for(int i = 0; i<8; i++){
- count += array[i];
+ count += (array&(1<<0))?1:0;
}
switch(count){
- case 1: if(this->read() == 0b10000000){
+ case 1: if(this->read() == 0x80){
l.speed(-(0.75 * MAXSPEED));
r.speed(MAXSPEED);
}
- else if(this->read() == 0b00000001){
+ else if(this->read() == 0x01){
l.speed(MAXSPEED);
r.speed(-(0.75 * MAXSPEED));
}
break;
- case 2: if(this->read() == 0b00011000){
+ case 2: if(this->read() == 0x18){
l.speed(MAXSPEED);
r.speed(MAXSPEED);
}
- else if(this->read() == 0b11000000){
+ else if(this->read() == 0xC0){
l.speed(-(0.5 * MAXSPEED));
r.speed(MAXSPEED);
}
- else if(this->read() == 0b00000011){
+ else if(this->read() == 0x03){
l.speed(MAXSPEED);
r.speed(-(0.5 * MAXSPEED));
}
- else if(this->read() == 0b01100000){
+ else if(this->read() == 0x60){
l.speed(0);
r.speed(MAXSPEED);
}
- else if(this->read() == 0b00000110){
+ else if(this->read() == 0x06){
l.speed(MAXSPEED);
r.speed(0);
}
- else if(this->read() == 0b00110000){
+ else if(this->read() == 0x30){
l.speed(0.5*MAXSPEED);
r.speed(MAXSPEED);
}
- else if(this->read() == 0b00001100){
+ else if(this->read() == 0x0C){
l.speed(MAXSPEED);
r.speed(0.5*MAXSPEED);
}
break;
- case 3: if(this->read() == 0b11100000){
+ case 3: if(this->read() == 0xE0){
l.speed(-(0.25*MAXSPEED));
r.speed(MAXSPEED);
}
- else if(this->read() == 0b00000111){
+ else if(this->read() == 0x07){
l.speed(MAXSPEED);
r.speed(-(0.25*MAXSPEED));
}
- else if(this->read() == 0b01110000){
+ else if(this->read() == 0x70){
l.speed(0.25*MAXSPEED);
r.speed(MAXSPEED);
}
- else if(this->read() == 0b00001110){
+ else if(this->read() == 0x0E){
l.speed(MAXSPEED);
r.speed(0.25*MAXSPEED);
}
- else if(this->read() == 0b00111000){
+ else if(this->read() == 0x38){
l.speed(0.5*MAXSPEED);
r.speed(MAXSPEED);
}
- else if(this->read() == 0b00011100){
+ else if(this->read() == 0x1C){
l.speed(MAXSPEED);
r.speed(0.5*MAXSPEED);
}
- break
+ break;
default: break;
}
