73
73
_MPU6050_ACCEL_CONFIG = 0x1C # Accelerometer specific configration register
74
74
_MPU6050_INT_PIN_CONFIG = 0x37 # Interrupt pin configuration register
75
75
_MPU6050_ACCEL_OUT = 0x3B # base address for sensor data reads
76
- _MPU6050_TEMP_H = 0x41 # Temperature data high byte register
77
- _MPU6050_TEMP_L = 0x42 # Temperature data low byte register
76
+ _MPU6050_TEMP_OUT = 0x41 # Temperature data high byte register
77
+ _MPU6050_GYRO_OUT = 0x43 # base address for sensor data reads
78
78
_MPU6050_USER_CTRL = 0x6A # FIFO and I2C Master control register
79
79
_MPU6050_PWR_MGMT_1 = 0x6B # Primary power/sleep control register
80
80
_MPU6050_PWR_MGMT_2 = 0x6C # Secondary power/sleep control register
81
81
_MPU6050_WHO_AM_I = 0x75 # Divice ID register
82
82
83
83
STANDARD_GRAVITY = 9.80665
84
- # typedef enum fsync_out {
85
- # MPU6050_FSYNC_OUT_DISABLED,
86
- # MPU6050_FSYNC_OUT_TEMP,
87
- # MPU6050_FSYNC_OUT_GYROX,
88
- # MPU6050_FSYNC_OUT_GYROY,
89
- # MPU6050_FSYNC_OUT_GYROZ,
90
- # MPU6050_FSYNC_OUT_ACCELX,
91
- # MPU6050_FSYNC_OUT_ACCELY,
92
- # MPU6050_FSYNC_OUT_ACCEL_Z,
93
- # } mpu6050_fsync_out_t;
84
+
94
85
95
86
# /**
96
87
# * @brief Clock source options
152
143
# * Allowed values for `setCycleRate`.
153
144
# */
154
145
# typedef enum cycle_rate {
155
- # MPU6050_CYCLE_1_25_HZ, # 1.25 Hz
156
- # MPU6050_CYCLE_5_HZ, # 5 Hz
157
- # MPU6050_CYCLE_20_HZ, # 20 Hz
158
- # MPU6050_CYCLE_40_HZ, # 40 Hz
146
+ MPU6050_CYCLE_1_25_HZ = 0 # 1.25 Hz
147
+ MPU6050_CYCLE_5_HZ = 1 # 5 Hz
148
+ MPU6050_CYCLE_20_HZ = 2 # 20 Hz
149
+ MPU6050_CYCLE_40_HZ = 3 # 40 Hz
159
150
# } mpu6050_cycle_rate_t;
160
151
class MPU6050 :
161
152
@@ -189,46 +180,52 @@ def reset(self):
189
180
_filter_bandwidth = RWBits (2 , _MPU6050_CONFIG , 3 )
190
181
_clock_source = RWBits (3 , _MPU6050_PWR_MGMT_1 , 0 )
191
182
sleep = RWBit (_MPU6050_PWR_MGMT_1 , 6 , 1 )
192
- _raw_data_array = StructArray (_MPU6050_ACCEL_OUT , ">h" , 7 )
183
+ _raw_accel_data = StructArray (_MPU6050_ACCEL_OUT , ">h" , 3 )
184
+ _raw_gyro_data = StructArray (_MPU6050_GYRO_OUT , ">h" , 3 )
185
+ _raw_temp_data = UnaryStruct (_MPU6050_TEMP_OUT , ">h" )
186
+ _cycle = RWBit (_MPU6050_PWR_MGMT_1 , 5 )
187
+ _cycle_rate = RWBits (2 , _MPU6050_PWR_MGMT_2 , 6 , 1 )
193
188
194
189
@property
195
190
def temperature (self ):
196
- """docs """
197
- raw_temperature = self ._raw_data_array [ 3 ][ 0 ]
191
+ """The current temperature in º C """
192
+ raw_temperature = self ._raw_temp_data
198
193
temp = (raw_temperature + 12412.0 ) / 340.0
199
194
return temp
200
195
201
196
@property
202
197
def acceleration (self ):
203
- raw_data = self ._raw_data_array
198
+ """Acceleration X, Y, and Z axis data in m/s^2"""
199
+ raw_data = self ._raw_accel_data
204
200
raw_x = raw_data [0 ][0 ]
205
201
raw_y = raw_data [1 ][0 ]
206
202
raw_z = raw_data [2 ][0 ]
207
-
203
+
208
204
accel_range = self ._accel_range
209
205
accel_scale = 1
210
- if ( accel_range == MPU6050_RANGE_16_G ) :
206
+ if accel_range == MPU6050_RANGE_16_G :
211
207
accel_scale = 2048
212
- if ( accel_range == MPU6050_RANGE_8_G ) :
208
+ if accel_range == MPU6050_RANGE_8_G :
213
209
accel_scale = 4096
214
- if ( accel_range == MPU6050_RANGE_4_G ) :
210
+ if accel_range == MPU6050_RANGE_4_G :
215
211
accel_scale = 8192
216
- if ( accel_range == MPU6050_RANGE_2_G ) :
212
+ if accel_range == MPU6050_RANGE_2_G :
217
213
accel_scale = 16384
218
214
219
215
# setup range dependant scaling
220
216
accel_x = (raw_x / accel_scale ) * STANDARD_GRAVITY
221
217
accel_y = (raw_y / accel_scale ) * STANDARD_GRAVITY
222
218
accel_z = (raw_z / accel_scale ) * STANDARD_GRAVITY
223
-
219
+
224
220
return (accel_x , accel_y , accel_z )
225
221
226
222
@property
227
223
def gyro (self ):
228
- raw_data = self ._raw_data_array
229
- raw_x = raw_data [4 ][0 ]
230
- raw_y = raw_data [5 ][0 ]
231
- raw_z = raw_data [6 ][0 ]
224
+ """Gyroscope X, Y, and Z axis data in º/s"""
225
+ raw_data = self ._raw_gyro_data
226
+ raw_x = raw_data [0 ][0 ]
227
+ raw_y = raw_data [1 ][0 ]
228
+ raw_z = raw_data [2 ][0 ]
232
229
233
230
gyro_scale = 1
234
231
gyro_range = self ._gyro_range
@@ -245,21 +242,17 @@ def gyro(self):
245
242
gyro_x = (raw_x / gyro_scale )
246
243
gyro_y = (raw_y / gyro_scale )
247
244
gyro_z = (raw_z / gyro_scale )
248
-
245
+
249
246
return (gyro_x , gyro_y , gyro_z )
250
247
251
- # mpu6050_gyro_range_t gyro_range = getGyroRange();
252
-
253
- # float gyro_scale = 1;
254
- # if (gyro_range == MPU6050_RANGE_250_DEG)
255
- # gyro_scale = 131;
256
- # if (gyro_range == MPU6050_RANGE_500_DEG)
257
- # gyro_scale = 65.5;
258
- # if (gyro_range == MPU6050_RANGE_1000_DEG)
259
- # gyro_scale = 32.8;
260
- # if (gyro_range == MPU6050_RANGE_2000_DEG)
261
- # gyro_scale = 16.4;
262
-
263
- # gyroX = rawGyro / gyro_scale;
264
- # gyroY = rawGyro / gyro_scale;
265
- # gyroZ = rawGyro / gyro_scale;
248
+ @property
249
+ def cycle (self ):
250
+ """Enable or disable perodic measurement at a rate set by `cycle_rate`.
251
+ If the sensor was in sleep mode, it will be waken up to cycle"""
252
+ return self ._cycle
253
+
254
+ @cycle .setter
255
+ def cycle (self , value ):
256
+ self .sleep = not value
257
+ self ._cycle = value
258
+
0 commit comments