Using accelerometers, gyrometers, and IMUs

https://itp.nyu.edu/physcomp/labs/lab-serial-imu-output-to-p5-js/

Trying out accelerometers, gyrometers, and IMUs with a basic sphere in p5.js. (Using WebSerial)

//load libraries
#include <Arduino_LSM6DS3.h>
#include <MadgwickAHRS.h>
 
// initialize a Madgwick filter:
Madgwick filter;
// sensor's sample rate is fixed at 104 Hz:
const float sensorRate = 104.00;
 
// values for orientation:
float heading = 0.0; //x-axis
float pitch = 0.0; //y-axis
float roll = 0.0; //z-axis

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;
 
  // 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
    //Madgwick Filter, gets rotation of 3 axis in degrees
    roll = filter.getRoll(); 
    pitch = filter.getPitch();
    heading = filter.getYaw();
  

  // if you get a byte in the serial port, (handshaking)
  if (Serial.available()) {
    char input = Serial.read();
    
    Serial.print(heading);
    Serial.print(",");
    Serial.print(pitch);
    Serial.print(",");  
    Serial.println(roll);
  }
}

https://editor.p5js.org/zixin.cheng/sketches/qr9jQFSvz

IMG_8019.MOV

Issue: Value of Heading would slowly decrease even when the Nano/breadboard is static.

Screen Shot 2022-11-01 at 5.13.33 PM.png

Arduino Serial Monitor

IMG_8053.MOV

Might need to test the code with another Arduino

9dof

magetometer

ascii less efficient but more readable

serial.port.forget() in browser console

mdn mozilla develop network