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 11 16:01:21 2010 +0000
Revision:
0:28b9e26e75ac
Child:
1:e3ea40a41d27

        

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 0:28b9e26e75ac 32 IRRangeFinder::IRRangeFinder (PinName pin, int type): _pin(pin) {
littlexc 0:28b9e26e75ac 33 sensor=type;
littlexc 0:28b9e26e75ac 34 };
littlexc 0:28b9e26e75ac 35 // returns value for range, greater than 4, 5, 6, 7, 8, 10, 12, 14, 20, 25, 30 cm
littlexc 0:28b9e26e75ac 36 int IRRangeFinder::read(void) {
littlexc 0:28b9e26e75ac 37 float temp = _pin.read();
littlexc 0:28b9e26e75ac 38 if (sensor==1) { //GP2D120
littlexc 0:28b9e26e75ac 39 if ( temp<0.1212) {
littlexc 0:28b9e26e75ac 40 return 30;
littlexc 0:28b9e26e75ac 41 } else if (temp<0.1515) {
littlexc 0:28b9e26e75ac 42 return 25;
littlexc 0:28b9e26e75ac 43 } else if (temp<0.2121) {
littlexc 0:28b9e26e75ac 44 return 20;
littlexc 0:28b9e26e75ac 45 } else if (temp<0.2727) {
littlexc 0:28b9e26e75ac 46 return 14;
littlexc 0:28b9e26e75ac 47 } else if (temp<0.303) {
littlexc 0:28b9e26e75ac 48 return 12;
littlexc 0:28b9e26e75ac 49 } else if (temp<0.3939) {
littlexc 0:28b9e26e75ac 50 return 10;
littlexc 0:28b9e26e75ac 51 } else if (temp<0.4848) {
littlexc 0:28b9e26e75ac 52 return 8;
littlexc 0:28b9e26e75ac 53 } else if (temp<0.5454) {
littlexc 0:28b9e26e75ac 54 return 7;
littlexc 0:28b9e26e75ac 55 } else if (temp<0.606) {
littlexc 0:28b9e26e75ac 56 return 6;
littlexc 0:28b9e26e75ac 57 } else if (temp<0.6969) {
littlexc 0:28b9e26e75ac 58 return 5;
littlexc 0:28b9e26e75ac 59 } else {
littlexc 0:28b9e26e75ac 60 return 4;
littlexc 0:28b9e26e75ac 61 }
littlexc 0:28b9e26e75ac 62 } else if (sensor == 2) { //GP2D12
littlexc 0:28b9e26e75ac 63 if ( temp<0.136) {
littlexc 0:28b9e26e75ac 64 return 80;
littlexc 0:28b9e26e75ac 65 } else if ( temp<0.145) {
littlexc 0:28b9e26e75ac 66 return 75;
littlexc 0:28b9e26e75ac 67 } else if ( temp<0.15) {
littlexc 0:28b9e26e75ac 68 return 70;
littlexc 0:28b9e26e75ac 69 } else if ( temp<0.17) {
littlexc 0:28b9e26e75ac 70 return 65;
littlexc 0:28b9e26e75ac 71 } else if ( temp<0.18) {
littlexc 0:28b9e26e75ac 72 return 60;
littlexc 0:28b9e26e75ac 73 } else if ( temp<0.19) {
littlexc 0:28b9e26e75ac 74 return 55;
littlexc 0:28b9e26e75ac 75 } else if ( temp<0.21) {
littlexc 0:28b9e26e75ac 76 return 50;
littlexc 0:28b9e26e75ac 77 } else if ( temp<0.23) {
littlexc 0:28b9e26e75ac 78 return 45;
littlexc 0:28b9e26e75ac 79 } else if (temp<0.26) {
littlexc 0:28b9e26e75ac 80 return 40;
littlexc 0:28b9e26e75ac 81 } else if (temp<0.30) {
littlexc 0:28b9e26e75ac 82 return 30;
littlexc 0:28b9e26e75ac 83 } else if (temp<0.34) {
littlexc 0:28b9e26e75ac 84 return 28;
littlexc 0:28b9e26e75ac 85 } else if (temp<0.42) {
littlexc 0:28b9e26e75ac 86 return 20;
littlexc 0:28b9e26e75ac 87 } else if (temp<0.46) {
littlexc 0:28b9e26e75ac 88 return 18;
littlexc 0:28b9e26e75ac 89 } else if (temp<0.50) {
littlexc 0:28b9e26e75ac 90 return 16;
littlexc 0:28b9e26e75ac 91 } else if (temp<0.56) {
littlexc 0:28b9e26e75ac 92 return 14;
littlexc 0:28b9e26e75ac 93 } else if (temp<0.64) {
littlexc 0:28b9e26e75ac 94 return 12;
littlexc 0:28b9e26e75ac 95 } else if (temp<0.74) {
littlexc 0:28b9e26e75ac 96 return 10;
littlexc 0:28b9e26e75ac 97 } else {
littlexc 0:28b9e26e75ac 98 return 9;
littlexc 0:28b9e26e75ac 99 }
littlexc 0:28b9e26e75ac 100 } else
littlexc 0:28b9e26e75ac 101 return -1;
littlexc 0:28b9e26e75ac 102 }
littlexc 0:28b9e26e75ac 103 //returns value for floating point given by analog in
littlexc 0:28b9e26e75ac 104 float IRRangeFinder::read_f(void) {
littlexc 0:28b9e26e75ac 105 return _pin.read();
littlexc 0:28b9e26e75ac 106 }
littlexc 0:28b9e26e75ac 107
littlexc 0:28b9e26e75ac 108 //returns value for voltage, given by (1/3.3)*(read_f)
littlexc 0:28b9e26e75ac 109 float IRRangeFinder::read_v(void) {
littlexc 0:28b9e26e75ac 110 float temp = _pin.read();
littlexc 0:28b9e26e75ac 111 temp = temp*0.303;
littlexc 0:28b9e26e75ac 112 return temp;
littlexc 0:28b9e26e75ac 113 }
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 Serial pc(USBTX, USBRX); // tx, rx
littlexc 0:28b9e26e75ac 129 AnalogIn IRRF(p18);
littlexc 0:28b9e26e75ac 130
littlexc 0:28b9e26e75ac 131 int main() {
littlexc 0:28b9e26e75ac 132 pc.printf("Hello World!");
littlexc 0:28b9e26e75ac 133 while (1) {
littlexc 0:28b9e26e75ac 134
littlexc 0:28b9e26e75ac 135 pc.printf("%x \n", IRRF.read_u16());
littlexc 0:28b9e26e75ac 136 wait(0.1);
littlexc 0:28b9e26e75ac 137
littlexc 0:28b9e26e75ac 138 }
littlexc 0:28b9e26e75ac 139 }*/