Skip to content

Commit

Permalink
Merge pull request #10 from RobTillaart/develop
Browse files Browse the repository at this point in the history
0.2.0 improve error handling
  • Loading branch information
RobTillaart authored Nov 3, 2020
2 parents 1fc14c1 + 3a9d11a commit b116412
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 28 deletions.
59 changes: 48 additions & 11 deletions GY521.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: GY521.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.5
// VERSION: 0.2.0
// PURPOSE: Arduino library for I2C GY521 accelerometer-gyroscope sensor
// URL: https://github.com/RobTillaart/GY521
//
Expand All @@ -12,6 +12,7 @@
// 0.1.3 2020-08-07 fix ESP support + pitch roll yaw demo
// 0.1.4 2020-09-29 fix #5 missing ;
// 0.1.5 2020-09-29 fix #6 fix math for Teensy
// 0.2.0 2020-11-03 improve error handling


#include "GY521.h"
Expand Down Expand Up @@ -57,15 +58,15 @@ bool GY521::begin()
bool GY521::isConnected()
{
Wire.beginTransmission(_address);
return Wire.endTransmission() == 0;
return (Wire.endTransmission() == 0);
}

bool GY521::wakeup()
{
Wire.beginTransmission(_address);
Wire.write(GY521_PWR_MGMT_1);
Wire.write(GY521_WAKEUP);
return Wire.endTransmission() == 0;
return (Wire.endTransmission() == 0);
}

int16_t GY521::read()
Expand Down Expand Up @@ -118,7 +119,7 @@ int16_t GY521::read()
_aaz += aze;

// Convert to Celsius
_temperature = (_temperature + 12412.0) * 0.00294117647; // == /340.0 + 36.53;
_temperature = _temperature * 0.00294117647 + 36.53; // == /340.0 + 36.53;

// Convert raw Gyro to degrees/seconds
_gx *= _raw2dps;
Expand All @@ -141,48 +142,72 @@ int16_t GY521::read()
return GY521_OK;
}

void GY521::setAccelSensitivity(uint8_t as)
bool GY521::setAccelSensitivity(uint8_t as)
{
_afs = as;
if (_afs > 3) _afs = 3;
uint8_t val = getRegister(GY521_ACCEL_CONFIG);
if (_error != 0)
{
return false;
}
// no need to write same value
if (((val >> 3) & 3) != _afs)
{
val &= 0xE7;
val |= (_afs << 3);
setRegister(GY521_ACCEL_CONFIG, val);
if (setRegister(GY521_ACCEL_CONFIG, val) != GY521_OK)
{
return false;
}
}
// calculate conversion factor.
_raw2g = (1 << _afs) / 16384.0;
return true;
}

uint8_t GY521::getAccelSensitivity()
{
uint8_t val = getRegister(GY521_ACCEL_CONFIG);
if (_error != GY521_OK)
{
return _error; // return and propagate error (best thing to do)
}
_afs = (val >> 3) & 3;
return _afs;
}

void GY521::setGyroSensitivity(uint8_t gs)
bool GY521::setGyroSensitivity(uint8_t gs)
{
_gfs = gs;
if (_gfs > 3) _gfs = 3;
uint8_t val = getRegister(GY521_GYRO_CONFIG);
if (_error != 0)
{
return false;
}
// no need to write same value
if (((val >> 3) & 3) != _gfs)
{
val &= 0xE7;
val |= (_gfs << 3);
setRegister(GY521_GYRO_CONFIG, val);
if (setRegister(GY521_GYRO_CONFIG, val) != GY521_OK)
{
return false;
}
}
// calculate conversion factor.
_raw2dps = (1 << _gfs) / 131.0;
return true;
}

uint8_t GY521::getGyroSensitivity()
{
uint8_t val = getRegister(GY521_GYRO_CONFIG);
if (_error != GY521_OK)
{
return _error; // return and propagate error (best thing to do)
}
_gfs = (val >> 3) & 3;
return _gfs;
}
Expand All @@ -193,17 +218,29 @@ uint8_t GY521::setRegister(uint8_t reg, uint8_t value)
Wire.write(reg);
Wire.write(value);
// no need to do anything if not connected.
if (Wire.endTransmission() != 0) return GY521_ERROR_WRITE;
if (Wire.endTransmission() != 0)
{
_error = GY521_ERROR_WRITE;
return _error;
}
return GY521_OK;
}

uint8_t GY521::getRegister(uint8_t reg)
{
Wire.beginTransmission(_address);
Wire.write(reg);
if (Wire.endTransmission() != 0) return GY521_ERROR_WRITE;
if (Wire.endTransmission() != 0)
{
_error = GY521_ERROR_WRITE;
return _error;
}
uint8_t n = Wire.requestFrom(_address, (uint8_t) 1);
if (n != 1) return GY521_ERROR_READ;
if (n != 1)
{
_error = GY521_ERROR_READ;
return _error;
}
uint8_t val = Wire.read();
return val;
}
Expand Down
35 changes: 21 additions & 14 deletions GY521.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// FILE: GY521.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.5
// VERSION: 0.2.0
// PURPOSE: Arduino library for I2C GY521 accelerometer-gyroscope sensor
// URL: https://github.com/RobTillaart/GY521
//
Expand All @@ -13,17 +13,18 @@
#include "Arduino.h"
#include "Wire.h"

