UltrasonicRanger_Example V1.0
Dependencies: mbed RangeFinder
main.cpp@6:86943d1ed254, 2019-06-24 (annotated)
- Committer:
- pmic
- Date:
- Mon Jun 24 09:29:33 2019 +0000
- Revision:
- 6:86943d1ed254
- Parent:
- 4:881e860d7de1
- Child:
- 8:25e1f2a7d3bc
correct comment
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
NickRyder | 0:186bb2174995 | 1 | /* Copyright (c) 2012 Nick Ryder, University of Oxford |
NickRyder | 0:186bb2174995 | 2 | * nick.ryder@physics.ox.ac.uk |
NickRyder | 0:186bb2174995 | 3 | * |
NickRyder | 0:186bb2174995 | 4 | * MIT License |
NickRyder | 0:186bb2174995 | 5 | * |
pmic | 3:4a2cd363c443 | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
pmic | 3:4a2cd363c443 | 7 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
pmic | 3:4a2cd363c443 | 8 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
pmic | 3:4a2cd363c443 | 9 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
NickRyder | 0:186bb2174995 | 10 | * furnished to do so, subject to the following conditions: |
NickRyder | 0:186bb2174995 | 11 | * |
pmic | 3:4a2cd363c443 | 12 | * The above copyright notice and this permission notice shall be included in all copies or |
NickRyder | 0:186bb2174995 | 13 | * substantial portions of the Software. |
NickRyder | 0:186bb2174995 | 14 | * |
pmic | 3:4a2cd363c443 | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
pmic | 3:4a2cd363c443 | 16 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
pmic | 3:4a2cd363c443 | 17 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
pmic | 3:4a2cd363c443 | 18 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
NickRyder | 0:186bb2174995 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
NickRyder | 0:186bb2174995 | 20 | */ |
NickRyder | 0:186bb2174995 | 21 | |
NickRyder | 0:186bb2174995 | 22 | #include "mbed.h" |
pmic | 3:4a2cd363c443 | 23 | #include "RangeFinder.h" |
pmic | 3:4a2cd363c443 | 24 | #include "IIR_filter.h" |
NickRyder | 0:186bb2174995 | 25 | |
pmic | 3:4a2cd363c443 | 26 | /* Notes pmic 21.06.2019: |
pmic | 3:4a2cd363c443 | 27 | - RangeFinder class extended with offset. |
pmic | 3:4a2cd363c443 | 28 | - Static measurements have shown that the actual scaling is approx. 5782 (instead of 5800). |
pmic | 3:4a2cd363c443 | 29 | - Avg. offset relative to top side of the board (pointing towards meas. direction) is 0.02 m |
pmic | 3:4a2cd363c443 | 30 | - With 7000 mus (7 ms) timout time the max. measured distance is approx. 1.14 m. Theoreticaly |
pmic | 4:881e860d7de1 | 31 | it is ( 0.007 s * 343.2 m/s )/2 = 1.201 m. If understood rigth the sensor does 8 measurements |
pmic | 3:4a2cd363c443 | 32 | and takes the average. 1.201 - 1.14 m = 0.0612 m := 357 mus -> 357/8 = 44.6 ms / measurement |
pmic | 3:4a2cd363c443 | 33 | - Min. distance is approx. 0.04 m (relative to top side board) |
pmic | 3:4a2cd363c443 | 34 | - Using 50 Hz (Ts = 20 ms) the max. delay is 7 ms (@ max. dist. 1.14 m), there for 13 ms remain for other tasks |
pmic | 3:4a2cd363c443 | 35 | (in real time implementation probably also possible to use 100 Hz) |
pmic | 3:4a2cd363c443 | 36 | - read_m() returns -1 if object is not within certain range |
pmic | 3:4a2cd363c443 | 37 | - sensor opening angle is approx. 15 deg |
pmic | 3:4a2cd363c443 | 38 | */ |
pmic | 3:4a2cd363c443 | 39 | |
pmic | 4:881e860d7de1 | 40 | /* Notes pmic 24.06.2019: |
pmic | 4:881e860d7de1 | 41 | - With 17500 mus (17.5 ms = 50/20*7000) timout time the max. measured distance is approx. 2.67 m. Theoreticaly |
pmic | 4:881e860d7de1 | 42 | it is ( 0.0175 s * 343.2 m/s )/2 = 3.003 m. |
pmic | 6:86943d1ed254 | 43 | - Using 20 Hz (Ts = 50 ms) the max. delay is 17.5 ms (@ max. dist. 2.67 m), there for 32.5 ms remain for other tasks |
pmic | 4:881e860d7de1 | 44 | */ |
pmic | 4:881e860d7de1 | 45 | |
pmic | 3:4a2cd363c443 | 46 | // PB_3 ist ist der 4te von unten rechts, aussen GND innen S16 |
NickRyder | 0:186bb2174995 | 47 | |
pmic | 3:4a2cd363c443 | 48 | /** Create a RangeFinder object |
pmic | 3:4a2cd363c443 | 49 | * @param pin Digital I/O pin the range finder is connected to. |
pmic | 3:4a2cd363c443 | 50 | * @param pulsetime Time of pulse to send to the rangefinder to trigger a measurement, in microseconds. |
pmic | 3:4a2cd363c443 | 51 | * @param scale Scaling of the range finder's output pulse from microseconds to meters. |
pmic | 3:4a2cd363c443 | 52 | * @param offset Offset of the range finder's output pulse to adjust absolut reference. |
pmic | 3:4a2cd363c443 | 53 | * @param timeout Time to wait for a pulse from the range finder before giving up. |
pmic | 3:4a2cd363c443 | 54 | * y = x/scale + offset |
pmic | 3:4a2cd363c443 | 55 | */ |
pmic | 4:881e860d7de1 | 56 | // RangeFinder rf(PB_3, 10, 5782.0, 0.02, 7000); // 50 Hz parametrization |
pmic | 4:881e860d7de1 | 57 | RangeFinder rf(PB_3, 10, 5782.0, 0.02, 17500); // 20 Hz parametrization |
pmic | 3:4a2cd363c443 | 58 | float d = 0.0f; |
pmic | 3:4a2cd363c443 | 59 | |
pmic | 3:4a2cd363c443 | 60 | Serial pc(SERIAL_TX, SERIAL_RX); // serial connection via USB - programmer |
pmic | 3:4a2cd363c443 | 61 | InterruptIn Button(USER_BUTTON); // User Button |
pmic | 3:4a2cd363c443 | 62 | Ticker LoopTimer; // interrupt for control loop |
pmic | 3:4a2cd363c443 | 63 | Timer t; // timer to analyse Button |
pmic | 3:4a2cd363c443 | 64 | |
pmic | 3:4a2cd363c443 | 65 | int k; |
pmic | 3:4a2cd363c443 | 66 | bool doRun = false; |
pmic | 4:881e860d7de1 | 67 | // float Ts = 0.02f; // sample time of main loop, 50 Hz |
pmic | 4:881e860d7de1 | 68 | float Ts = 0.05f; // sample time of main loop, 20 Hz |
pmic | 3:4a2cd363c443 | 69 | |
pmic | 3:4a2cd363c443 | 70 | IIR_filter pt1(0.2f, Ts, 1.0f); |
pmic | 3:4a2cd363c443 | 71 | float df = 0.0f; |
pmic | 3:4a2cd363c443 | 72 | |
pmic | 3:4a2cd363c443 | 73 | /* |
pmic | 3:4a2cd363c443 | 74 | // Sensor 1 |
pmic | 3:4a2cd363c443 | 75 | float usGain = 0.0001728f; |
pmic | 3:4a2cd363c443 | 76 | float usOffset = 0.0215916f; |
pmic | 3:4a2cd363c443 | 77 | // Sensor 2 |
pmic | 3:4a2cd363c443 | 78 | float usGain = 0.0001731f; |
pmic | 3:4a2cd363c443 | 79 | float usOffset = 0.0186031f; |
pmic | 3:4a2cd363c443 | 80 | */ |
pmic | 3:4a2cd363c443 | 81 | |
pmic | 3:4a2cd363c443 | 82 | // user defined functions |
pmic | 3:4a2cd363c443 | 83 | void updateLoop(void); // loop (via interrupt) |
pmic | 3:4a2cd363c443 | 84 | void pressed(void); // user Button pressed |
pmic | 3:4a2cd363c443 | 85 | void released(void); // user Button released |
NickRyder | 0:186bb2174995 | 86 | |
pmic | 3:4a2cd363c443 | 87 | // main program and control loop |
pmic | 3:4a2cd363c443 | 88 | // ----------------------------------------------------------------------------- |
pmic | 3:4a2cd363c443 | 89 | int main() |
pmic | 3:4a2cd363c443 | 90 | { |
pmic | 3:4a2cd363c443 | 91 | pc.baud(2000000); // for serial comm. |
pmic | 3:4a2cd363c443 | 92 | LoopTimer.attach(&updateLoop, Ts); // attach loop to timer interrupt |
pmic | 3:4a2cd363c443 | 93 | Button.fall(&pressed); // attach key pressed function |
pmic | 3:4a2cd363c443 | 94 | Button.rise(&released); // attach key pressed function |
pmic | 3:4a2cd363c443 | 95 | k = 0; |
pmic | 3:4a2cd363c443 | 96 | pt1.reset(0.0f); |
pmic | 3:4a2cd363c443 | 97 | } |
pmic | 3:4a2cd363c443 | 98 | |
pmic | 3:4a2cd363c443 | 99 | // original |
pmic | 3:4a2cd363c443 | 100 | /* |
pmic | 3:4a2cd363c443 | 101 | if (d == -1.0) { |
pmic | 3:4a2cd363c443 | 102 | printf("Timeout Error.\n"); |
pmic | 3:4a2cd363c443 | 103 | } else if (d > 5.0) { |
pmic | 3:4a2cd363c443 | 104 | // Seeed's sensor has a maximum range of 4m, it returns |
pmic | 3:4a2cd363c443 | 105 | // something like 7m if the ultrasound pulse isn't reflected. |
pmic | 3:4a2cd363c443 | 106 | printf("No object within detection range.\n"); |
pmic | 3:4a2cd363c443 | 107 | } else { |
pmic | 3:4a2cd363c443 | 108 | printf("Distance = %f m.\n", d); |
pmic | 3:4a2cd363c443 | 109 | } |
pmic | 3:4a2cd363c443 | 110 | */ |
pmic | 3:4a2cd363c443 | 111 | |
pmic | 3:4a2cd363c443 | 112 | // the updateLoop starts as soon as you pressed the blue botton |
pmic | 3:4a2cd363c443 | 113 | void updateLoop(void) |
pmic | 3:4a2cd363c443 | 114 | { |
pmic | 3:4a2cd363c443 | 115 | // d = usGain*rf.read_m() + usOffset; |
pmic | 3:4a2cd363c443 | 116 | d = rf.read_m(); |
pmic | 3:4a2cd363c443 | 117 | df = pt1(d); |
pmic | 3:4a2cd363c443 | 118 | if(doRun) { |
pmic | 4:881e860d7de1 | 119 | /* |
pmic | 3:4a2cd363c443 | 120 | // use this section to do dynamical measurements |
pmic | 3:4a2cd363c443 | 121 | if(doRun && k++ < 4000) { |
pmic | 3:4a2cd363c443 | 122 | pc.printf("%10i %10.9e\r\n", k, d); |
NickRyder | 0:186bb2174995 | 123 | } |
pmic | 4:881e860d7de1 | 124 | */ |
pmic | 4:881e860d7de1 | 125 | ///* |
pmic | 3:4a2cd363c443 | 126 | // use this section to do static measurements |
pmic | 3:4a2cd363c443 | 127 | if(doRun && k++%25 == 0) { |
pmic | 3:4a2cd363c443 | 128 | pc.printf("%10i %10.6e\r\n", k, df); |
pmic | 3:4a2cd363c443 | 129 | } |
pmic | 4:881e860d7de1 | 130 | //*/ |
NickRyder | 0:186bb2174995 | 131 | } |
pmic | 3:4a2cd363c443 | 132 | } |
pmic | 3:4a2cd363c443 | 133 | |
pmic | 3:4a2cd363c443 | 134 | // buttonhandling |
pmic | 3:4a2cd363c443 | 135 | // ----------------------------------------------------------------------------- |
pmic | 3:4a2cd363c443 | 136 | // start timer as soon as button is pressed |
pmic | 3:4a2cd363c443 | 137 | void pressed() |
pmic | 3:4a2cd363c443 | 138 | { |
pmic | 3:4a2cd363c443 | 139 | t.start(); |
pmic | 3:4a2cd363c443 | 140 | } |
pmic | 3:4a2cd363c443 | 141 | |
pmic | 3:4a2cd363c443 | 142 | // evaluating statemachine |
pmic | 3:4a2cd363c443 | 143 | void released() |
pmic | 3:4a2cd363c443 | 144 | { |
pmic | 3:4a2cd363c443 | 145 | // toggle state over boolean |
pmic | 3:4a2cd363c443 | 146 | if(doRun) { |
pmic | 3:4a2cd363c443 | 147 | k = 0; |
pmic | 3:4a2cd363c443 | 148 | pt1.reset(0.0f); |
pmic | 3:4a2cd363c443 | 149 | } |
pmic | 3:4a2cd363c443 | 150 | doRun = !doRun; |
pmic | 3:4a2cd363c443 | 151 | t.stop(); |
pmic | 3:4a2cd363c443 | 152 | t.reset(); |
pmic | 3:4a2cd363c443 | 153 | } |
pmic | 3:4a2cd363c443 | 154 |