Ejemplo n.º 1
0
    def __init__(self, address=0x77, mode=3):
        self.i2c = PyComms(address)
        self.address = address

        # Make sure the specified mode is in the appropriate range
        if ((mode < 0) | (mode > 3)):
            self.mode = self.__BMP085_STANDARD
        else:
            self.mode = mode

        # Read the calibration data
        self.readCalibrationData()
Ejemplo n.º 2
0
    def __init__(self, address=0x77, mode=3):
        self.i2c = PyComms(address)
        self.address = address

        # Make sure the specified mode is in the appropriate range
        if (mode < 0) | (mode > 3):
            self.mode = self.__BMP085_STANDARD
        else:
            self.mode = mode

        # Read the calibration data
        self.readCalibrationData()
Ejemplo n.º 3
0
 def __init__(self, address=HMC5883L_DEFAULT_ADDRESS):
     self.i2c = PyComms(address)
     self.address = address
Ejemplo n.º 4
0
class HMC5883L:
    # Register map based on Jeff Rowberg <*****@*****.**> source code at
    # https://github.com/jrowberg/i2cdevlib/

    HMC5883L_ADDRESS = 0x1E  # this device only has one address
    HMC5883L_DEFAULT_ADDRESS = 0x1E

    HMC5883L_RA_CONFIG_A = 0x00
    HMC5883L_RA_CONFIG_B = 0x01
    HMC5883L_RA_MODE = 0x02
    HMC5883L_RA_DATAX_H = 0x03
    HMC5883L_RA_DATAX_L = 0x04
    HMC5883L_RA_DATAZ_H = 0x05
    HMC5883L_RA_DATAZ_L = 0x06
    HMC5883L_RA_DATAY_H = 0x07
    HMC5883L_RA_DATAY_L = 0x08
    HMC5883L_RA_STATUS = 0x09
    HMC5883L_RA_ID_A = 0x0A
    HMC5883L_RA_ID_B = 0x0B
    HMC5883L_RA_ID_C = 0x0C

    HMC5883L_CRA_AVERAGE_BIT = 6
    HMC5883L_CRA_AVERAGE_LENGTH = 2
    HMC5883L_CRA_RATE_BIT = 4
    HMC5883L_CRA_RATE_LENGTH = 3
    HMC5883L_CRA_BIAS_BIT = 1
    HMC5883L_CRA_BIAS_LENGTH = 2

    HMC5883L_AVERAGING_1 = 0x00
    HMC5883L_AVERAGING_2 = 0x01
    HMC5883L_AVERAGING_4 = 0x02
    HMC5883L_AVERAGING_8 = 0x03

    HMC5883L_RATE_0P75 = 0x00
    HMC5883L_RATE_1P5 = 0x01
    HMC5883L_RATE_3 = 0x02
    HMC5883L_RATE_7P5 = 0x03
    HMC5883L_RATE_15 = 0x04
    HMC5883L_RATE_30 = 0x05
    HMC5883L_RATE_75 = 0x06

    HMC5883L_BIAS_NORMAL = 0x00
    HMC5883L_BIAS_POSITIVE = 0x01
    HMC5883L_BIAS_NEGATIVE = 0x02

    HMC5883L_CRB_GAIN_BIT = 7
    HMC5883L_CRB_GAIN_LENGTH = 3

    HMC5883L_GAIN_1370 = 0x00
    HMC5883L_GAIN_1090 = 0x01
    HMC5883L_GAIN_820 = 0x02
    HMC5883L_GAIN_660 = 0x03
    HMC5883L_GAIN_440 = 0x04
    HMC5883L_GAIN_390 = 0x05
    HMC5883L_GAIN_330 = 0x06
    HMC5883L_GAIN_220 = 0x07

    HMC5883L_MODEREG_BIT = 1
    HMC5883L_MODEREG_LENGTH = 2

    HMC5883L_MODE_CONTINUOUS = 0x00
    HMC5883L_MODE_SINGLE = 0x01
    HMC5883L_MODE_IDLE = 0x02

    HMC5883L_STATUS_LOCK_BIT = 1
    HMC5883L_STATUS_READY_BIT = 0

    mode = 0

    def __init__(self, address=HMC5883L_DEFAULT_ADDRESS):
        self.i2c = PyComms(address)
        self.address = address

    def initialize(self):
        # write CONFIG_A register
        self.i2c.write8(
            self.HMC5883L_RA_CONFIG_A,
            (self.HMC5883L_AVERAGING_8 <<
             (self.HMC5883L_CRA_AVERAGE_BIT -
              self.HMC5883L_CRA_AVERAGE_LENGTH + 1)) |
            (self.HMC5883L_RATE_15 <<
             (self.HMC5883L_CRA_RATE_BIT - self.HMC5883L_CRA_RATE_LENGTH + 1))
            |
            (self.HMC5883L_BIAS_NORMAL <<
             (self.HMC5883L_CRA_BIAS_BIT - self.HMC5883L_CRA_BIAS_LENGTH + 1)))

        # write CONFIG_B register
        self.setGain(self.HMC5883L_GAIN_1090)

        # write MODE register
        self.setMode(self.HMC5883L_MODE_SINGLE)

    def testConnection(self):
        pass

    def getSampleAveraging(self):
        pass

    def setSampleAveraging(self, value):
        self.i2c.writeBits(self.HMC5883L_RA_CONFIG_A,
                           self.HMC5883L_CRA_AVERAGE_BIT,
                           self.HMC5883L_CRA_AVERAGE_LENGTH, value)

    def getDataRate(self):
        pass

    def setDataRate(self, value):
        self.i2c.writeBits(self.HMC5883L_RA_CONFIG_A,
                           self.HMC5883L_CRA_RATE_BIT,
                           self.HMC5883L_CRA_RATE_LENGTH, value)

    def getMeasurementBias(self):
        pass

    def setMeasurementBias(self, value):
        self.i2c.writeBits(self.HMC5883L_RA_CONFIG_A,
                           self.HMC5883L_CRA_BIAS_BIT,
                           self.HMC5883L_CRA_BIAS_LENGTH, value)

    def getGain(self):
        pass

    def setGain(self, value):
        self.i2c.write8(
            self.HMC5883L_RA_CONFIG_B, value <<
            (self.HMC5883L_CRB_GAIN_BIT - self.HMC5883L_CRB_GAIN_LENGTH + 1))

    def getMode(self):
        pass

    def setMode(self, newMode):
        # use this method to guarantee that bits 7-2 are set to zero, which is a
        # requirement specified in the datasheet
        self.i2c.write8(
            self.HMC5883L_RA_MODE, self.mode <<
            (self.HMC5883L_MODEREG_BIT - self.HMC5883L_MODEREG_LENGTH + 1))
        self.mode = newMode  # track to tell if we have to clear bit 7 after a read

    def getHeading(self):
        packet = self.i2c.readBytesListS(self.HMC5883L_RA_DATAX_H, 6)
        if (self.mode == self.HMC5883L_MODE_SINGLE):
            self.i2c.write8(
                self.HMC5883L_RA_MODE, self.HMC5883L_MODE_SINGLE <<
                (self.HMC5883L_MODEREG_BIT - self.HMC5883L_MODEREG_LENGTH + 1))

        data = {
            'x': ((packet[0] << 8) | packet[1]),
            'y': ((packet[4] << 8) | packet[5]),
            'z': ((packet[2] << 8) | packet[3])
        }

        return data

    def getHeadingX(self):
        # each axis read requires that ALL axis registers be read, even if only
        # one is used; this was not done ineffiently in the code by accident
        packet = self.i2c.readBytesListS(self.HMC5883L_RA_DATAX_H, 6)
        if (self.mode == self.HMC5883L_MODE_SINGLE):
            self.i2c.write8(
                self.HMC5883L_RA_MODE, self.HMC5883L_MODE_SINGLE <<
                (self.HMC5883L_MODEREG_BIT - self.HMC5883L_MODEREG_LENGTH + 1))

        return ((packet[0] << 8) | packet[1])

    def getHeadingY(self):
        # each axis read requires that ALL axis registers be read, even if only
        # one is used; this was not done ineffiently in the code by accident
        packet = self.i2c.readBytesListS(self.HMC5883L_RA_DATAX_H, 6)
        if (self.mode == self.HMC5883L_MODE_SINGLE):
            self.i2c.write8(
                self.HMC5883L_RA_MODE, self.HMC5883L_MODE_SINGLE <<
                (self.HMC5883L_MODEREG_BIT - self.HMC5883L_MODEREG_LENGTH + 1))

        return ((packet[4] << 8) | packet[5])

    def getHeadingZ(self):
        # each axis read requires that ALL axis registers be read, even if only
        # one is used; this was not done ineffiently in the code by accident
        packet = self.i2c.readBytesListS(self.HMC5883L_RA_DATAX_H, 6)
        if (self.mode == self.HMC5883L_MODE_SINGLE):
            self.i2c.write8(
                self.HMC5883L_RA_MODE, self.HMC5883L_MODE_SINGLE <<
                (self.HMC5883L_MODEREG_BIT - self.HMC5883L_MODEREG_LENGTH + 1))

        return ((packet[2] << 8) | packet[3])

    def getLockStatus(self):
        result = self.i2c.readBit(self.HMC5883L_RA_STATUS,
                                  self.HMC5883L_STATUS_LOCK_BIT)
        return result

    def getReadyStatus(self):
        result = self.i2c.readBit(self.HMC5883L_RA_STATUS,
                                  self.HMC5883L_STATUS_READY_BIT)
        return result

    def getIDA(self):
        result = self.i2c.readByte(self.HMC5883L_RA_ID_A)
        return result

    def getIDB(self):
        result = self.i2c.readByte(self.HMC5883L_RA_ID_B)
        return result

    def getIDC(self):
        result = self.i2c.readByte(self.HMC5883L_RA_ID_C)
        return result
