test

Dependencies:   RemoteIR mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IRSensor.h Source File

IRSensor.h

00001 #ifndef IRSENSOR_H
00002 #define IRSENSOR_H
00003 
00004 #include "mbed.h"
00005 
00006 extern Serial pc;
00007 
00008 class IRSensor {
00009 public:
00010     DigitalOut _enable;
00011     AnalogIn _input;
00012     volatile float value; 
00013     float COEFF_1;
00014     float COEFF_2;
00015     float COEFF_3;
00016     float COEFF_4;
00017     float COEFF_5;
00018     float COEFF_6;
00019     float COEFF_7;
00020 
00021     float last_read[5];
00022     
00023     IRSensor(PinName enable, PinName input, float c1 = 0.217722944294793f, float c2 = -5.50965509541424f, 
00024                         float c3 = 55.0111470003071f, float c4 = -229.934241342364f, float c5 = 506.986371752309f, 
00025                         float c6 = -502.862134567129f, float c7 = 177.712915019135f) : _enable(enable), _input(input), 
00026                         COEFF_1(c1), COEFF_2(c2), COEFF_3(c3), COEFF_4(c4), COEFF_5(c5), COEFF_6(c6), COEFF_7(c7) {
00027     }
00028     
00029     //Get the value in encoder units, samples?... 
00030     float readIR(){
00031         float sum = 0;
00032 
00033         
00034         //Each duration takes 100us, 5 times = 0.5ms
00035         for (int i = 0; i < 5; i++)
00036         {
00037             //Turn on IR LED tx
00038             _enable = 1;
00039     
00040             //Wait for capacitor to fire, 10us
00041             wait_us(6);
00042             
00043             last_read[i] = _input.read();
00044             if (i){
00045                 sum += last_read[i];
00046             }
00047             //Wait 5us for turning off IR LED tx
00048             _enable = 0;
00049     
00050             //Wait 85us for turning on IR LED tx
00051             wait_us(75);
00052         }
00053         
00054         sum /= 4;
00055         value = sum;
00056         
00057         float square = sum * sum;
00058         float cube = square * sum;
00059         float _value = 0;
00060         _value += COEFF_1 / cube;
00061         _value += COEFF_2 / square;
00062         _value += COEFF_3 / sum;
00063         _value += COEFF_4;
00064         _value += COEFF_5 * sum;
00065         _value += COEFF_6 * square;
00066         _value += COEFF_7 * square * sum;
00067         
00068         return value;
00069     }
00070 
00071     //Get the number of cells away wall is
00072     float cell_dist(){
00073         return readIR() / 18;
00074     }
00075 
00076     //Shorthand for read()
00077     operator float() {
00078         return readIR();
00079     }
00080 
00081 };
00082 
00083 extern IRSensor rightIR;
00084 extern IRSensor rightDiagonalIR;
00085 extern IRSensor rightFrontIR;
00086 extern IRSensor leftFrontIR;
00087 extern IRSensor leftDiagonalIR;
00088 extern IRSensor leftIR;
00089 
00090 
00091 #endif