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