Go Down

Topic: I2C read error with MPU6050 (possible conflict with SoftwareSerial) (Read 123 times) previous topic - next topic

andzerb

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:

Code: [Select]
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
}





GolamMostafa

What kind of UART driven device/sensor have you connected with a software serial port using the following commands (assume UNO Platform)? There is no trace about it in your codes?

Code: [Select]
#include<SoftwareSerial.h>
SoftwareSerial myDevice(SRX, STX);  //assign DPin numbers other than 0, 1 for SRX, STX

void setup()
{
   myDevice.begin(9600);
}

pylon

The main error is in the following line of code:

Code: [Select]
    odrive_serial.begin(115200);

SoftwareSerial won't work with any baud rate above 38400. Even that baud rate (and everything above 9600 baud) will work only if the counterpart is very timing tolerant. Additionally the Arduino is blocked during the the transfer of a complete byte. So if you have a quite busy serial communication the Arduino will do nothing else than trying to interpret the serial bits.

I don't know much about your setup but it seems that a Leonardo or better a Mega2560 will better fit your needs than the UNO because both have a free hardware serial interface.

Go Up