Skip to content

Commit 27a0149

Browse files
Tested, renamed main() for library use
Tested cpp code works to read mpu. main() renamed test() so I can call it in other code.
1 parent 4bd085d commit 27a0149

File tree

4 files changed

+29
-24
lines changed

4 files changed

+29
-24
lines changed

i2c/mpu6050_i2c/mpu6050.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
#include "mpu6050.hpp"
2-
#include "mpu6050_i2c.h"
3-
#include "pico/stdlib.h"
42
#include "pico/binary_info.h"
53
#include "hardware/i2c.h"
6-
7-
static MPU6050_Scale MPU6050::accel_scale;
8-
static MPU6050_Scale MPU6050::gyro_scale;
4+
#include "mpu6050_i2c.h"
95

106
/*MPU6050::MPU6050() : //TODO: is there a good way to do this
117
chip_temp(*temp) {
@@ -17,8 +13,8 @@ chip_temp(*temp) {
1713

1814
MPU6050::MPU6050(float *accel_out, float *gyro_out) :
1915
accel(accel_out), gyro(gyro_out), chip_temp(temp) {
20-
accel_scale = MPU_FS_0;
21-
gyro_scale = MPU_FS_0;
16+
accel_scale = 0;
17+
gyro_scale = 0;
2218
//I2C init
2319
i2c_init(i2c_default, 400 * 1000); // Max bus speed 400 kHz
2420
gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
@@ -27,25 +23,25 @@ MPU6050::MPU6050(float *accel_out, float *gyro_out) :
2723
gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN);
2824
}
2925

30-
MPU6050::power(uint8_t CLKSEL, bool temp_disable, bool sleep, bool cycle) {
26+
void MPU6050::power(uint8_t CLKSEL, bool temp_disable, bool sleep, bool cycle) {
3127
mpu6050_power(CLKSEL, temp_disable, sleep, cycle);
3228
}
3329

34-
MPU6050::reset(void) {
30+
void MPU6050::reset(void) {
3531
mpu6050_reset();
3632
}
3733

38-
MPU6050::setscale_accel(uint8_t scale_num) {
39-
accel_scale = (MPU6050_Scale)(scale_num & 3);
40-
mpu6050_setscale_accel(accel_scale);
34+
void MPU6050::setscale_accel(uint8_t scale_num) {
35+
accel_scale = scale_num & 3;
36+
mpu6050_setscale_accel((MPU6050_Scale)accel_scale);
4137
}
4238

43-
MPU6050::setscale_gyro(uint8_t scale_num) {
44-
gyro_scale = (MPU6050_Scale)(scale_num & 3);
45-
mpu6050_setscale_gyro(gyro_scale);
39+
void MPU6050::setscale_gyro(uint8_t scale_num) {
40+
gyro_scale = scale_num & 3;
41+
mpu6050_setscale_gyro((MPU6050_Scale)gyro_scale);
4642
}
4743

48-
MPU6050::read(void) {
49-
mpu6050_read(accel, gyro, &temp, accel_scale, gyro_scale);
44+
void MPU6050::read(void) {
45+
mpu6050_read(accel, gyro, &temp, (MPU6050_Scale)accel_scale, (MPU6050_Scale)gyro_scale);
5046
}
5147

i2c/mpu6050_i2c/mpu6050.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
/* class for using an MPU6050 IMU sensor on pi pico.
22
An abstraction of pico example C code. */
3+
#include "pico/stdlib.h"
34

45
class MPU6050 {
56
float *accel, *gyro, temp;
7+
uint8_t accel_scale, gyro_scale;
68
public:
7-
const float &accel[3], &gyro[3], &chip_temp; // [m/s^2], [rad/s], [C]
9+
//const float *accel, *gyro, &chip_temp; // [m/s^2], [rad/s], [C]
10+
const float &chip_temp;
811
MPU6050(void);
912
MPU6050(float *accel_out, float *gyro_out);
1013
void power(uint8_t CLKSEL, bool temp_disable, bool sleep, bool cycle);
1114
void reset(void);
1215
void setscale_accel(uint8_t scale_num); //scale 0-3 is 2g, 4g, 8g, or 16g
1316
void setscale_gyro(uint8_t scale_num); // scale 0-3 is 250, 500, 1000, or 2000 deg/s
1417
void read(void);
15-
}
18+
};

