Mario Bambagini / Mbed 2 deprecated gp2d12_example

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * here is an example for using the GP2d12 sensor.
00003  * Basically I interpolated the output curve (output voltage wrt distance) whose
00004  * Matlab code is reported below.
00005  * I'm too lazy for creating a class, so the code has been provided within the 
00006  * main function
00007  */
00008 #include "mbed.h"
00009 
00010 DigitalOut myled(LED1);
00011 
00012 AnalogIn s (p20);
00013 
00014 #define k_5 12466.0
00015 #define k_4 -23216.0
00016 #define k_3 14974.0
00017 #define k_2 -3585.0
00018 #define k_1 19.0
00019 #define k_0 96.0
00020 
00021 int main() {
00022     while(1) {
00023         float val = s.read();
00024         float res = 0.0;
00025         res += k_5*(val*val*val*val*val);
00026         res += k_4*(val*val*val*val);
00027         res += k_3*(val*val*val);
00028         res += k_2*(val*val);
00029         res += k_1*val;
00030         res += k_0;
00031         printf("%f, dst=%f\n\r", val, res);
00032         myled = 1;
00033         wait(0.2);
00034         myled = 0;
00035         wait(0.2);
00036     }
00037 }
00038 
00039 /*
00040 Matlab commands:
00041 Y = [10    15    20    25    30    35    40    50    60    70    80   90    100];
00042 X = [0.70  0.57  0.39  0.32  0.28  0.24  0.20  0.17  0.13  0.12  0.1  0.01  0.0001];
00043 p = polyfit(X, Y, 5);
00044 x_test = [0:0.05:0.7];
00045 for i=1:length(x_test)
00046     y_test(i) = polyval(p, x_test(i));
00047 end
00048 polyval(p,0.5)
00049 %x = 0.5;
00050 %res = p(1)*(x^4) + p(2)*(x^3) + p(3)*(x^2) + p(4)*(x^1) + p(5)*(x^0);
00051 figure
00052 plot(X, Y, 'b')
00053 hold on
00054 plot(x_test, y_test, 'r')
00055 */