Skip to content

Commit 604efb9

Browse files
add irq, C++ timing
1 parent 7bf6b47 commit 604efb9

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

i2c/mpu6050_i2c/mpu6050.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
#include "mpu6050.hpp"
22
#include "mpu6050_i2c.h"
33

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+
424
/*MPU6050::MPU6050() : //TODO: smart pointers
525
chip_temp(*temp) {
626
accel = {0., 0., 0.};
@@ -45,4 +65,16 @@ bool MPU6050::is_connected() {
4565
mpu6050_setbusaddr(bus_addr);
4666
return mpu6050_is_connected();
4767
}
68+
69+
void MPU6050::set_timing(uint8_t lowpass_filter_cfg, uint8_t sample_rate_div) {
70+
mpu6050_setbusaddr(bus_addr);
71+
mpu6050_set_timing(lowpass_filter_cfg, sample_rate_div);
72+
}
73+
74+
MPU6050TimingParams MPU6050::read_timing(void) {
75+
mpu6050_timing_params_t accel_timing_c, gyro_timing_c;
76+
mpu6050_read_timing(&accel_timing_c, &gyro_timing_c);
77+
return MPU6050TimingParams(convert(&accel_timing_c), convert(&gyro_timing_c));
78+
}
79+
4880

i2c/mpu6050_i2c/mpu6050.hpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@
22
An abstraction of pico example C code.
33
TODO:
44
smart ptrs for results
5-
set timing
6-
set and read filter cutoff
7-
interrupts
85
*/
96
#include "stdint.h"
107

8+
struct MPU6050SensorTimingParams {
9+
MPU6050SensorTimingParams() = default;
10+
MPU6050SensorTimingParams(int _bandwidth, float _delay, float _sample_rate);
11+
int bandwidth; // lowpass filter bandwidth [Hz]
12+
float delay; // lowpass filter delay [ms]
13+
float sample_rate; // rate of new data loading in the register [Hz]
14+
};
15+
struct MPU6050TimingParams {
16+
MPU6050TimingParams(uint8_t lowpass_filter_cfg, uint8_t sample_rate_div);
17+
MPU6050TimingParams(const MPU6050SensorTimingParams &_accel_timing, const MPU6050SensorTimingParams &_gyro_timing);
18+
MPU6050SensorTimingParams accel_timing, gyro_timing;
19+
};
20+
1121
class MPU6050 {
1222
public:
1323
const float &chip_temp; // [C]
@@ -24,6 +34,8 @@ class MPU6050 {
2434
void setscale_gyro(Scale scale); // scale 0-3 is 250, 500, 1000, or 2000 deg/s
2535
void read(void);
2636
bool is_connected(void);
37+
void set_timing(uint8_t lowpass_filter_cfg, uint8_t sample_rate_div);
38+
MPU6050TimingParams read_timing(void);
2739
private:
2840
float *accel, *gyro, temp;
2941
enum Scale accel_scale, gyro_scale;

i2c/mpu6050_i2c/mpu6050_i2c.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
static uint8_t bus_addr = 0x68;
3535
// Register addresses in the MPU6050 to read and write data to
3636
const uint8_t REG_ACCEL_OUT = 0x3B, REG_GYRO_OUT = 0x43, REG_TEMP_OUT = 0x41,
37+
REG_INT_PIN_CFG = 0x37, REG_INT_ENABLE = 0x38, REG_INT_STATUS = 0x3A,
3738
REG_SMPRT_DIV = 0x19, REG_SIGNAL_PATH_RESET = 0x68, REG_PWR_MGMT_1 = 0x6B, REG_WHO_AM_I = 0x75,
3839
REG_CONFIG = 0x1A, REG_GYRO_CONFIG = 0x1B, REG_ACCEL_CONFIG = 0x1C;
3940

@@ -172,6 +173,17 @@ void mpu6050_calc_timing(uint8_t filter_cfg, uint8_t sample_rate_div,
172173
}
173174
}
174175

176+
void mpu6050_configure_interrupt(bool active_low, bool open_drain, bool latch_pin, bool read_clear, bool enable) {
177+
mpu6050_writereg(REG_INT_PIN_CFG, ((uint8_t)active_low << 7) + ((uint8_t)open_drain << 6)
178+
+ ((uint8_t)latch_pin << 5) + ((uint8_t)read_clear << 4));
179+
mpu6050_writereg(REG_INT_ENABLE, (uint8_t)enable);
180+
}
181+
uint8_t mpu6050_read_interrupt_status() {
182+
uint8_t interrupt_status;
183+
mpu6050_readreg(REG_INT_STATUS, &interrupt_status, 1);
184+
return interrupt_status;
185+
}
186+
175187
bool mpu6050_is_connected(void) {
176188
uint8_t who_are_you = 0;
177189
mpu6050_readreg(REG_WHO_AM_I, &who_are_you, 1);

i2c/mpu6050_i2c/mpu6050_i2c.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,20 @@ bool mpu6050_is_connected();
3737
void mpu6050_accel_selftest_on(); // TODO: find out what "self test" does
3838
// turn on the mpu6050's gyroscope self test. Turn it off after a few 100ms with mpu6050_setscale_gyro
3939
void mpu6050_gyro_selftest_on();
40+
4041
// configure lowpass filter and sample rate. Higher filter_cfg -> slower change, higher sample_rate_div -> slower sampling
4142
void mpu6050_set_timing(uint8_t lowpass_filter_cfg, uint8_t sample_rate_div);
4243
void mpu6050_read_timing(mpu6050_timing_params_t *accel_timing, mpu6050_timing_params_t *gyro_timing);
4344
void mpu6050_calc_timing(uint8_t filter_cfg, uint8_t sample_rate_div,
4445
mpu6050_timing_params_t *accel_timing, mpu6050_timing_params_t *gyro_timing);
4546

47+
void mpu6050_configure_interrupt(bool active_low, // Whether the INT pin is active low or active high
48+
bool open_drain, // Whether the INT pin is push-pull or open-drain
49+
bool latch_pin, // Whether the INT pin latches or pulses for 50us
50+
bool read_clear, // Whether interrupt status bits are cleared by reading interrupt status (default) or on any read
51+
bool enable); // Turn interrupts on or off
52+
uint8_t mpu6050_read_interrupt_status(); // 0 = no interrupts set, 1 = data ready
53+
4654
//set and use scaling. The first read() after setscale() might not have the updated scaling.
4755
void mpu6050_setscale_accel(MPU6050_Scale accel_scale);
4856
void mpu6050_setscale_gyro(MPU6050_Scale gyro_scale);

0 commit comments

Comments
 (0)