#define GY521_LIB_VERSION (F("0.1.5"))
#define GY521_LIB_VERSION (F("0.2.0"))

#ifndef GY521_THROTTLE_TIME
#define GY521_THROTTLE_TIME 10 // milliseconds
#endif

// ERRORCODES getRaw()
#define GY521_OK 0
#define GY521_THROTTLED 1
#define GY521_ERROR_READ -1
#define GY521_ERROR_WRITE -2
// ERROR CODES
#define GY521_OK 0
#define GY521_THROTTLED 1
#define GY521_ERROR_READ -1
#define GY521_ERROR_WRITE -2
#define GY521_ERROR_NOT_CONNECTED -3


class GY521
Expand All @@ -44,36 +45,41 @@ class GY521
// 0..65535 millis == roughly 1 minute.
void setThrottleTime(uint16_t ti ) { _throttleTime = ti; };
uint16_t getThrottleTime() { return _throttleTime; };

// returns GY521_OK or one of the error codes above.
int16_t read();

// SET BEFORE READ
// as = 0,1,2,3 ==> 2g 4g 8g 16g
void setAccelSensitivity(uint8_t as);
bool setAccelSensitivity(uint8_t as);
uint8_t getAccelSensitivity(); // returns 0,1,2,3
// gs = 0,1,2,3 ==> 250, 500, 1000, 2000 degrees/second
bool setGyroSensitivity(uint8_t gs);
uint8_t getGyroSensitivity(); // returns 0,1,2,3

// CALL AFTER READ
float getAccelX() { return _ax; };
float getAccelY() { return _ay; };
float getAccelZ() { return _az; };
float getAngleX() { return _aax; };
float getAngleY() { return _aay; };
float getAngleZ() { return _aaz; };

float getTemperature() { return _temperature; };

// gs = 0,1,2,3 ==> 250, 500, 1000, 2000 degrees/second
void setGyroSensitivity(uint8_t gs);
uint8_t getGyroSensitivity(); // returns 0,1,2,3
float getGyroX() { return _gx; };
float getGyroY() { return _gy; };
float getGyroZ() { return _gz; };

float getPitch() { return _pitch; };
float getRoll() { return _roll; };
float getYaw() { return _yaw; };

// last time sensor is actually read.
uint32_t lastTime() { return _lastTime; };

// generic worker to get access to all functionality
uint8_t setRegister(uint8_t reg, uint8_t value);
uint8_t getRegister(uint8_t reg);
// get last error and reset error to OK.
int16_t getError() { return _error; _error = GY521_OK; };

// callibration errors
float axe = 0, aye = 0, aze = 0; // accelerometer errors
Expand All @@ -84,6 +90,7 @@ class GY521
bool _throttle = true; // to prevent reading too fast
uint16_t _throttleTime = GY521_THROTTLE_TIME;
uint32_t _lastTime = 0; // to measure duration for math & throttle
int16_t _error = GY521_OK; // initially everything is OK

uint8_t _afs = 0; // sensitivity factor
float _raw2g = 1.0/16384.0; // raw data to gravity g's
Expand Down
8 changes: 7 additions & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
GY521 KEYWORD1

# Methods and Functions (KEYWORD2)
begin KEYWORD2
isConnected KEYWORD2
wakeup KEYWORD2
read KEYWORD2
setThrottle KEYWORD2
getThrottle KEYWORD2
setThrottleTime KEYWORD2
getThrottleTime KEYWORD2

setAccelSensitivity KEYWORD2
getAccelSensitivity KEYWORD2
Expand All @@ -31,11 +35,13 @@ getYaw KEYWORD2
lastTime KEYWORD2
setRegister KEYWORD2
getRegister KEYWORD2
getError KEYWORD2

# Constants (LITERAL1)
GY521_LIB_VERSION LITERAL1
GY521_THROTTLE_TIME LITERAL1
GY521_OK LITERAL1
GY521_THROTTLED LITERAL1
GY521_ERROR_READ LITERAL1
GY521_ERROR_WRITE LITERAL1
GY521_ERROR_WRITE LITERAL1
GY521_ERROR_NOT_CONNECTED LITERAL1
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/GY521.git"
},
"version":"0.1.5",
"version":"0.2.0",
"frameworks": "arduino",
"platforms": "*"
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=GY521
version=0.1.5
version=0.2.0
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=Arduino library for GY521 angle measurement
Expand Down

0 comments on commit b116412

Please sign in to comment.