Ejemplo n.º 5
0
 def __init__(self, address = HMC5883L_DEFAULT_ADDRESS):
     self.i2c = PyComms(address)
     self.address = address
Ejemplo n.º 6
0
class HMC5883L:
    # Register map based on Jeff Rowberg <*****@*****.**> source code at
    # https://github.com/jrowberg/i2cdevlib/
    
    HMC5883L_ADDRESS            = 0x1E # this device only has one address
    HMC5883L_DEFAULT_ADDRESS    = 0x1E

    HMC5883L_RA_CONFIG_A        = 0x00
    HMC5883L_RA_CONFIG_B        = 0x01
    HMC5883L_RA_MODE            = 0x02
    HMC5883L_RA_DATAX_H         = 0x03
    HMC5883L_RA_DATAX_L         = 0x04
    HMC5883L_RA_DATAZ_H         = 0x05
    HMC5883L_RA_DATAZ_L         = 0x06
    HMC5883L_RA_DATAY_H         = 0x07
    HMC5883L_RA_DATAY_L         = 0x08
    HMC5883L_RA_STATUS          = 0x09
    HMC5883L_RA_ID_A            = 0x0A
    HMC5883L_RA_ID_B            = 0x0B
    HMC5883L_RA_ID_C            = 0x0C

    HMC5883L_CRA_AVERAGE_BIT    = 6
    HMC5883L_CRA_AVERAGE_LENGTH = 2
    HMC5883L_CRA_RATE_BIT       = 4
    HMC5883L_CRA_RATE_LENGTH    = 3
    HMC5883L_CRA_BIAS_BIT       = 1
    HMC5883L_CRA_BIAS_LENGTH    = 2

    HMC5883L_AVERAGING_1        = 0x00
    HMC5883L_AVERAGING_2        = 0x01
    HMC5883L_AVERAGING_4        = 0x02
    HMC5883L_AVERAGING_8        = 0x03

    HMC5883L_RATE_0P75          = 0x00
    HMC5883L_RATE_1P5           = 0x01
    HMC5883L_RATE_3             = 0x02
    HMC5883L_RATE_7P5           = 0x03
    HMC5883L_RATE_15            = 0x04
    HMC5883L_RATE_30            = 0x05
    HMC5883L_RATE_75            = 0x06

    HMC5883L_BIAS_NORMAL        = 0x00
    HMC5883L_BIAS_POSITIVE      = 0x01
    HMC5883L_BIAS_NEGATIVE      = 0x02

    HMC5883L_CRB_GAIN_BIT       = 7
    HMC5883L_CRB_GAIN_LENGTH    = 3

    HMC5883L_GAIN_1370          = 0x00
    HMC5883L_GAIN_1090          = 0x01
    HMC5883L_GAIN_820           = 0x02
    HMC5883L_GAIN_660           = 0x03
    HMC5883L_GAIN_440           = 0x04
    HMC5883L_GAIN_390           = 0x05
    HMC5883L_GAIN_330           = 0x06
    HMC5883L_GAIN_220           = 0x07

    HMC5883L_MODEREG_BIT        = 1
    HMC5883L_MODEREG_LENGTH     = 2

    HMC5883L_MODE_CONTINUOUS    = 0x00
    HMC5883L_MODE_SINGLE        = 0x01
    HMC5883L_MODE_IDLE          = 0x02

    HMC5883L_STATUS_LOCK_BIT    = 1
    HMC5883L_STATUS_READY_BIT   = 0   
    
    mode = 0

    def __init__(self, address = HMC5883L_DEFAULT_ADDRESS):
        self.i2c = PyComms(address)
        self.address = address
        
    def initialize(self):
        # write CONFIG_A register
        self.i2c.write8(self.HMC5883L_RA_CONFIG_A,
            (self.HMC5883L_AVERAGING_8 << (self.HMC5883L_CRA_AVERAGE_BIT - self.HMC5883L_CRA_AVERAGE_LENGTH + 1)) |
            (self.HMC5883L_RATE_15     << (self.HMC5883L_CRA_RATE_BIT - self.HMC5883L_CRA_RATE_LENGTH + 1)) |
            (self.HMC5883L_BIAS_NORMAL << (self.HMC5883L_CRA_BIAS_BIT - self.HMC5883L_CRA_BIAS_LENGTH + 1)))

        # write CONFIG_B register
        self.setGain(self.HMC5883L_GAIN_1090);
    
        # write MODE register
        self.setMode(self.HMC5883L_MODE_SINGLE);      
        
    def testConnection(self):
        pass
        
    def getSampleAveraging(self):
        pass
    
    def setSampleAveraging(self, value):
        self.i2c.writeBits(self.HMC5883L_RA_CONFIG_A, self.HMC5883L_CRA_AVERAGE_BIT, self.HMC5883L_CRA_AVERAGE_LENGTH, value)
        
    def getDataRate(self):
        pass
    
    def setDataRate(self, value):
        self.i2c.writeBits(self.HMC5883L_RA_CONFIG_A, self.HMC5883L_CRA_RATE_BIT, self.HMC5883L_CRA_RATE_LENGTH, value)
        
    def getMeasurementBias(self):
        pass
    
    def setMeasurementBias(self, value):
        self.i2c.writeBits(self.HMC5883L_RA_CONFIG_A, self.HMC5883L_CRA_BIAS_BIT, self.HMC5883L_CRA_BIAS_LENGTH, value);
        
    def getGain(self):
        pass
        
    def setGain(self, value):
        self.i2c.write8(self.HMC5883L_RA_CONFIG_B, value << (self.HMC5883L_CRB_GAIN_BIT - self.HMC5883L_CRB_GAIN_LENGTH + 1))
        
    def getMode(self):
        pass
    
    def setMode(self, newMode):
        # use this method to guarantee that bits 7-2 are set to zero, which is a
        # requirement specified in the datasheet
        self.i2c.write8(self.HMC5883L_RA_MODE, self.mode << (self.HMC5883L_MODEREG_BIT - self.HMC5883L_MODEREG_LENGTH + 1))
        self.mode = newMode # track to tell if we have to clear bit 7 after a read
     
    def getHeading(self):
        packet = self.i2c.readBytesListS(self.HMC5883L_RA_DATAX_H, 6)
        if (self.mode == self.HMC5883L_MODE_SINGLE):
            self.i2c.write8(self.HMC5883L_RA_MODE, self.HMC5883L_MODE_SINGLE << (self.HMC5883L_MODEREG_BIT - self.HMC5883L_MODEREG_LENGTH + 1))  
           
        data = {
            'x' : ((packet[0] << 8) | packet[1]),
            'y' : ((packet[4] << 8) | packet[5]),
            'z' : ((packet[2] << 8) | packet[3])}
            
        return data    
        
    def getHeadingX(self):
        # each axis read requires that ALL axis registers be read, even if only
        # one is used; this was not done ineffiently in the code by accident
        packet = self.i2c.readBytesListS(self.HMC5883L_RA_DATAX_H, 6)
        if (self.mode == self.HMC5883L_MODE_SINGLE):
            self.i2c.write8(self.HMC5883L_RA_MODE, self.HMC5883L_MODE_SINGLE << (self.HMC5883L_MODEREG_BIT - self.HMC5883L_MODEREG_LENGTH + 1))
            
        return ((packet[0] << 8) | packet[1])    
        
    def getHeadingY(self):
        # each axis read requires that ALL axis registers be read, even if only
        # one is used; this was not done ineffiently in the code by accident
        packet = self.i2c.readBytesListS(self.HMC5883L_RA_DATAX_H, 6)
        if (self.mode == self.HMC5883L_MODE_SINGLE):
            self.i2c.write8(self.HMC5883L_RA_MODE, self.HMC5883L_MODE_SINGLE << (self.HMC5883L_MODEREG_BIT - self.HMC5883L_MODEREG_LENGTH + 1))
            
        return ((packet[4] << 8) | packet[5])    
        
    def getHeadingZ(self):
        # each axis read requires that ALL axis registers be read, even if only
        # one is used; this was not done ineffiently in the code by accident
        packet = self.i2c.readBytesListS(self.HMC5883L_RA_DATAX_H, 6)
        if (self.mode == self.HMC5883L_MODE_SINGLE):
            self.i2c.write8(self.HMC5883L_RA_MODE, self.HMC5883L_MODE_SINGLE << (self.HMC5883L_MODEREG_BIT - self.HMC5883L_MODEREG_LENGTH + 1))
            
        return ((packet[2] << 8) | packet[3])    
        
    def getLockStatus(self):
        result = self.i2c.readBit(self.HMC5883L_RA_STATUS, self.HMC5883L_STATUS_LOCK_BIT)
        return result
        
    def getReadyStatus(self):
        result = self.i2c.readBit(self.HMC5883L_RA_STATUS, self.HMC5883L_STATUS_READY_BIT)
        return result
        
    def getIDA(self):
        result = self.i2c.readByte(self.HMC5883L_RA_ID_A)
        return result
        
    def getIDB(self):
        result = self.i2c.readByte(self.HMC5883L_RA_ID_B)
        return result

    def getIDC(self):
        result = self.i2c.readByte(self.HMC5883L_RA_ID_C)
        return result       
