Obtaining orientation from an IMU

Have a look at this library by adafruit that interfaces between the arduino and the lsm6ds3trc

You’ll also need some code to calculate the 3d distance travel between each loop. I’d recommend a crude linear algebra approach.


/* UNTESTED CODE!!!
THIS CODE IS INCOMPLETE AND FOR INSTRUCTIONAL PURPOSES ONLY */
struct GVec {
   float x;
   float y;
   float z;

  float dist(GVec v) {
    float dx = x - v.x;
    float dy = y - v.y;
    float dz = z - v.z;
    return (float) Math.sqrt(dx*dx + dy*dy + dz*dz);
  }
}

//Init a global vector.
GVec cacheVector;
  
/* SETUP REQUIRED! See Library Documentation */
void setup{// CODE GOES HERE!}

void loop() {
  // Get a new normalized sensor event
  sensors_event_t accel;
  sensors_event_t gyro;
  sensors_event_t temp;
  lsm6ds3trc.getEvent(&accel, &gyro, &temp);

  /* Calculate the distance traveled form last loop in radians */
  GVec vzero;
  vzero.x = gyro.gyro.x;
  vzero.y = gyro.gyro.y;
  vzero.z = gyro.gyro.z;
  Serial.println(vzero.dist(cacheVector));
  cacheVector = vzero;
  delay(1000);
}
2 Likes