1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
| import ch347
class MPU6050:
GRAVITIY_MS2 = 9.80665 address = None driver = None
ACCEL_SCALE_MODIFIER_2G = 16384.0 ACCEL_SCALE_MODIFIER_4G = 8192.0 ACCEL_SCALE_MODIFIER_8G = 4096.0 ACCEL_SCALE_MODIFIER_16G = 2048.0
GYRO_SCALE_MODIFIER_250DEG = 131.0 GYRO_SCALE_MODIFIER_500DEG = 65.5 GYRO_SCALE_MODIFIER_1000DEG = 32.8 GYRO_SCALE_MODIFIER_2000DEG = 16.4
ACCEL_RANGE_2G = 0x00 ACCEL_RANGE_4G = 0x08 ACCEL_RANGE_8G = 0x10 ACCEL_RANGE_16G = 0x18
GYRO_RANGE_250DEG = 0x00 GYRO_RANGE_500DEG = 0x08 GYRO_RANGE_1000DEG = 0x10 GYRO_RANGE_2000DEG = 0x18
FILTER_BW_256=0x00 FILTER_BW_188=0x01 FILTER_BW_98=0x02 FILTER_BW_42=0x03 FILTER_BW_20=0x04 FILTER_BW_10=0x05 FILTER_BW_5=0x06
PWR_MGMT_1 = 0x6B PWR_MGMT_2 = 0x6C
ACCEL_XOUT0 = 0x3B ACCEL_YOUT0 = 0x3D ACCEL_ZOUT0 = 0x3F
TEMP_OUT0 = 0x41
GYRO_XOUT0 = 0x43 GYRO_YOUT0 = 0x45 GYRO_ZOUT0 = 0x47
ACCEL_CONFIG = 0x1C GYRO_CONFIG = 0x1B MPU_CONFIG = 0x1A
def __init__(self, address=0x68, dll="CH347DLLA64.dll", device_index=0): self.address = address << 1 self.driver = ch347.CH347Driver(dll) self.device_index = device_index
self.driver.open_device(self.device_index) self.driver.stream_i2c(self.device_index, [self.address, self.PWR_MGMT_1, 0x00], 0)
def read_byte_data(self, register): raw_data = self.driver.stream_i2c(self.device_index, [self.address, register], 1) return raw_data[0] def write_byte_data(self, register, value): return self.driver.stream_i2c(self.device_index, [self.address, register, value], 0) def read_i2c_word(self, register): """Read two i2c registers and combine them.
register -- the first register to read from. Returns the combined read results. """ raw_data = self.driver.stream_i2c(self.device_index, [self.address, register], 2)
value = raw_data[0] << 8 | raw_data[1]
if (value >= 0x8000): return -((65535 - value) + 1) else: return value
def get_temp(self): """Reads the temperature from the onboard temperature sensor of the MPU-6050.
Returns the temperature in degrees Celcius. """ raw_temp = self.read_i2c_word(self.TEMP_OUT0)
actual_temp = (raw_temp / 340.0) + 36.53
return actual_temp
def set_accel_range(self, accel_range): """Sets the range of the accelerometer to range.
accel_range -- the range to set the accelerometer to. Using a pre-defined range is advised. """ self.write_byte_data(self.ACCEL_CONFIG, 0x00)
self.write_byte_data(self.ACCEL_CONFIG, accel_range)
def read_accel_range(self, raw = False): """Reads the range the accelerometer is set to.
If raw is True, it will return the raw value from the ACCEL_CONFIG register If raw is False, it will return an integer: -1, 2, 4, 8 or 16. When it returns -1 something went wrong. """ raw_data = self.read_byte_data(self.ACCEL_CONFIG)
if raw is True: return raw_data elif raw is False: if raw_data == self.ACCEL_RANGE_2G: return 2 elif raw_data == self.ACCEL_RANGE_4G: return 4 elif raw_data == self.ACCEL_RANGE_8G: return 8 elif raw_data == self.ACCEL_RANGE_16G: return 16 else: return -1
def get_accel_data(self, g = False): """Gets and returns the X, Y and Z values from the accelerometer.
If g is True, it will return the data in g If g is False, it will return the data in m/s^2 Returns a dictionary with the measurement results. """ x = self.read_i2c_word(self.ACCEL_XOUT0) y = self.read_i2c_word(self.ACCEL_YOUT0) z = self.read_i2c_word(self.ACCEL_ZOUT0)
accel_scale_modifier = None accel_range = self.read_accel_range(True)
if accel_range == self.ACCEL_RANGE_2G: accel_scale_modifier = self.ACCEL_SCALE_MODIFIER_2G elif accel_range == self.ACCEL_RANGE_4G: accel_scale_modifier = self.ACCEL_SCALE_MODIFIER_4G elif accel_range == self.ACCEL_RANGE_8G: accel_scale_modifier = self.ACCEL_SCALE_MODIFIER_8G elif accel_range == self.ACCEL_RANGE_16G: accel_scale_modifier = self.ACCEL_SCALE_MODIFIER_16G else: print("Unkown range - accel_scale_modifier set to self.ACCEL_SCALE_MODIFIER_2G") accel_scale_modifier = self.ACCEL_SCALE_MODIFIER_2G
x = x / accel_scale_modifier y = y / accel_scale_modifier z = z / accel_scale_modifier
if g is True: return {'x': x, 'y': y, 'z': z} elif g is False: x = x * self.GRAVITIY_MS2 y = y * self.GRAVITIY_MS2 z = z * self.GRAVITIY_MS2 return {'x': x, 'y': y, 'z': z}
def set_gyro_range(self, gyro_range): """Sets the range of the gyroscope to range.
gyro_range -- the range to set the gyroscope to. Using a pre-defined range is advised. """ self.write_byte_data(self.GYRO_CONFIG, 0x00)
self.write_byte_data(self.GYRO_CONFIG, gyro_range)
def set_filter_range(self, filter_range=FILTER_BW_256): """Sets the low-pass bandpass filter frequency""" EXT_SYNC_SET = self.read_byte_data(self.MPU_CONFIG) & 0b00111000 return self.write_byte_data(self.MPU_CONFIG, EXT_SYNC_SET | filter_range)
def read_gyro_range(self, raw = False): """Reads the range the gyroscope is set to.
If raw is True, it will return the raw value from the GYRO_CONFIG register. If raw is False, it will return 250, 500, 1000, 2000 or -1. If the returned value is equal to -1 something went wrong. """ raw_data = self.read_byte_data(self.GYRO_CONFIG)
if raw is True: return raw_data elif raw is False: if raw_data == self.GYRO_RANGE_250DEG: return 250 elif raw_data == self.GYRO_RANGE_500DEG: return 500 elif raw_data == self.GYRO_RANGE_1000DEG: return 1000 elif raw_data == self.GYRO_RANGE_2000DEG: return 2000 else: return -1
def get_gyro_data(self): """Gets and returns the X, Y and Z values from the gyroscope.
Returns the read values in a dictionary. """ x = self.read_i2c_word(self.GYRO_XOUT0) y = self.read_i2c_word(self.GYRO_YOUT0) z = self.read_i2c_word(self.GYRO_ZOUT0)
gyro_scale_modifier = None gyro_range = self.read_gyro_range(True)
if gyro_range == self.GYRO_RANGE_250DEG: gyro_scale_modifier = self.GYRO_SCALE_MODIFIER_250DEG elif gyro_range == self.GYRO_RANGE_500DEG: gyro_scale_modifier = self.GYRO_SCALE_MODIFIER_500DEG elif gyro_range == self.GYRO_RANGE_1000DEG: gyro_scale_modifier = self.GYRO_SCALE_MODIFIER_1000DEG elif gyro_range == self.GYRO_RANGE_2000DEG: gyro_scale_modifier = self.GYRO_SCALE_MODIFIER_2000DEG else: print("Unkown range - gyro_scale_modifier set to self.GYRO_SCALE_MODIFIER_250DEG") gyro_scale_modifier = self.GYRO_SCALE_MODIFIER_250DEG
x = x / gyro_scale_modifier y = y / gyro_scale_modifier z = z / gyro_scale_modifier
return {'x': x, 'y': y, 'z': z}
def get_all_data(self): """Reads and returns all the available data.""" temp = self.get_temp() accel = self.get_accel_data() gyro = self.get_gyro_data()
return [accel, gyro, temp] def close(self): self.driver.close_device(self.device_index)
if __name__ == "__main__": mpu = MPU6050() print(mpu.get_temp()) accel_data = mpu.get_accel_data(True) print(accel_data['x']) print(accel_data['y']) print(accel_data['z']) gyro_data = mpu.get_gyro_data() print(gyro_data['x']) print(gyro_data['y']) print(gyro_data['z']) mpu.close()
|