Interface library for the Devantech SRF02/SRF08 ultrasonic i2c rangers. Depends on I2cRtosDriver lib!
Srf0208IFTest02.h@3:70c946ba29cc, 2013-06-01 (annotated)
- Committer:
- humlet
- Date:
- Sat Jun 01 07:40:00 2013 +0000
- Revision:
- 3:70c946ba29cc
commitNpublish
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
humlet | 3:70c946ba29cc | 1 | #include "Srf0208IF.h" |
humlet | 3:70c946ba29cc | 2 | #include "I2CMasterRtos.h" |
humlet | 3:70c946ba29cc | 3 | #include "Serial.h" |
humlet | 3:70c946ba29cc | 4 | #include "us_ticker_api.h" |
humlet | 3:70c946ba29cc | 5 | #include "Thread.h" |
humlet | 3:70c946ba29cc | 6 | #include "math.h" |
humlet | 3:70c946ba29cc | 7 | |
humlet | 3:70c946ba29cc | 8 | using namespace mbed; |
humlet | 3:70c946ba29cc | 9 | using namespace rtos; |
humlet | 3:70c946ba29cc | 10 | |
humlet | 3:70c946ba29cc | 11 | I2CMasterRtos i2c(p28, p27,100000); |
humlet | 3:70c946ba29cc | 12 | |
humlet | 3:70c946ba29cc | 13 | Srf08IF srf08(0xe0,i2c); |
humlet | 3:70c946ba29cc | 14 | Srf02IF srf02(0xe6,i2c); |
humlet | 3:70c946ba29cc | 15 | |
humlet | 3:70c946ba29cc | 16 | int doit() |
humlet | 3:70c946ba29cc | 17 | { |
humlet | 3:70c946ba29cc | 18 | Thread::wait(1000); |
humlet | 3:70c946ba29cc | 19 | printf("\n\n\n########### STARTING ###############\n\n\n"); |
humlet | 3:70c946ba29cc | 20 | Thread::wait(1000); |
humlet | 3:70c946ba29cc | 21 | |
humlet | 3:70c946ba29cc | 22 | if(0) { // reset i3c addresses |
humlet | 3:70c946ba29cc | 23 | Srf02IF srf(0xe0,i2c); |
humlet | 3:70c946ba29cc | 24 | srf.resetI2CAdress(0xe6); |
humlet | 3:70c946ba29cc | 25 | return 0; |
humlet | 3:70c946ba29cc | 26 | } |
humlet | 3:70c946ba29cc | 27 | |
humlet | 3:70c946ba29cc | 28 | |
humlet | 3:70c946ba29cc | 29 | while(1) { |
humlet | 3:70c946ba29cc | 30 | int t0 = us_ticker_read(); |
humlet | 3:70c946ba29cc | 31 | srf02.triggerEchoMeasurement(); |
humlet | 3:70c946ba29cc | 32 | int t1 = us_ticker_read(); |
humlet | 3:70c946ba29cc | 33 | srf08.triggerRanging(); |
humlet | 3:70c946ba29cc | 34 | //srf08.triggerANNRanging(); |
humlet | 3:70c946ba29cc | 35 | int t2 = us_ticker_read(); |
humlet | 3:70c946ba29cc | 36 | |
humlet | 3:70c946ba29cc | 37 | Thread::wait(100); |
humlet | 3:70c946ba29cc | 38 | //while(!(srf08.rangingCompleted() && srf02.rangingCompleted())) printf("."); |
humlet | 3:70c946ba29cc | 39 | //while(!(srf02.rangingCompleted())) printf("."); |
humlet | 3:70c946ba29cc | 40 | //printf("\n"); |
humlet | 3:70c946ba29cc | 41 | |
humlet | 3:70c946ba29cc | 42 | int d02=srf02.readDistance_mm(); |
humlet | 3:70c946ba29cc | 43 | int d08=srf08.readDistance_mm(); |
humlet | 3:70c946ba29cc | 44 | srf08.burstRead(); |
humlet | 3:70c946ba29cc | 45 | |
humlet | 3:70c946ba29cc | 46 | |
humlet | 3:70c946ba29cc | 47 | int l08=srf08.getBrightnessFromRawBuffer(); |
humlet | 3:70c946ba29cc | 48 | const int echos = 5; |
humlet | 3:70c946ba29cc | 49 | printf("l=%d d08=%d d02=%d ",l08,d08,d02); |
humlet | 3:70c946ba29cc | 50 | for(int i=0; i<echos; ++i) printf("D%d=%d ",i,srf08.getDistanceFromRawBuffer_mm(i)); |
humlet | 3:70c946ba29cc | 51 | //printf("\n"); |
humlet | 3:70c946ba29cc | 52 | |
humlet | 3:70c946ba29cc | 53 | int t02a = srf02.readTransitTime_us(); |
humlet | 3:70c946ba29cc | 54 | int t08 = srf08.readTransitTime_us(); |
humlet | 3:70c946ba29cc | 55 | int ok = srf02.triggerRanging(); |
humlet | 3:70c946ba29cc | 56 | Thread::wait(100); |
humlet | 3:70c946ba29cc | 57 | int t02b = srf02.readTransitTime_us(); |
humlet | 3:70c946ba29cc | 58 | int dmin = srf02.readMinimalDistance_mm(); |
humlet | 3:70c946ba29cc | 59 | int t02c = t02a-(t2-t1); |
humlet | 3:70c946ba29cc | 60 | printf(" dt08m=%d d02m=%d d02c=%d dt02mc=%d ds0208=%d dmin=%d\n", |
humlet | 3:70c946ba29cc | 61 | Srf0208IF::time2dist_us2mm(t08), |
humlet | 3:70c946ba29cc | 62 | Srf0208IF::time2dist_us2mm(t02b), |
humlet | 3:70c946ba29cc | 63 | Srf0208IF::time2dist_us2mm(t02c)-130, |
humlet | 3:70c946ba29cc | 64 | Srf0208IF::time2dist_us2mm(t02b-t02c), |
humlet | 3:70c946ba29cc | 65 | Srf0208IF::time2dist_us2mm(t08-t02b), |
humlet | 3:70c946ba29cc | 66 | dmin); |
humlet | 3:70c946ba29cc | 67 | Thread::wait(100); |
humlet | 3:70c946ba29cc | 68 | /* |
humlet | 3:70c946ba29cc | 69 | srf08.triggerANNRanging(); |
humlet | 3:70c946ba29cc | 70 | Thread::wait(100); |
humlet | 3:70c946ba29cc | 71 | srf08.burstRead(); |
humlet | 3:70c946ba29cc | 72 | printf("d=%d ",srf08.getDistanceFromRawBuffer_mm(0)); |
humlet | 3:70c946ba29cc | 73 | const int slots=32; |
humlet | 3:70c946ba29cc | 74 | for(int i=0; i<slots; ++i) printf("%d ",srf08.getANNSlotDataFromRawBuffer(i)); |
humlet | 3:70c946ba29cc | 75 | printf("\n"); |
humlet | 3:70c946ba29cc | 76 | */ |
humlet | 3:70c946ba29cc | 77 | Thread::wait(100); |
humlet | 3:70c946ba29cc | 78 | } |
humlet | 3:70c946ba29cc | 79 | } |