Skip to content

Commit ce8b1dc

Browse files
split mpu6050 lib (c and c++) from demo main, refactor mostly comments
the code that's been running with simplewalker. Also add self-test even though idk what it does.
1 parent 27a0149 commit ce8b1dc

File tree

6 files changed

+348
-304
lines changed

6 files changed

+348
-304
lines changed

i2c/mpu6050_i2c/CMakeLists.txt

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
add_executable(mpu6050_i2c
2-
mpu6050_i2c.c
3-
)
4-
5-
# pull in common dependencies and additional i2c hardware support
6-
target_link_libraries(mpu6050_i2c pico_stdlib hardware_i2c)
7-
8-
# create map/bin/hex file etc.
9-
pico_add_extra_outputs(mpu6050_i2c)
10-
11-
# add url via pico_set_program_url
12-
example_auto_set_url(mpu6050_i2c)
1+
add_library(MPU6050_i2c_pico_lib mpu6050_i2c.c)
2+
target_include_directories(MPU6050_i2c_pico_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
3+
# pull in common dependencies and additional i2c hardware support
4+
target_link_libraries(MPU6050_i2c_pico_lib pico_stdlib hardware_i2c)
5+
6+
add_library(MPU6050_i2c_pico_cpp_lib mpu6050.cpp mpu6050_i2c.c)
7+
target_include_directories(MPU6050_i2c_pico_cpp_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
8+
target_link_libraries(MPU6050_i2c_pico_cpp_lib pico_stdlib hardware_i2c)
9+
10+
11+
add_executable(mpu6050_i2c mpu6050_i2c_main.c)
12+
target_link_libraries(mpu6050_i2c MPU6050_i2c_pico_lib)
13+
14+
# create map/bin/hex file etc.
15+
pico_add_extra_outputs(mpu6050_i2c)
16+
# enable usb output, disable uart output
17+
pico_enable_stdio_usb(mpu6050_i2c 1)
18+
pico_enable_stdio_uart(mpu6050_i2c 0)
19+
20+
# add url via pico_set_program_url
21+
example_auto_set_url(mpu6050_i2c)

i2c/mpu6050_i2c/mpu6050.cpp

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,42 @@
1-
#include "mpu6050.hpp"
2-
#include "pico/binary_info.h"
3-
#include "hardware/i2c.h"
4-
#include "mpu6050_i2c.h"
5-
6-
/*MPU6050::MPU6050() : //TODO: is there a good way to do this
7-
chip_temp(*temp) {
8-
accel = {0., 0., 0.};
9-
gyro = {0., 0., 0.};
10-
temp = {};
11-
MPU6050(accel, gyro, temp);
12-
} */
13-
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;
18-
//I2C init
19-
i2c_init(i2c_default, 400 * 1000); // Max bus speed 400 kHz
20-
gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
21-
gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C);
22-
gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN);
23-
gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN);
24-
}
25-
26-
void MPU6050::power(uint8_t CLKSEL, bool temp_disable, bool sleep, bool cycle) {
27-
mpu6050_power(CLKSEL, temp_disable, sleep, cycle);
28-
}
29-
30-
void MPU6050::reset(void) {
31-
mpu6050_reset();
32-
}
33-
34-
void MPU6050::setscale_accel(uint8_t scale_num) {
35-
accel_scale = scale_num & 3;
36-
mpu6050_setscale_accel((MPU6050_Scale)accel_scale);
37-
}
38-
39-
void MPU6050::setscale_gyro(uint8_t scale_num) {
40-
gyro_scale = scale_num & 3;
41-
mpu6050_setscale_gyro((MPU6050_Scale)gyro_scale);
42-
}
43-
44-
void MPU6050::read(void) {
45-
mpu6050_read(accel, gyro, &temp, (MPU6050_Scale)accel_scale, (MPU6050_Scale)gyro_scale);
46-
}
1+
#include "mpu6050.hpp"
2+
#include "pico/binary_info.h"
3+
#include "hardware/i2c.h"
4+
#include "mpu6050_i2c.h"
5+
6+
/*MPU6050::MPU6050() : //TODO: smart pointers
7+
chip_temp(*temp) {
8+
accel = {0., 0., 0.};
9+
gyro = {0., 0., 0.};
10+
temp = {};
11+
MPU6050(accel, gyro, temp);
12+
} */
13+
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;
18+
setup_MPU6050_i2c();
19+
}
20+
21+
void MPU6050::power(uint8_t CLKSEL, bool temp_disable, bool sleep, bool cycle) {
22+
mpu6050_power(CLKSEL, temp_disable, sleep, cycle);
23+
}
24+
25+
void MPU6050::reset(void) {
26+
mpu6050_reset();
27+
}
28+
29+
void MPU6050::setscale_accel(uint8_t scale_num) {
30+
accel_scale = scale_num & 3;
31+
mpu6050_setscale_accel((MPU6050_Scale)accel_scale);
32+
}
33+
34+
void MPU6050::setscale_gyro(uint8_t scale_num) {
35+
gyro_scale = scale_num & 3;
36+
mpu6050_setscale_gyro((MPU6050_Scale)gyro_scale);
37+
}
38+
39+
void MPU6050::read(void) {
40+
mpu6050_read(accel, gyro, &temp, (MPU6050_Scale)accel_scale, (MPU6050_Scale)gyro_scale);
41+
}
4742

i2c/mpu6050_i2c/mpu6050.hpp

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
1-
/* class for using an MPU6050 IMU sensor on pi pico.
2-
An abstraction of pico example C code. */
3-
#include "pico/stdlib.h"
4-
5-
class MPU6050 {
6-
float *accel, *gyro, temp;
7-
uint8_t accel_scale, gyro_scale;
8-
public:
9-
//const float *accel, *gyro, &chip_temp; // [m/s^2], [rad/s], [C]
10-
const float &chip_temp;
11-
MPU6050(void);
12-
MPU6050(float *accel_out, float *gyro_out);
13-
void power(uint8_t CLKSEL, bool temp_disable, bool sleep, bool cycle);
14-
void reset(void);
15-
void setscale_accel(uint8_t scale_num); //scale 0-3 is 2g, 4g, 8g, or 16g
16-
void setscale_gyro(uint8_t scale_num); // scale 0-3 is 250, 500, 1000, or 2000 deg/s
17-
void read(void);
18-
};
1+
/* class for using an MPU6050 IMU sensor on pi pico.
2+
An abstraction of pico example C code.
3+
TODO:
4+
smart ptrs for results
5+
C++ scale enums
6+
mpu6050_t struct in C code for storing scales and bus addr
7+
*/
8+
#include "pico/stdlib.h"
9+
10+
class MPU6050 {
11+
float *accel, *gyro, temp;
12+
uint8_t accel_scale, gyro_scale;
13+
public:
14+
const float &chip_temp; // [C]
15+
16+
MPU6050(); //TODO
17+
MPU6050(float *accel_out, float *gyro_out); // [m/s^2], [rad/s]
18+
19+
void power(uint8_t CLKSEL, bool temp_disable, bool sleep, bool cycle);
20+
void reset(void);
21+
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
24+
void read(void);
25+
};

0 commit comments

Comments
 (0)