Theoretical: Measuring distance with inertial sensor

I had an idea, could you measure distance with the inertial sensor? The inertial sensor gives you acceleration which could be turned into velocity. @7996D_CarsonW and I made a simple code to try and test it out but, we can’t get it to print any value on the brain and when we print the raw acceleration form the inertial sensor it says 0.

double Acceleration = 0.0; 
double CurrentAcceleration = 0.0; // Acceleration in m/s

float TimeStep = 0.01; //Seconds

double CurrentVelocity = 0.0;
double PreviousVelocity = 0.0;

double CurrentPosition = 0.0;
double PreviousPosition = 0.0;

int x = 1;
int xLimit = 9;

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  while (1){
    if (x >= xLimit){

      Brain.Screen.clearScreen();
      x = 1;
    }

    if (x < xLimit){
      vex::task::sleep(TimeStep * 100);

      Acceleration = InertialSensor.acceleration(zaxis); // G-Force
      CurrentAcceleration = Acceleration * 9.80665; // m/s

      CurrentVelocity = PreviousVelocity + (CurrentAcceleration * TimeStep);
      CurrentPosition = PreviousPosition + (CurrentVelocity * TimeStep);

      // Makes it print mulitple times before it clears
      Brain.Screen.setCursor(x + 1, 2);
      Brain.Screen.print(Acceleration);

      CurrentVelocity = PreviousVelocity;
      CurrentPosition = PreviousPosition;

      x = x + 1; // Adds 1 to x
    }
  }
}

2 Likes

I have tried this, but the update rate of the sensor didn’t seem to be fast enough to accurately measure the distance, and it would give very inaccurate values after a few seconds

You might want to calibrate the sensor for best results

3 Likes

It updates at 100hz, seems plenty fast enough.

Well, that’s what I was told was the reason for the inaccuracy, but whatever the reason, it was way too inaccurate for what I was using it for (skills)

10ms is a long time to an embedded system. There’s also the fact that any error in the first integral is only going to accumulate again in the second. It’s a downward spiral of bad values. Good luck though, I’m curious to see your results

7 Likes

I tried this a long time ago and with even a 100 micro second wait, it would be hugely inaccurate after just a few seconds.

1 Like