1- from __future__ import print_function
21"""This program handles the communication over I2C
32between a Raspberry Pi and a MPU-6050 Gyroscope / Accelerometer combo.
43Made by: MrTijn/Tijndagamer
54Released under the MIT License
65Copyright 2015
76"""
87
9- import smbus2 as smbus
108import time
9+ import smbus2 as smbus
1110
1211class MPU6050 :
1312
@@ -88,10 +87,9 @@ def read_i2c_word(self, register):
8887
8988 value = (high << 8 ) + low
9089
91- if ( value >= 0x8000 ) :
90+ if value >= 0x8000 :
9291 return - ((65535 - value ) + 1 )
93- else :
94- return value
92+ return value
9593
9694 # MPU-6050 Methods
9795
@@ -120,7 +118,7 @@ def set_accel_range(self, accel_range):
120118 # Write the new range to the ACCEL_CONFIG register
121119 self .bus .write_byte_data (self .address , self .ACCEL_CONFIG , accel_range )
122120
123- def read_accel_range (self , raw = False ):
121+ def read_accel_range (self , raw = False ):
124122 """Reads the range the accelerometer is set to.
125123 If raw is True, it will return the raw value from the ACCEL_CONFIG
126124 register
@@ -141,10 +139,9 @@ def read_accel_range(self, raw = False):
141139 return 8
142140 elif raw_data == self .ACCEL_RANGE_16G :
143141 return 16
144- else :
145- return - 1
142+ return - 1
146143
147- def get_accel_data (self , g = False ):
144+ def get_accel_data (self , g = False ):
148145 """Gets and returns the X, Y and Z values from the accelerometer.
149146 If g is True, it will return the data in g
150147 If g is False, it will return the data in m/s^2
@@ -174,13 +171,11 @@ def get_accel_data(self, g = False):
174171 y = y / accel_scale_modifier
175172 z = z / accel_scale_modifier
176173
177- if g is True :
178- return {'x' : x , 'y' : y , 'z' : z }
179- elif g is False :
174+ if g is False :
180175 x = x * self .GRAVITIY_MS2
181176 y = y * self .GRAVITIY_MS2
182177 z = z * self .GRAVITIY_MS2
183- return {'x' : x , 'y' : y , 'z' : z }
178+ return {'x' : x , 'y' : y , 'z' : z }
184179
185180 def set_gyro_range (self , gyro_range ):
186181 """Sets the range of the gyroscope to range.
@@ -193,7 +188,7 @@ def set_gyro_range(self, gyro_range):
193188 # Write the new range to the ACCEL_CONFIG register
194189 self .bus .write_byte_data (self .address , self .GYRO_CONFIG , gyro_range )
195190
196- def read_gyro_range (self , raw = False ):
191+ def read_gyro_range (self , raw = False ):
197192 """Reads the range the gyroscope is set to.
198193 If raw is True, it will return the raw value from the GYRO_CONFIG
199194 register.
@@ -214,8 +209,7 @@ def read_gyro_range(self, raw = False):
214209 return 1000
215210 elif raw_data == self .GYRO_RANGE_2000DEG :
216211 return 2000
217- else :
218- return - 1
212+ return - 1
219213
220214 def get_gyro_data (self ):
221215 """Gets and returns the X, Y and Z values from the gyroscope.
@@ -249,9 +243,9 @@ def get_gyro_data(self):
249243
250244 def get_all_data (self ):
251245 """Reads and returns all the available data."""
252- temp = get_temp ()
253- accel = get_accel_data ()
254- gyro = get_gyro_data ()
246+ temp = self . get_temp ()
247+ accel = self . get_accel_data ()
248+ gyro = self . get_gyro_data ()
255249
256250 return [accel , gyro , temp ]
257251
@@ -267,4 +261,4 @@ def get_all_data(self):
267261 if abs (gyro_data ['z' ]) > mpu .GYRO_THRESHOLD_Z :
268262 dz = gyro_data ['z' ] * dt
269263 z = z + dz
270- print ( "z: " , z , end = "\r " )
264+ print ("z: " , z , end = "\r " )
0 commit comments