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