1212#include "hardware/i2c.h"
1313#include "mpu6050_i2c.h"
1414
15- /* Example code to talk to a MPU6050 MEMS accelerometer and gyroscope
16-
17- This is taking to simple approach of simply reading registers. It's perfectly
18- possible to link up an interrupt line and set things up to read from the
19- inbuilt FIFO to make it more useful.
20-
21- NOTE: Ensure the device is capable of being driven at 3.3v NOT 5v. The Pico
22- GPIO (and therefor I2C) cannot be used at 5v. You will need to use a level
23- shifter on the I2C lines if you want to run the board at 5v.
24-
25- Connections on Raspberry Pi Pico board, other boards may vary.
26-
27- GPIO PICO_DEFAULT_I2C_SDA_PIN (On Pico this is GP4 (pin 6)) -> SDA on MPU6050 board
28- GPIO PICO_DEFAULT_I2C_SCL_PIN (On Pico this is GP5 (pin 7)) -> SCL on MPU6050 board
29- 3.3v (pin 36) -> VCC on MPU6050 board
30- GND (pin 38) -> GND on MPU6050 board
31- */
3215
3316// By default these devices are on bus address 0x68
3417static uint8_t bus_addr = 0x68 ;
@@ -43,6 +26,7 @@ const float gyro_scale_rad[] = {M_PI / 180. * 250. / 0x7fff, M_PI / 180. * 500.
4326 M_PI / 180. * 1000. / 0x7fff , M_PI / 180. * 2000. / 0x7fff };
4427const float accel_scale_vals [] = {2 * 9.81 / 0x7fff , 4 * 9.81 / 0x7fff , 8 * 9.81 / 0x7fff , 16 * 9.81 / 0x7fff };
4528
29+ // timing data, indexed with lowpass_filter_cfg
4630const int accel_bandwidth_lookup [] = {260 , 184 , 94 , 44 , 21 , 10 , 5 };
4731const float accel_delay_lookup [] = {0 , 2.0 , 3.0 , 4.9 , 8.5 , 13.8 , 19.0 };
4832const int gyro_bandwidth_lookup [] = {256 , 188 , 98 , 42 , 20 , 10 , 5 };
@@ -98,10 +82,12 @@ void mpu6050_reset() {
9882 sleep_ms (100 );
9983}
10084
101- void mpu6050_read_raw (int16_t accel [3 ], int16_t gyro [ 3 ], int16_t * temp ) {
85+ void mpu6050_read_accel_raw (int16_t accel [3 ]) {
10286 mpu6050_readreg16 (REG_ACCEL_OUT , accel , 3 );
87+ }
88+
89+ void mpu6050_read_gyro_raw (int16_t gyro [3 ]) {
10390 mpu6050_readreg16 (REG_GYRO_OUT , gyro , 3 );
104- mpu6050_readreg16 (REG_TEMP_OUT , temp , 1 );
10591}
10692
10793void mpu6050_read (float accel [3 ], float gyro_rad [3 ], float * temp , MPU6050_Scale accel_scale , MPU6050_Scale gyro_scale ) {
@@ -112,6 +98,21 @@ void mpu6050_read(float accel[3], float gyro_rad[3], float *temp, MPU6050_Scale
11298 if (temp ) * temp = mpu6050_scale_temp (buffer [3 ]);
11399 mpu6050_scale_gyro_rad (gyro_rad , buffer + 4 , gyro_scale );
114100}
101+ void mpu6050_read_accel (float accel [3 ], MPU6050_Scale accel_scale ) {
102+ int16_t buffer [3 ];
103+ mpu6050_readreg16 (REG_ACCEL_OUT , buffer , 3 );
104+ mpu6050_scale_accel (accel , buffer , accel_scale );
105+ }
106+ void mpu6050_read_gyro_rad (float gyro [3 ], MPU6050_Scale gyro_scale ) {
107+ int16_t buffer [3 ];
108+ mpu6050_readreg16 (REG_GYRO_OUT , buffer , 3 );
109+ mpu6050_scale_gyro_rad (gyro , buffer , gyro_scale );
110+ }
111+ void mpu6050_read_gyro_deg (float gyro [3 ], MPU6050_Scale gyro_scale ) {
112+ int16_t buffer [3 ];
113+ mpu6050_readreg16 (REG_GYRO_OUT , buffer , 3 );
114+ mpu6050_scale_gyro_deg (gyro , buffer , gyro_scale );
115+ }
115116
116117// functions to set and calculate with sensitivity
117118void mpu6050_setscale_gyro (MPU6050_Scale gyro_scale ) {
@@ -208,42 +209,3 @@ bool setup_MPU6050_i2c() {
208209 return true;
209210#endif
210211}
211-
212- int run_MPU6050_demo () {
213- MPU6050_Scale testscale = MPU_FS_0 ;
214- mpu6050_setscale_accel (testscale );
215- mpu6050_setscale_gyro (testscale );
216-
217- int16_t accel_raw [3 ], gyro_raw [3 ], temp_raw ;
218- float acceleration [3 ], gyro [3 ];
219-
220- mpu6050_reset ();
221- //setting CLKSEL = 1 gets better results than 0 if gyro is running and no sleep mode
222- mpu6050_power (1 , false, false, false);
223-
224- mpu6050_set_timing (5 , 99 ); // 10 Hz
225-
226- while (1 ) {
227- while (!mpu6050_is_connected ()) {
228- printf ("MPU6050 is not connected...\n" );
229- sleep_ms (1000 );
230- }
231- uint64_t time_us = to_us_since_boot (get_absolute_time ());
232- mpu6050_read_raw (accel_raw , gyro_raw , & temp_raw );
233- time_us = to_us_since_boot (get_absolute_time ()) - time_us ;
234-
235- // These are the raw numbers from the chip
236- printf ("Raw Acc. X = %d, Y = %d, Z = %d\n" , accel_raw [0 ], accel_raw [1 ], accel_raw [2 ]);
237- printf ("Raw Gyro. X = %d, Y = %d, Z = %d\n" , gyro_raw [0 ], gyro_raw [1 ], gyro_raw [2 ]);
238- // This is chip temperature.
239- printf ("Temp [C] = %f\n" , mpu6050_scale_temp (temp_raw ));
240- printf ("Read time: %llu us; \t\t Accel and Gyro Scale = %d\n" , time_us , (int )testscale );
241- mpu6050_scale_accel (acceleration , accel_raw , testscale );
242- mpu6050_scale_gyro_rad (gyro , gyro_raw , testscale );
243- printf ("Acc [m/s^2] X = %f, Y = %f, Z = %f\n" , acceleration [0 ], acceleration [1 ], acceleration [2 ]);
244- printf ("Gyro [rad/s] X = %f, Y = %f, Z = %f\n" , gyro [0 ], gyro [1 ], gyro [2 ]);
245-
246- sleep_ms (100 );
247- }
248- return 0 ;
249- }
0 commit comments