Ejemplo n.º 7
0
class BMP085:
    i2c = None

    # Operating Modes
    __BMP085_ULTRALOWPOWER = 0
    __BMP085_STANDARD = 1
    __BMP085_HIGHRES = 2
    __BMP085_ULTRAHIGHRES = 3

    # BMP085 Registers
    __BMP085_CAL_AC1 = 0xAA  # R   Calibration data (16 bits)
    __BMP085_CAL_AC2 = 0xAC  # R   Calibration data (16 bits)
    __BMP085_CAL_AC3 = 0xAE  # R   Calibration data (16 bits)
    __BMP085_CAL_AC4 = 0xB0  # R   Calibration data (16 bits)
    __BMP085_CAL_AC5 = 0xB2  # R   Calibration data (16 bits)
    __BMP085_CAL_AC6 = 0xB4  # R   Calibration data (16 bits)
    __BMP085_CAL_B1 = 0xB6  # R   Calibration data (16 bits)
    __BMP085_CAL_B2 = 0xB8  # R   Calibration data (16 bits)
    __BMP085_CAL_MB = 0xBA  # R   Calibration data (16 bits)
    __BMP085_CAL_MC = 0xBC  # R   Calibration data (16 bits)
    __BMP085_CAL_MD = 0xBE  # R   Calibration data (16 bits)
    __BMP085_CONTROL = 0xF4
    __BMP085_TEMPDATA = 0xF6
    __BMP085_PRESSUREDATA = 0xF6
    __BMP085_READTEMPCMD = 0x2E
    __BMP085_READPRESSURECMD = 0x34

    # Private Fields
    _cal_AC1 = 0
    _cal_AC2 = 0
    _cal_AC3 = 0
    _cal_AC4 = 0
    _cal_AC5 = 0
    _cal_AC6 = 0
    _cal_B1 = 0
    _cal_B2 = 0
    _cal_MB = 0
    _cal_MC = 0
    _cal_MD = 0

    def __init__(self, address=0x77, mode=3):
        self.i2c = PyComms(address)
        self.address = address

        # Make sure the specified mode is in the appropriate range
        if (mode < 0) | (mode > 3):
            self.mode = self.__BMP085_STANDARD
        else:
            self.mode = mode

        # Read the calibration data
        self.readCalibrationData()

    def readCalibrationData(self):
        # Reads the calibration data from the IC
        self._cal_AC1 = self.i2c.readS16(self.__BMP085_CAL_AC1)  # INT16
        self._cal_AC2 = self.i2c.readS16(self.__BMP085_CAL_AC2)  # INT16
        self._cal_AC3 = self.i2c.readS16(self.__BMP085_CAL_AC3)  # INT16
        self._cal_AC4 = self.i2c.readU16(self.__BMP085_CAL_AC4)  # UINT16
        self._cal_AC5 = self.i2c.readU16(self.__BMP085_CAL_AC5)  # UINT16
        self._cal_AC6 = self.i2c.readU16(self.__BMP085_CAL_AC6)  # UINT16
        self._cal_B1 = self.i2c.readS16(self.__BMP085_CAL_B1)  # INT16
        self._cal_B2 = self.i2c.readS16(self.__BMP085_CAL_B2)  # INT16
        self._cal_MB = self.i2c.readS16(self.__BMP085_CAL_MB)  # INT16
        self._cal_MC = self.i2c.readS16(self.__BMP085_CAL_MC)  # INT16
        self._cal_MD = self.i2c.readS16(self.__BMP085_CAL_MD)  # INT16

    def readRawTemp(self):
        # Reads the raw (uncompensated) temperature from the sensor
        self.i2c.write8(self.__BMP085_CONTROL, self.__BMP085_READTEMPCMD)
        time.sleep(0.005)  # Wait 5ms
        raw = self.i2c.readU16(self.__BMP085_TEMPDATA)

        return raw

    def readRawPressure(self):
        # Reads the raw (uncompensated) pressure level from the sensor
        self.i2c.write8(self.__BMP085_CONTROL, self.__BMP085_READPRESSURECMD + (self.mode << 6))
        if self.mode == self.__BMP085_ULTRALOWPOWER:
            time.sleep(0.005)
        elif self.mode == self.__BMP085_HIGHRES:
            time.sleep(0.014)
        elif self.mode == self.__BMP085_ULTRAHIGHRES:
            time.sleep(0.026)
        else:
            time.sleep(0.008)

        msb = self.i2c.readU8(self.__BMP085_PRESSUREDATA)
        lsb = self.i2c.readU8(self.__BMP085_PRESSUREDATA + 1)
        xlsb = self.i2c.readU8(self.__BMP085_PRESSUREDATA + 2)

        raw = ((msb << 16) + (lsb << 8) + xlsb) >> (8 - self.mode)

        return raw

    def readTemperature(self):
        # Gets the compensated temperature in degrees celcius
        UT = 0
        X1 = 0
        X2 = 0
        B5 = 0
        temp = 0.0

        # Read raw temp before aligning it with the calibration values
        UT = self.readRawTemp()
        X1 = ((UT - self._cal_AC6) * self._cal_AC5) >> 15
        X2 = (self._cal_MC << 11) / (X1 + self._cal_MD)
        B5 = X1 + X2
        temp = ((B5 + 8) >> 4) / 10.0

        return temp

    def readPressure(self):
        # Gets the compensated pressure in pascal
        UT = 0
        UP = 0
        B3 = 0
        B5 = 0
        B6 = 0
        X1 = 0
        X2 = 0
        X3 = 0
        p = 0
        B4 = 0
        B7 = 0

        UT = self.readRawTemp()
        UP = self.readRawPressure()

        # True Temperature Calculations
        X1 = ((UT - self._cal_AC6) * self._cal_AC5) >> 15
        X2 = (self._cal_MC << 11) / (X1 + self._cal_MD)
        B5 = X1 + X2

        # Pressure Calculations
        B6 = B5 - 4000
        X1 = (self._cal_B2 * (B6 * B6) >> 12) >> 11
        X2 = (self._cal_AC2 * B6) >> 11
        X3 = X1 + X2
        B3 = (((self._cal_AC1 * 4 + X3) << self.mode) + 2) / 4

        X1 = (self._cal_AC3 * B6) >> 13
        X2 = (self._cal_B1 * ((B6 * B6) >> 12)) >> 16
        X3 = ((X1 + X2) + 2) >> 2
        B4 = (self._cal_AC4 * (X3 + 32768)) >> 15
        B7 = (UP - B3) * (50000 >> self.mode)

        if B7 < 0x80000000:
            p = (B7 * 2) / B4
        else:
            p = (B7 / B4) * 2

        X1 = (p >> 8) * (p >> 8)
        X1 = (X1 * 3038) >> 16
        X2 = (-7375 * p) >> 16

        p = p + ((X1 + X2 + 3791) >> 4)

        return p

    def readAltitude(self, seaLevelPressure=101325):
        # Calculates the altitude in meters
        altitude = 0.0
        pressure = float(self.readPressure())
        altitude = 44330.0 * (1.0 - pow(pressure / seaLevelPressure, 0.1903))

        return altitude
        return 0
