Skip to content

Commit 8cce5bd

Browse files
scale test, C++ enum and bus_addr
add scale_test executable demonstrating C++lib and scaling, create enum MPU6050::Scale, C++ set bus_addr, remove #include stdlib.h from headers
1 parent ce8b1dc commit 8cce5bd

File tree

5 files changed

+67
-21
lines changed

5 files changed

+67
-21
lines changed

i2c/mpu6050_i2c/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,13 @@ pico_enable_stdio_uart(mpu6050_i2c 0)
1919

2020
# add url via pico_set_program_url
2121
example_auto_set_url(mpu6050_i2c)
22+
23+
24+
add_executable(mpu6050_i2c_scale_test mpu6050_scale_test.cpp)
25+
target_link_libraries(mpu6050_i2c_scale_test MPU6050_i2c_pico_cpp_lib)
26+
27+
# create map/bin/hex file etc.
28+
pico_add_extra_outputs(mpu6050_i2c_scale_test)
29+
# enable usb output, disable uart output
30+
pico_enable_stdio_usb(mpu6050_i2c_scale_test 1)
31+
pico_enable_stdio_uart(mpu6050_i2c_scale_test 0)

i2c/mpu6050_i2c/mpu6050.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#include "mpu6050.hpp"
2-
#include "pico/binary_info.h"
3-
#include "hardware/i2c.h"
42
#include "mpu6050_i2c.h"
53

64
/*MPU6050::MPU6050() : //TODO: smart pointers
@@ -11,32 +9,33 @@ chip_temp(*temp) {
119
MPU6050(accel, gyro, temp);
1210
} */
1311

14-
MPU6050::MPU6050(float *accel_out, float *gyro_out) :
15-
accel(accel_out), gyro(gyro_out), chip_temp(temp) {
16-
accel_scale = 0;
17-
gyro_scale = 0;
12+
MPU6050::MPU6050(float *accel_out, float *gyro_out, uint8_t addr) :
13+
chip_temp(temp), accel(accel_out), gyro(gyro_out), accel_scale(Scale_0), gyro_scale(Scale_0), bus_addr(addr) {
1814
setup_MPU6050_i2c();
1915
}
2016

2117
void MPU6050::power(uint8_t CLKSEL, bool temp_disable, bool sleep, bool cycle) {
18+
mpu6050_setbusaddr(bus_addr);
2219
mpu6050_power(CLKSEL, temp_disable, sleep, cycle);
2320
}
2421

2522
void MPU6050::reset(void) {
23+
mpu6050_setbusaddr(bus_addr);
2624
mpu6050_reset();
2725
}
2826

29-
void MPU6050::setscale_accel(uint8_t scale_num) {
30-
accel_scale = scale_num & 3;
31-
mpu6050_setscale_accel((MPU6050_Scale)accel_scale);
27+
void MPU6050::setscale_accel(MPU6050::Scale scale) {
28+
mpu6050_setbusaddr(bus_addr);
29+
mpu6050_setscale_accel((MPU6050_Scale)scale);
3230
}
3331

34-
void MPU6050::setscale_gyro(uint8_t scale_num) {
35-
gyro_scale = scale_num & 3;
36-
mpu6050_setscale_gyro((MPU6050_Scale)gyro_scale);
32+
void MPU6050::setscale_gyro(MPU6050::Scale scale) {
33+
mpu6050_setbusaddr(bus_addr);
34+
mpu6050_setscale_gyro((MPU6050_Scale)scale);
3735
}
3836

3937
void MPU6050::read(void) {
38+
mpu6050_setbusaddr(bus_addr);
4039
mpu6050_read(accel, gyro, &temp, (MPU6050_Scale)accel_scale, (MPU6050_Scale)gyro_scale);
4140
}
4241

i2c/mpu6050_i2c/mpu6050.hpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,28 @@
22
An abstraction of pico example C code.
33
TODO:
44
smart ptrs for results
5-
C++ scale enums
6-
mpu6050_t struct in C code for storing scales and bus addr
5+
set timing
6+
set and read filter cutoff
7+
interrupts
78
*/
8-
#include "pico/stdlib.h"
9+
#include "stdint.h"
910

1011
class MPU6050 {
11-
float *accel, *gyro, temp;
12-
uint8_t accel_scale, gyro_scale;
1312
public:
1413
const float &chip_temp; // [C]
1514

1615
MPU6050(); //TODO
17-
MPU6050(float *accel_out, float *gyro_out); // [m/s^2], [rad/s]
16+
MPU6050(float *accel_out, float *gyro_out, uint8_t i2c_addr=0x68); // [m/s^2], [rad/s]
1817

1918
void power(uint8_t CLKSEL, bool temp_disable, bool sleep, bool cycle);
2019
void reset(void);
2120

22-
void setscale_accel(uint8_t scale_num); //scale 0-3 is 2g, 4g, 8g, or 16g
23-
void setscale_gyro(uint8_t scale_num); // scale 0-3 is 250, 500, 1000, or 2000 deg/s
21+
enum Scale {Scale_0 = 0, Scale_1, Scale_2, Scale_3};
22+
void setscale_accel(Scale scale); //scale 0-3 is 2g, 4g, 8g, or 16g
23+
void setscale_gyro(Scale scale); // scale 0-3 is 250, 500, 1000, or 2000 deg/s
2424
void read(void);
25+
private:
26+
float *accel, *gyro, temp;
27+
enum Scale accel_scale, gyro_scale;
28+
uint8_t bus_addr;
2529
};

i2c/mpu6050_i2c/mpu6050_i2c.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ Niraj Patel March 2022 */
55
extern "C" {
66
#endif
77

8-
#include "pico/stdlib.h"
8+
#include "stdint.h"
9+
#include "stddef.h"
910

1011
typedef enum MPU6050_Scale {MPU_FS_0, MPU_FS_1, MPU_FS_2, MPU_FS_3} MPU6050_Scale;
1112

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* mpu6050 I2c C++ example demonstrating the mpu6050's scaling feature
3+
*/
4+
5+
#include <stdio.h>
6+
#include "mpu6050.hpp"
7+
#include "pico/stdlib.h"
8+
9+
int main() {
10+
float accel[3], gyro[3]; // TODO we shouldn't need this
11+
MPU6050 *IMU = new MPU6050(accel, gyro);
12+
printf("MPU6050 Scaling test\n");
13+
IMU->reset();
14+
IMU->power(1, false, false, false);
15+
printf("Scaled Accelerometer and Gyroscope readings\n");
16+
printf("Should be roughly equal before and after scale changes\n");
17+
int accel_scale = 0, gyro_scale = 2;
18+
while (true) {
19+
IMU->setscale_accel((MPU6050::Scale)(accel_scale));
20+
IMU->setscale_gyro((MPU6050::Scale)(gyro_scale));
21+
printf("\nAcclerometer scale: %i, Gyroscope scale: %i\n", accel_scale, gyro_scale);
22+
for (int i = 0; i < 100; i++) {
23+
IMU->read();
24+
printf("%+6f %+6f %+6f | %+6f %+6f %+6f\r",
25+
accel[0], accel[1], accel[2], gyro[0], gyro[1], gyro[2]);
26+
if (i == 0) { printf("\n"); }
27+
sleep_ms(10);
28+
}
29+
accel_scale = (accel_scale + 1) % 4;
30+
gyro_scale = (gyro_scale + 1) % 4;
31+
}
32+
}

0 commit comments

Comments
 (0)