MBot Software Library  v1.0
An API documentation to mbot_firmware repository
mpu.h
1 
34 #ifndef RC_MPU_MPU_H
35 #define RC_MPU_MPU_H
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 
42 #include <stdio.h>
43 #include <stdint.h>
44 
45 #include "dmp_firmware.h"
46 #include "mpu_defs.h"
47 #include "dmpKey.h"
48 #include "dmpmap.h"
49 #include <pico/stdlib.h>
50 #include <pico/binary_info.h>
51 #include <hardware/i2c.h>
52 #include <hardware/gpio.h>
53 #include <hardware/flash.h>
54 
55 
56 // macros
57 #ifndef ARRAY_SIZE
58 #define ARRAY_SIZE(array) sizeof(array)/sizeof(array[0])
59 #endif
60 
61 #ifndef min
62 #define min(a, b) ((a < b) ? a : b)
63 #endif
64 
65 #ifndef __unused
66 #define __unused __attribute__ ((unused))
67 #endif
68 
69 #ifndef unlikely
70 #define unlikely(x) __builtin_expect (!!(x), 0)
71 #endif
72 
73 #ifndef likely
74 #define likely(x) __builtin_expect (!!(x), 1)
75 #endif
76 
77 // Flash-based address of the last sector
78 #define FLASH_TARGET_OFFSET (PICO_FLASH_SIZE_BYTES - FLASH_SECTOR_SIZE)
79 
80 // there should be 28 or 35 bytes in the FIFO if the magnetometer is disabled
81 // or enabled.
82 #define FIFO_LEN_QUAT_TAP 20 // 16 for quat, 4 for tap
83 #define FIFO_LEN_QUAT_ACCEL_GYRO_TAP 32 // 16 quat, 6 accel, 6 gyro, 4 tap
84 #define MAX_FIFO_BUFFER (FIFO_LEN_QUAT_ACCEL_GYRO_TAP*5)
85 
86 // error threshold checks
87 #define QUAT_ERROR_THRESH (1L<<16) // very precise threshold
88 #define QUAT_MAG_SQ_NORMALIZED (1L<<28)
89 #define QUAT_MAG_SQ_MIN (QUAT_MAG_SQ_NORMALIZED - QUAT_ERROR_THRESH)
90 #define QUAT_MAG_SQ_MAX (QUAT_MAG_SQ_NORMALIZED + QUAT_ERROR_THRESH)
91 #define GYRO_CAL_THRESH 50 // std dev below which to consider still
92 #define ACCEL_CAL_THRESH 100 // std dev below which to consider still
93 #define GYRO_OFFSET_THRESH 500
94 
98 #define DMP_AUTO_CAL_GYRO 0
99 #define DMP_FETCH_ACCEL_GYRO 0
100 #define DMP_INIT_ORIENTATION ORIENTATION_Z_UP
101 #define DMP_TAP_THRESHOLD 210
102 #define DMP_CFG_SAMPLE_RATE 100
103 #define DMP_ENABLE_MAGNETOMETER 0
104 #define DEBUG_OUTPUT 0
105 
106 #define rc_MPU_DEFAULT_I2C_ADDR 0x68
107 #define rc_MPU_ALT_I2C_ADDR 0x69
108 #define rc_MPU_INTERRUPT_GPIO 22
109 #define rc_MPU_I2C_BAUDRATE 400000
110 
111 // defines for index location within TaitBryan and quaternion vectors
112 #define TB_PITCH_X 0
113 #define TB_ROLL_Y 1
114 #define TB_YAW_Z 2
115 #define QUAT_W 0
116 #define QUAT_X 1
117 #define QUAT_Y 2
118 #define QUAT_Z 3
119 
120 #define DEG_TO_RAD 0.0174532925199
121 #define RAD_TO_DEG 57.295779513
122 #define MS2_TO_G 0.10197162129
123 #define G_TO_MS2 9.80665
124 #define PI M_PI
125 #define TWO_PI (2.0 * M_PI)
126 #define GYRO_TO_DEG 2000.0/32768.0;
127 
128 #define HAS_MAG
129 
130 
131 //static i2c_inst_t* callback_i2c;
132 
133 // enums credit from the RC Control Library
134 // Copyright (c) 2016 Strawson Design
135 // Copyright (c) 2020 BeagleBoard.org Foundation
136 
137 // used in accordance with their MIT license
138 
146 typedef enum rc_mpu_accel_fsr_t{
147  ACCEL_FSR_2G,
148  ACCEL_FSR_4G,
149  ACCEL_FSR_8G,
150  ACCEL_FSR_16G
152 
160 typedef enum rc_mpu_gyro_fsr_t{
161  GYRO_FSR_250DPS,
162  GYRO_FSR_500DPS,
163  GYRO_FSR_1000DPS,
164  GYRO_FSR_2000DPS
166 
176 typedef enum rc_mpu_accel_dlpf_t{
177  ACCEL_DLPF_OFF,
178  ACCEL_DLPF_460,
179  ACCEL_DLPF_184,
180  ACCEL_DLPF_92,
181  ACCEL_DLPF_41,
182  ACCEL_DLPF_20,
183  ACCEL_DLPF_10,
184  ACCEL_DLPF_5
186 
196 typedef enum rc_mpu_gyro_dlpf_t{
197  GYRO_DLPF_OFF,
198  GYRO_DLPF_250,
199  GYRO_DLPF_184,
200  GYRO_DLPF_92,
201  GYRO_DLPF_41,
202  GYRO_DLPF_20,
203  GYRO_DLPF_10,
204  GYRO_DLPF_5
206 
213 typedef enum rc_mpu_orientation_t{
214  ORIENTATION_Z_UP = 136,
215  ORIENTATION_Z_DOWN = 396,
216  ORIENTATION_X_UP = 14,
217  ORIENTATION_X_DOWN = 266,
218  ORIENTATION_Y_UP = 112,
219  ORIENTATION_Y_DOWN = 336,
220  ORIENTATION_X_FORWARD = 133,
221  ORIENTATION_X_BACK = 161
223 
229 typedef enum rc_mpu_cfg_offset_t{
230  MPU_GYRO_CONFIG_ADDR = 0,
231  MPU_ACCEL_CONFIG_ADDR = 6,
232  MPU_MAG_CONFIG_ADDR = 54
234 
242 typedef struct rc_mpu_config_t{
246  i2c_inst_t* i2c_bus;
247  uint8_t i2c_addr;
250 
259 
271 
273 
274 
283 typedef struct rc_mpu_data_t{
286  double accel[3];
287  double gyro[3];
288  double mag[3];
289  double temp;
291 
294  int16_t raw_gyro[3];
295  int16_t raw_accel[3];
296  double accel_to_ms2;
297  double gyro_to_degs;
299 
302  double dmp_quat[4];
303  double dmp_TaitBryan[3];
308 
311  double fused_quat[4];
312  double fused_TaitBryan[3];
317 
324 void rc_dmp_callback(uint gpio, uint32_t events);
325 
326 void mpu9250_read_raw(i2c_inst_t* i2c, int16_t accel[3], int16_t gyro[3], int16_t mag[3], int16_t *temp);
327 
330 
339 
348 
358 int rc_mpu_power_off(void);
359 
361 
362 
365 
391 
392 
402 
403 
413 
414 
426 
427 
441 int rc_mpu_read_mag(rc_mpu_data_t* data);
443 
444 
447 
463 
464 
473 //int rc_mpu_set_dmp_callback(void (*func)(void));
474 
475 
476 // /**
477 // * @brief blocking function that returns once new DMP data is available
478 // *
479 // * @return Returns 0 once new data is available, 1 if the MPU is shutting
480 // * down due to rc_mpu_power_off, or -1 on error.
481 // */
482 // int rc_mpu_block_until_dmp_data(void);
483 
484 
485 // /**
486 // * @brief calculates number of nanoseconds since the last DMP interrupt
487 // *
488 // * @return nanoseconds since last interrupt, or -1 if no interrupt received
489 // * yet.
490 // */
491 // int64_t rc_mpu_nanos_since_last_dmp_interrupt(void);
492 
493 
501 //int rc_mpu_set_tap_callback(void (*func)(int direction, int counter));
502 
503 
504 // /**
505 // * @brief blocking function that returns when a tap is detected
506 // *
507 // * @return Returns 0 once a tap is detected, 1 if the MPU is shutting down
508 // * due to rc_mpu_power_off(), or -1 on error.
509 // */
510 // int rc_mpu_block_until_tap(void);
511 
512 
513 // /**
514 // * @brief calculates nanoseconds since last tap was detected
515 // *
516 // * @return nanoseconds since last tap, or -1 if no tap has been detected
517 // * yet.
518 // */
519 // int64_t rc_mpu_nanos_since_last_tap(void);
521 
522 
523 
526 
539 
540 
553 
554 
567 
568 int rc_mpu_reset_accel_cal(i2c_inst_t* i2c);
569 
579 int rc_mpu_is_gyro_calibrated(void);
580 
581 
591 int rc_mpu_is_mag_calibrated(void);
592 
593 
604 
605 
606 int rc_print_gyro_calibration(i2c_inst_t* i2c);
607 
609 
610 
611 #ifdef __cplusplus
612 }
613 #endif
614 
615 #endif // rc_MPU_MPU_H
int rc_mpu_read_accel(rc_mpu_data_t *data)
Reads accelerometer data from the MPU.
Definition: mpu.c:139
double fused_quat[4]
fused and normalized quaternion
Definition: mpu.h:311
uint8_t i2c_addr
default is 0x68, pull pin ad0 high to make it 0x69
Definition: mpu.h:247
rc_mpu_orientation_t
Orientation of the sensor.
Definition: mpu.h:213
int last_tap_direction
direction of last tap, 1-6 corresponding to X+ X- Y+ Y- Z+ Z-
Definition: mpu.h:305
double gyro[3]
gyroscope (XYZ) in units of degrees/s
Definition: mpu.h:287
int rc_mpu_read_mag(rc_mpu_data_t *data)
Reads magnetometer data from the MPU.
Definition: mpu.c:178
struct rc_mpu_data_t rc_mpu_data_t
data struct populated with new sensor data
int rc_mpu_is_gyro_calibrated(void)
Checks if a gyro calibration file is saved to disk.
Definition: mpu.c:2838
rc_mpu_accel_fsr_t accel_fsr
accelerometer full scale range, default ACCEL_FSR_8G
Definition: mpu.h:253
double dmp_TaitBryan[3]
Tait-Bryan angles (roll pitch yaw) in radians from DMP based on ONLY Accel/Gyro.
Definition: mpu.h:303
int tap_threshold
threshold impulse for triggering a tap in units of mg/ms
Definition: mpu.h:269
rc_mpu_gyro_fsr_t gyro_fsr
gyroscope full scale range, default GYRO_FSR_2000DPS
Definition: mpu.h:254
int rc_mpu_read_temp(rc_mpu_data_t *data)
Reads thermometer data from the MPU.
Definition: mpu.c:243
rc_mpu_gyro_fsr_t
gyroscope full scale range options
Definition: mpu.h:160
int mag_sample_rate_div
magnetometer_sample_rate = dmp_sample_rate/mag_sample_rate_div, default: 4
Definition: mpu.h:268
double gyro_to_degs
conversion rate from raw gyroscope to degrees/s
Definition: mpu.h:297
double accel[3]
accelerometer (XYZ) in units of m/s^2
Definition: mpu.h:286
double mag[3]
magnetometer (XYZ) in units of uT
Definition: mpu.h:288
int dmp_fetch_accel_gyro
set to 1 to optionally raw accel/gyro when reading DMP quaternion, default: 0 (off)
Definition: mpu.h:263
rc_mpu_gyro_dlpf_t
gyroscope digital low-pass filter options
Definition: mpu.h:196
double dmp_quat[4]
normalized quaternion from DMP based on ONLY Accel/Gyro
Definition: mpu.h:302
rc_mpu_accel_fsr_t
accelerometer full scale range options
Definition: mpu.h:146
int rc_mpu_is_mag_calibrated(void)
Checks if a magnetometer calibration file is saved to disk.
Definition: mpu.c:2844
double fused_TaitBryan[3]
fused Tait-Bryan angles (roll pitch yaw) in radians
Definition: mpu.h:312
i2c_inst_t * i2c_bus
i2c instance
Definition: mpu.h:246
int16_t raw_gyro[3]
raw gyroscope (XYZ)from 16-bit ADC
Definition: mpu.h:294
rc_mpu_cfg_offset_t
Offset in memory of config data.
Definition: mpu.h:229
int rc_print_gyro_calibration(i2c_inst_t *i2c)
Definition: mpu.c:2009
int rc_mpu_is_accel_calibrated(void)
Checks if an accelerometer calibration file is saved to disk.
Definition: mpu.c:2850
int dmp_sample_rate
sample rate in hertz, 200,100,50,40,25,20,10,8,5,4
Definition: mpu.h:262
double temp
thermometer, in units of degrees Celsius
Definition: mpu.h:289
int rc_mpu_initialize_dmp(rc_mpu_data_t *data, rc_mpu_config_t conf)
Initializes the MPU in DMP mode, see rc_test_dmp example.
Definition: mpu.c:630
int dmp_auto_calibrate_gyro
set to 1 to let DMP auto calibrate the gyro while in use, default: 0 (off)
Definition: mpu.h:264
double compass_heading_raw
unfiltered heading from magnetometer
Definition: mpu.h:314
double accel_to_ms2
conversion rate from raw accelerometer to m/s^2
Definition: mpu.h:296
int show_warnings
set to 1 to print i2c_bus warnings for debug
Definition: mpu.h:248
int16_t raw_accel[3]
raw accelerometer (XYZ) from 16-bit ADC
Definition: mpu.h:295
int tap_detected
set to 1 if there was a tap detect on the last dmp sample, reset to 0 on next sample
Definition: mpu.h:304
int rc_mpu_power_off(void)
Powers off the MPU.
Definition: mpu.c:602
int last_tap_count
current counter of rapid consecutive taps
Definition: mpu.h:306
int read_mag_after_callback
reads magnetometer after DMP callback function to improve latency, default 1 (true)
Definition: mpu.h:267
int rc_mpu_read_gyro(rc_mpu_data_t *data)
Reads gyroscope data from the MPU.
Definition: mpu.c:158
double compass_heading
fused heading filtered with gyro and accel data, same as Tait-Bryan yaw
Definition: mpu.h:313
int rc_mpu_set_config_to_default(rc_mpu_config_t *conf)
Resets a config struct to defaults.
Definition: mpu.c:133
void rc_dmp_callback(uint gpio, uint32_t events)
Definition: mpu.c:2856
struct rc_mpu_config_t rc_mpu_config_t
configuration of the mpu sensor
rc_mpu_gyro_dlpf_t gyro_dlpf
internal low pass filter cutoff, default GYRO_DLPF_184
Definition: mpu.h:256
int gpio_interrupt_pin
gpio pin, default 21 on Robotics Cape and BB Blue
Definition: mpu.h:245
rc_mpu_accel_dlpf_t
accelerometer digital low-pass filter options
Definition: mpu.h:176
double compass_time_constant
time constant (seconds) for filtering compass with gyroscope yaw value, default 25
Definition: mpu.h:266
int rc_mpu_initialize(rc_mpu_data_t *data, rc_mpu_config_t conf)
Sets up the MPU in normal one-shot sampling mode.
rc_mpu_config_t rc_mpu_default_config(void)
Returns an rc_mpu_config_t struct with default settings.
Definition: mpu.c:103
rc_mpu_accel_dlpf_t accel_dlpf
internal low pass filter cutoff, default ACCEL_DLPF_184
Definition: mpu.h:255
int rc_mpu_calibrate_mag_routine(rc_mpu_config_t conf)
Runs magnetometer calibration routine.
Definition: mpu.c:2414
int rc_mpu_calibrate_accel_routine(rc_mpu_config_t conf)
Runs accelerometer calibration routine.
Definition: mpu.c:2646
int rc_mpu_calibrate_gyro_routine(rc_mpu_config_t conf)
Runs gyroscope calibration routine.
Definition: mpu.c:2251
rc_mpu_orientation_t orient
DMP orientation matrix, see rc_mpu_orientation_t.
Definition: mpu.h:265
int enable_magnetometer
magnetometer use is optional, set to 1 to enable, default 0 (off)
Definition: mpu.h:257
configuration of the mpu sensor
Definition: mpu.h:242
data struct populated with new sensor data
Definition: mpu.h:283