Sharp Long Range IR Sensor testing and calibrating
Dependencies: mbed
main.cpp@0:d9f7e20a900f, 2016-04-01 (annotated)
- Committer:
- j_j205
- Date:
- Fri Apr 01 18:29:38 2016 +0000
- Revision:
- 0:d9f7e20a900f
Long Range Sensor testing and calibrating
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
j_j205 | 0:d9f7e20a900f | 1 | #include "mbed.h" |
j_j205 | 0:d9f7e20a900f | 2 | #include <cmath> // pow |
j_j205 | 0:d9f7e20a900f | 3 | |
j_j205 | 0:d9f7e20a900f | 4 | Serial pc(USBTX, USBRX); |
j_j205 | 0:d9f7e20a900f | 5 | |
j_j205 | 0:d9f7e20a900f | 6 | DigitalOut led_red(LED_RED); |
j_j205 | 0:d9f7e20a900f | 7 | DigitalOut led_green(LED_GREEN); |
j_j205 | 0:d9f7e20a900f | 8 | AnalogIn sensor1(PTB2); |
j_j205 | 0:d9f7e20a900f | 9 | AnalogIn sensor2(PTB3); |
j_j205 | 0:d9f7e20a900f | 10 | float dist; |
j_j205 | 0:d9f7e20a900f | 11 | |
j_j205 | 0:d9f7e20a900f | 12 | //float getDist(AnalogIn& sensor); |
j_j205 | 0:d9f7e20a900f | 13 | float distAdjL(double sensor_read); |
j_j205 | 0:d9f7e20a900f | 14 | float distAdjR(double sensor_read); |
j_j205 | 0:d9f7e20a900f | 15 | |
j_j205 | 0:d9f7e20a900f | 16 | int main() |
j_j205 | 0:d9f7e20a900f | 17 | { |
j_j205 | 0:d9f7e20a900f | 18 | pc.baud(115200); |
j_j205 | 0:d9f7e20a900f | 19 | |
j_j205 | 0:d9f7e20a900f | 20 | float percent1 = 0.0; |
j_j205 | 0:d9f7e20a900f | 21 | float voltage1 = 0.0; |
j_j205 | 0:d9f7e20a900f | 22 | float percent2 = 0.0; |
j_j205 | 0:d9f7e20a900f | 23 | float voltage2 = 0.0; |
j_j205 | 0:d9f7e20a900f | 24 | int count; |
j_j205 | 0:d9f7e20a900f | 25 | char choice; |
j_j205 | 0:d9f7e20a900f | 26 | |
j_j205 | 0:d9f7e20a900f | 27 | pc.printf("Enter 'c' for calibration mode or 't' for testing mode.\n"); |
j_j205 | 0:d9f7e20a900f | 28 | pc.printf("Press 'e' to exit.\n"); |
j_j205 | 0:d9f7e20a900f | 29 | choice = pc.getc(); |
j_j205 | 0:d9f7e20a900f | 30 | |
j_j205 | 0:d9f7e20a900f | 31 | switch(choice) |
j_j205 | 0:d9f7e20a900f | 32 | { |
j_j205 | 0:d9f7e20a900f | 33 | case 'C': |
j_j205 | 0:d9f7e20a900f | 34 | case 'c': |
j_j205 | 0:d9f7e20a900f | 35 | count = 5; |
j_j205 | 0:d9f7e20a900f | 36 | led_red = 0; |
j_j205 | 0:d9f7e20a900f | 37 | led_green = 1; |
j_j205 | 0:d9f7e20a900f | 38 | pc.printf("\nMove target to listed distance and press space bar to measure\n"); |
j_j205 | 0:d9f7e20a900f | 39 | pc.printf("Press 'e' to exit.\n"); |
j_j205 | 0:d9f7e20a900f | 40 | |
j_j205 | 0:d9f7e20a900f | 41 | pc.printf("\nDist Perc1 Volt1 Perc2 Volt2"); |
j_j205 | 0:d9f7e20a900f | 42 | pc.printf("\n%*i", 4, count); |
j_j205 | 0:d9f7e20a900f | 43 | choice = pc.getc(); |
j_j205 | 0:d9f7e20a900f | 44 | |
j_j205 | 0:d9f7e20a900f | 45 | while(choice != 'q') |
j_j205 | 0:d9f7e20a900f | 46 | { |
j_j205 | 0:d9f7e20a900f | 47 | led_red = 0; |
j_j205 | 0:d9f7e20a900f | 48 | led_green = 1; |
j_j205 | 0:d9f7e20a900f | 49 | for(int i = 0; i < 250; i++) |
j_j205 | 0:d9f7e20a900f | 50 | { |
j_j205 | 0:d9f7e20a900f | 51 | percent1 += sensor1.read(); |
j_j205 | 0:d9f7e20a900f | 52 | voltage1 += 3.3 * sensor1.read(); |
j_j205 | 0:d9f7e20a900f | 53 | percent2 += sensor2.read(); |
j_j205 | 0:d9f7e20a900f | 54 | voltage2 += 3.3 * sensor2.read(); |
j_j205 | 0:d9f7e20a900f | 55 | wait(32e-3); |
j_j205 | 0:d9f7e20a900f | 56 | } |
j_j205 | 0:d9f7e20a900f | 57 | |
j_j205 | 0:d9f7e20a900f | 58 | pc.printf(" %f %f %f %f", percent1/250.0, voltage1/250.0, percent2/250.0, voltage2/250.0 ); |
j_j205 | 0:d9f7e20a900f | 59 | percent1 = 0.0; |
j_j205 | 0:d9f7e20a900f | 60 | voltage1 = 0.0; |
j_j205 | 0:d9f7e20a900f | 61 | percent2 = 0.0; |
j_j205 | 0:d9f7e20a900f | 62 | voltage2 = 0.0; |
j_j205 | 0:d9f7e20a900f | 63 | count++; |
j_j205 | 0:d9f7e20a900f | 64 | led_red = 1; |
j_j205 | 0:d9f7e20a900f | 65 | led_green = 0; |
j_j205 | 0:d9f7e20a900f | 66 | |
j_j205 | 0:d9f7e20a900f | 67 | pc.printf("\n%*i", 4, count); |
j_j205 | 0:d9f7e20a900f | 68 | choice = pc.getc(); |
j_j205 | 0:d9f7e20a900f | 69 | } |
j_j205 | 0:d9f7e20a900f | 70 | break; |
j_j205 | 0:d9f7e20a900f | 71 | |
j_j205 | 0:d9f7e20a900f | 72 | case 'T': |
j_j205 | 0:d9f7e20a900f | 73 | case 't': |
j_j205 | 0:d9f7e20a900f | 74 | count = 5; |
j_j205 | 0:d9f7e20a900f | 75 | pc.printf("\nMove target to distance and press space bar to measure\n"); |
j_j205 | 0:d9f7e20a900f | 76 | pc.printf("Press 'e' to exit.\n"); |
j_j205 | 0:d9f7e20a900f | 77 | |
j_j205 | 0:d9f7e20a900f | 78 | pc.printf("\nDist Meas1 AdjM1 Meas2 AdjM2"); |
j_j205 | 0:d9f7e20a900f | 79 | pc.printf("\n%*i", 4, count + 10); |
j_j205 | 0:d9f7e20a900f | 80 | choice = pc.getc(); |
j_j205 | 0:d9f7e20a900f | 81 | |
j_j205 | 0:d9f7e20a900f | 82 | while(choice != 'q') |
j_j205 | 0:d9f7e20a900f | 83 | { |
j_j205 | 0:d9f7e20a900f | 84 | led_red = 0; |
j_j205 | 0:d9f7e20a900f | 85 | led_green = 1; |
j_j205 | 0:d9f7e20a900f | 86 | for(int i = 0; i < 50; i++) |
j_j205 | 0:d9f7e20a900f | 87 | { |
j_j205 | 0:d9f7e20a900f | 88 | percent1 += sensor1.read(); |
j_j205 | 0:d9f7e20a900f | 89 | percent2 += sensor2.read(); |
j_j205 | 0:d9f7e20a900f | 90 | wait(32e-3); |
j_j205 | 0:d9f7e20a900f | 91 | } |
j_j205 | 0:d9f7e20a900f | 92 | |
j_j205 | 0:d9f7e20a900f | 93 | pc.printf(" %f %f %f %f", 3.5097*pow((percent1/50.0), -1.425), distAdjL(percent1/50.0), 3.4617*pow((percent2/50.0), -1.506), distAdjR(percent2/50.0) ); |
j_j205 | 0:d9f7e20a900f | 94 | percent1 = 0.0; |
j_j205 | 0:d9f7e20a900f | 95 | percent2 = 0.0; |
j_j205 | 0:d9f7e20a900f | 96 | count++; |
j_j205 | 0:d9f7e20a900f | 97 | led_red = 1; |
j_j205 | 0:d9f7e20a900f | 98 | led_green = 0; |
j_j205 | 0:d9f7e20a900f | 99 | |
j_j205 | 0:d9f7e20a900f | 100 | pc.printf("\n%*i", 4, count); |
j_j205 | 0:d9f7e20a900f | 101 | choice = pc.getc(); |
j_j205 | 0:d9f7e20a900f | 102 | } |
j_j205 | 0:d9f7e20a900f | 103 | break; |
j_j205 | 0:d9f7e20a900f | 104 | |
j_j205 | 0:d9f7e20a900f | 105 | case 'Q': |
j_j205 | 0:d9f7e20a900f | 106 | case 'q': |
j_j205 | 0:d9f7e20a900f | 107 | break; |
j_j205 | 0:d9f7e20a900f | 108 | |
j_j205 | 0:d9f7e20a900f | 109 | default: |
j_j205 | 0:d9f7e20a900f | 110 | pc.printf("\nInvalid entry. You're fired."); |
j_j205 | 0:d9f7e20a900f | 111 | } |
j_j205 | 0:d9f7e20a900f | 112 | } |
j_j205 | 0:d9f7e20a900f | 113 | |
j_j205 | 0:d9f7e20a900f | 114 | float distAdjL(double sensor_read) |
j_j205 | 0:d9f7e20a900f | 115 | { |
j_j205 | 0:d9f7e20a900f | 116 | double dist_meas = 3.5097*pow(sensor_read, -1.425); |
j_j205 | 0:d9f7e20a900f | 117 | |
j_j205 | 0:d9f7e20a900f | 118 | double read_error = (0.0052*pow(dist_meas, 2.0)) - (0.3218*dist_meas) |
j_j205 | 0:d9f7e20a900f | 119 | + 2.6653; |
j_j205 | 0:d9f7e20a900f | 120 | |
j_j205 | 0:d9f7e20a900f | 121 | return float(dist_meas + read_error); |
j_j205 | 0:d9f7e20a900f | 122 | } |
j_j205 | 0:d9f7e20a900f | 123 | |
j_j205 | 0:d9f7e20a900f | 124 | float distAdjR(double sensor_read) |
j_j205 | 0:d9f7e20a900f | 125 | { |
j_j205 | 0:d9f7e20a900f | 126 | double dist_meas = 3.4617*pow(sensor_read, -1.506); |
j_j205 | 0:d9f7e20a900f | 127 | |
j_j205 | 0:d9f7e20a900f | 128 | double read_error = (0.0062*pow(dist_meas, 2.0)) - (0.385*dist_meas) |
j_j205 | 0:d9f7e20a900f | 129 | + 3.1692; |
j_j205 | 0:d9f7e20a900f | 130 | |
j_j205 | 0:d9f7e20a900f | 131 | return float(dist_meas + read_error); |
j_j205 | 0:d9f7e20a900f | 132 | } |