|
| 1 | +from __future__ import print_function |
| 2 | +"""This program handles the communication over I2C |
| 3 | +between a Raspberry Pi and a MPU-6050 Gyroscope / Accelerometer combo. |
| 4 | +Made by: MrTijn/Tijndagamer |
| 5 | +Released under the MIT License |
| 6 | +Copyright 2015 |
| 7 | +""" |
| 8 | + |
| 9 | +import smbus2 as smbus |
| 10 | +import time |
| 11 | + |
| 12 | +class MPU6050: |
| 13 | + |
| 14 | + # Global Variables |
| 15 | + GRAVITIY_MS2 = 9.80665 |
| 16 | + address = None |
| 17 | + bus = smbus.SMBus(1) |
| 18 | + |
| 19 | + # Scale Modifiers |
| 20 | + ACCEL_SCALE_MODIFIER_2G = 16384.0 |
| 21 | + ACCEL_SCALE_MODIFIER_4G = 8192.0 |
| 22 | + ACCEL_SCALE_MODIFIER_8G = 4096.0 |
| 23 | + ACCEL_SCALE_MODIFIER_16G = 2048.0 |
| 24 | + |
| 25 | + GYRO_SCALE_MODIFIER_250DEG = 131.0 |
| 26 | + GYRO_SCALE_MODIFIER_500DEG = 65.5 |
| 27 | + GYRO_SCALE_MODIFIER_1000DEG = 32.8 |
| 28 | + GYRO_SCALE_MODIFIER_2000DEG = 16.4 |
| 29 | + |
| 30 | + # Pre-defined ranges |
| 31 | + ACCEL_RANGE_2G = 0x00 |
| 32 | + ACCEL_RANGE_4G = 0x08 |
| 33 | + ACCEL_RANGE_8G = 0x10 |
| 34 | + ACCEL_RANGE_16G = 0x18 |
| 35 | + |
| 36 | + GYRO_RANGE_250DEG = 0x00 |
| 37 | + GYRO_RANGE_500DEG = 0x08 |
| 38 | + GYRO_RANGE_1000DEG = 0x10 |
| 39 | + GYRO_RANGE_2000DEG = 0x18 |
| 40 | + |
| 41 | + # MPU-6050 Registers |
| 42 | + PWR_MGMT_1 = 0x6B |
| 43 | + PWR_MGMT_2 = 0x6C |
| 44 | + |
| 45 | + SELF_TEST_X = 0x0D |
| 46 | + SELF_TEST_Y = 0x0E |
| 47 | + SELF_TEST_Z = 0x0F |
| 48 | + SELF_TEST_A = 0x10 |
| 49 | + |
| 50 | + ACCEL_XOUT0 = 0x3B |
| 51 | + ACCEL_XOUT1 = 0x3C |
| 52 | + ACCEL_YOUT0 = 0x3D |
| 53 | + ACCEL_YOUT1 = 0x3E |
| 54 | + ACCEL_ZOUT0 = 0x3F |
| 55 | + ACCEL_ZOUT1 = 0x40 |
| 56 | + |
| 57 | + TEMP_OUT0 = 0x41 |
| 58 | + TEMP_OUT1 = 0x42 |
| 59 | + |
| 60 | + GYRO_XOUT0 = 0x43 |
| 61 | + GYRO_XOUT1 = 0x44 |
| 62 | + GYRO_YOUT0 = 0x45 |
| 63 | + GYRO_YOUT1 = 0x46 |
| 64 | + GYRO_ZOUT0 = 0x47 |
| 65 | + GYRO_ZOUT1 = 0x48 |
| 66 | + |
| 67 | + ACCEL_CONFIG = 0x1C |
| 68 | + GYRO_CONFIG = 0x1B |
| 69 | + |
| 70 | + GYRO_THRESHOLD_Z = 1.5 |
| 71 | + |
| 72 | + def __init__(self, address): |
| 73 | + self.address = address |
| 74 | + |
| 75 | + # Wake up the MPU-6050 since it starts in sleep mode |
| 76 | + self.bus.write_byte_data(self.address, self.PWR_MGMT_1, 0x00) |
| 77 | + |
| 78 | + # I2C communication methods |
| 79 | + |
| 80 | + def read_i2c_word(self, register): |
| 81 | + """Read two i2c registers and combine them. |
| 82 | + register -- the first register to read from. |
| 83 | + Returns the combined read results. |
| 84 | + """ |
| 85 | + # Read the data from the registers |
| 86 | + high = self.bus.read_byte_data(self.address, register) |
| 87 | + low = self.bus.read_byte_data(self.address, register + 1) |
| 88 | + |
| 89 | + value = (high << 8) + low |
| 90 | + |
| 91 | + if (value >= 0x8000): |
| 92 | + return -((65535 - value) + 1) |
| 93 | + else: |
| 94 | + return value |
| 95 | + |
| 96 | + # MPU-6050 Methods |
| 97 | + |
| 98 | + def get_temp(self): |
| 99 | + """Reads the temperature from the onboard temperature sensor of the MPU-6050. |
| 100 | + Returns the temperature in degrees Celcius. |
| 101 | + """ |
| 102 | + # Get the raw data |
| 103 | + raw_temp = self.read_i2c_word(self.TEMP_OUT0) |
| 104 | + |
| 105 | + # Get the actual temperature using the formule given in the |
| 106 | + # MPU-6050 Register Map and Descriptions revision 4.2, page 30 |
| 107 | + actual_temp = (raw_temp / 340) + 36.53 |
| 108 | + |
| 109 | + # Return the temperature |
| 110 | + return actual_temp |
| 111 | + |
| 112 | + def set_accel_range(self, accel_range): |
| 113 | + """Sets the range of the accelerometer to range. |
| 114 | + accel_range -- the range to set the accelerometer to. Using a |
| 115 | + pre-defined range is advised. |
| 116 | + """ |
| 117 | + # First change it to 0x00 to make sure we write the correct value later |
| 118 | + self.bus.write_byte_data(self.address, self.ACCEL_CONFIG, 0x00) |
| 119 | + |
| 120 | + # Write the new range to the ACCEL_CONFIG register |
| 121 | + self.bus.write_byte_data(self.address, self.ACCEL_CONFIG, accel_range) |
| 122 | + |
| 123 | + def read_accel_range(self, raw = False): |
| 124 | + """Reads the range the accelerometer is set to. |
| 125 | + If raw is True, it will return the raw value from the ACCEL_CONFIG |
| 126 | + register |
| 127 | + If raw is False, it will return an integer: -1, 2, 4, 8 or 16. When it |
| 128 | + returns -1 something went wrong. |
| 129 | + """ |
| 130 | + # Get the raw value |
| 131 | + raw_data = self.bus.read_byte_data(self.address, self.ACCEL_CONFIG) |
| 132 | + |
| 133 | + if raw is True: |
| 134 | + return raw_data |
| 135 | + elif raw is False: |
| 136 | + if raw_data == self.ACCEL_RANGE_2G: |
| 137 | + return 2 |
| 138 | + elif raw_data == self.ACCEL_RANGE_4G: |
| 139 | + return 4 |
| 140 | + elif raw_data == self.ACCEL_RANGE_8G: |
| 141 | + return 8 |
| 142 | + elif raw_data == self.ACCEL_RANGE_16G: |
| 143 | + return 16 |
| 144 | + else: |
| 145 | + return -1 |
| 146 | + |
| 147 | + def get_accel_data(self, g = False): |
| 148 | + """Gets and returns the X, Y and Z values from the accelerometer. |
| 149 | + If g is True, it will return the data in g |
| 150 | + If g is False, it will return the data in m/s^2 |
| 151 | + Returns a dictionary with the measurement results. |
| 152 | + """ |
| 153 | + # Read the data from the MPU-6050 |
| 154 | + x = self.read_i2c_word(self.ACCEL_XOUT0) |
| 155 | + y = self.read_i2c_word(self.ACCEL_YOUT0) |
| 156 | + z = self.read_i2c_word(self.ACCEL_ZOUT0) |
| 157 | + |
| 158 | + accel_scale_modifier = None |
| 159 | + accel_range = self.read_accel_range(True) |
| 160 | + |
| 161 | + if accel_range == self.ACCEL_RANGE_2G: |
| 162 | + accel_scale_modifier = self.ACCEL_SCALE_MODIFIER_2G |
| 163 | + elif accel_range == self.ACCEL_RANGE_4G: |
| 164 | + accel_scale_modifier = self.ACCEL_SCALE_MODIFIER_4G |
| 165 | + elif accel_range == self.ACCEL_RANGE_8G: |
| 166 | + accel_scale_modifier = self.ACCEL_SCALE_MODIFIER_8G |
| 167 | + elif accel_range == self.ACCEL_RANGE_16G: |
| 168 | + accel_scale_modifier = self.ACCEL_SCALE_MODIFIER_16G |
| 169 | + else: |
| 170 | + print("Unkown range - accel_scale_modifier set to self.ACCEL_SCALE_MODIFIER_2G") |
| 171 | + accel_scale_modifier = self.ACCEL_SCALE_MODIFIER_2G |
| 172 | + |
| 173 | + x = x / accel_scale_modifier |
| 174 | + y = y / accel_scale_modifier |
| 175 | + z = z / accel_scale_modifier |
| 176 | + |
| 177 | + if g is True: |
| 178 | + return {'x': x, 'y': y, 'z': z} |
| 179 | + elif g is False: |
| 180 | + x = x * self.GRAVITIY_MS2 |
| 181 | + y = y * self.GRAVITIY_MS2 |
| 182 | + z = z * self.GRAVITIY_MS2 |
| 183 | + return {'x': x, 'y': y, 'z': z} |
| 184 | + |
| 185 | + def set_gyro_range(self, gyro_range): |
| 186 | + """Sets the range of the gyroscope to range. |
| 187 | + gyro_range -- the range to set the gyroscope to. Using a pre-defined |
| 188 | + range is advised. |
| 189 | + """ |
| 190 | + # First change it to 0x00 to make sure we write the correct value later |
| 191 | + self.bus.write_byte_data(self.address, self.GYRO_CONFIG, 0x00) |
| 192 | + |
| 193 | + # Write the new range to the ACCEL_CONFIG register |
| 194 | + self.bus.write_byte_data(self.address, self.GYRO_CONFIG, gyro_range) |
| 195 | + |
| 196 | + def read_gyro_range(self, raw = False): |
| 197 | + """Reads the range the gyroscope is set to. |
| 198 | + If raw is True, it will return the raw value from the GYRO_CONFIG |
| 199 | + register. |
| 200 | + If raw is False, it will return 250, 500, 1000, 2000 or -1. If the |
| 201 | + returned value is equal to -1 something went wrong. |
| 202 | + """ |
| 203 | + # Get the raw value |
| 204 | + raw_data = self.bus.read_byte_data(self.address, self.GYRO_CONFIG) |
| 205 | + |
| 206 | + if raw is True: |
| 207 | + return raw_data |
| 208 | + elif raw is False: |
| 209 | + if raw_data == self.GYRO_RANGE_250DEG: |
| 210 | + return 250 |
| 211 | + elif raw_data == self.GYRO_RANGE_500DEG: |
| 212 | + return 500 |
| 213 | + elif raw_data == self.GYRO_RANGE_1000DEG: |
| 214 | + return 1000 |
| 215 | + elif raw_data == self.GYRO_RANGE_2000DEG: |
| 216 | + return 2000 |
| 217 | + else: |
| 218 | + return -1 |
| 219 | + |
| 220 | + def get_gyro_data(self): |
| 221 | + """Gets and returns the X, Y and Z values from the gyroscope. |
| 222 | + Returns the read values in a dictionary. |
| 223 | + """ |
| 224 | + # Read the raw data from the MPU-6050 |
| 225 | + x = self.read_i2c_word(self.GYRO_XOUT0) |
| 226 | + y = self.read_i2c_word(self.GYRO_YOUT0) |
| 227 | + z = self.read_i2c_word(self.GYRO_ZOUT0) |
| 228 | + |
| 229 | + gyro_scale_modifier = None |
| 230 | + gyro_range = self.read_gyro_range(True) |
| 231 | + |
| 232 | + if gyro_range == self.GYRO_RANGE_250DEG: |
| 233 | + gyro_scale_modifier = self.GYRO_SCALE_MODIFIER_250DEG |
| 234 | + elif gyro_range == self.GYRO_RANGE_500DEG: |
| 235 | + gyro_scale_modifier = self.GYRO_SCALE_MODIFIER_500DEG |
| 236 | + elif gyro_range == self.GYRO_RANGE_1000DEG: |
| 237 | + gyro_scale_modifier = self.GYRO_SCALE_MODIFIER_1000DEG |
| 238 | + elif gyro_range == self.GYRO_RANGE_2000DEG: |
| 239 | + gyro_scale_modifier = self.GYRO_SCALE_MODIFIER_2000DEG |
| 240 | + else: |
| 241 | + print("Unkown range - gyro_scale_modifier set to self.GYRO_SCALE_MODIFIER_250DEG") |
| 242 | + gyro_scale_modifier = self.GYRO_SCALE_MODIFIER_250DEG |
| 243 | + |
| 244 | + x = x / gyro_scale_modifier |
| 245 | + y = y / gyro_scale_modifier |
| 246 | + z = z / gyro_scale_modifier |
| 247 | + |
| 248 | + return {'x': x, 'y': y, 'z': z} |
| 249 | + |
| 250 | + def get_all_data(self): |
| 251 | + """Reads and returns all the available data.""" |
| 252 | + temp = get_temp() |
| 253 | + accel = get_accel_data() |
| 254 | + gyro = get_gyro_data() |
| 255 | + |
| 256 | + return [accel, gyro, temp] |
| 257 | + |
| 258 | +if __name__ == "__main__": |
| 259 | + z = 0.0 |
| 260 | + t = time.time() |
| 261 | + mpu = MPU6050(0x68) |
| 262 | + mpu.set_gyro_range(mpu.GYRO_RANGE_250DEG) |
| 263 | + while True: |
| 264 | + gyro_data = mpu.get_gyro_data() |
| 265 | + dt = time.time() - t |
| 266 | + t = time.time() |
| 267 | + if abs(gyro_data['z']) > mpu.GYRO_THRESHOLD_Z: |
| 268 | + dz = gyro_data['z'] * dt |
| 269 | + z = z + dz |
| 270 | + print( "z: ", z, end="\r") |
0 commit comments