Interface library for the Devantech SRF02/SRF08 ultrasonic i2c rangers. Depends on I2cRtosDriver lib!
Revision 3:70c946ba29cc, committed 2013-06-01
- Comitter:
- humlet
- Date:
- Sat Jun 01 07:40:00 2013 +0000
- Parent:
- 2:dfc8b09b4e3b
- Commit message:
- commitNpublish
Changed in this revision
--- a/Srf0208IF.cpp Sun May 26 20:52:27 2013 +0000 +++ b/Srf0208IF.cpp Sat Jun 01 07:40:00 2013 +0000 @@ -2,10 +2,8 @@ #include "Thread.h" #include "I2CMasterRtos.h" -#include "mbed.h" - using namespace mbed; -using namespace rtos; +using namespace rtos; bool Srf0208IF::triggerRanging() { @@ -16,7 +14,6 @@ { char result[2]; bool ok = !m_i2c.read(m_adr, 0x02, result, 2); - printf("%d %d \n",result[0],result[1]); return ok ? twoByte2int(result) : -1; } void Srf0208IF::resetI2CAdress(int newAddress)
--- a/Srf0208IF.h Sun May 26 20:52:27 2013 +0000 +++ b/Srf0208IF.h Sat Jun 01 07:40:00 2013 +0000 @@ -18,26 +18,26 @@ /// little helper that translates two raw bytes into a word static int twoByte2int(const char* data){ - return ((static_cast<uint16_t>(*(data)))<<8) | static_cast<uint16_t>(*(data+1)); + return ((static_cast<int>(*(data)))<<8) | static_cast<int>(*(data+1)); } /// little helper that translates the us transit time into a distance in mm static int time2dist_us2mm(int us){ - return us!=-1 ? (us*sonicSpeed)>>1 : us; + return us!=-1 ? (us*sonicSpeed)>>11 : -1; } - /// sonic speed in m/s @ 21°C ... c=(331.5+0.6*T/°C)m/s - /// To calculate the distance in mm: Multiply the µs measurement with this constant and divide by 2 + /// sonic speed in m/s @ 21C ... c=(331.5+0.6*T/C)m/s + /// To calculate the distance in mm: Multiply the us measurement with this constant and divide by 2 static const int sonicSpeed = 344; /// The constructor Srf0208IF(int adr, I2CMasterRtos& i2c): m_adr(adr), m_i2c(i2c){}; - /// Trigger ranging. Result can be read back after 66ms via triggerEchoMeasurement() + /// Trigger ranging. Result can be read back after 66ms via triggerEchoMeasurement(). /// @return true on success bool triggerRanging(); - /// Read back measured transit time in µs of the last triggerRanging() or triggerEchoMeasurement() request. + /// Read back measured transit time in us of the last triggerRanging() or triggerEchoMeasurement() request. /// Depending on the distance the Measurement takes up to 66ms. /// @return -1 on error int readTransitTime_us(); @@ -49,21 +49,21 @@ return time2dist_us2mm(readTransitTime_us()); }; - /// Assign a new adress to the SRF ranger device. Available 8bit addresses: E0, E2, E4 ... FE + /// Assign a new adress to the SRF ranger device. Available 8bit addresses: E0, E2, E4 ... FE. /// Ensure that only one SRF device is connected to the bus void resetI2CAdress(int newAddress); - /// Reads the SRF's SW revision + /// Reads the SRF's SW revision. /// @return -1 on error int readSWRevision(); - /// Returns true if ranging is done + /// Returns true if ranging is done. bool rangingCompleted(){ return readSWRevision()!=-1; } }; -/// Provides special features of the SRF02 +/// Provides an interface to special features of the SRF02 class Srf02IF : public Srf0208IF { public: @@ -71,19 +71,19 @@ /// The constructor Srf02IF(int adr, I2CMasterRtos& i2c):Srf0208IF(adr,i2c){}; - /// Just send a ping + /// Just send a ping. /// @return true on success bool triggerPing(); - /// Just wait for an echo and measure the time - /// Result can be read back after 66ms via triggerEchoMeasurement() + /// Just wait for an echo and measure the time. + /// Result can be read back after 66ms via triggerEchoMeasurement(). /// @return true on success bool triggerEchoMeasurement(); - /// Read current minimum range from srf02 + /// Read current minimum range from srf02. int readMinimalTransitTime_us(); - /// Read current minimum range from srf02 + /// Read current minimum range from srf02. int readMinimalDistance_mm(){ return time2dist_us2mm(readMinimalTransitTime_us()); } @@ -100,7 +100,7 @@ /// The constructor Srf08IF(int adr, I2CMasterRtos& i2c):Srf0208IF(adr,i2c){}; - /// read light sensor measurement + /// read light sensor measurement. /// @return -1 on error int readBrightness(); @@ -109,19 +109,19 @@ bool triggerANNRanging(); /// Set maximum distance. Use to reduce measurement time. - /// @param maxDst maximum distance 0..0xff (1+maxDst)*43mm => 43mm..11m / 3ms..64ms + /// @param maxDst maximum distance 0..0xff (1+maxDst)*43mm => 43mm..11m / 3ms..64ms. /// @return true on success bool writeMaxDist(int maxDst); /// Set/Limit maximum gain. Use to suppress roving echos from previous measurements. /// Necessary at mesurement rates higher than 1/66ms=15Hz. - /// During measurement the analog signal amplification is increased every 70µs until + /// During measurement the analog signal amplification is increased every 70us until /// the maximum gain is reached after 31 steps at ~390mm. /// @param maxGain 0..31 / 0x0..0x1f =>magic non linear function=> 94..1025 (see SRF08 docu) /// @return true on success bool writeMaxGain(int maxGain); - /// Get transit time (µs) of n'th echo from internal buffer. + /// Get transit time (us) of n'th echo from internal buffer. /// Call burstRead() before! /// @param echoIdx 0..16 int getTransitTimeFromRawBuffer_us(int echoIdx) const { @@ -142,12 +142,12 @@ } /// Get ANN data from internal buffer /// Call burstRead() before! - /// Each slot is 2048µs/353mm wide. + /// Each slot is 2048us/353mm wide. /// @param slotIdx slot 0..31 int getANNSlotDataFromRawBuffer(int slotIdx) const { return (static_cast<int>(m_rawBuffer[slotIdx+4])); } - /// Reads all data from start to stop register in one go and store it in class internal buffer. + /// Reads all data from start to stop register in one go and stores it in a class internal buffer. See getXXXXXFromRawBuffer_us() functions. /// - reg 0x00: SW Revision /// - reg 0x01: light sensor (SRF08 only) /// - reg 0x02+0x03: range high+low byte
--- a/Srf0208IFTest01.h Sun May 26 20:52:27 2013 +0000 +++ b/Srf0208IFTest01.h Sat Jun 01 07:40:00 2013 +0000 @@ -1,3 +1,6 @@ +// attempt to realize a triangulation using one srf08 (sender) and two srf02 (additional receivers) +// Does not work yet ... doubt that it will ever work + #include "Srf0208IF.h" #include "I2CMasterRtos.h" #include "Serial.h" @@ -10,7 +13,7 @@ Serial pc(USBTX, USBRX); -I2CMasterRtos i2c(p28, p27,400000); +I2CMasterRtos i2c(p28, p27,100000); Srf08IF mid(0xe0,i2c); Srf02IF right(0xe2,i2c);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Srf0208IFTest02.h Sat Jun 01 07:40:00 2013 +0000 @@ -0,0 +1,79 @@ +#include "Srf0208IF.h" +#include "I2CMasterRtos.h" +#include "Serial.h" +#include "us_ticker_api.h" +#include "Thread.h" +#include "math.h" + +using namespace mbed; +using namespace rtos; + +I2CMasterRtos i2c(p28, p27,100000); + +Srf08IF srf08(0xe0,i2c); +Srf02IF srf02(0xe6,i2c); + +int doit() +{ + Thread::wait(1000); + printf("\n\n\n########### STARTING ###############\n\n\n"); + Thread::wait(1000); + + if(0) { // reset i3c addresses + Srf02IF srf(0xe0,i2c); + srf.resetI2CAdress(0xe6); + return 0; + } + + + while(1) { + int t0 = us_ticker_read(); + srf02.triggerEchoMeasurement(); + int t1 = us_ticker_read(); + srf08.triggerRanging(); + //srf08.triggerANNRanging(); + int t2 = us_ticker_read(); + + Thread::wait(100); + //while(!(srf08.rangingCompleted() && srf02.rangingCompleted())) printf("."); + //while(!(srf02.rangingCompleted())) printf("."); + //printf("\n"); + + int d02=srf02.readDistance_mm(); + int d08=srf08.readDistance_mm(); + srf08.burstRead(); + + + int l08=srf08.getBrightnessFromRawBuffer(); + const int echos = 5; + printf("l=%d d08=%d d02=%d ",l08,d08,d02); + for(int i=0; i<echos; ++i) printf("D%d=%d ",i,srf08.getDistanceFromRawBuffer_mm(i)); + //printf("\n"); + + int t02a = srf02.readTransitTime_us(); + int t08 = srf08.readTransitTime_us(); + int ok = srf02.triggerRanging(); + Thread::wait(100); + int t02b = srf02.readTransitTime_us(); + int dmin = srf02.readMinimalDistance_mm(); + int t02c = t02a-(t2-t1); + printf(" dt08m=%d d02m=%d d02c=%d dt02mc=%d ds0208=%d dmin=%d\n", + Srf0208IF::time2dist_us2mm(t08), + Srf0208IF::time2dist_us2mm(t02b), + Srf0208IF::time2dist_us2mm(t02c)-130, + Srf0208IF::time2dist_us2mm(t02b-t02c), + Srf0208IF::time2dist_us2mm(t08-t02b), + dmin); + Thread::wait(100); + /* + srf08.triggerANNRanging(); + Thread::wait(100); + srf08.burstRead(); + printf("d=%d ",srf08.getDistanceFromRawBuffer_mm(0)); + const int slots=32; + for(int i=0; i<slots; ++i) printf("%d ",srf08.getANNSlotDataFromRawBuffer(i)); + printf("\n"); + */ + Thread::wait(100); + } +} \ No newline at end of file