|
| 1 | +#include "mpu6050.hpp" |
| 2 | +#include "mpu6050_i2c.h" |
| 3 | + |
| 4 | + |
| 5 | +MPU6050SensorTimingParams::MPU6050SensorTimingParams(int _bandwidth, float _delay, float _sample_rate) |
| 6 | + : bandwidth(_bandwidth), delay(_delay), sample_rate(_sample_rate) {} |
| 7 | + |
| 8 | +inline const MPU6050SensorTimingParams convert(mpu6050_timing_params_t *c_struct) { // unfortunate awkwardness of wrapping a C lib |
| 9 | + return MPU6050SensorTimingParams(c_struct->bandwidth, c_struct->delay, c_struct->sample_rate); |
| 10 | +} |
| 11 | + |
| 12 | +MPU6050TimingParams::MPU6050TimingParams(uint8_t lowpass_filter_cfg, uint8_t sample_rate_div) { |
| 13 | + mpu6050_timing_params_t accel_timing_c, gyro_timing_c; |
| 14 | + mpu6050_calc_timing(lowpass_filter_cfg, sample_rate_div, &accel_timing_c, &gyro_timing_c); |
| 15 | + accel_timing = convert(&accel_timing_c); |
| 16 | + gyro_timing = convert(&gyro_timing_c); |
| 17 | +} |
| 18 | + |
| 19 | +MPU6050TimingParams::MPU6050TimingParams(const MPU6050SensorTimingParams &_accel_timing, |
| 20 | + const MPU6050SensorTimingParams &_gyro_timing) |
| 21 | + : accel_timing(_accel_timing), gyro_timing(_gyro_timing) {} |
| 22 | + |
| 23 | + |
| 24 | +MPU6050::MPU6050(float *accel_out, float *gyro_out, uint8_t addr) : |
| 25 | + chip_temp(temp), accel(accel_out), gyro(gyro_out), accel_scale(Scale_0), gyro_scale(Scale_0), bus_addr(addr) { |
| 26 | + setup_MPU6050_i2c(); |
| 27 | +} |
| 28 | + |
| 29 | +void MPU6050::power(uint8_t CLKSEL, bool temp_disable, bool sleep, bool cycle) { |
| 30 | + mpu6050_setbusaddr(bus_addr); |
| 31 | + mpu6050_power(CLKSEL, temp_disable, sleep, cycle); |
| 32 | +} |
| 33 | + |
| 34 | +void MPU6050::reset(void) { |
| 35 | + mpu6050_setbusaddr(bus_addr); |
| 36 | + mpu6050_reset(); |
| 37 | +} |
| 38 | + |
| 39 | +void MPU6050::setscale_accel(MPU6050::Scale scale) { |
| 40 | + mpu6050_setbusaddr(bus_addr); |
| 41 | + mpu6050_setscale_accel((MPU6050_Scale)scale); |
| 42 | + accel_scale = scale; |
| 43 | +} |
| 44 | + |
| 45 | +void MPU6050::setscale_gyro(MPU6050::Scale scale) { |
| 46 | + mpu6050_setbusaddr(bus_addr); |
| 47 | + mpu6050_setscale_gyro((MPU6050_Scale)scale); |
| 48 | + gyro_scale = scale; |
| 49 | +} |
| 50 | + |
| 51 | +void MPU6050::read(void) { |
| 52 | + mpu6050_setbusaddr(bus_addr); |
| 53 | + mpu6050_read(accel, gyro, &temp, (MPU6050_Scale)accel_scale, (MPU6050_Scale)gyro_scale); |
| 54 | +} |
| 55 | + |
| 56 | +void MPU6050::read_accel(void) { |
| 57 | + mpu6050_setbusaddr(bus_addr); |
| 58 | + mpu6050_read_accel(accel, (MPU6050_Scale)accel_scale); |
| 59 | +} |
| 60 | + |
| 61 | +void MPU6050::read_gyro(void) { |
| 62 | + mpu6050_setbusaddr(bus_addr); |
| 63 | + mpu6050_read_gyro_rad(gyro, (MPU6050_Scale)gyro_scale); |
| 64 | +} |
| 65 | + |
| 66 | +bool MPU6050::is_connected() { |
| 67 | + mpu6050_setbusaddr(bus_addr); |
| 68 | + return mpu6050_is_connected(); |
| 69 | +} |
| 70 | + |
| 71 | +void MPU6050::set_timing(uint8_t lowpass_filter_cfg, uint8_t sample_rate_div) { |
| 72 | + mpu6050_setbusaddr(bus_addr); |
| 73 | + mpu6050_set_timing(lowpass_filter_cfg, sample_rate_div); |
| 74 | +} |
| 75 | + |
| 76 | +MPU6050TimingParams MPU6050::read_timing(void) { |
| 77 | + mpu6050_setbusaddr(bus_addr); |
| 78 | + mpu6050_timing_params_t accel_timing_c, gyro_timing_c; |
| 79 | + mpu6050_read_timing(&accel_timing_c, &gyro_timing_c); |
| 80 | + return MPU6050TimingParams(convert(&accel_timing_c), convert(&gyro_timing_c)); |
| 81 | +} |
| 82 | + |
| 83 | +void MPU6050::configure_interrupt(bool active_low, bool open_drain, bool latch_pin, bool read_clear, bool enable) { |
| 84 | + mpu6050_setbusaddr(bus_addr); |
| 85 | + mpu6050_configure_interrupt(active_low, open_drain, latch_pin, read_clear, enable); |
| 86 | +} |
| 87 | + |
| 88 | +uint8_t MPU6050::read_interrupt_status() { |
| 89 | + mpu6050_setbusaddr(bus_addr); |
| 90 | + return mpu6050_read_interrupt_status(); |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | +float MPU6050_max_accel(MPU6050::Scale scale) { |
| 95 | + return accel_scale_vals[(int)scale]; |
| 96 | +} |
| 97 | +float MPU6050_max_gyro_rad(MPU6050::Scale scale) { |
| 98 | + return gyro_scale_rad[(int)scale]; |
| 99 | +} |
0 commit comments