miseru
Dependencies: ColorSensor HMC6352 Servo TextLCD mbed
ping/ping.cpp@0:4c9a3b107572, 2013-09-12 (annotated)
- Committer:
- OGA
- Date:
- Thu Sep 12 12:10:38 2013 +0000
- Revision:
- 0:4c9a3b107572
sensei
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
OGA | 0:4c9a3b107572 | 1 | #include "mbed.h" |
OGA | 0:4c9a3b107572 | 2 | #include "ping.h" |
OGA | 0:4c9a3b107572 | 3 | |
OGA | 0:4c9a3b107572 | 4 | //DigitalOut myled = LED1; |
OGA | 0:4c9a3b107572 | 5 | |
OGA | 0:4c9a3b107572 | 6 | extern Timer timer2; |
OGA | 0:4c9a3b107572 | 7 | |
OGA | 0:4c9a3b107572 | 8 | uint16_t ultrasonicVal[ALL_ULTRASONIC]; |
OGA | 0:4c9a3b107572 | 9 | double ultrasonicValue[ALL_ULTRASONIC] = {0}; |
OGA | 0:4c9a3b107572 | 10 | |
OGA | 0:4c9a3b107572 | 11 | |
OGA | 0:4c9a3b107572 | 12 | void Ultrasonic() |
OGA | 0:4c9a3b107572 | 13 | { |
OGA | 0:4c9a3b107572 | 14 | for(int i = 0 ; i < ALL_ULTRASONIC; i++){ |
OGA | 0:4c9a3b107572 | 15 | |
OGA | 0:4c9a3b107572 | 16 | uint8_t flag = 0; |
OGA | 0:4c9a3b107572 | 17 | |
OGA | 0:4c9a3b107572 | 18 | DigitalOut PingPinOut(ultrasonic_pin[i]); |
OGA | 0:4c9a3b107572 | 19 | PingPinOut = 1; |
OGA | 0:4c9a3b107572 | 20 | wait_us(10); |
OGA | 0:4c9a3b107572 | 21 | PingPinOut = 0; |
OGA | 0:4c9a3b107572 | 22 | DigitalIn PingPin(ultrasonic_pin[i]); |
OGA | 0:4c9a3b107572 | 23 | timer2.reset(); |
OGA | 0:4c9a3b107572 | 24 | while(PingPin == 0){ |
OGA | 0:4c9a3b107572 | 25 | if(timer2.read_us() > 1500){ //1.5ms以上応答なし |
OGA | 0:4c9a3b107572 | 26 | ultrasonicValue[i] = PING_ERR; |
OGA | 0:4c9a3b107572 | 27 | flag = 1; |
OGA | 0:4c9a3b107572 | 28 | break; |
OGA | 0:4c9a3b107572 | 29 | } |
OGA | 0:4c9a3b107572 | 30 | } |
OGA | 0:4c9a3b107572 | 31 | |
OGA | 0:4c9a3b107572 | 32 | timer2.reset(); |
OGA | 0:4c9a3b107572 | 33 | while(PingPin == 1){ |
OGA | 0:4c9a3b107572 | 34 | if((timer2.read_us() > 18500) || (flag == 1)){ //18.5ms以上のパルス |
OGA | 0:4c9a3b107572 | 35 | ultrasonicValue[i] = PING_ERR; |
OGA | 0:4c9a3b107572 | 36 | flag = 1; |
OGA | 0:4c9a3b107572 | 37 | break; |
OGA | 0:4c9a3b107572 | 38 | } |
OGA | 0:4c9a3b107572 | 39 | } |
OGA | 0:4c9a3b107572 | 40 | |
OGA | 0:4c9a3b107572 | 41 | if(flag == 0){ |
OGA | 0:4c9a3b107572 | 42 | ultrasonicValue[i] = timer2.read_us() / 1000000.0 / 2.0 * 340.0 * 1000.0; //mm MAX:3145 |
OGA | 0:4c9a3b107572 | 43 | ultrasonicVal[i] = (int)(ultrasonicValue[i] * 10.0); |
OGA | 0:4c9a3b107572 | 44 | }else{ |
OGA | 0:4c9a3b107572 | 45 | ultrasonicVal[i] = PING_ERR; |
OGA | 0:4c9a3b107572 | 46 | } |
OGA | 0:4c9a3b107572 | 47 | |
OGA | 0:4c9a3b107572 | 48 | } |
OGA | 0:4c9a3b107572 | 49 | } |