45 #include "dmp_firmware.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>
58 #define ARRAY_SIZE(array) sizeof(array)/sizeof(array[0])
62 #define min(a, b) ((a < b) ? a : b)
66 #define __unused __attribute__ ((unused))
70 #define unlikely(x) __builtin_expect (!!(x), 0)
74 #define likely(x) __builtin_expect (!!(x), 1)
78 #define FLASH_TARGET_OFFSET (PICO_FLASH_SIZE_BYTES - FLASH_SECTOR_SIZE)
82 #define FIFO_LEN_QUAT_TAP 20
83 #define FIFO_LEN_QUAT_ACCEL_GYRO_TAP 32
84 #define MAX_FIFO_BUFFER (FIFO_LEN_QUAT_ACCEL_GYRO_TAP*5)
87 #define QUAT_ERROR_THRESH (1L<<16)
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
92 #define ACCEL_CAL_THRESH 100
93 #define GYRO_OFFSET_THRESH 500
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
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
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
125 #define TWO_PI (2.0 * M_PI)
126 #define GYRO_TO_DEG 2000.0/32768.0;
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
230 MPU_GYRO_CONFIG_ADDR = 0,
231 MPU_ACCEL_CONFIG_ADDR = 6,
232 MPU_MAG_CONFIG_ADDR = 54
326 void mpu9250_read_raw(i2c_inst_t* i2c, int16_t accel[3], int16_t gyro[3], int16_t mag[3], int16_t *temp);
568 int rc_mpu_reset_accel_cal(i2c_inst_t* i2c);
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