def _readData8bit(self, register_name): high = wiringpi.wiringPiI2CReadReg8(self.device, self.registers[register_name]) low = wiringpi.wiringPiI2CReadReg8(self.device, self.registers[register_name] + 1) value = ((high << 8) | low) return value if value > 32786 else value - 65546
def __init__(self): global a a = wpi.wiringPiI2CSetup(0x1e) #set range wpi.wiringPiI2CWriteReg8(a, 0x01, 0b001 << 5) #setMeasurementMode value = wpi.wiringPiI2CReadReg8(a, 0x02) value &= 0b11111100 value |= 0b00 wpi.wiringPiI2CWriteReg8(a, 0x02, value) #setDataRate value = wpi.wiringPiI2CReadReg8(a, 0x00) value &= 0b11100011 value |= (0b101 << 2) wpi.wiringPiI2CWriteReg8(a, 0x00, value) #setSamples value = wpi.wiringPiI2CReadReg8(a, 0x00) value &= 0b10011111 value |= (0b11 << 5) wpi.wiringPiI2CWriteReg8(a, 0x00, value)
def init(self): global a a=wpi.wiringPiI2CSetup(0x1e) #set range wpi.wiringPiI2CWriteReg8(a,0x01, 0b001 << 5); #setMeasurementMode value = wpi.wiringPiI2CReadReg8(a,0x02); value &= 0b11111100; value |= 0b00; wpi.wiringPiI2CWriteReg8(a,0x02, value); #setDataRate value = wpi.wiringPiI2CReadReg8(a,0x00); value &= 0b11100011; value |= (0b101 << 2); wpi.wiringPiI2CWriteReg8(a,0x00, value); #setSamples value = wpi.wiringPiI2CReadReg8(a,0x00); value &= 0b10011111; value |= (0b11 << 5); wpi.wiringPiI2CWriteReg8(a,0x00, value);
def clear_prog_loaded_bootloader_flag(self): #as this is going to end up resetting the controllers then remove the update job for the moment self.__scheduler.remove_job('hub_update_job') #wait for any reads in progress to stop sleep(0.5) ctrl1_flag = wiringpi.wiringPiI2CReadReg8(self.__controller1, 0x33) ctlr2_flag = wiringpi.wiringPiI2CReadReg8(self.__controller2, 0x33) if ctrl1_flag == 0xFF and ctlr2_flag == 0xFF: return True else: return False
def __refresh_data_from_hub(self): #get voltage bytes from hub board_voltage_LSB = wiringpi.wiringPiI2CReadReg8( self.__controller1, 0x22) board_voltage_MSB = wiringpi.wiringPiI2CReadReg8( self.__controller1, 0x23) #combine 8 bit numbers into one 16bit number, then divide by 100 as #voltage is of the form 500 = 5.00v self.__board_voltage = ((board_voltage_MSB << 8) | (board_voltage_LSB & 0xff)) / 100 #get the port current readings, each LSB is 13.3mA, so multiply by 13.3 to get current in mA self.__port_1_current = wiringpi.wiringPiI2CReadReg8( self.__controller1, 0x00) * 13.3 self.__port_2_current = wiringpi.wiringPiI2CReadReg8( self.__controller1, 0x10) * 13.3 self.__port_3_current = wiringpi.wiringPiI2CReadReg8( self.__controller2, 0x00) * 13.3 self.__port_4_current = wiringpi.wiringPiI2CReadReg8( self.__controller2, 0x10) * 13.3 self.__port_1_power = self.__port_1_current * self.__board_voltage / 1000 self.__port_2_power = self.__port_2_current * self.__board_voltage / 1000 self.__port_3_power = self.__port_3_current * self.__board_voltage / 1000 self.__port_4_power = self.__port_4_current * self.__board_voltage / 1000 #get mains current bytes from hub mains_current_LSB = wiringpi.wiringPiI2CReadReg8( self.__controller1, 0x20) mains_current_MSB = wiringpi.wiringPiI2CReadReg8( self.__controller1, 0x21) #combine 8 bit numbers into one 16bit number #TODO turn get_mains_current into a current reading - at the moment its a counts reading from #the ADC proportional to current - might be easiest in firmware update. #Should prob correct to calibrated voltage in firmware #10bit ADC so 1023 counts max @ 5V #-5A to +5A range where -5A is 0V (0 counts) and +5A is 5V (1023 counts), 0A is 2.5v (511 counts) # 1 counts = 0.00489V #current sensor has a response of 0.5V/A supply_voltage_adjusted_1_count = self.__board_voltage / 1023 sensor_response_volts_per_amp = 0.5 mains_current_counts = ((mains_current_MSB << 8) | (mains_current_LSB & 0xff)) #need to offset by center point of supply voltage or 511 counts if mains_current_counts > 511: #positive current in mA self.__mains_current = (( (mains_current_counts - 511) * supply_voltage_adjusted_1_count) / sensor_response_volts_per_amp) * 1000 else: #negative current in mA self.__mains_current = (( (511 - mains_current_counts) * supply_voltage_adjusted_1_count) / sensor_response_volts_per_amp) * 1000
def readGyro(self): buff=[0,0,0,0,0,0,0,0] axis=[0,0,0,0] for i in range(8): buff[i]=wiringPiI2CReadReg8(self.gy, 0x1b+i) axis[0] = ((buff[2] << 8) | buff[3]) - self.g_offx axis[1] = ((buff[4] << 8) | buff[5]) - self.g_offy axis[2] = ((buff[6] << 8) | buff[7]) - self.g_offz axis[3] = ((buff[0] << 8) | buff[1]) return axis
def init_start(self): # Setup I2C interface for the device. self.set_all_pwm(0, 0) wp.wiringPiI2CWriteReg8(self.fd, MODE1, OUTDRV); wp.wiringPiI2CWriteReg8(self.fd, MODE1, ALLCALL); time.sleep(0.005) # wait for oscillator mode1 = wp.wiringPiI2CReadReg8(self.fd, MODE1); mode1 = mode1 & ~SLEEP; # wake up (reset sleep) wp.wiringPiI2CWriteReg8(self.fd, MODE1, mode1); time.sleep(0.005) # wait for oscillator self.set_pwm_freq(1000)
def readAcc(self): acc=self.acc buff=[0,0,0,0,0,0] axis=[0,0,0] for i in range(6): buff[i]=wiringPiI2CReadReg8(acc, DATAX0+i) axis[0] = ((buff[1]) << 8) | buff[0] axis[1] = ((buff[3]) << 8) | buff[2] axis[2] = ((buff[5]) << 8) | buff[4] return map(lambda x: x * 0.0039, axis)
def read_register(fd): secs = b2s(wiringpi.wiringPiI2CReadReg8(fd, 0x00), 0x7F) mins = b2s(wiringpi.wiringPiI2CReadReg8(fd, 0x01), 0x7F) hours = b2s(wiringpi.wiringPiI2CReadReg8(fd, 0x02) - 0b10000000, 0x3F) week = b2s(wiringpi.wiringPiI2CReadReg8(fd, 0x03), 0x3F) day = b2s(wiringpi.wiringPiI2CReadReg8(fd, 0x04), 0x1F) mon = b2s(wiringpi.wiringPiI2CReadReg8(fd, 0x05), 0x07) year = b2s(wiringpi.wiringPiI2CReadReg8(fd, 0x06), 0xFF) + 1970 print("week:%d mon:%d day:%d hours:%d mins:%d secs:%d year:%d" % (week, mon, day, hours, mins, secs, year))
def takeGyroSample(self): gx_low = wp.wiringPiI2CReadReg8(self.m_i2cHandle, RegMap.REG_GYRO_X_LOW) gx_high = wp.wiringPiI2CReadReg8(self.m_i2cHandle, RegMap.REG_GYRO_X_HIGH) gy_low = wp.wiringPiI2CReadReg8(self.m_i2cHandle, RegMap.REG_GYRO_Y_LOW) gy_high = wp.wiringPiI2CReadReg8(self.m_i2cHandle, RegMap.REG_GYRO_Y_HIGH) gz_low = wp.wiringPiI2CReadReg8(self.m_i2cHandle, RegMap.REG_GYRO_Z_LOW) gz_high = wp.wiringPiI2CReadReg8(self.m_i2cHandle, RegMap.REG_GYRO_Z_HIGH) self.m_gx = (gx_high << 8) | gx_low self.m_gy = (gy_high << 8) | gy_low self.m_gz = (gz_high << 8) | gz_low self.m_gx = self.m_gyroFactor * self._get2sComplement( self.m_gx) / 32768. self.m_gy = self.m_gyroFactor * self._get2sComplement( self.m_gy) / 32768. self.m_gz = self.m_gyroFactor * self._get2sComplement( self.m_gz) / 32768.
def __init__(self,devid=0x76): '''Initialze the I2C port with wiringpi and start the BMP280 sensor. Arguments: devid = the device ID of the sensor, defaults to 0x76''' wp.wiringPiSetupGpio() # Use the GPIO numbering scheme. self.fh = wp.wiringPiI2CSetup(devid) # Open com to devide dat = wp.wiringPiI2CReadReg8(self.fh,0xD0) # Check device ID if dat != 0x58: print('''The device at address 0x{:02x} returned 0x{:02x} instead of 0x58.\n Probably this is the wrong device.'''.format(devid,dat)) return(0) wp.wiringPiI2CWriteReg8(self.fh,0xF4,0b00100111) # Set for normal operation, 1x oversampling.
def _read_reg(self, reg): """ Reads one byte of *data* from register *reg* Parameters ---------- reg: int register value to write byte to Returns ------- 8 bit of data read from *reg* """ return wp.wiringPiI2CReadReg8(self.device_id, reg)
def readCompass(self): comp=self.comp buff=[0,0,0,0,0,0] axis=[0,0,0] for i in range(6): buff[i]=wiringPiI2CReadReg8(comp, 0x03+i) axis[0] = buff[0]<<8 axis[0] |= buff[1] axis[2] = buff[2]<<8 axis[2] |= buff[3] axis[1] = buff[4]<<8 axis[1] |= buff[5] return map(lambda x: x * 0.92, axis)
def get_gyroscope_xyz(): fd = wiringpi.wiringPiI2CSetup(0x6B) wiringpi.wiringPiI2CWriteReg8(fd, 0x20, 15) x = twos_complement_combine(wiringpi.wiringPiI2CReadReg8(fd, 0x29), wiringpi.wiringPiI2CReadReg8( fd, 0x29)) / 32768 # left, right y = twos_complement_combine(wiringpi.wiringPiI2CReadReg8( fd, 0x2B), wiringpi.wiringPiI2CReadReg8(fd, 0x2A)) / 32768 # up, down z = twos_complement_combine(wiringpi.wiringPiI2CReadReg8( fd, 0x2D), wiringpi.wiringPiI2CReadReg8(fd, 0x2C)) / 32768 # rotation return jsonify({"gyroscope": [str(x), str(y), str(z)]})
def set_pwm_freq(self, freq_hz): """Set the PWM frequency to the provided value in hertz.""" prescaleval = 25000000.0 # 25MHz prescaleval /= 4096.0 # 12-bit prescaleval /= float(freq_hz) prescaleval -= 0.5 logger.debug('Setting PWM frequency to {0} Hz'.format(freq_hz)) logger.debug('Estimated pre-scale: {0}'.format(prescaleval)) prescale = int(math.floor(prescaleval + 0.5)) logger.debug('Final pre-scale: {0}'.format(prescale)) oldmode = wp.wiringPiI2CReadReg8(self.fd, MODE1); newmode = (oldmode & 0x7F) | 0x10; wp.wiringPiI2CWriteReg8(self.fd, MODE1, newmode) # go to sleep wp.wiringPiI2CWriteReg8(self.fd, PRESCALE, prescale) wp.wiringPiI2CWriteReg8(self.fd, MODE1, oldmode) time.sleep(0.005) wp.wiringPiI2CWriteReg8(self.fd, MODE1, oldmode | 0x80)
def _setPWMFreq(self, freq): self.freq = (1000 if freq > 1000 else freq if freq < 400 else 400) prescale = int(25000000 / (4096 * freq) - 0.5) settings = wiringpi.wiringPiI2CReadReg8(self.device, self.registers["MODE1"]) & 0x7F sleep = settings | 0x10 wake = settings & 0xEF restart = wake | 0x80 wiringpi.wiringPiI2CWriteReg8(self.device, self.registers["MODE1"], sleep) wiringpi.wiringPiI2CWriteReg8(self.device, self.registers["PRESCALE"], prescale) wiringpi.wiringPiI2CWriteReg8(self.device, self.registers["MODE1"], wake) time.sleep(0.001) wiringpi.wiringPiI2CWriteReg8(self.device, self.registers["MODE1"], restart)
def read(self): #Offset Set xOffset = float(-10.0) yOffset = float(-547.0) mgPerDigit = float(0.92) headingDegress = 0 #float(headingDegress) for i in range(1, 11): X = wpi.wiringPiI2CReadReg8( a, 0x03) << 8 | wpi.wiringPiI2CReadReg8(a, 0x04) Y = wpi.wiringPiI2CReadReg8( a, 0x07) << 8 | wpi.wiringPiI2CReadReg8(a, 0x08) Z = wpi.wiringPiI2CReadReg8( a, 0x05) << 8 | wpi.wiringPiI2CReadReg8(a, 0x06) if X > 32768: X = -(0xFFFF - X + 1) if Y > 32768: Y = -(0xFFFF - Y + 1) if Z > 32768: Z = -(0xFFFF - Z + 1) XAxis = (float(X) - xOffset) * mgPerDigit YAxis = (float(Y) - yOffset) * mgPerDigit ZAxis = float(Z) * mgPerDigit heading = math.atan2(YAxis, XAxis) declinationAngle = (114.0 + (43.0 / 60.0)) / (180 / math.pi) heading = heading + declinationAngle #change to BG6. if (heading < 0): heading += 2 * math.pi if (heading > 2 * math.pi): heading -= 2 * math.pi headingDegress = headingDegress + heading * 18.0 / math.pi #180/10 #headingDegress = heading * 180/math.pi; headingDegress = 360.0 - headingDegress return headingDegress
def Read_Data(self, fd): x_msb = wp.wiringPiI2CReadReg8(self.fd, self.read_start_address) x_lsb = wp.wiringPiI2CReadReg8(self.fd, self.read_start_address + 1) z_msb = wp.wiringPiI2CReadReg8(self.fd, self.read_start_address + 2) z_lsb = wp.wiringPiI2CReadReg8(self.fd, self.read_start_address + 3) y_msb = wp.wiringPiI2CReadReg8(self.fd, self.read_start_address + 4) y_lsb = wp.wiringPiI2CReadReg8(self.fd, self.read_start_address + 5) X = x_msb * 256 + x_lsb Y = y_msb * 256 + y_lsb Z = z_msb * 256 + z_lsb X = X - 65536 if X > 32768 else X Y = Y - 65536 if Y > 32768 else Y Z = Z - 65536 if Z > 32768 else Z return X, Y, Z
def read(self): AZ = 0 #Read data back from MMA8452Q_REG_STATUS(0x00), 7 bytes #Status register, X-Axis MSB, X-Axis LSB, Y-Axis MSB, Y-Axis LSB, Z-Axis MSB, Z-Axis LSB for i in range(1, 51): data = wpi.wiringPiI2CReadReg8(b, 0x01) # Convert the data xAccl = (wpi.wiringPiI2CReadReg8(b, 0x01) * 256 + wpi.wiringPiI2CReadReg8(b, 0x02)) / 16 if xAccl > 2047: xAccl -= 4096 yAccl = (wpi.wiringPiI2CReadReg8(b, 0x03) * 256 + wpi.wiringPiI2CReadReg8(b, 0x04)) / 16 if yAccl > 2047: yAccl -= 4096 zAccl = (wpi.wiringPiI2CReadReg8(b, 0x05) * 256 + wpi.wiringPiI2CReadReg8(b, 0x06)) / 16 if zAccl > 2047: zAccl -= 4096 if yAccl > 1024: yAccl = 1024 if yAccl < -1024: yAccl = -1024 AZ = AZ + 9 / 5.0 - math.acos( float(yAccl) / float(1024)) * 18 / 5.0 / math.pi #90/10 180/10 return AZ
def takeAccSample(self): ax_low = wp.wiringPiI2CReadReg8(self.m_i2cHandle, RegMap.REG_ACC_X_LOW) ax_high = wp.wiringPiI2CReadReg8(self.m_i2cHandle, RegMap.REG_ACC_X_HIGH) ay_low = wp.wiringPiI2CReadReg8(self.m_i2cHandle, RegMap.REG_ACC_Y_LOW) ay_high = wp.wiringPiI2CReadReg8(self.m_i2cHandle, RegMap.REG_ACC_Y_HIGH) az_low = wp.wiringPiI2CReadReg8(self.m_i2cHandle, RegMap.REG_ACC_Z_LOW) az_high = wp.wiringPiI2CReadReg8(self.m_i2cHandle, RegMap.REG_ACC_Z_HIGH) self.m_ax = (ax_high << 8) | ax_low self.m_ay = (ay_high << 8) | ay_low self.m_az = (az_high << 8) | az_low self.m_ax = self.m_accFactor * self._get2sComplement( self.m_ax) / 32768. self.m_ay = self.m_accFactor * self._get2sComplement( self.m_ay) / 32768. self.m_az = self.m_accFactor * self._get2sComplement( self.m_az) / 32768.
def readData(self): a = wiringpi.wiringPiI2CReadReg8(self.i2c, 0x00) b = wiringpi.wiringPiI2CReadReg8(self.i2c, 0x01) # c = wiringpi.wiringPiI2CReadReg8(self.i2c,0x02) d = wiringpi.wiringPiI2CReadReg8(self.i2c, 0x03) return a, b, d
def get_Temp(): fd = wiringpi.wiringPiI2CSetup(0x40) temp_org = wiringpi.wiringPiI2CReadReg8(fd, 0x03) temp = (temp_org << 8) | temp_org T = -46.85 + 175.72 / 65536 * temp return T
def get_Humid(): fd = wiringpi.wiringPiI2CSetup(0x40) humd_org = wiringpi.wiringPiI2CReadReg8(fd, 0x05) humd = (humd_org << 8) | humd_org RH = -6.0 + 125.0 / 65536 * humd return RH
wp.wiringPiI2CWriteReg8(devHandle, 0x1b, 0x00) wp.wiringPiI2CWriteReg8(devHandle, 0x1c, 0x18) def get2sComplement(val): if (val & (1 << (16 - 1))) != 0: val = val - (1 << 16) return val if i2cHandle is None: print 'there was anerror initializing the i2c interface' else: configureIMU(i2cHandle) res = wp.wiringPiI2CReadReg8(i2cHandle, 0x75) print 'whoami: ', res while (True): ax_low = wp.wiringPiI2CReadReg8(i2cHandle, 0x3c) ax_high = wp.wiringPiI2CReadReg8(i2cHandle, 0x3b) ay_low = wp.wiringPiI2CReadReg8(i2cHandle, 0x3e) ay_high = wp.wiringPiI2CReadReg8(i2cHandle, 0x3d) az_low = wp.wiringPiI2CReadReg8(i2cHandle, 0x40) az_high = wp.wiringPiI2CReadReg8(i2cHandle, 0x3f) ax = ax_high << 8 | ax_low ay = ay_high << 8 | ay_low
'''ultrasonic ranging using I2C''' '''software tools : wiringpi''' '''hardware : KS103 ''' import wiringpi as wpi address = 0x74 #i2c device address h = wpi.wiringPiI2CSetup(address) #open device at address wr_cmd = 0xb0 #range 0-5m, return distance(mm) #rd_cmd = 0xb2 ##range 0-5m, return flight time(us), remember divided by 2 try: while True: wpi.wiringPiI2CWriteReg8(h, 0x2, wr_cmd) wpi.delay(1000) #unit:ms MIN ~ 33 HighByte = wpi.wiringPiI2CReadReg8(h, 0x2) LowByte = wpi.wiringPiI2CReadReg8(h, 0x3) Dist = (HighByte << 8) + LowByte print('Distance:', Dist / 10.0, 'cm') except KeyboardInterrupt: pass print('Range over!')
def read8(self, addr=0x02): return wpi.wiringPiI2CReadReg8(self.fd, addr)
import wiringpi as wpi import math ACC = wpi.wiringPiI2CSetup(0x18) wpi.wiringPiI2CWriteReg8(ACC, 0x20, 0x27) while True: ACC_x = wpi.wiringPiI2CReadReg8(ACC, 0x28) << 8 | wpi.wiringPiI2CReadReg8( ACC, 0x29) # ACC_y = wpi.wiringPiI2CReadReg8(ACC, 0x2a) << 8 | wpi.wiringPiI2CReadReg8( ACC, 0x2b) # ACC_z = wpi.wiringPiI2CReadReg8(ACC, 0x2c) << 8 | wpi.wiringPiI2CReadReg8( ACC, 0x2d) # # # # # if ACC_x > 32768: ACC_x = -(65535 - ACC_x + 1) # # if ACC_y > 32768: ACC_y = -(65535 - ACC_y + 1) # # if ACC_z > 32768: ACC_z = -(65535 - ACC_z + 1) #
def get_ultrasound(self): result = wiringpi.wiringPiI2CReadReg8(self.file_descriptor, 0x30) if result == -1: return result else: return result
def get_state(self): result = wiringpi.wiringPiI2CReadReg8(self.file_descriptor, 0x90) if result == -1: return result else: return result
def _setupDevice(self): settings = wiringpi.wiringPiI2CReadReg8(self.device, self.registers["MODE1"]) & 0x7F auto_increment = settings | 0x20 wiringpi.wiringPiI2CWriteReg8(self.device, self.registers["MODE1"], auto_increment)
import wiringpi as wp # # Define a function to decode BCD encoded numbers. def decBCD(num): return ((num / 16) * 10 + (num % 16)) # # Open the RTC # fc = wp.wiringPiI2CSetup(0x68) # # We read the registers one at a time. secs = wp.wiringPiI2CReadReg8(fc, 0x00) mins = wp.wiringPiI2CReadReg8(fc, 0x01) hour = wp.wiringPiI2CReadReg8(fc, 0x02) day = wp.wiringPiI2CReadReg8(fc, 0x03) dat = wp.wiringPiI2CReadReg8(fc, 0x04) mon = wp.wiringPiI2CReadReg8(fc, 0x05) yr = wp.wiringPiI2CReadReg8(fc, 0x06) cent = wp.wiringPiI2CReadReg8(fc, 0x07) temp1 = wp.wiringPiI2CReadReg8(fc, 0x11) temp2 = wp.wiringPiI2CReadReg8(fc, 0x12) year = decBCD(yr) month = decBCD(mon & 0x7f) date = decBCD(dat) if (mon & 0x80) > 0: