Skip to content

Commit

Permalink
Remove i2c blocking loop
Browse files Browse the repository at this point in the history
  • Loading branch information
platisd committed Apr 7, 2018
1 parent 712e5aa commit 88461a9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
12 changes: 5 additions & 7 deletions src/Gyroscope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void Gyroscope::begin(unsigned short samplingPeriod){
void Gyroscope::update(){
if (millis()- _prevSample > _samplingRate){
float gyroRate = 0;
int gyroValue = getGyroValues();
int gyroValue = getGyroValues();
short drift = _gyroOffset - gyroValue;
if (abs(drift) > GYRO_THRESHOLD){ //if there has been a big enough drift (trying to contemplate for the noise)
gyroRate = drift * GYRO_SENSITIVITY;
Expand All @@ -51,7 +51,7 @@ void Gyroscope::update(){

void Gyroscope::initializeGyro(){
Wire.begin(); //initialize the i2c connection
setupL3G4200D(2000); // Configure L3G4200 at 2000 deg/sec. Other options: 250, 500 (NOT suggested, will have to redetermine offset)
setupL3G4200D(2000); // Configure L3G4200 at 2000 deg/sec. Other options: 250, 500 (NOT suggested, will have to redetermine offset)
}

/* based on the bildr.org example: http://bildr.org/2011/06/l3g4200d-arduino/ */
Expand Down Expand Up @@ -91,7 +91,7 @@ void Gyroscope::setupL3G4200D(int scale){
}

void Gyroscope::writeRegister(int deviceAddress, byte address, byte val) {
Wire.beginTransmission(deviceAddress); // start transmission to device
Wire.beginTransmission(deviceAddress); // start transmission to device
Wire.write(address); // send register address
Wire.write(val); // send value to write
Wire.endTransmission(); // end transmission
Expand All @@ -104,9 +104,7 @@ int Gyroscope::readRegister(int deviceAddress, byte address){
Wire.endTransmission();
Wire.requestFrom(deviceAddress, 1); // read a byte

while(!Wire.available()) {
//waiting
}
if(!Wire.available()) return -1;

v = Wire.read();

Expand All @@ -120,5 +118,5 @@ int Gyroscope::calibrate(int measurements){ //use this function in order to dete
sum += getGyroValues();
delay(10);
}
return sum/measurements; //return the average
return sum/measurements; //return the average
}
4 changes: 2 additions & 2 deletions src/SRF08.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ unsigned int SRF08::getDistance(){
Wire.write(byte(0x02));
Wire.endTransmission();
Wire.requestFrom(_address, uint8_t (2));
while (Wire.available() < 2);
if (!Wire.available()) return -1; // Return a large error-like value
byte high = Wire.read();
byte low = Wire.read();
return (high << 8) + low;
Expand All @@ -60,7 +60,7 @@ unsigned short SRF08::getLightReading(){
Wire.write(byte(0x01));
Wire.endTransmission();
Wire.requestFrom(_address, uint8_t (1));
while (!Wire.available());
if (!Wire.available()) return -1; // Return a large error-like value
return Wire.read();
}

Expand Down

0 comments on commit 88461a9

Please sign in to comment.