libary to return a range for sharp IR rangefinders. values are taken from the datasheet. note this is not accurate, but is good enough for seeing if things work

Dependents:   GP2D12rangefinder Initialmbedrobotprogram mbedrobot

Committer:
littlexc
Date:
Thu Nov 25 15:06:32 2010 +0000
Revision:
1:e3ea40a41d27
Parent:
0:28b9e26e75ac

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
littlexc 0:28b9e26e75ac 1 /*Libary for GP2D120
littlexc 0:28b9e26e75ac 2 *
littlexc 0:28b9e26e75ac 3 *
littlexc 0:28b9e26e75ac 4 * by Christopher Hasler.
littlexc 0:28b9e26e75ac 5 *
littlexc 0:28b9e26e75ac 6 *
littlexc 0:28b9e26e75ac 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
littlexc 0:28b9e26e75ac 8 * of this software and associated documentation files (the "Software"), to deal
littlexc 0:28b9e26e75ac 9 * in the Software without restriction, including without limitation the rights
littlexc 0:28b9e26e75ac 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
littlexc 0:28b9e26e75ac 11 * copies of the Software, and to permit persons to whom the Software is
littlexc 0:28b9e26e75ac 12 * furnished to do so, subject to the following conditions:
littlexc 0:28b9e26e75ac 13 *
littlexc 0:28b9e26e75ac 14 * The above copyright notice and this permission notice shall be included in
littlexc 0:28b9e26e75ac 15 * all copies or substantial portions of the Software.
littlexc 0:28b9e26e75ac 16 *
littlexc 0:28b9e26e75ac 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
littlexc 0:28b9e26e75ac 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
littlexc 0:28b9e26e75ac 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
littlexc 0:28b9e26e75ac 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
littlexc 0:28b9e26e75ac 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
littlexc 0:28b9e26e75ac 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
littlexc 0:28b9e26e75ac 23 * THE SOFTWARE.
littlexc 0:28b9e26e75ac 24 */
littlexc 0:28b9e26e75ac 25
littlexc 0:28b9e26e75ac 26
littlexc 0:28b9e26e75ac 27 #include "mbed.h"
littlexc 0:28b9e26e75ac 28 #include "GP2xx.h"
littlexc 0:28b9e26e75ac 29 /*
littlexc 0:28b9e26e75ac 30 * @param ain analog in from sensor
littlexc 0:28b9e26e75ac 31 */
littlexc 1:e3ea40a41d27 32
littlexc 0:28b9e26e75ac 33 IRRangeFinder::IRRangeFinder (PinName pin, int type): _pin(pin) {
littlexc 0:28b9e26e75ac 34 sensor=type;
littlexc 0:28b9e26e75ac 35 };
littlexc 0:28b9e26e75ac 36 // returns value for range, greater than 4, 5, 6, 7, 8, 10, 12, 14, 20, 25, 30 cm
littlexc 0:28b9e26e75ac 37 int IRRangeFinder::read(void) {
littlexc 0:28b9e26e75ac 38 float temp = _pin.read();
littlexc 0:28b9e26e75ac 39 if (sensor==1) { //GP2D120
littlexc 0:28b9e26e75ac 40 if ( temp<0.1212) {
littlexc 0:28b9e26e75ac 41 return 30;
littlexc 0:28b9e26e75ac 42 } else if (temp<0.1515) {
littlexc 0:28b9e26e75ac 43 return 25;
littlexc 0:28b9e26e75ac 44 } else if (temp<0.2121) {
littlexc 0:28b9e26e75ac 45 return 20;
littlexc 0:28b9e26e75ac 46 } else if (temp<0.2727) {
littlexc 0:28b9e26e75ac 47 return 14;
littlexc 0:28b9e26e75ac 48 } else if (temp<0.303) {
littlexc 0:28b9e26e75ac 49 return 12;
littlexc 0:28b9e26e75ac 50 } else if (temp<0.3939) {
littlexc 0:28b9e26e75ac 51 return 10;
littlexc 0:28b9e26e75ac 52 } else if (temp<0.4848) {
littlexc 0:28b9e26e75ac 53 return 8;
littlexc 0:28b9e26e75ac 54 } else if (temp<0.5454) {
littlexc 0:28b9e26e75ac 55 return 7;
littlexc 0:28b9e26e75ac 56 } else if (temp<0.606) {
littlexc 0:28b9e26e75ac 57 return 6;
littlexc 0:28b9e26e75ac 58 } else if (temp<0.6969) {
littlexc 0:28b9e26e75ac 59 return 5;
littlexc 0:28b9e26e75ac 60 } else {
littlexc 0:28b9e26e75ac 61 return 4;
littlexc 0:28b9e26e75ac 62 }
littlexc 0:28b9e26e75ac 63 } else if (sensor == 2) { //GP2D12
littlexc 0:28b9e26e75ac 64 if ( temp<0.136) {
littlexc 0:28b9e26e75ac 65 return 80;
littlexc 0:28b9e26e75ac 66 } else if ( temp<0.145) {
littlexc 0:28b9e26e75ac 67 return 75;
littlexc 0:28b9e26e75ac 68 } else if ( temp<0.15) {
littlexc 0:28b9e26e75ac 69 return 70;
littlexc 0:28b9e26e75ac 70 } else if ( temp<0.17) {
littlexc 0:28b9e26e75ac 71 return 65;
littlexc 0:28b9e26e75ac 72 } else if ( temp<0.18) {
littlexc 0:28b9e26e75ac 73 return 60;
littlexc 0:28b9e26e75ac 74 } else if ( temp<0.19) {
littlexc 0:28b9e26e75ac 75 return 55;
littlexc 0:28b9e26e75ac 76 } else if ( temp<0.21) {
littlexc 0:28b9e26e75ac 77 return 50;
littlexc 0:28b9e26e75ac 78 } else if ( temp<0.23) {
littlexc 0:28b9e26e75ac 79 return 45;
littlexc 0:28b9e26e75ac 80 } else if (temp<0.26) {
littlexc 0:28b9e26e75ac 81 return 40;
littlexc 0:28b9e26e75ac 82 } else if (temp<0.30) {
littlexc 0:28b9e26e75ac 83 return 30;
littlexc 0:28b9e26e75ac 84 } else if (temp<0.34) {
littlexc 0:28b9e26e75ac 85 return 28;
littlexc 0:28b9e26e75ac 86 } else if (temp<0.42) {
littlexc 0:28b9e26e75ac 87 return 20;
littlexc 0:28b9e26e75ac 88 } else if (temp<0.46) {
littlexc 0:28b9e26e75ac 89 return 18;
littlexc 0:28b9e26e75ac 90 } else if (temp<0.50) {
littlexc 0:28b9e26e75ac 91 return 16;
littlexc 0:28b9e26e75ac 92 } else if (temp<0.56) {
littlexc 0:28b9e26e75ac 93 return 14;
littlexc 0:28b9e26e75ac 94 } else if (temp<0.64) {
littlexc 0:28b9e26e75ac 95 return 12;
littlexc 0:28b9e26e75ac 96 } else if (temp<0.74) {
littlexc 0:28b9e26e75ac 97 return 10;
littlexc 0:28b9e26e75ac 98 } else {
littlexc 0:28b9e26e75ac 99 return 9;
littlexc 0:28b9e26e75ac 100 }
littlexc 0:28b9e26e75ac 101 } else
littlexc 0:28b9e26e75ac 102 return -1;
littlexc 0:28b9e26e75ac 103 }
littlexc 0:28b9e26e75ac 104 //returns value for floating point given by analog in
littlexc 0:28b9e26e75ac 105 float IRRangeFinder::read_f(void) {
littlexc 0:28b9e26e75ac 106 return _pin.read();
littlexc 0:28b9e26e75ac 107 }
littlexc 0:28b9e26e75ac 108
littlexc 0:28b9e26e75ac 109 //returns value for voltage, given by (1/3.3)*(read_f)
littlexc 0:28b9e26e75ac 110 float IRRangeFinder::read_v(void) {
littlexc 0:28b9e26e75ac 111 float temp = _pin.read();
littlexc 0:28b9e26e75ac 112 temp = temp*0.303;
littlexc 0:28b9e26e75ac 113 return temp;
littlexc 0:28b9e26e75ac 114 }
littlexc 0:28b9e26e75ac 115
littlexc 0:28b9e26e75ac 116
littlexc 0:28b9e26e75ac 117
littlexc 0:28b9e26e75ac 118
littlexc 0:28b9e26e75ac 119
littlexc 0:28b9e26e75ac 120
littlexc 0:28b9e26e75ac 121
littlexc 0:28b9e26e75ac 122
littlexc 0:28b9e26e75ac 123
littlexc 0:28b9e26e75ac 124
littlexc 0:28b9e26e75ac 125
littlexc 0:28b9e26e75ac 126
littlexc 0:28b9e26e75ac 127
littlexc 0:28b9e26e75ac 128 /*
littlexc 0:28b9e26e75ac 129 Serial pc(USBTX, USBRX); // tx, rx
littlexc 0:28b9e26e75ac 130 AnalogIn IRRF(p18);
littlexc 0:28b9e26e75ac 131
littlexc 0:28b9e26e75ac 132 int main() {
littlexc 0:28b9e26e75ac 133 pc.printf("Hello World!");
littlexc 0:28b9e26e75ac 134 while (1) {
littlexc 0:28b9e26e75ac 135
littlexc 0:28b9e26e75ac 136 pc.printf("%x \n", IRRF.read_u16());
littlexc 0:28b9e26e75ac 137 wait(0.1);
littlexc 0:28b9e26e75ac 138
littlexc 0:28b9e26e75ac 139 }
littlexc 0:28b9e26e75ac 140 }*/