Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

While fix #8

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=MPU6050
version=1.0
author=
maintainer=
sentence=MPU6050 Triple Axis Gyroscope & Accelerometer
paragraph=
category=Uncategorized
url=
architectures=*
38 changes: 26 additions & 12 deletions MPU6050.cpp → src/MPU6050.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,10 @@ Vector MPU6050::readRawAccel(void)
Wire.endTransmission();

Wire.beginTransmission(mpuAddress);
Wire.requestFrom(mpuAddress, 6);

while (Wire.available() < 6);
if (Wire.requestFrom(mpuAddress, 6) != 6) {
errno = ERR_NOT_ENOUGH_BYTES;
return ra;
}

#if ARDUINO >= 100
uint8_t xha = Wire.read();
Expand Down Expand Up @@ -413,10 +414,12 @@ Vector MPU6050::readRawGyro(void)
#endif
Wire.endTransmission();

Wire.beginTransmission(mpuAddress);
Wire.requestFrom(mpuAddress, 6);

if (Wire.requestFrom(mpuAddress, 6) != 6) {
errno = ERR_NOT_ENOUGH_BYTES;
return rg;
}

while (Wire.available() < 6);

#if ARDUINO >= 100
uint8_t xha = Wire.read();
Expand Down Expand Up @@ -612,7 +615,11 @@ void MPU6050::setThreshold(uint8_t multiple)
// Remember old threshold value
actualThreshold = multiple;
}

uint8_t MPU6050::getErrno(void)
{
uint8_t errno;
return errno;
}
// Fast read 8-bit from register
uint8_t MPU6050::fastRegister8(uint8_t reg)
{
Expand All @@ -627,7 +634,10 @@ uint8_t MPU6050::fastRegister8(uint8_t reg)
Wire.endTransmission();

Wire.beginTransmission(mpuAddress);
Wire.requestFrom(mpuAddress, 1);
if (Wire.requestFrom(mpuAddress, 1) != 1) {
errno = ERR_NOT_ENOUGH_BYTES;
return 0;
}
#if ARDUINO >= 100
value = Wire.read();
#else
Expand All @@ -652,8 +662,10 @@ uint8_t MPU6050::readRegister8(uint8_t reg)
Wire.endTransmission();

Wire.beginTransmission(mpuAddress);
Wire.requestFrom(mpuAddress, 1);
while(!Wire.available()) {};
if (Wire.requestFrom(mpuAddress, 1) != 1) {
return 0;
}

#if ARDUINO >= 100
value = Wire.read();
#else
Expand Down Expand Up @@ -691,8 +703,10 @@ int16_t MPU6050::readRegister16(uint8_t reg)
Wire.endTransmission();

Wire.beginTransmission(mpuAddress);
Wire.requestFrom(mpuAddress, 2);
while(!Wire.available()) {};
if (Wire.requestFrom(mpuAddress, 2) != 2) {
errno = ERR_NOT_ENOUGH_BYTES;
return 0;
}
#if ARDUINO >= 100
uint8_t vha = Wire.read();
uint8_t vla = Wire.read();
Expand Down
7 changes: 7 additions & 0 deletions MPU6050.h → src/MPU6050.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef VECTOR_STRUCT_H
#define VECTOR_STRUCT_H

// Errors
#define ERR_NOERR 0
#define ERR_NOT_ENOUGH_BYTES 1

struct Vector
{
float XAxis;
Expand Down Expand Up @@ -230,6 +235,7 @@ class MPU6050
Vector readNormalizeAccel(void);
Vector readScaledAccel(void);

uint8_t getErrno(void);
private:
Vector ra, rg; // Raw vectors
Vector na, ng; // Normalized vectors
Expand All @@ -242,6 +248,7 @@ class MPU6050
bool useCalibrate;
int mpuAddress;

uint8_t errno;
uint8_t fastRegister8(uint8_t reg);

uint8_t readRegister8(uint8_t reg);
Expand Down