Odrive + MPU6050+ Arduino operation

Hi All

I was having trouble getting the odrive to work with MPU6050 and I seem to have found the solution although I need a clarification. Bear with me and let me walkthrough what I discovered. I am using jrowberg mpu library which can be found here.

I modified the code a bit so that I can use it as a class which can be found here.(gyro.cpp)

Initially I had the calibration routine as so and put it in setup():

  Serial1 << "r axis" << L1_MOTOR << ".encoder.is_ready\n";
  bool L_done=odrive.readInt();
  Serial1 << "r axis" << R0_MOTOR << ".encoder.is_ready\n";
  bool R_done=odrive.readInt();
  requested_state = ODriveArduino::AXIS_STATE_ENCODER_OFFSET_CALIBRATION;
  while(L_done==0){
    odrive.run_state(L1_MOTOR, requested_state, true);
    delay(50);
    Serial1 << "r axis" << L1_MOTOR << ".encoder.is_ready\n";
    delay(50);
    L_done=odrive.readInt();
  }
  while(R_done==0){
    odrive.run_state(R0_MOTOR, requested_state, true);
    delay(50);
    Serial1 << "r axis" << R0_MOTOR << ".encoder.is_ready\n";
    delay(50);
    R_done=odrive.readInt();
  }

I put this code before I initialise the mpu since putting this code block after the mpu initialisation cause the arduino to freeze after some time once it enters the closed-loop state. I initialised the closed-loop state through serial in loop(). Even so, arduino still freezes after some time. Instead I tried by initialising the closed loop state immediately after calibration like so:

Serial1 << "r axis" << L1_MOTOR << ".encoder.is_ready\n";
  bool L_done=odrive.readInt();
  Serial1 << "r axis" << R0_MOTOR << ".encoder.is_ready\n";
  bool R_done=odrive.readInt();
  requested_state = ODriveArduino::AXIS_STATE_ENCODER_OFFSET_CALIBRATION;
  while(L_done==0){
    odrive.run_state(L1_MOTOR, requested_state, true);
    delay(50);
    Serial1 << "r axis" << L1_MOTOR << ".encoder.is_ready\n";
    delay(50);
    L_done=odrive.readInt();
    requested_state = ODriveArduino::AXIS_STATE_CLOSED_LOOP_CONTROL;
    odrive.run_state(L1_MOTOR, requested_state, false);
  }
  requested_state = ODriveArduino::AXIS_STATE_ENCODER_OFFSET_CALIBRATION;
  while(R_done==0){
    odrive.run_state(R0_MOTOR, requested_state, true);
    delay(50);
    Serial1 << "r axis" << R0_MOTOR << ".encoder.is_ready\n";
    delay(50);
    R_done=odrive.readInt();
    requested_state = ODriveArduino::AXIS_STATE_CLOSED_LOOP_CONTROL;
    odrive.run_state(R0_MOTOR, requested_state, false);
  }

This time it works. Arduino doesn’t freeze anymore! I couldn’t find out why this is the case. I tried looking into the odrive firmware folder but to no avail.

Anyone here knows why entering closed-loop state immediately after calibration doesn’t cause arduino to freeze?

Turns out doing the above cause the DMP to fail initialisation. I connected the INT pin to pin 2 on my arduino. DMP then worked but later it cause arduino to freeze again…