def magnetometer(): #I2C code for reading the magnetometer counter = 0 global Zmag_raw global Ymag_raw global Xmag_raw global Zmag global Ymag global Xmag global mag_vector for counter in xrange(0,10): #send the commands several times to ensure Magnetometer receives the command Adafruit_I2C.write8(i2c, 0x00, 0x18) # 1 Average, 15 Hz, normal measurement Adafruit_I2C.write8(i2c, 0x01, 0x20) # Set Gain Adafruit_I2C.write8(i2c, 0x02, 0x00) # Continuous measurement counter += 1 z_l = Adafruit_I2C.readU8(i2c, Z_lsb) #Data is stored in 2 registers on the chip, the Least significant and the z_m = Adafruit_I2C.readS8(i2c, Z_msb) #most significant. Both must be read and the two combined into the actual number Zmag_raw = z_m << 8 | z_l Zmag = Zmag_raw/2048.0 * 1.3 #Calculate Gauss value from the raw 12 bit number y_l = Adafruit_I2C.readU8(i2c, Y_lsb) #rinse and repeat for each axis y_m = Adafruit_I2C.readS8(i2c, Y_msb) Ymag_raw = y_m << 8 | y_l Ymag = Ymag_raw/2048.0 * 1.3 x_l = Adafruit_I2C.readU8(i2c, X_lsb) x_m = Adafruit_I2C.readS8(i2c, X_msb) Xmag_raw = x_m << 8 | x_l Xmag = Xmag_raw/2048.0 * 1.3 mag_vector = np.array([Xmag,Ymag,Zmag]) return
def read_sensor(self): hival = Adafruit_I2C.write8(self.sensor_address, self.MEASURE_REG, self.MEASURE_VAL) sleep(.02) loval = Adafruit_I2C.readU8(self.sensor_address, self.DISTANCE_REG_LO) hival = Adafruit_I2C.readS8(self.sensor_address, self.DISTANCE_REG_HI) return ((hival << 8) + loval)
def magnetometer(): #I2C code for reading the magnetometer counter = 0 global Zmag_raw global Ymag_raw global Xmag_raw global Zmag global Ymag global Xmag global mag_vector for counter in xrange( 0, 10 ): #send the commands several times to ensure Magnetometer receives the command Adafruit_I2C.write8(i2c, 0x00, 0x18) # 1 Average, 15 Hz, normal measurement Adafruit_I2C.write8(i2c, 0x01, 0x20) # Set Gain Adafruit_I2C.write8(i2c, 0x02, 0x00) # Continuous measurement counter += 1 z_l = Adafruit_I2C.readU8( i2c, Z_lsb ) #Data is stored in 2 registers on the chip, the Least significant and the z_m = Adafruit_I2C.readS8( i2c, Z_msb ) #most significant. Both must be read and the two combined into the actual number Zmag_raw = z_m << 8 | z_l Zmag = Zmag_raw / 2048.0 * 1.3 #Calculate Gauss value from the raw 12 bit number y_l = Adafruit_I2C.readU8(i2c, Y_lsb) #rinse and repeat for each axis y_m = Adafruit_I2C.readS8(i2c, Y_msb) Ymag_raw = y_m << 8 | y_l Ymag = Ymag_raw / 2048.0 * 1.3 x_l = Adafruit_I2C.readU8(i2c, X_lsb) x_m = Adafruit_I2C.readS8(i2c, X_msb) Xmag_raw = x_m << 8 | x_l Xmag = Xmag_raw / 2048.0 * 1.3 mag_vector = np.array([Xmag, Ymag, Zmag]) return
# I2C code for reading the magnetometer counter = 0 for counter in xrange( 0, 10 ): #send the commands several times to ensure Magnetometer receives the command Adafruit_I2C.write8(i2c, 0x00, 0x18) # 1 Average, 15 Hz, normal measurement Adafruit_I2C.write8(i2c, 0x01, 0x20) # Set Gain Adafruit_I2C.write8(i2c, 0x02, 0x00) # Continuous measurement counter += 1 z_l = Adafruit_I2C.readU8( i2c, Z_lsb ) #Data is stored in 2 registers on the chip, the Least significant and the z_m = Adafruit_I2C.readS8( i2c, Z_msb ) #most significant. Both must be read and the two combined into the actual number Zmag_raw = z_m << 8 | z_l Zmag = Zmag_raw / 2048.0 * 1.3 #Calculate Gauss value from the raw 12 bit number y_l = Adafruit_I2C.readU8(i2c, Y_lsb) #rinse and repeat for each axis y_m = Adafruit_I2C.readS8(i2c, Y_msb) Ymag_raw = y_m << 8 | y_l Ymag = Ymag_raw / 2048.0 * 1.3 x_l = Adafruit_I2C.readU8(i2c, X_lsb) x_m = Adafruit_I2C.readS8(i2c, X_msb) Xmag_raw = x_m << 8 | x_l Xmag = Xmag_raw / 2048.0 * 1.3 mag_vector = np.array([Xmag, Ymag, Zmag])
i2c = Adafruit_I2C(mag_addr) #i2c_read = Adafruit_I2C(mag_read_addr) #i2c_write = Adafruit_I2C(mag_write_addr) while 1: x = 0 time.sleep(1) for x in xrange(0, 10): Adafruit_I2C.write8(i2c, 0x00, 0x18) # 1 Average, 15 Hz, normal measurement Adafruit_I2C.write8(i2c, 0x01, 0x20) # Set Gain Adafruit_I2C.write8(i2c, 0x02, 0x00) # Continuous measurement x += 1 z_l = Adafruit_I2C.readU8(i2c, Z_lsb) z_m = Adafruit_I2C.readS8(i2c, Z_msb) z = z_m << 8 | z_l z = z / 2048.0 * 1.3 y_l = Adafruit_I2C.readU8(i2c, Y_lsb) y_m = Adafruit_I2C.readS8(i2c, Y_msb) y = y_m << 8 | y_l y = y / 2048.0 * 1.3 x_l = Adafruit_I2C.readU8(i2c, X_lsb) x_m = Adafruit_I2C.readS8(i2c, X_msb) x = x_m << 8 | x_l x = x / 2048.0 * 1.3 a = np.array([x, y, z]) print 'Norm = ' + str(np.linalg.norm(a))
i2c = Adafruit_I2C(mag_addr) #i2c_read = Adafruit_I2C(mag_read_addr) #i2c_write = Adafruit_I2C(mag_write_addr) while 1: x = 0 time.sleep(1) for x in xrange(0,10): Adafruit_I2C.write8(i2c, 0x00, 0x18) # 1 Average, 15 Hz, normal measurement Adafruit_I2C.write8(i2c, 0x01, 0x20) # Set Gain Adafruit_I2C.write8(i2c, 0x02, 0x00) # Continuous measurement x += 1 z_l = Adafruit_I2C.readU8(i2c, Z_lsb) z_m = Adafruit_I2C.readS8(i2c, Z_msb) z = z_m << 8 | z_l z = z/2048.0 * 1.3 y_l = Adafruit_I2C.readU8(i2c, Y_lsb) y_m = Adafruit_I2C.readS8(i2c, Y_msb) y = y_m << 8 | y_l y = y/2048.0 * 1.3 x_l = Adafruit_I2C.readU8(i2c, X_lsb) x_m = Adafruit_I2C.readS8(i2c, X_msb) x = x_m << 8 | x_l x = x/2048.0 * 1.3 a = np.array([x,y,z]) print 'Norm = ' + str(np.linalg.norm(a));
# get gps data gps = ser.readline() while(not gps.startswith('$GPRMC')): gps = ser.readline() gprmc.parse(gps) # I2C code for reading the magnetometer counter = 0 for counter in xrange(0,10): #send the commands several times to ensure Magnetometer receives the command Adafruit_I2C.write8(i2c, 0x00, 0x18) # 1 Average, 15 Hz, normal measurement Adafruit_I2C.write8(i2c, 0x01, 0x20) # Set Gain Adafruit_I2C.write8(i2c, 0x02, 0x00) # Continuous measurement counter += 1 z_l = Adafruit_I2C.readU8(i2c, Z_lsb) #Data is stored in 2 registers on the chip, the Least significant and the z_m = Adafruit_I2C.readS8(i2c, Z_msb) #most significant. Both must be read and the two combined into the actual number Zmag_raw = z_m << 8 | z_l Zmag = Zmag_raw/2048.0 * 1.3 #Calculate Gauss value from the raw 12 bit number y_l = Adafruit_I2C.readU8(i2c, Y_lsb) #rinse and repeat for each axis y_m = Adafruit_I2C.readS8(i2c, Y_msb) Ymag_raw = y_m << 8 | y_l Ymag = Ymag_raw/2048.0 * 1.3 x_l = Adafruit_I2C.readU8(i2c, X_lsb) x_m = Adafruit_I2C.readS8(i2c, X_msb) Xmag_raw = x_m << 8 | x_l Xmag = Xmag_raw/2048.0 * 1.3 mag_vector = np.array([Xmag,Ymag,Zmag]) # print 'Norm = ' + str(np.linalg.norm(mag_vector));