GP20A41SK IR sensor
Revision 0:a17fcd8e5d32, committed 2017-07-03
- Comitter:
- mrvalon
- Date:
- Mon Jul 03 23:07:51 2017 +0000
- Commit message:
- GP20A41SK IR Distance sensor
Changed in this revision
IR_Dist.cpp | Show annotated file Show diff for this revision Revisions of this file |
IR_Dist.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r a17fcd8e5d32 IR_Dist.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/IR_Dist.cpp Mon Jul 03 23:07:51 2017 +0000 @@ -0,0 +1,22 @@ +#include "IR_Dist.h" +#include "mbed.h" +/* +*IR_Dist.cpp +*/ +IR_Dist::IR_Dist(PinName a) : IRSens(a){} + + float IR_Dist::Equation() { + volts = IRSens.read_u16()* 0.02441406; // 5v / 2048 = 0.02441406 + + return 5034.2 * pow(volts,-0.959); // x = 5034.2x-0.959 +} + +//return distance in cm +float IR_Dist::distance(){ + correction = 1; + som = Equation(); + distance_cm = (som - correction); + + return distance_cm; + +} \ No newline at end of file
diff -r 000000000000 -r a17fcd8e5d32 IR_Dist.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/IR_Dist.h Mon Jul 03 23:07:51 2017 +0000 @@ -0,0 +1,46 @@ +/* File: IR_Dist.h + * Author: Geert Huisman + * Board: STM NUCLEO F401RE, + * Hardware: IR Range GP20A41SK, + * + * Desc: driver for GP20A41SK IR Distance sensor. The returned range + * is in units of cm meters. + * + * + * +*/ + +/* EXAMPLE +#include "mbed.h" +#include "IR_Dist.h" + +//A0 Analoogin + IR_Dist IRSense(A0); +int main() { + while(1) { + + float distance = IRSense.distance(); + printf("distance %2.0f \n",distance); + wait(1.0); + + } +} +*/ + +#ifndef IR_Dist_H +#define IR_Dist_H +#include "mbed.h" + +class IR_Dist { + public: + IR_Dist(PinName a); + float Equation(); + float distance(); + + private: + AnalogIn IRSens; + int16_t volts; + float som, correction, distance_cm; +}; + +#endif \ No newline at end of file