Hello,
I have big problem with my project and i have to ask You for help.
I want implement Madgwick libary to my projct on Arduino Nano 33 BLE. I read data from IMU sensor with use ArduinoLSM9DS1 libary.
First time when I used Arduino and IMU I used Euler angles to receive pitch, roll, and yaw. Everytching was working preety well.
Nextly I wanted to use Madgwick libary with quaternions and I receiving really crazy numbers in output.
Maybe someone of You done this before and can give me some advise? Why this dont working properly.
I calibrated manetometer,
Read data from sensor with properly format,
Data have this frequency:
Accelerometer -119H
Gyro -119Hz
Magneto - 20Hz
#include "Arduino.h"
#include "./../lib/Arduino_LSM9DS1/src/Arduino_LSM9DS1.h"
#include "./../lib/ArduinoBLE/src/ArduinoBLE.h"
#include "./../lib/Madgwick/src/MadgwickAHRS.h"
#include "./../lib/MahonyAHRS-master/src/MahonyAHRS.h"
#include "./../lib/MegunoLink/MegunoLink.h"
float accX, accY, accZ, accHZ, gyrHZ, gyrX, gyrY, gyrZ, magX, magY, magZ;
float heading, roll, pitch, yaw, thetaM, phiM;
float thetaFnew, thetaFold =0, phiFnew, phiFold =0;
float thetaG =0, phiG=0, dt, q1, q0, q2, q3;
unsigned long millisOld;
float theta, phi;
float psi, phiRad, thetaRad, Xm, Ym, psiM, psiFold, psiFnew;
unsigned long millisPerReading, microsPrevious;
Madgwick filter;
void setup() {
Serial.begin(9600);
while (!Serial);
if (!IMU.begin()) {
while (1);
}
filter.begin(119.0); //Magdgwick
millisOld = millis();
millisPerReading = 0.0085;
}
void loop() {
if (IMU.accelerationAvailable()&& IMU.gyroscopeAvailable()&&IMU.magneticFieldAvailable()) {
IMU.readAcceleration(accX, accY, accZ);
IMU.readGyroscope(gyrX, gyrY, gyrZ);
IMU.readMagneticField(magX, magY, magZ);
accHZ = IMU.accelerationSampleRate();
gyrHZ = IMU.gyroscopeSampleRate();
if (((millis()-millisOld)/1000) >= microsPerReading) {
Serial.print((millis()-millisOld)/1000.0);
Serial.print('\t');
//Madgwick + offset magnetometr
//filter.update(gyrX, gyrY, gyrZ, accX, accY, accZ, magX+5, magY-10, magZ);
filter.updateIMU(gyrX, gyrY, gyrZ, accX, accY, accZ);
}
// thetaM = atan2(accX,accZ)/2/3.141592654*360; //Euler angles
// phiM = atan2(accY,accZ)/2/3.141592654*360;
// thetaFnew = 0.9 * thetaFold + 0.1 * thetaM;
// phiFnew = 0.9 * phiFold + 0.1 * phiM;
//dt = (millis()-millisOld)/1000.0;
//filter.Rate(119.00);
// millisOld = millis();
// theta = (theta + gyrY *dt)*0.95 + thetaM *0.05;
// phi = (phi - gyrX *dt)*0.95 + phiM *0.05;
// thetaG = thetaG + gyrY * dt;
// phiG = phiG - gyrX * dt;
// phiRad = phi /360*(2*3.141592654);
// thetaRad = theta /360*(2*3.141592654);
// Xm=magX*cos(thetaRad)-magY*sin(phiRad)*sin(thetaRad)+magZ*cos(phiRad)*sin(thetaRad);
// Ym=magY*cos(phiRad)+magZ*sin(phiRad);
// Ym = magX * sin(phiRad) * sin(thetaRad)+ magY * cos(phiRad) - magZ * cos(thetaRad) * sin(phiRad);
// Xm = magX * cos(thetaRad) + magZ *sin(thetaRad);
// psi = atan2(Ym,Xm)/(2*3.141592654)*360;
roll = filter.getRoll(); //Madgwick
pitch = filter.getPitch();
yaw = filter.getYaw();
q0 = filter.getQ0();
q1 = filter.getQ1();
q2 = filter.getQ2();
q3 = filter.getQ3();
Serial.print(q0);
Serial.print(',');
Serial.print(q1);
Serial.print(',');
Serial.print(q2);
Serial.print(',');
Serial.print(q3);
Serial.print(',');
Serial.print(roll);
Serial.print(',');
Serial.print(pitch);
Serial.print(',');
Serial.println(yaw);
millisOld = millis();
}
}