@@ -36,10 +36,10 @@ const uint8_t REG_PWR_MGMT_1 = 0x6B, REG_ACCEL_OUT = 0x3B, REG_GYRO_OUT = 0x43,
36
36
REG_SIGNAL_PATH_RESET = 0x68 , REG_GYRO_CONFIG = 0x1B , REG_ACCEL_CONFIG = 0x1C ;
37
37
typedef enum MPU6050_Scale {MPU_FS_0 , MPU_FS_1 , MPU_FS_2 , MPU_FS_3 } MPU6050_Scale ;
38
38
39
- float gyro_scale_deg [] = {250. / 0x7fff , 500. / 0x7fff , 1000. / 0x7fff , 2000. / 0x7fff };
40
- float gyro_scale_rad [] = {M_PI / 180. * 250. / 0x7fff , M_PI / 180. * 500. / 0x7fff ,
39
+ const float gyro_scale_deg [] = {250. / 0x7fff , 500. / 0x7fff , 1000. / 0x7fff , 2000. / 0x7fff };
40
+ const float gyro_scale_rad [] = {M_PI / 180. * 250. / 0x7fff , M_PI / 180. * 500. / 0x7fff ,
41
41
M_PI / 180. * 1000. / 0x7fff , M_PI / 180. * 2000. / 0x7fff };
42
- float accel_scale [] = {2. / 0x7fff , 4. / 0x7fff , 8. / 0x7fff , 16. / 0x7fff };
42
+ const float accel_scale [] = {2 * 9.81 / 0x7fff , 4 * 9.81 / 0x7fff , 8 * 9.81 / 0x7fff , 16 * 9.81 / 0x7fff };
43
43
44
44
45
45
////////////////////////////////////////////////////lower level functions for i2c
@@ -102,31 +102,45 @@ void mpu6050_read_raw(int16_t accel[3], int16_t gyro[3], int16_t *temp) {
102
102
mpu6050_readreg16 (REG_TEMP_OUT , temp , 1 );
103
103
}
104
104
105
+ void mpu6050_read (float accel [3 ], float gyro_rad [3 ], float * temp , MPU6050_Scale accel_scale , MPU6050_Scale gyro_scale ) {
106
+ int16_t buffer [7 ];
107
+ mpu6050_readreg16 (REG_ACCEL_OUT , buffer , 7 ); //reads all at same time
108
+ mpu6050_scale_accel (accel , buffer , accel_scale );
109
+ if (temp ) * temp = mpu6050_scale_temp (buffer [3 ]);
110
+ mpu6050_scale_gyro_rad (gyro , buffer + 4 , gyro_scale );
111
+ }
112
+
105
113
// set and use sensitivity
106
- void mpu6050_setscale ( MPU6050_Scale accel_scale , MPU6050_Scale gyro_scale ) {
114
+ void mpu6050_setscale_gyro ( MPU6050_Scale gyro_scale ) {
107
115
mpu6050_writereg (REG_GYRO_CONFIG , (uint8_t )gyro_scale << 3 );
116
+ }
117
+ void mpu6050_setscale_accel (MPU6050_Scale accel_scale ) {
108
118
mpu6050_writereg (REG_ACCEL_CONFIG , (uint8_t )accel_scale << 3 );
109
119
}
110
- void mpu6050_scale_accel (float * accel , int16_t * accel_raw , MPU6050_Scale scale ) {
120
+ void mpu6050_scale_accel (float accel [ 3 ] , int16_t accel_raw [ 3 ] , MPU6050_Scale scale ) {
111
121
for (int i = 0 ; i < 3 ; i ++ ) {
112
122
accel [i ] = accel_raw [i ] * accel_scale [scale ];
113
123
}
114
124
}
115
- void mpu6050_scale_gyro_deg (float * gyro , int16_t * gyro_raw , MPU6050_Scale scale ) {
125
+ void mpu6050_scale_gyro_deg (float gyro [ 3 ] , int16_t gyro_raw [ 3 ] , MPU6050_Scale scale ) {
116
126
for (int i = 0 ; i < 3 ; i ++ ) {
117
127
gyro [i ] = gyro_raw [i ] * gyro_scale_deg [scale ];
118
128
}
119
129
}
120
- void mpu6050_scale_gyro_rad (float * gyro , int16_t * gyro_raw , MPU6050_Scale scale ) {
130
+ void mpu6050_scale_gyro_rad (float gyro [ 3 ] , int16_t gyro_raw [ 3 ] , MPU6050_Scale scale ) {
121
131
for (int i = 0 ; i < 3 ; i ++ ) {
122
132
gyro [i ] = gyro_raw [i ] * gyro_scale_rad [scale ];
123
133
}
124
134
}
135
+ float mpu6050_scale_temp (float temp_raw ) {
136
+ return (temp_raw / 340.0 ) + 36.53 ;
137
+ }
138
+
125
139
//TODO: set timing
126
140
//TODO: set and read filter cutoff
127
141
//TODO: read self test
128
142
129
-
143
+ //TODO: functional mpu test
130
144
int main () {
131
145
stdio_init_all ();
132
146
#if !defined(i2c_default ) || !defined(PICO_DEFAULT_I2C_SDA_PIN ) || !defined(PICO_DEFAULT_I2C_SCL_PIN )
@@ -148,7 +162,8 @@ int main() {
148
162
//setting CLKSEL = 1 gets better results than 0 if gyro is running and no sleep mode
149
163
mpu6050_power (1 , false, false, false);
150
164
MPU6050_Scale testscale = MPU_FS_0 ;
151
- mpu6050_setscale (testscale , testscale );
165
+ mpu6050_setscale_accel (testscale );
166
+ mpu6050_setscale_gyro (testscale );
152
167
153
168
int16_t accel_raw [3 ], gyro_raw [3 ], temp_raw ;
154
169
float acceleration [3 ], gyro [3 ];
@@ -157,14 +172,12 @@ int main() {
157
172
mpu6050_read_raw (accel_raw , gyro_raw , & temp_raw );
158
173
//TODO time read function
159
174
160
- // These are the raw numbers from the chip, so will need tweaking to be really useful.
161
- // See the datasheet for more information
175
+ // These are the raw numbers from the chip
162
176
printf ("Raw Acc. X = %d, Y = %d, Z = %d\n" , accel_raw [0 ], accel_raw [1 ], accel_raw [2 ]);
163
177
printf ("Raw Gyro. X = %d, Y = %d, Z = %d\n" , gyro_raw [0 ], gyro_raw [1 ], gyro_raw [2 ]);
164
178
// Temperature is simple so use the datasheet calculation to get deg C.
165
179
// Note this is chip temperature.
166
- printf ("Temp [C] = %f \t\t Scale = %d\n" , (temp_raw / 340.0 ) + 36.53 , testscale );
167
- //include the scaling
180
+ printf ("Temp [C] = %f \t\t Scale = %d\n" , mpu6050_scale_temp (temp_raw ), testscale );
168
181
mpu6050_scale_accel (acceleration , accel_raw , testscale );
169
182
mpu6050_scale_gyro_rad (gyro , gyro_raw , testscale );
170
183
printf ("Acc [m/s^2] X = %f, Y = %f, Z = %f\n" , acceleration [0 ], acceleration [1 ], acceleration [2 ]);
0 commit comments