Hi!
Newbie to the forum here. I'm building a balancing robot using an Uno, the Odrive controller, and a MPU6050 IMU breakout. I've cobbled together some code mostly using an example from a Kalman filter library. The Odrive controller communicates over SoftwareSerial, and if I don't enable this connection, I can read IMU data consistently fine. But if I do, after a few seconds I get the error, "i2cread failed: 4". Not sure what's going on. Apologies in advanced for my extremely hacky code (below).
An excerpt:
uint8_t i2cRead(uint8_t registerAddress, uint8_t *data, uint8_t nbytes) {
uint32_t timeOutTimer;
Wire.beginTransmission(IMUAddress);
Wire.write(registerAddress);
uint8_t rcode = Wire.endTransmission(false); // Don't release the bus
if (rcode) {
Serial.print(F("i2cRead failed: "));
Serial.println(rcode);
return rcode; // See: http://arduino.cc/en/Reference/WireEndTransmission
}
Wire.requestFrom(IMUAddress, nbytes, (uint8_t)true); // Send a repeated start and then release the bus after reading
for (uint8_t i = 0; i < nbytes; i++) {
if (Wire.available())
data[i] = Wire.read();
else {
timeOutTimer = micros();
while (((micros() - timeOutTimer) < I2C_TIMEOUT) && !Wire.available());
if (Wire.available())
data[i] = Wire.read();
else {
Serial.println(F("i2cRead timeout"));
return 5; // This error value is not already taken by endTransmission
}
}
}
return 0; // Success
}
balancer_kalman.zip (4.99 KB)