Where would I use the atan2f function to determine degrees of the X axis instead of linear mapping as I've done in the code? (apparently the 'small angle approximation is not optimal')
Was told to use "float angle = atan2f(x, z) * 180 / M_PI;" but It might I'm not sure where/how to use it.
For further context I'm interested in the best measurement of the X axis in degrees and what the code would actually look like as X will be changing quite fast.
Thanks so much for any help
#include <Arduino_LSM9DS1.h>
#define fmap(value, oldmin, oldmax, newmin, newmax) (((value) - (oldmin)) * ((newmax) - (newmin)) / ((oldmax) - (oldmin)) + (newmin)) // allows for mapping of decimals
float x, y, z;
float degreesX = 0;
float degreesY = 0;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Started");
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
Serial.print("Accelerometer sample rate = ");
Serial.print(IMU.accelerationSampleRate());
Serial.println("Hz");
}
void loop() {
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(x, y, z);
}
if (x > 0) {
x = 100 * x;
degreesX = fmap(x, 0, 97, 0, 90);
Serial.print("Tilting up ");
Serial.print(degreesX);
Serial.println(" degrees");
}
if (x < -0) {
x = 100 * x;
degreesX = fmap(x, 0, -100, 0, 90);
Serial.print("Tilting down ");
Serial.print(degreesX);
Serial.println(" degrees");
}
delay(100);
}
Linear mapping would be used when you have a device that measures angles directly. The arctangent is used when you have measurements on two orthogonal axes.
The arctangent is the mathematically correct approach to perform the vector analysis. The "small angle" linear approximation works well only if the tilt angle is less than a few degrees.
Thanks! I've got it set up like below just to test and the angle reads different to the linear mapping I did (I'm using the Arduino 33 BLE) I'm guessing I've done something wrong?
Those "decimals" are simply wrong, so what is the point of looking at them?
The accelerometer is noisy enough that even when using the correct (atan2) formula, you can't accurately resolve 0.1 degree differences in tilt, unless you very carefully calibrate the accelerometer, and average hundreds of measurements.
Had no idea was just told that that's how I should do it.
This is just a simple bit of fun, does not need to be very precise but I'll filter the IMU output somehow (might be better or worse than the IR sensor I used for this. Not sure how to do it yet, but that's the fun
Just jumped on this one because it was built into the board, thanks though! Hopefully it will be fine, seemed to be pretty responsive/accurate enough even using fmap for what I want