test

Dependencies:   HMC6352 PID mbed

Fork of ver1_2_2 by ryo seki

Committer:
akudohune
Date:
Sun Mar 10 07:31:31 2013 +0000
Revision:
1:89408fff7cc9
Parent:
0:74bf4953c0d1
new_cup;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
akudohune 0:74bf4953c0d1 1
akudohune 0:74bf4953c0d1 2 #include "mbed.h"
akudohune 0:74bf4953c0d1 3 #include "ultrasonic.h"
akudohune 0:74bf4953c0d1 4
akudohune 0:74bf4953c0d1 5
akudohune 0:74bf4953c0d1 6 extern Timer timer2;
akudohune 0:74bf4953c0d1 7 extern Serial pc; // tx, rx
akudohune 0:74bf4953c0d1 8
akudohune 1:89408fff7cc9 9 double ultrasonicVal[ALL_ULTRASONIC] = {0};
akudohune 0:74bf4953c0d1 10
akudohune 0:74bf4953c0d1 11
akudohune 0:74bf4953c0d1 12 void Ultrasonic()
akudohune 0:74bf4953c0d1 13 {
akudohune 0:74bf4953c0d1 14 for(uint8_t i = 0 ; i < ALL_ULTRASONIC ; i++){
akudohune 0:74bf4953c0d1 15 uint8_t flag = 0;
akudohune 0:74bf4953c0d1 16
akudohune 0:74bf4953c0d1 17 DigitalOut PingPinOut(ultrasonic_pin[i]);
akudohune 0:74bf4953c0d1 18 PingPinOut = 1;
akudohune 0:74bf4953c0d1 19 wait_us(10);
akudohune 0:74bf4953c0d1 20 PingPinOut = 0;
akudohune 0:74bf4953c0d1 21 DigitalIn PingPin(ultrasonic_pin[i]);
akudohune 0:74bf4953c0d1 22 timer2.reset();
akudohune 0:74bf4953c0d1 23 while(PingPin == 0){
akudohune 0:74bf4953c0d1 24 if(timer2.read_us() > 1000){ //1ms以上応答なし
akudohune 0:74bf4953c0d1 25 ultrasonicVal[i] = PING_ERR;
akudohune 0:74bf4953c0d1 26 flag = 1;
akudohune 0:74bf4953c0d1 27 break;
akudohune 0:74bf4953c0d1 28 }
akudohune 0:74bf4953c0d1 29 }
akudohune 0:74bf4953c0d1 30 timer2.reset();
akudohune 0:74bf4953c0d1 31 while(PingPin == 1){
akudohune 0:74bf4953c0d1 32 if((timer2.read_us() > 18500) || (flag == 1)){ //18.5ms以上のパルス
akudohune 0:74bf4953c0d1 33 ultrasonicVal[i] = PING_ERR;
akudohune 0:74bf4953c0d1 34 flag = 1;
akudohune 0:74bf4953c0d1 35 break;
akudohune 0:74bf4953c0d1 36 }
akudohune 0:74bf4953c0d1 37 }
akudohune 0:74bf4953c0d1 38 if(flag == 0){
akudohune 1:89408fff7cc9 39 ultrasonicVal[i] = timer2.read_us() / 1000000.0 / 2.0 * 340.0 * 1000.0; //cm
akudohune 0:74bf4953c0d1 40 }
akudohune 0:74bf4953c0d1 41 }
akudohune 1:89408fff7cc9 42 //pc.printf("%f\n",ultrasonicVal[0] + ultrasonicVal[2]);
akudohune 1:89408fff7cc9 43 //pc.printf("compass.sample = %f\n",compass.sample() / 1.0);
akudohune 0:74bf4953c0d1 44
akudohune 0:74bf4953c0d1 45 }