Ejemplo n.º 8
0
 def __init__(self, address=0x40):
     self.i2c = PyComms(address)
     self.address = address
     self.i2c.write8(self.__MODE1, 0x00)
Ejemplo n.º 9
0
class PCA9685():
    i2c = None

    # Registers/etc.
    __SUBADR1 = 0x02
    __SUBADR2 = 0x03
    __SUBADR3 = 0x04
    __MODE1 = 0x00
    __PRESCALE = 0xFE
    __LED0_ON_L = 0x06
    __LED0_ON_H = 0x07
    __LED0_OFF_L = 0x08
    __LED0_OFF_H = 0x09
    __ALLLED_ON_L = 0xFA
    __ALLLED_ON_H = 0xFB
    __ALLLED_OFF_L = 0xFC
    __ALLLED_OFF_H = 0xFD

    def __init__(self, address=0x40):
        self.i2c = PyComms(address)
        self.address = address
        self.i2c.write8(self.__MODE1, 0x00)

    def setPWMFreq(self, freq):
        # Sets the PWM frequency"
        prescaleval = 25000000.0    # 25MHz
        prescaleval /= 4096.0       # 12-bit
        prescaleval /= float(freq)
        prescaleval -= 1.0
        prescale = math.floor(prescaleval + 0.5)

        oldmode = self.i2c.readU8(self.__MODE1);
        newmode = (oldmode & 0x7F) | 0x10             # sleep
        self.i2c.write8(self.__MODE1, newmode)        # go to sleep
        self.i2c.write8(self.__PRESCALE, int(math.floor(prescale)))
        self.i2c.write8(self.__MODE1, oldmode)
        time.sleep(0.005)
        self.i2c.write8(self.__MODE1, oldmode | 0x80)

    def setPWM(self, channel, on, off):
        # Sets a single PWM channel
        self.i2c.write8(self.__LED0_ON_L + 4 * channel, on & 0xFF)
        self.i2c.write8(self.__LED0_ON_H + 4 * channel, on >> 8)
        self.i2c.write8(self.__LED0_OFF_L + 4 * channel, off & 0xFF)
        self.i2c.write8(self.__LED0_OFF_H + 4 * channel, off >> 8)