i2c/mpu6050_i2c/mpu6050_i2c.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "pico/stdlib.h"
1111
#include "pico/binary_info.h"
1212
#include "hardware/i2c.h"
13+
#include "mpu6050_i2c.h"
1314

1415
/* Example code to talk to a MPU6050 MEMS accelerometer and gyroscope
1516
@@ -34,7 +35,6 @@ static uint8_t bus_addr = 0x68;
3435
// Register addresses in the MPU6050 to read and write data to
3536
const uint8_t REG_PWR_MGMT_1 = 0x6B, REG_ACCEL_OUT = 0x3B, REG_GYRO_OUT = 0x43, REG_TEMP_OUT = 0x41,
3637
REG_SIGNAL_PATH_RESET = 0x68, REG_GYRO_CONFIG = 0x1B, REG_ACCEL_CONFIG = 0x1C;
37-
typedef enum MPU6050_Scale {MPU_FS_0, MPU_FS_1, MPU_FS_2, MPU_FS_3} MPU6050_Scale;
3838

3939
const float gyro_scale_deg[] = {250. / 0x7fff, 500. / 0x7fff, 1000. / 0x7fff, 2000. / 0x7fff};
4040
const float gyro_scale_rad[] = {M_PI / 180. * 250. / 0x7fff, M_PI / 180. * 500. / 0x7fff,
@@ -107,7 +107,7 @@ void mpu6050_read(float accel[3], float gyro_rad[3], float *temp, MPU6050_Scale
107107
mpu6050_readreg16(REG_ACCEL_OUT, buffer, 7); //reads all at same time
108108
mpu6050_scale_accel(accel, buffer, accel_scale);
109109
if (temp) *temp = mpu6050_scale_temp(buffer[3]);
110-
mpu6050_scale_gyro_rad(gyro, buffer + 4, gyro_scale);
110+
mpu6050_scale_gyro_rad(gyro_rad, buffer + 4, gyro_scale);
111111
}
112112

113113
// set and use sensitivity
@@ -141,7 +141,7 @@ float mpu6050_scale_temp(float temp_raw) {
141141
//TODO: read self test
142142

143143
//TODO: functional mpu test
144-
int main() {
144+
int test() {
145145
stdio_init_all();
146146
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
147147
#warning i2c/mpu6050_i2c example requires a board with I2C pins

i2c/mpu6050_i2c/mpu6050_i2c.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
/* header for accessing mpu6050 functions in pico-examples
22
33
Niraj Patel March 2022 */
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
47

58
#include "pico/stdlib.h"
6-
#include "pico/binary_info.h"
79

810
typedef enum MPU6050_Scale {MPU_FS_0, MPU_FS_1, MPU_FS_2, MPU_FS_3} MPU6050_Scale;
911

1012
// lower level functions for i2c
1113
void mpu6050_writereg(uint8_t reg, uint8_t value); //write one byte to a register
1214
void mpu6050_readreg(uint8_t reg, uint8_t *out, size_t length);
13-
void mpu6050_readreg16(uint8_t reg, uint16_t *out, size_t length);
15+
void mpu6050_readreg16(uint8_t reg, int16_t *out, size_t length);
1416
void mpu6050_setbusaddr(uint8_t addr); //set the i2c bus address for communication. MPU6050 must already have this value.
1517

1618
// higher level mpu6050 functions
@@ -29,3 +31,7 @@ float mpu6050_scale_temp(float temp_raw);
2931
void mpu6050_read_raw(int16_t accel[3], int16_t gyro[3], int16_t *temp); //read raw data values. Could read different timesteps.
3032
void mpu6050_read(float accel[3], float gyro[3], float *temp,
3133
MPU6050_Scale accel_scale, MPU6050_Scale gyro_scale); //reads all at same timestep, converts. temp can be NULL.
34+
35+
#ifdef __cplusplus
36+
}
37+
#endif

0 commit comments

Comments
 (0)