Skip to content

Instantly share code, notes, and snippets.

@shfitz
Created November 17, 2020 03:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shfitz/a8704a890497cd43789a3bc029179245 to your computer and use it in GitHub Desktop.
Save shfitz/a8704a890497cd43789a3bc029179245 to your computer and use it in GitHub Desktop.
// based off of Tom Igoe's version here : https://itp.nyu.edu/physcomp/lessons/accelerometers-gyros-and-imus-the-basics
#include <Arduino_LSM9DS1.h>
#include "MadgwickAHRS.h"
// initialize a Madgwick filter:
Madgwick filter;
// sensor's sample rate is fixed at 104 Hz:
const float sensorRate = 104.00;
void setup() {
Serial.begin(9600);
// attempt to start the IMU:
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU");
// stop here if you can't access the IMU:
while (true);
}
// start the filter to run at the sample rate:
filter.begin(sensorRate);
}
void loop() {
// values for acceleration and rotation:
float xAcc, yAcc, zAcc;
float xGyro, yGyro, zGyro;
// values for orientation:
float roll, pitch, heading;
// check if the IMU is ready to read:
if (IMU.accelerationAvailable() &&
IMU.gyroscopeAvailable()) {
// read accelerometer &and gyrometer:
IMU.readAcceleration(xAcc, yAcc, zAcc);
IMU.readGyroscope(xGyro, yGyro, zGyro);
// update the filter, which computes orientation:
filter.updateIMU(xGyro, yGyro, zGyro, xAcc, yAcc, zAcc);
// print the heading, pitch and roll
roll = filter.getRoll();
pitch = filter.getPitch();
heading = filter.getYaw();
Serial.print("Orientation: ");
Serial.print(heading);
Serial.print(" ");
Serial.print(pitch);
Serial.print(" ");
Serial.println(roll);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment