From 3a9d11a43ec178180c8c271df4fb0029cdc41791 Mon Sep 17 00:00:00 2001 From: rob tillaart Date: Tue, 3 Nov 2020 10:59:19 +0100 Subject: [PATCH] 0.2.0 improve error handling --- GY521.cpp | 59 +++++++++++++++++++++++++++++++++++++--------- GY521.h | 35 ++++++++++++++++----------- keywords.txt | 8 ++++++- library.json | 2 +- library.properties | 2 +- 5 files changed, 78 insertions(+), 28 deletions(-) diff --git a/GY521.cpp b/GY521.cpp index 428cbd2..caddb89 100644 --- a/GY521.cpp +++ b/GY521.cpp @@ -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 // @@ -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" @@ -57,7 +58,7 @@ bool GY521::begin() bool GY521::isConnected() { Wire.beginTransmission(_address); - return Wire.endTransmission() == 0; + return (Wire.endTransmission() == 0); } bool GY521::wakeup() @@ -65,7 +66,7 @@ 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() @@ -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; @@ -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; } @@ -193,7 +218,11 @@ 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; } @@ -201,9 +230,17 @@ 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; } diff --git a/GY521.h b/GY521.h index d32356f..1984ef9 100644 --- a/GY521.h +++ b/GY521.h @@ -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 // @@ -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 @@ -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 @@ -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 diff --git a/keywords.txt b/keywords.txt index 9c601aa..5f5831a 100644 --- a/keywords.txt +++ b/keywords.txt @@ -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 @@ -31,6 +35,7 @@ getYaw KEYWORD2 lastTime KEYWORD2 setRegister KEYWORD2 getRegister KEYWORD2 +getError KEYWORD2 # Constants (LITERAL1) GY521_LIB_VERSION LITERAL1 @@ -38,4 +43,5 @@ GY521_THROTTLE_TIME LITERAL1 GY521_OK LITERAL1 GY521_THROTTLED LITERAL1 GY521_ERROR_READ LITERAL1 -GY521_ERROR_WRITE LITERAL1 \ No newline at end of file +GY521_ERROR_WRITE LITERAL1 +GY521_ERROR_NOT_CONNECTED LITERAL1 \ No newline at end of file diff --git a/library.json b/library.json index 0d79565..2b34b11 100644 --- a/library.json +++ b/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/GY521.git" }, - "version":"0.1.5", + "version":"0.2.0", "frameworks": "arduino", "platforms": "*" } diff --git a/library.properties b/library.properties index 6554a83..520e117 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=GY521 -version=0.1.5 +version=0.2.0 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for GY521 angle measurement