From 06753bc2b14d0dc4f78ad236922a0717aef104ac Mon Sep 17 00:00:00 2001 From: structix Date: Mon, 5 Sep 2016 20:26:51 +0200 Subject: [PATCH] read temperature register, register constants, modified makefile and readme --- README.md | 11 ++++- gy521example.c | 2 +- libgy521.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++++- libgy521.h | 5 +++ makefile | 2 +- 5 files changed, 127 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6e7a778..e1cfc27 100644 --- a/README.md +++ b/README.md @@ -1 +1,10 @@ -# Library for the GY-521 Breakboard \ No newline at end of file +# Library for the GY-521 Breakboard + +## Compiling +This library depends on the wiringPi library. Make sure to install it first (`sudo apt-get install wiringpi` but often it's preinstalled on raspbian). +To compile just type `make` in the projects root. + + + +## Documentation +[MPU-6050 Datasheet](https://www.olimex.com/Products/Modules/Sensors/MOD-MPU6050/resources/RM-MPU-60xxA_rev_4.pdf) diff --git a/gy521example.c b/gy521example.c index bf3a987..edd39d5 100644 --- a/gy521example.c +++ b/gy521example.c @@ -8,7 +8,7 @@ int main(void) { setupgy521(); while (1) { - printf("AngleX = %f, AngleY = %f, AngleZ = %f\n", getAngleX(), getAngleY(), getAngleZ()); + printf("AngleX = %f, AngleY = %f, AngleZ = %f, Temperature = %f\n", getAngleX(), getAngleY(), getAngleZ(), getTemp()); delay(100); } } diff --git a/libgy521.c b/libgy521.c index 3673d40..abb4695 100644 --- a/libgy521.c +++ b/libgy521.c @@ -7,6 +7,8 @@ //Constants +//######## Obsolete ########// + #define MPU6050_GYRO_XOUT_H 0x43 //R #define MPU6050_GYRO_YOUT_H 0x45 //R #define MPU6050_GYRO_ZOUT_H 0x47 //R @@ -18,18 +20,124 @@ #define MPU6050_PWR_MGMT_1 0x6B //R/W #define MPU6050_I2C_ADDRESS 0x68 //I2C +//######## End Obsolete ########// + +#define GRAVITIY_MS2 9.80665 + +// Scale Modifiers +#define ACCEL_SCALE_MODIFIER_2G 16384.0 +#define ACCEL_SCALE_MODIFIER_4G 8192.0 +#define ACCEL_SCALE_MODIFIER_8G 4096.0 +#define ACCEL_SCALE_MODIFIER_16G 2048.0 + +#define GYRO_SCALE_MODIFIER_250DEG 131.0 +#define GYRO_SCALE_MODIFIER_500DEG 65.5 +#define GYRO_SCALE_MODIFIER_1000DEG 32.8 +#define GYRO_SCALE_MODIFIER_2000DEG 16.4 + +// Pre-defined ranges +#define ACCEL_RANGE_2G 0x00 +#define ACCEL_RANGE_4G 0x08 +#define ACCEL_RANGE_8G 0x10 +#define ACCEL_RANGE_16G 0x18 + +#define GYRO_RANGE_250DEG 0x00 +#define GYRO_RANGE_500DEG 0x08 +#define GYRO_RANGE_1000DEG 0x10 +#define GYRO_RANGE_2000DEG 0x18 + +// MPU-6050 Registers +#define PWR_MGMT_1 0x6B //R/W +#define PWR_MGMT_2 0x6C + +#define SELF_TEST_X 0x0D +#define SELF_TEST_Y 0x0E +#define SELF_TEST_Z 0x0F +#define SELF_TEST_A 0x10 + +#define ACCEL_XOUT0 0x3B +#define ACCEL_XOUT1 0x3C +#define ACCEL_YOUT0 0x3D +#define ACCEL_YOUT1 0x3E +#define ACCEL_ZOUT0 0x3F +#define ACCEL_ZOUT1 0x40 + +#define TEMP_OUT0 0x41 +#define TEMP_OUT1 0x42 + +#define GYRO_XOUT0 0x43 //R +#define GYRO_XOUT1 0x44 +#define GYRO_YOUT0 0x45 //R +#define GYRO_YOUT1 0x46 +#define GYRO_ZOUT0 0x47 //R +#define GYRO_ZOUT1 0x48 + +#define ACCEL_CONFIG 0x1C +#define GYRO_CONFIG 0x1B + int id; int setupgy521(void) { if ((id = wiringPiI2CSetup(MPU6050_I2C_ADDRESS)) != -1) { - wiringPiI2CReadReg8(id, MPU6050_PWR_MGMT_1); - wiringPiI2CWriteReg16(id, MPU6050_PWR_MGMT_1, 0); + wiringPiI2CReadReg8(id, PWR_MGMT_1); + wiringPiI2CWriteReg16(id, PWR_MGMT_1, 0); return EXIT_SUCCESS; } else { return EXIT_FAILURE; } } +int read_i2c_word(int reg) { + return wiringPiI2CReadReg8(id, reg); +} + +int read_i2c_word16(int reg) { + return wiringPiI2CReadReg16(id, reg); +} + +float getTemp(void) { + /** + * Formula given in + * MPU-6050 Register Map and Descriptions revision 4.2, page 30 + */ + return (read_i2c_word16(TEMP_OUT0) / 340.0) + 36.53; //16 might be bullshit +} + +void setAccelerometerRange(int range) { + //First set data to 0x00 + wiringPiI2CWriteReg8(id, ACCEL_CONFIG, 0x00); + //Then write the correct value to the register + wiringPiI2CWriteReg8(id, ACCEL_CONFIG, range); +} + +int readAccelerometerRange(int raw) { + int rawData = wiringPiI2CReadReg8(id, ACCEL_CONFIG); + int range; + if (raw != 0) { + range = rawData; + } else { + switch (rawData) { + case ACCEL_RANGE_2G: + range = 2; + break; + case ACCEL_RANGE_4G: + range = 4; + break; + case ACCEL_RANGE_8G: + range = 8; + break; + case ACCEL_RANGE_16G: + range = 16; + break; + default: + range = -1; //Error + break; //Good manner ;) + } + } + + return range; +} + int getGyroX(void) { return wiringPiI2CReadReg8(id, MPU6050_GYRO_XOUT_H); } diff --git a/libgy521.h b/libgy521.h index 076fb23..c88e93a 100644 --- a/libgy521.h +++ b/libgy521.h @@ -8,3 +8,8 @@ int getAccelZ(void); float getAngleX(void); float getAngleY(void); float getAngleZ(void); +int read_i2c_word(int reg); +float getTemp(void); +void setAccelerometerRange(int range); +int readAccelerometerRange(int raw); +int read_i2c_word16(int reg); diff --git a/makefile b/makefile index 1a4797a..842fa82 100644 --- a/makefile +++ b/makefile @@ -9,7 +9,7 @@ LDFLAGS = -lwiringPi -lm OBJ = gy521example.o libgy521.o -waterpicore: $(OBJ) +libgy521: $(OBJ) $(CC) $(CFLAGS) -o gy521example $(OBJ) $(LDFLAGS) %.o: %.c