Interface library for the Devantech SRF02/SRF08 ultrasonic i2c rangers. Depends on I2cRtosDriver lib!

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Srf0208IF.h Source File

Srf0208IF.h

00001 #ifndef SRF0208IF_H
00002 #define SRF0208IF_H
00003 #include "stdint.h"
00004 
00005 namespace mbed
00006 {
00007 
00008 class I2CMasterRtos;
00009 
00010 /// common base interface class for SRF02 and SRF08 utrasonic rangers
00011 class Srf0208IF
00012 {
00013 protected:
00014     int m_adr;
00015     I2CMasterRtos& m_i2c;
00016  
00017 public:
00018     
00019     /// little helper that translates two raw bytes into a word
00020     static int twoByte2int(const char* data){
00021         return ((static_cast<int>(*(data)))<<8) | static_cast<int>(*(data+1));
00022     }
00023     /// little helper that translates the us transit time into a distance in mm
00024     static int time2dist_us2mm(int us){
00025         return us!=-1 ? (us*sonicSpeed)>>11 : -1;  
00026     }
00027 
00028 
00029     /// sonic speed in m/s @ 21C  ...  c=(331.5+0.6*T/C)m/s
00030     /// To calculate the distance in mm: Multiply the us measurement with this constant and divide by 2
00031     static const int sonicSpeed = 344;
00032 
00033     /// The constructor
00034     Srf0208IF(int adr, I2CMasterRtos& i2c): m_adr(adr), m_i2c(i2c){};
00035 
00036     /// Trigger ranging. Result can be read back after 66ms via triggerEchoMeasurement().
00037     /// @return true on success
00038     bool triggerRanging();
00039     
00040     /// Read back measured transit time in us of the last triggerRanging() or triggerEchoMeasurement() request.
00041     /// Depending on the distance the Measurement takes up to 66ms.
00042     /// @return -1 on error
00043     int readTransitTime_us();
00044 
00045     /// Read back measured distance in mm of the triggerRanging() or triggerEchoMeasurement() request.
00046     /// Depending on the distance the Measurement takes up to 66ms.
00047     /// @return -1 on error
00048     int readDistance_mm() {
00049         return time2dist_us2mm(readTransitTime_us());
00050     };
00051 
00052     /// Assign a new adress to the SRF ranger device. Available 8bit addresses: E0, E2, E4 ... FE. 
00053     /// Ensure that only one SRF device is connected to the bus
00054     void resetI2CAdress(int newAddress);
00055 
00056     /// Reads the SRF's SW revision.
00057     /// @return -1 on error
00058     int readSWRevision();
00059 
00060     /// Returns true if ranging is done.
00061     bool rangingCompleted(){
00062         return readSWRevision()!=-1;
00063     }
00064 };
00065 
00066 /// Provides an interface to special features of the SRF02
00067 class Srf02IF : public Srf0208IF
00068 {
00069 public:
00070 
00071     /// The constructor
00072     Srf02IF(int adr, I2CMasterRtos& i2c):Srf0208IF(adr,i2c){};
00073 
00074     /// Just send a ping.
00075     /// @return true on success
00076     bool triggerPing();
00077 
00078     /// Just wait for an echo and measure the time.
00079     /// Result can be read back after 66ms via triggerEchoMeasurement().
00080     /// @return true on success
00081     bool triggerEchoMeasurement();
00082 
00083     /// Read current minimum range from srf02.
00084     int readMinimalTransitTime_us();
00085     
00086     /// Read current minimum range from srf02.
00087     int readMinimalDistance_mm(){
00088         return time2dist_us2mm(readMinimalTransitTime_us());
00089     }
00090 
00091 };
00092 
00093 /// Provides special features of the SRF08
00094 class Srf08IF : public Srf0208IF
00095 {
00096     char m_rawBuffer[36];
00097 
00098 public:
00099 
00100     /// The constructor
00101     Srf08IF(int adr, I2CMasterRtos& i2c):Srf0208IF(adr,i2c){};
00102 
00103     /// read light sensor measurement.
00104     /// @return -1 on error
00105     int readBrightness();
00106 
00107     /// trigger ANN mode ranging
00108     /// @return true on success
00109     bool triggerANNRanging();
00110 
00111     /// Set maximum distance. Use to reduce measurement time.
00112     /// @param maxDst maximum distance 0..0xff  (1+maxDst)*43mm  =>  43mm..11m / 3ms..64ms.
00113     /// @return true on success
00114     bool writeMaxDist(int maxDst);
00115 
00116     /// Set/Limit maximum gain. Use to suppress roving echos from previous measurements.
00117     /// Necessary at mesurement rates higher than 1/66ms=15Hz.
00118     /// During measurement the analog signal amplification is increased every 70us until
00119     /// the maximum gain is reached after 31 steps at ~390mm.
00120     /// @param maxGain 0..31 / 0x0..0x1f  =>magic non linear function=> 94..1025 (see SRF08 docu)
00121     /// @return true on success
00122     bool writeMaxGain(int maxGain);
00123 
00124     /// Get transit time (us) of n'th echo from internal buffer.
00125     /// Call burstRead() before!
00126     /// @param echoIdx 0..16
00127     int getTransitTimeFromRawBuffer_us(int echoIdx) const {
00128         return twoByte2int(&(m_rawBuffer[(++echoIdx)<<1])); // (1+idx)*2
00129     }
00130     
00131     /// Get distanc (mm) of n'th echo from internal buffer.
00132     /// Call burstRead() before!
00133     /// @param echoIdx 0..16
00134     int getDistanceFromRawBuffer_mm(int echoIdx) const {
00135         return time2dist_us2mm(getTransitTimeFromRawBuffer_us(echoIdx));
00136     }
00137     
00138     /// Get light sensor data from internal buffer.
00139     /// Call burstRead() before!
00140     int getBrightnessFromRawBuffer() const {
00141         return static_cast<int>(m_rawBuffer[1]);
00142     }
00143     /// Get ANN data from internal buffer
00144     /// Call burstRead() before!
00145     /// Each slot is 2048us/353mm wide.
00146     /// @param slotIdx slot 0..31
00147     int getANNSlotDataFromRawBuffer(int slotIdx) const {
00148         return (static_cast<int>(m_rawBuffer[slotIdx+4]));
00149     }
00150     /// Reads all data from start to stop register in one go and stores it in a class internal buffer. See getXXXXXFromRawBuffer_us() functions.
00151     /// - reg 0x00: SW Revision
00152     /// - reg 0x01: light sensor (SRF08 only)
00153     /// - reg 0x02+0x03: range high+low byte
00154     /// - reg 0x03+0x04: srf02: min range high+low byte  srf08: 2nd echo
00155     /// - reg 0x04+0x05 .. 34+35: 3rd..17th echo high+low byte
00156     /// @return true on success
00157     bool burstRead(int startReg=1, int stopReg=35);
00158 };
00159 
00160 }
00161 #endif
00162