Library to use the ultrasonic sensor
Dependents: test_ultrasonic AEB Car_Simulator
Ultrasonic.cpp@3:9b06e5793b8b, 2016-06-05 (annotated)
- Committer:
- AndreaAndreoli
- Date:
- Sun Jun 05 12:23:48 2016 +0000
- Revision:
- 3:9b06e5793b8b
- Parent:
- 2:12da800fd10a
- Child:
- 4:8c97476a5ebf
created class Ultrasonic (not working);
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AndreaAndreoli | 0:72ec72845f71 | 1 | #include "mbed.h" |
AndreaAndreoli | 0:72ec72845f71 | 2 | #include "Ultrasonic.h" |
AndreaAndreoli | 0:72ec72845f71 | 3 | |
AndreaAndreoli | 0:72ec72845f71 | 4 | |
AndreaAndreoli | 3:9b06e5793b8b | 5 | Ultrasonic::Ultrasonic(PinName trigger, PinName echo): _trigger(trigger) , _echo(echo) |
AndreaAndreoli | 3:9b06e5793b8b | 6 | { |
AndreaAndreoli | 3:9b06e5793b8b | 7 | //_trigger = trigger; |
AndreaAndreoli | 3:9b06e5793b8b | 8 | //_echo = echo; |
AndreaAndreoli | 3:9b06e5793b8b | 9 | Ultrasonic_init(); |
AndreaAndreoli | 3:9b06e5793b8b | 10 | } |
AndreaAndreoli | 3:9b06e5793b8b | 11 | |
AndreaAndreoli | 3:9b06e5793b8b | 12 | |
AndreaAndreoli | 0:72ec72845f71 | 13 | |
AndreaAndreoli | 0:72ec72845f71 | 14 | /* |
AndreaAndreoli | 0:72ec72845f71 | 15 | * Call this function and it will return the distance in centimeter |
AndreaAndreoli | 0:72ec72845f71 | 16 | */ |
AndreaAndreoli | 3:9b06e5793b8b | 17 | int Ultrasonic::read_cm() |
AndreaAndreoli | 0:72ec72845f71 | 18 | { |
AndreaAndreoli | 0:72ec72845f71 | 19 | return DistanceCM; |
AndreaAndreoli | 0:72ec72845f71 | 20 | } |
AndreaAndreoli | 0:72ec72845f71 | 21 | |
AndreaAndreoli | 0:72ec72845f71 | 22 | |
AndreaAndreoli | 3:9b06e5793b8b | 23 | void Ultrasonic::start() |
AndreaAndreoli | 0:72ec72845f71 | 24 | { |
AndreaAndreoli | 0:72ec72845f71 | 25 | timer.start(); |
AndreaAndreoli | 0:72ec72845f71 | 26 | } |
AndreaAndreoli | 0:72ec72845f71 | 27 | |
AndreaAndreoli | 3:9b06e5793b8b | 28 | void Ultrasonic::stop() |
AndreaAndreoli | 0:72ec72845f71 | 29 | { |
AndreaAndreoli | 0:72ec72845f71 | 30 | DistanceCM = timer.read_us()/58; |
AndreaAndreoli | 0:72ec72845f71 | 31 | timer.stop(); |
AndreaAndreoli | 0:72ec72845f71 | 32 | //pc.printf("Distance: %f \n", DistanceCM); |
AndreaAndreoli | 0:72ec72845f71 | 33 | timer.reset(); |
AndreaAndreoli | 1:b3518845e71a | 34 | timeout.detach(); |
AndreaAndreoli | 0:72ec72845f71 | 35 | } |
AndreaAndreoli | 0:72ec72845f71 | 36 | |
AndreaAndreoli | 3:9b06e5793b8b | 37 | void Ultrasonic::trig() |
AndreaAndreoli | 0:72ec72845f71 | 38 | { |
AndreaAndreoli | 3:9b06e5793b8b | 39 | timeout.attach(this, this.timeout_err, 0.05); |
AndreaAndreoli | 3:9b06e5793b8b | 40 | _trigger = 1; |
AndreaAndreoli | 0:72ec72845f71 | 41 | wait_us(10); |
AndreaAndreoli | 3:9b06e5793b8b | 42 | _trigger = 0; |
AndreaAndreoli | 0:72ec72845f71 | 43 | } |
AndreaAndreoli | 0:72ec72845f71 | 44 | |
AndreaAndreoli | 0:72ec72845f71 | 45 | |
AndreaAndreoli | 3:9b06e5793b8b | 46 | void Ultrasonic::Ultrasonic_init() |
AndreaAndreoli | 0:72ec72845f71 | 47 | { |
AndreaAndreoli | 3:9b06e5793b8b | 48 | DistanceCM = 0; |
AndreaAndreoli | 0:72ec72845f71 | 49 | timer.reset(); |
AndreaAndreoli | 3:9b06e5793b8b | 50 | _echo.rise(&Ultrasonic::start); |
AndreaAndreoli | 3:9b06e5793b8b | 51 | _echo.fall(&stop); |
AndreaAndreoli | 0:72ec72845f71 | 52 | tick.attach(&trig,0.06); |
AndreaAndreoli | 1:b3518845e71a | 53 | } |
AndreaAndreoli | 1:b3518845e71a | 54 | |
AndreaAndreoli | 3:9b06e5793b8b | 55 | void Ultrasonic::timeout_err() |
AndreaAndreoli | 1:b3518845e71a | 56 | { |
AndreaAndreoli | 1:b3518845e71a | 57 | // Timeout elapsed -> there is some problem |
AndreaAndreoli | 1:b3518845e71a | 58 | // out the distance to zero to highligth error |
AndreaAndreoli | 1:b3518845e71a | 59 | |
AndreaAndreoli | 1:b3518845e71a | 60 | DistanceCM = 0; |
AndreaAndreoli | 0:72ec72845f71 | 61 | } |