Ejemplo n.º 10
0
 def __init__(self, address=0x40):
     self.i2c = PyComms(address)
     self.address = address
     self.i2c.write8(self.__MODE1, 0x00)
Ejemplo n.º 11
0
class PCA9685():
    i2c = None

    # Registers/etc.
    __SUBADR1 = 0x02
    __SUBADR2 = 0x03
    __SUBADR3 = 0x04
    __MODE1 = 0x00
    __PRESCALE = 0xFE
    __LED0_ON_L = 0x06
    __LED0_ON_H = 0x07
    __LED0_OFF_L = 0x08
    __LED0_OFF_H = 0x09
    __ALLLED_ON_L = 0xFA
    __ALLLED_ON_H = 0xFB
    __ALLLED_OFF_L = 0xFC
    __ALLLED_OFF_H = 0xFD

    def __init__(self, address=0x40):
        self.i2c = PyComms(address)
        self.address = address
        self.i2c.write8(self.__MODE1, 0x00)

    def setPWMFreq(self, freq):
        # Sets the PWM frequency"
        prescaleval = 25000000.0  # 25MHz
        prescaleval /= 4096.0  # 12-bit
        prescaleval /= float(freq)
        prescaleval -= 1.0
        prescale = math.floor(prescaleval + 0.5)

        oldmode = self.i2c.readU8(self.__MODE1)
        newmode = (oldmode & 0x7F) | 0x10  # sleep
        self.i2c.write8(self.__MODE1, newmode)  # go to sleep
        self.i2c.write8(self.__PRESCALE, int(math.floor(prescale)))
        self.i2c.write8(self.__MODE1, oldmode)
        time.sleep(0.005)
        self.i2c.write8(self.__MODE1, oldmode | 0x80)

    def setPWM(self, channel, on, off):
        # Sets a single PWM channel
        self.i2c.write8(self.__LED0_ON_L + 4 * channel, on & 0xFF)
        self.i2c.write8(self.__LED0_ON_H + 4 * channel, on >> 8)
        self.i2c.write8(self.__LED0_OFF_L + 4 * channel, off & 0xFF)
        self.i2c.write8(self.__LED0_OFF_H + 4 * channel, off >> 8)
