Works with the parallax ping sensor. Uses a timer and interrupts to get the time and calculate the distance
Dependents: Wifly_TCPEchoServer_Sensors ROBOT_v01 ROBOT_v02 mbed_blinky ... more
Ping.cpp@0:1e4b66122368, 2011-03-12 (annotated)
- Committer:
- rosienej
- Date:
- Sat Mar 12 22:26:10 2011 +0000
- Revision:
- 0:1e4b66122368
beta
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rosienej | 0:1e4b66122368 | 1 | #include "Ping.h" |
rosienej | 0:1e4b66122368 | 2 | |
rosienej | 0:1e4b66122368 | 3 | #include "mbed.h" |
rosienej | 0:1e4b66122368 | 4 | |
rosienej | 0:1e4b66122368 | 5 | Ping::Ping(PinName PING_PIN) |
rosienej | 0:1e4b66122368 | 6 | : _event(PING_PIN) |
rosienej | 0:1e4b66122368 | 7 | , _cmd(PING_PIN) |
rosienej | 0:1e4b66122368 | 8 | , _timer() |
rosienej | 0:1e4b66122368 | 9 | { |
rosienej | 0:1e4b66122368 | 10 | _event.rise(this,&Ping::_Starts); |
rosienej | 0:1e4b66122368 | 11 | _event.fall(this,&Ping::_Stops); |
rosienej | 0:1e4b66122368 | 12 | _SPEED_OF_SOUND_CM = 33; |
rosienej | 0:1e4b66122368 | 13 | } |
rosienej | 0:1e4b66122368 | 14 | |
rosienej | 0:1e4b66122368 | 15 | void Ping::_Starts(void) |
rosienej | 0:1e4b66122368 | 16 | { |
rosienej | 0:1e4b66122368 | 17 | _Valid = false; // start the timere, and invalidate the current time. |
rosienej | 0:1e4b66122368 | 18 | _Busy = true; |
rosienej | 0:1e4b66122368 | 19 | _timer.start(); |
rosienej | 0:1e4b66122368 | 20 | _Time = _timer.read_us(); |
rosienej | 0:1e4b66122368 | 21 | } |
rosienej | 0:1e4b66122368 | 22 | |
rosienej | 0:1e4b66122368 | 23 | void Ping::_Stops(void) |
rosienej | 0:1e4b66122368 | 24 | { |
rosienej | 0:1e4b66122368 | 25 | _Valid = true; // When it stops, update the time |
rosienej | 0:1e4b66122368 | 26 | _Busy = false; |
rosienej | 0:1e4b66122368 | 27 | _Time = _timer.read_us()-_Time; |
rosienej | 0:1e4b66122368 | 28 | } |
rosienej | 0:1e4b66122368 | 29 | |
rosienej | 0:1e4b66122368 | 30 | void Ping::Send() |
rosienej | 0:1e4b66122368 | 31 | { |
rosienej | 0:1e4b66122368 | 32 | |
rosienej | 0:1e4b66122368 | 33 | _cmd.output(); |
rosienej | 0:1e4b66122368 | 34 | _cmd.write(0); // see the ping documentation http://www.parallax.com/Portals/0/Downloads/docs/prod/acc/28015-PING-v1.6.pdf |
rosienej | 0:1e4b66122368 | 35 | wait_us(3); |
rosienej | 0:1e4b66122368 | 36 | _cmd.write(1); |
rosienej | 0:1e4b66122368 | 37 | wait_us(3); |
rosienej | 0:1e4b66122368 | 38 | _cmd.write(0); |
rosienej | 0:1e4b66122368 | 39 | _cmd.input(); |
rosienej | 0:1e4b66122368 | 40 | |
rosienej | 0:1e4b66122368 | 41 | } |
rosienej | 0:1e4b66122368 | 42 | void Ping::Set_Speed_of_Sound(int SoS_ms ) |
rosienej | 0:1e4b66122368 | 43 | { |
rosienej | 0:1e4b66122368 | 44 | _SPEED_OF_SOUND_CM = SoS_ms; |
rosienej | 0:1e4b66122368 | 45 | } |
rosienej | 0:1e4b66122368 | 46 | |
rosienej | 0:1e4b66122368 | 47 | int Ping::Read_cm() |
rosienej | 0:1e4b66122368 | 48 | // -1 means not valid. |
rosienej | 0:1e4b66122368 | 49 | { |
rosienej | 0:1e4b66122368 | 50 | if(_Valid && ~_Busy) |
rosienej | 0:1e4b66122368 | 51 | return ((_Time*_SPEED_OF_SOUND_CM)/1000); |
rosienej | 0:1e4b66122368 | 52 | else |
rosienej | 0:1e4b66122368 | 53 | return -1; |
rosienej | 0:1e4b66122368 | 54 | } |