LSM9DS1 with AHRS Library

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
alexic
 
Posts: 5
Joined: Sat Jul 18, 2020 12:05 pm

LSM9DS1 with AHRS Library

Post by alexic »

I am trying to use the Adafruit_AHRS library running on an Arduino Uno with the LSM9DS1 breakout board.
The inital example is using LSM6DS_LIS3MDL breakout but some lines ate the begining show that LSM9DS0 and LSM9DS1 can also be used:

Code: Select all

// uncomment one combo 9-DoF!
//#include "LSM6DS_LIS3MDL.h"  // can adjust to LSM6DS33, LSM6DS3U, LSM6DSOX...
#include "LSM9DS.h"           // LSM9DS1 or LSM9DS0
//#include "NXP_FXOS_FXAS.h"  // NXP 9-DoF breakout
// pick your filter! slower == better quality output
//Adafruit_NXPSensorFusion filter; // slowest
//Adafruit_Madgwick filter;  // faster than NXP
Adafruit_Mahony filter;  // fastest/smalleset
I have to mention that I did my best to troubleshoot this during several days, I performed the calibration of the sensors using MotionCal.exe,

The problem is that the rotation angles are calculated wrongly: the object rotates in 3D but after stoping in real life it rotates in the other direction in the 3D program.
From the datasheet of LSM9DS0 and LSM9DS1 which should be similar, I found a huge difference in how the axis of the 3 sensors are aligned.
LSM9DS0.png
LSM9DS0.png (83.67 KiB) Viewed 631 times
LSM9DS1.png
LSM9DS1.png (102.43 KiB) Viewed 631 times
What I found is that the X and Y axis of accelerometer and gyroscope have to be inversed in the filter calculations as follows:

Code: Select all

// default (original code)
//filter.update(gx, gy, gz, accel.acceleration.x, accel.acceleration.y, accel.acceleration.z, mag.magnetic.x, mag.magnetic.y, mag.magnetic.z);

// just accelerometer and gyro
filter.update(gy, -gx, gz, -accel.acceleration.y, accel.acceleration.x, accel.acceleration.z, 0, 0, 0);

The problem remains with the magnetometer axis for which I can't figure how to rotate them the match the filter algorithm.
Can anyone at least try the AHRS library with the LSM9DS1 sensor. Or even with LSM9DS0 and confirm that it works.

Thank you.

User avatar
sj_remington
 
Posts: 998
Joined: Mon Jul 27, 2020 4:51 pm

Re: LSM9DS1 with AHRS Library

Post by sj_remington »

It should work fine if you align all the axes to point in the same directions and make certain that all are right handed coordinate systems. By default, the coordinate systems for the LSM9DS1 accelerometer and gyro are left handed.

I suggest to leave the magnetometer as is, which will then define the yaw angle origin as magnetic North aligned with the magnetometer X axis, and invert/transpose other axes as required. In other words:

Code: Select all

filter.update(-gy, -gx, gz, -accel.acceleration.y, -accel.acceleration.x, accel.acceleration.z, mag.magnetic.x, mag.magnetic.y, mag.magnetic.z);
Also, make sure that the gyro scale factor is correct, and in the actual filter code gx, gy and gz have units of radians/second. Of course it is essential that the sensors are calibrated and offset corrected. Sorry, don't have one to test, so keep us informed.

User avatar
alexic
 
Posts: 5
Joined: Sat Jul 18, 2020 12:05 pm

Re: LSM9DS1 with AHRS Library

Post by alexic »

Thank you for your response and suggestion.
Today I have made a sanity check and separated the gyro and only fed them into the algorithm then took only the accelerometer readings and fed them to the algorithm while the rest of the sensors I set them as 0 value. I noticed that my gyro started to drift quite a lot and the model is rotating by itself even though the IMU is static on the table.

Unfortunatelly the calibration schetch included in the library folder does not offer gyro calibration nor accelerometer calibration, which are essential for the well functioning of the algorithm.
They really are not hard to implement, just take a few hundreds of samples while the sensor is kept still, average the samples and subtract from the sensor values.

I am not sure I will go further with this library since I have found others that manage the complete procedure from start to finish.
If I ever come back to this library I will write the calibration functions and contribuite to the code.

User avatar
sj_remington
 
Posts: 998
Joined: Mon Jul 27, 2020 4:51 pm

Re: LSM9DS1 with AHRS Library

Post by sj_remington »

For calibration of both the magnetometer and accelerometer, I use the procedure first posted in http://sailboatinstruments.blogspot.com ... -part.html

It is by far the best procedure published to date. An example of using it in a difficult case is posted at https://forum.pololu.com/t/correcting-t ... eter/14315

User avatar
sjgregorski
 
Posts: 2
Joined: Thu Feb 07, 2019 8:25 pm

Re: LSM9DS1 with AHRS Library

Post by sjgregorski »

I also am having difficulties with the Adafruit AHRS examples using a Feather M0 express and a LSM9DS0 sensor. I've successfully performed both the magnetic and gyro calibrations and have them stored on the microcontroller. I can read back the calibrations correctly. However when I run the AHRS examples using any of the AHRS algorithms, the orientation vector will start to slowly rotate even if the sensor is completely still. It was my understanding that this should not happen since the mag data (which is not changing in this case since the sensor is not moving) should provide an absolute reference to help overcome the small amount of gyro offset that will exist even with correct calibration.

Any ideas ?

Anyway, a previous poster stated "I am not sure I will go further with this library since I have found others that manage the complete procedure from start to finish.". Care to share what libraries you are using ? I'd hate to bang my head against this if there is something out there that works better !!!!

User avatar
alexic
 
Posts: 5
Joined: Sat Jul 18, 2020 12:05 pm

Re: LSM9DS1 with AHRS Library

Post by alexic »

sjgregorski wrote:I also am having difficulties with the Adafruit AHRS examples using a Feather M0 express and a LSM9DS0 sensor. I've successfully performed both the magnetic and gyro calibrations and have them stored on the microcontroller. I can read back the calibrations correctly. However when I run the AHRS examples using any of the AHRS algorithms, the orientation vector will start to slowly rotate even if the sensor is completely still. It was my understanding that this should not happen since the mag data (which is not changing in this case since the sensor is not moving) should provide an absolute reference to help overcome the small amount of gyro offset that will exist even with correct calibration.

Any ideas ?

Anyway, a previous poster stated "I am not sure I will go further with this library since I have found others that manage the complete procedure from start to finish.". Care to share what libraries you are using ? I'd hate to bang my head against this if there is something out there that works better !!!!
What you first need to do is change the default code and re-arange the sensors how I or sjrem suggested.
You can also experiment yourself by taking one sensor at a time separately, for example accelerometer first and try to change the sign and order of the 3 axis.

I tried multiple libraries but the library that I found to be the most complete is developed by the user femmeverbeek found here on the Arduino forum alogside with discussion on how to use it and a video example of calibrating: https://forum.arduino.cc/?topic=686002

User avatar
sj_remington
 
Posts: 998
Joined: Mon Jul 27, 2020 4:51 pm

Re: LSM9DS1 with AHRS Library

Post by sj_remington »

The axis alignment for the LSM9DS1 magnetometer is corrected for this version of the Mahony fusion filter, which works very well:
https://github.com/jremington/LSM9DS1-AHRS

Callibration of at least the gyro and magnetometer is required, but the accelerometer seems fine (at least in my examples). But it never hurts to be thorough.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”