Ejemplo n.º 12
0
 def __init__(self, address = HMC5883L_DEFAULT_ADDRESS, channel=1):
     self.i2c = PyComms(address, channel)
     self.address = address
Ejemplo n.º 13
0
class BMP085:
    i2c = None

    # Operating Modes
    __BMP085_ULTRALOWPOWER = 0
    __BMP085_STANDARD = 1
    __BMP085_HIGHRES = 2
    __BMP085_ULTRAHIGHRES = 3

    # BMP085 Registers
    __BMP085_CAL_AC1 = 0xAA  # R   Calibration data (16 bits)
    __BMP085_CAL_AC2 = 0xAC  # R   Calibration data (16 bits)
    __BMP085_CAL_AC3 = 0xAE  # R   Calibration data (16 bits)
    __BMP085_CAL_AC4 = 0xB0  # R   Calibration data (16 bits)
    __BMP085_CAL_AC5 = 0xB2  # R   Calibration data (16 bits)
    __BMP085_CAL_AC6 = 0xB4  # R   Calibration data (16 bits)
    __BMP085_CAL_B1 = 0xB6  # R   Calibration data (16 bits)
    __BMP085_CAL_B2 = 0xB8  # R   Calibration data (16 bits)
    __BMP085_CAL_MB = 0xBA  # R   Calibration data (16 bits)
    __BMP085_CAL_MC = 0xBC  # R   Calibration data (16 bits)
    __BMP085_CAL_MD = 0xBE  # R   Calibration data (16 bits)
    __BMP085_CONTROL = 0xF4
    __BMP085_TEMPDATA = 0xF6
    __BMP085_PRESSUREDATA = 0xF6
    __BMP085_READTEMPCMD = 0x2E
    __BMP085_READPRESSURECMD = 0x34

    # Private Fields
    _cal_AC1 = 0
    _cal_AC2 = 0
    _cal_AC3 = 0
    _cal_AC4 = 0
    _cal_AC5 = 0
    _cal_AC6 = 0
    _cal_B1 = 0
    _cal_B2 = 0
    _cal_MB = 0
    _cal_MC = 0
    _cal_MD = 0

    def __init__(self, address=0x77, mode=3):
        self.i2c = PyComms(address)
        self.address = address

        # Make sure the specified mode is in the appropriate range
        if ((mode < 0) | (mode > 3)):
            self.mode = self.__BMP085_STANDARD
        else:
            self.mode = mode

        # Read the calibration data
        self.readCalibrationData()

    def readCalibrationData(self):
        # Reads the calibration data from the IC
        self._cal_AC1 = self.i2c.readS16(self.__BMP085_CAL_AC1)  # INT16
        self._cal_AC2 = self.i2c.readS16(self.__BMP085_CAL_AC2)  # INT16
        self._cal_AC3 = self.i2c.readS16(self.__BMP085_CAL_AC3)  # INT16
        self._cal_AC4 = self.i2c.readU16(self.__BMP085_CAL_AC4)  # UINT16
        self._cal_AC5 = self.i2c.readU16(self.__BMP085_CAL_AC5)  # UINT16
        self._cal_AC6 = self.i2c.readU16(self.__BMP085_CAL_AC6)  # UINT16
        self._cal_B1 = self.i2c.readS16(self.__BMP085_CAL_B1)  # INT16
        self._cal_B2 = self.i2c.readS16(self.__BMP085_CAL_B2)  # INT16
        self._cal_MB = self.i2c.readS16(self.__BMP085_CAL_MB)  # INT16
        self._cal_MC = self.i2c.readS16(self.__BMP085_CAL_MC)  # INT16
        self._cal_MD = self.i2c.readS16(self.__BMP085_CAL_MD)  # INT16

    def readRawTemp(self):
        # Reads the raw (uncompensated) temperature from the sensor
        self.i2c.write8(self.__BMP085_CONTROL, self.__BMP085_READTEMPCMD)
        time.sleep(0.005)  # Wait 5ms
        raw = self.i2c.readU16(self.__BMP085_TEMPDATA)

        return raw

    def readRawPressure(self):
        # Reads the raw (uncompensated) pressure level from the sensor
        self.i2c.write8(self.__BMP085_CONTROL,
                        self.__BMP085_READPRESSURECMD + (self.mode << 6))
        if (self.mode == self.__BMP085_ULTRALOWPOWER):
            time.sleep(0.005)
        elif (self.mode == self.__BMP085_HIGHRES):
            time.sleep(0.014)
        elif (self.mode == self.__BMP085_ULTRAHIGHRES):
            time.sleep(0.026)
        else:
            time.sleep(0.008)

        msb = self.i2c.readU8(self.__BMP085_PRESSUREDATA)
        lsb = self.i2c.readU8(self.__BMP085_PRESSUREDATA + 1)
        xlsb = self.i2c.readU8(self.__BMP085_PRESSUREDATA + 2)

        raw = ((msb << 16) + (lsb << 8) + xlsb) >> (8 - self.mode)

        return raw

    def readTemperature(self):
        # Gets the compensated temperature in degrees celcius
        UT = 0
        X1 = 0
        X2 = 0
        B5 = 0
        temp = 0.0

        # Read raw temp before aligning it with the calibration values
        UT = self.readRawTemp()
        X1 = ((UT - self._cal_AC6) * self._cal_AC5) >> 15
        X2 = (self._cal_MC << 11) / (X1 + self._cal_MD)
        B5 = X1 + X2
        temp = ((B5 + 8) >> 4) / 10.0

        return temp

    def readPressure(self):
        # Gets the compensated pressure in pascal
        UT = 0
        UP = 0
        B3 = 0
        B5 = 0
        B6 = 0
        X1 = 0
        X2 = 0
        X3 = 0
        p = 0
        B4 = 0
        B7 = 0

        UT = self.readRawTemp()
        UP = self.readRawPressure()

        # True Temperature Calculations
        X1 = ((UT - self._cal_AC6) * self._cal_AC5) >> 15
        X2 = (self._cal_MC << 11) / (X1 + self._cal_MD)
        B5 = X1 + X2

        # Pressure Calculations
        B6 = B5 - 4000
        X1 = (self._cal_B2 * (B6 * B6) >> 12) >> 11
        X2 = (self._cal_AC2 * B6) >> 11
        X3 = X1 + X2
        B3 = (((self._cal_AC1 * 4 + X3) << self.mode) + 2) / 4

        X1 = (self._cal_AC3 * B6) >> 13
        X2 = (self._cal_B1 * ((B6 * B6) >> 12)) >> 16
        X3 = ((X1 + X2) + 2) >> 2
        B4 = (self._cal_AC4 * (X3 + 32768)) >> 15
        B7 = (UP - B3) * (50000 >> self.mode)

        if (B7 < 0x80000000):
            p = (B7 * 2) / B4
        else:
            p = (B7 / B4) * 2

        X1 = (p >> 8) * (p >> 8)
        X1 = (X1 * 3038) >> 16
        X2 = (-7375 * p) >> 16

        p = p + ((X1 + X2 + 3791) >> 4)

        return p

    def readAltitude(self, seaLevelPressure=101325):
        # Calculates the altitude in meters
        altitude = 0.0
        pressure = float(self.readPressure())
        altitude = 44330.0 * (1.0 - pow(pressure / seaLevelPressure, 0.1903))

        return altitude
        return 0
Ejemplo n.º 14
0
 def __init__(self, address=HMC5883L_DEFAULT_ADDRESS, channel=1):
     self.i2c = PyComms(address, channel)
     self.address = address