DUE TOMORROW Not connected to MPU6050 and still printing 87.5*

Wowza! I’ve been trying to connect the photon to an MPU6050 and I’m running into so many issues!
Even though I have reflashed my particle photon with new code it’s still printing in the format of old code.
Occasionally when flashing the device I get the error “unexpected end of multi-part data” (I’m using the Particle Dev Desktop IDE). Below is the code that I wrote. I am also trying to calibrate my device but the functions to reset the MPU’s offset are not working.
Thanks in advance for the help!

#include "MPU6050.h"
#include <math.h>
#include "I2cMaster.h"
SYSTEM_MODE(AUTOMATIC);

WireMaster WireAlt;
#undef Wire
#define Wire WireAlt
// MPU variables:
MPU6050 accelgyro;
int16_t ax, ay, az, gx, gy, gz;
int ax_avg = 0;
int ay_avg = 0;
int az_avg = 0;

long ax_sum = 0;
long ay_sum = 0;
long az_sum = 0;
// offsets (redMPU):
// int accelOffsetX = -3315;
// int accelOffsetY = -2122;
// int accelOffsetZ = 887;
// int gyroOffsetX = 67;
// int gyroOffsetY = -80;
// int gyroOffsetZ = -4;

// offsets (greenMPU):
int16_t accelOffsetX = -92;
int16_t accelOffsetY = -4620;
int16_t accelOffsetZ = 670;
// int gyroOffsetX = -304;
// int gyroOffsetY = -42;
// int gyroOffsetZ = -67;

int buffersize = 1000; //The number of readings that will be used to find the average offset.

// constants
const float SCALE = 16384.0;
const float PI = 3.1415926;

void setup() {
    Wire.begin();
    Serial.begin(9600);
    while (Serial.available() && Serial.read()); // empty buffer
      while (!Serial.available()){
        Serial.println(F("Send any character to start sketch."));
        delay(1500);
    } while (Serial.available() && Serial.read()); // empty buffer again

    Serial.println("Calibration beginning");
    delay(2000);
    Serial.println("Your device should be resting horizontally. Do not touch it until it has finished.");
    delay(2000);
    Serial.println("Initializing I2C devices...");
    accelgyro.initialize();

    // Cerify the connection:
    Serial.println("Testing device connections...");
    Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
    delay(1000);
    accelgyro.setXAccelOffset(0);
    accelgyro.setYAccelOffset(0);
    accelgyro.setZAccelOffset(0);

    averageSensorVals();
    calibrate();
}

void loop() {
  float angle = 0;
  float angleDeg = 0;
  accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  float xAcc = float(ax + accelOffsetX) / SCALE;
  float yAcc = float(ay + accelOffsetY) / SCALE;
  float zAcc = float(az + accelOffsetZ) / SCALE;

  angle = zAcc / (sqrt (pow (xAcc, 2) + pow(yAcc, 2) + pow(zAcc, 2)));
  Serial.print("Angle before cosine: "); Serial.println(angle);
  angleDeg = acos(angle) * (180.0 / PI);
  if (angleDeg > 15){
    Particle.publish("Warning your house is at a dangerous angle, call for consultation. \n Angle:    ", String(angleDeg), 42);
  }
  Serial.print("Angle:   "); Serial.println(angleDeg);
  Particle.publish("Angle:   ", String(angleDeg), 42);
  delay(500);
}
void averageSensorVals(){
  Serial.println("--------");
  ax_sum = 0;
  ay_sum = 0;
  az_sum = 0;

  for (long i = 0; i < (buffersize + 101); ++i){
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
    if (i > 100 && i <= (buffersize+100)){
      ax_sum += ax;
      ay_sum += ay;
      az_sum += az;
    }
    if (i == (buffersize + 100)){
      ax_avg = (ax_sum / buffersize);
      ay_avg = (ay_sum / buffersize);
      az_sum = (az_sum / buffersize);
    }
    delay(2);
  }
  Serial.println("avg sensor vals complete");
}

void calibrate(){
  Serial.println("Calibrating offsets");
  bool done = false;
  accelOffsetX = -ax_sum / 8;
  accelOffsetY = -ay_sum / 8;
  accelOffsetZ = (16384 - az_sum) / 8;

  while (done == false){
    int ready = 0;
    Serial.println("*******");
    accelgyro.setXAccelOffset(accelOffsetX);
    accelgyro.setYAccelOffset(accelOffsetY);
    accelgyro.setZAccelOffset(accelOffsetZ);
    averageSensorVals();
    switch(ready){
      case 0:
        Serial.print("ax_sum"); Serial.println(ax_sum);
        if (abs(ax_sum) <= 8) ++ready;
        else accelOffsetX = accelOffsetX - ax_sum / 8;

      case 1:
        Serial.print("ay_sum"); Serial.println(ay_sum);
        if (abs(ay_sum) <= 8) ++ready;
        else accelOffsetX = accelOffsetY - ay_sum / 8;

      case 2:
        Serial.print("az_sum"); Serial.println(az_sum);
        Serial.print("ready: "); Serial.println(ready);
        if (abs(az_sum) <= 8) ++ready;
        else accelOffsetZ = accelOffsetX + 16384 - az_sum / 8;
        break;

      case 3:
        Serial.print("X offset:   "); Serial.println(accelOffsetX);
        Serial.print("Y offset:   "); Serial.println(accelOffsetY);
        Serial.print("Z offset:   "); Serial.println(accelOffsetZ);
        Serial.println("Calibration finished.");
        done = true;
        break;
      }
    }
  }

Hi @N00b

Normally when you do not get the code you expect on the device after flashing, it is due to incompatible versions of the system vs. user firmware. If you are using the webIDE (build.particle.io) then you can click on the little “target” icon on the left and then on the > next to the device to see what firmware revision is on the device versus what you are building against.

3 Likes

Thanks for your reply! I checked my device firmware using particle build. The system firmware target is 0.7.0 and the firmware on the device is 0.7.0 as well. So, I think they match.

With Desktop IDE chances are also that you are not actually building the project you are editing.
Dev does not build the project connected to the file you have open in the editor but the porject you have opened via the Open Folder option.

2 Likes

Would it work to try and run the project through https://build.particle.io/?

If the required libraries are available for Web IDE.

I've wondered why you actually use I2cMaster instead of the default Wire.

This also looks like some "legacy" code.
You can actually have if (Serial.available() && Serial.read()) as one command

  if (Serial.read() >= 0)

since Serial.read() internally calls Serial.available() and returns -1 when nothing is available.

Thanks! I require the I2Cdev library but I was getting the error “‘BUFFER_LENGTH’ was not declared in this scope” so I thought maybe I2cMaster would work for the function I needed (which is the functions to reset the offset.) Is there any other library that contains those functions? Or a better way to go about it? This is my first photon project so, I am piecing together a lot of other people’s work.

I’m not seeing any need for either I2Cdev (other than the copy included in the MPU6050 library itself) nor I2cMaster in your code nor the MPU6050 library.
So removing I2cMaster and this …

WireMaster WireAlt;
#undef Wire
#define Wire WireAlt

… should work just as well and remove potential roots for the misbehaviour from the picture.

But since MPU6040 library was contributed by @peekay123 it might be best to loop him in here.

Thanks! I removed i2c dev and the wire stuff. Now the code is running. The problem that I have run into now is that it gets stuck, it won’t finish calibrating.

@N00b, the MPU6050 library in the web IDE is one I ported quite a long time ago. I recently used a different library to create a Sandgrains animation on an RGB Matrix Panel:

I had zero problems with this library and it has some solid examples to work from.

1 Like