Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arduino Nano 33 BLE with LSM9DS1 sensor -> problem with implementation Madgwick libary #14

Open
mataldomen opened this issue Feb 16, 2020 · 119 comments

Comments

@mataldomen
Copy link

Hey Kris,

I am writing to You becouse I have big problem with my Arduino board and I can't resolve this from week.

I want to implement Madgwick filter and quaternions into my Arduino Nano 33 BLE board. After that i receive a lot of really crazy numbers without any sense.

I configured My board with ArduinoLSM9DS1 libary (https://www.arduino.cc/en/Reference/ArduinoLSM9DS1) it's really simply part of code in comparison into yours. For me is perfect beacouse I am beginner.

In my first use of this board I used data from sensors and I calculated Eulers angles to receive pitch,roll and yaw. I received preety good numbers.

Nextly I want to implement madgwick from here(https://x-io.co.uk/open-source-imu-and-ahrs-algorithms/)

Maybe You know with Your experience why I still cant use Madgwick on my board ? Maybe You done someting before on this board ?

@kriswiner
Copy link
Owner

kriswiner commented Feb 16, 2020 via email

@MichaelFBA
Copy link

Hi Kris

How did you go translating the example for the nano 33?
I am also struggling a little getting accurate results with the new boards.

Did you want to share some code maybe we can figure it out together?

@kriswiner
Copy link
Owner

kriswiner commented Feb 23, 2020 via email

@MichaelFBA
Copy link

Ah sorry I meant @mataldomen :)

I've managed to get some calibration data from motioncal but reapplying the offsets has been a little difficult. I was hoping to have some realtime calibration methods when the device starts up.

I'll try your methods to see if I can implement it.
Cheers

@MichaelFBA
Copy link

@mataldomen

You can try this version of the Madgwick lib for the nano 33 ble.
I still need to figure out the calibration step, but might help getting you somewhere if you dont need calibrated data.

#include <ArduinoBLE.h>
#include <Arduino_LSM9DS1.h>
#include <MadgwickAHRS.h>

// Madgwick
Madgwick filter;
// sensor's sample rate is fixed at 119 Hz:
const float sensorRate = 119;

float roll, pitch, heading;

// BLE Service
BLEService imuService("917649A0-D98E-11E5-9EEC-0002A5D5C51B"); // Custom UUID

// BLE Characteristic
BLECharacteristic imuCharacteristic("917649A1-D98E-11E5-9EEC-0002A5D5C51B", BLERead | BLENotify, 12);

long previousMillis = 0;  // last timechecked, in ms
unsigned long micros_per_reading, micros_previous;

void setup() {
  Serial.begin(115200);    // initialize serial communication

  pinMode(LED_BUILTIN, OUTPUT); // initialize the built-in LED pin to indicate when a central is connected

  // begin initialization
  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    while (1);
  }

  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
    while (1);
  }

  // Setup bluetooth
  BLE.setLocalName("ArduinoIMU");
  BLE.setAdvertisedService(imuService);
  imuService.addCharacteristic(imuCharacteristic);
  BLE.addService(imuService);

  // start advertising
  BLE.advertise();
  Serial.println("Bluetooth device active, waiting for connections...");

  // start the filter to run at the sample rate:
  filter.begin(119);

  delay(10000);

  Serial.print("Accelerometer sample rate = ");
  Serial.print(IMU.accelerationSampleRate());
  Serial.println(" Hz");
  Serial.println();
  Serial.println("Acceleration in G's");
  Serial.println("X\tY\tZ");
  Serial.print("Gyroscope sample rate = ");
  Serial.print(IMU.gyroscopeSampleRate());
  Serial.println(" Hz");
  Serial.println();
  Serial.println("Gyroscope in degrees/second");
  Serial.println("X\tY\tZ");
  Serial.print("Magnetic field sample rate = ");
  Serial.print(IMU.magneticFieldSampleRate());
  Serial.println(" uT");
  Serial.println();
  Serial.println("Magnetic Field in uT");
  Serial.println("X\tY\tZ");

  micros_per_reading = 1000000 / 119;
  micros_previous = micros();
}


// send IMU data
void sendSensorData() {

  float ax, ay, az; // Acceleration
  float gx, gy, gz; // Gyroscope
  float mx, my, mz; // Magnometer

  // read orientation x, y and z eulers
  IMU.readAcceleration(ax, ay, az);
  IMU.readGyroscope(gx, gy, gz);
  IMU.readMagneticField(mx, my, mz);

  filter.update(gx, gy, gz, ax, ay, az, -mx, my, mz); //for all 3
  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);

  // Send 3x eulers over bluetooth as 1x byte array
  float data[3];
  data[0] = heading;
  data[1] = pitch;
  data[2] = roll;
  imuCharacteristic.setValue((byte *) &data, 12);

}

void loop() {
  // wait for a BLE central
  BLEDevice central = BLE.central();

  // if a BLE central is connected to the peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's BT address:
    Serial.println(central.address());
    // turn on the LED to indicate the connection:
    digitalWrite(LED_BUILTIN, HIGH);

    // while the central is connected:
    while (central.connected()) {
      unsigned long micros_now;
      micros_now = micros();

      if (micros_now - micros_previous >= micros_per_reading) {
        if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable() && IMU.magneticFieldAvailable()) { // XX
          sendSensorData();
          micros_previous = micros_previous + micros_per_reading;
        }
      }
    }
    // when the central disconnects, turn off the LED:
    digitalWrite(LED_BUILTIN, LOW);
    Serial.print("Disconnected from central: ");
    Serial.println(central.address());
  }
}

@mataldomen
Copy link
Author

Hi @MichaelFBA
Thank You for your help and code.
Code looks and work really similar which my. I was sure about that numbers which We receive from Arduino are faulty beacuse I bad use Madgwick libary.
I didnt think before about that sensors without calibration in real time can create so big mistake. Probably You are right.

Do You have any ideas how calibrate them properly ?

@MichaelFBA
Copy link

This repo has examples of how to calibrate the sensor at runtime, we would need to modify it to conform with the Arduino imu API.

I had a quick look at trying to migrate the code but haven't managed to finish it yet. There is a lot going on.

@elsatch
Copy link

elsatch commented Apr 20, 2020

As I was researching this topic, I discovered this message on the Arduino forum that could provide additional insights: https://forum.arduino.cc/index.php?topic=663160.msg4467543#msg4467543

Accoding to the illustration presented, magnetometer axis are flippped from the ones on accelerometerand gyroscopes. It also seems that rotation is left handed!

@kriswiner
Copy link
Owner

kriswiner commented Apr 20, 2020 via email

@elsatch
Copy link

elsatch commented Apr 22, 2020

Thanks @kriswiner for your response!

@Jeremiah1327
Copy link

Hello @MichaelFBA and @mataldomen , were either of you able to calibrate this sensor for the Arduino Nano 33 BLE? I have been stuck on this for a few days now. I tried changing the sensor axis and still cannot get it to work.

@mataldomen
Copy link
Author

mataldomen commented May 10, 2020

Hello,
Unfortunately I quit my project without solved this problem. When I read about my wrong setup of the axis two weeks ago I tried to do this properly again and nothing improves.

I cant help You. If You have to set orientation based on quaternions maybe You should buy another sensor which calculating quaternions without madgwick library. On market is big choice but these sensors are more expensive than this in our Arduinos :D

@MrStashley
Copy link

MrStashley commented Jun 1, 2020

https://www.youtube.com/watch?v=T9jXoG0QYIA

@mataldomen @MichaelFBA @Jeremiah1327 @elsatch I hope I'm not too late for you guys but I've recently have been working on the same problem and I've found some resources. As you can see from this video it's possible to calibrate the magnetometer without any libraries in a sort of brute force manner by taking a bunch of measurements in different places over a period of time, finding the median x y and z values and shifting them to 0 where 0 - median would be the offset that you could then add to every magnetometer reading and achieve calibration. I haven't looked just yet but I bet there are some 3D plotting libraries to help with this as well. It also seemed implied by the video that accelerometer values and gyroscope values were okay and didn't need to be calibrated, but I'm not sure if that is the case. I have noticed that the accelerometer and gyroscope read a little bit of noise when at rest and I think that disregarding values below some noise threshold might be enough.

I've also looked into motion cal https://learn.adafruit.com/adafruit-sensorlab-magnetometer-calibration/magnetic-calibration-with-motioncal but I'm struggling to get the sketches to work on the Nano 33 BLE Sense, if some of you guys want to try them, let me know if you can get them to work.

I've also experimented with using this algorithm: https://x-io.co.uk/open-source-imu-and-ahrs-algorithms/ in my receiver program to calculate a quaternion of rotation given the raw sensor data over bluetooth from my arduino, but the end result seems to be very inacurate and some of the axes seem to be wrong. I haven't calibrated yet so that might be part of my issue, also I have been randomly experimenting with the beta and sample period parameters since I'm not really sure what they're supposed to be, so that also might be a contributing factor lol

but that's where I've gotten so far, let me know if anyone has more information, thanks!

@kriswiner
Copy link
Owner

kriswiner commented Jun 1, 2020 via email

@MrStashley
Copy link

Apologies, I’m a bit new to this. I saw that but I was a bit deterred because there was a lot of code that I didn’t understand. Is this a library that you can get from the arduino store or at least import into the arduino editor? And what would we need to change to make it compatible with the arduino nano 33 ble sense?

@kriswiner
Copy link
Owner

kriswiner commented Jun 1, 2020 via email

@Jeremiah1327
Copy link

@MrStashley I spent many hours a day for over a week trying to get it to work. I watched a lot of YouTube videos about IMU calibration methods and tried to modify the numerous LSM9DS1 codes. I tried to modify @kriswiner 's code but still could not get it figured out. I bought the Arduino Nano BlE Sense for the purpose of having a built in IMU that would be easy to use. I decided to just try out different IMU/Microcontrollers with working libraries. I simply do not have the time and coding experience to do get it working with the BLE Sense and am not willing to sacrifice more time to get it to work, as I have deadlines for my project. If you end up figuring it out though, that would be awesome.

@MrStashley
Copy link

MrStashley commented Jun 1, 2020

@Jeremiah1327 ah okay. I'm still doing research and I haven't yet gotten around to trying to get @kriswiner 's code to work, but I have looked at and I see some stuff that seems promising, and getting another IMU is a last resort for me because I need the ble chip, but it might come to that for me as well. Which IMU did you end up buying that worked for you?

@MrStashley
Copy link

@kriswiner do you know how I could implement the writeByte method in a way that would work on my nano 33 ble sense?

@kriswiner
Copy link
Owner

kriswiner commented Jun 2, 2020 via email

@MrStashley
Copy link

Ah okay. I thought Wire was a library that I might have to replace but my editor seems to think it's fine. The error I'm getting now is that I2C_NOSTOP was not declared in this scope and it seems that it wasn't declared in your code either. Is this a constant that I can define or something native to a certain kind of board?

@kriswiner
Copy link
Owner

kriswiner commented Jun 2, 2020 via email

@MrStashley
Copy link

ahh okay my bad I didn't notice. Thanks a ton :)

@MrStashley
Copy link

So the writeByte method causes the Mbed OS to crash. It could be an issue with the actual registers used in the calls but the first writeByte call in accelgyrocal and in magcal crashes the OS. I'm sure this problem is way out of my expertise league, and it might just be a compatibility issue that's impossible to fix, but can you think of anything that might be related to the issue that I could look into?

@kriswiner
Copy link
Owner

kriswiner commented Jun 2, 2020 via email

@MrStashley
Copy link

Ah okay that's what I'm finding out lol. It sucks because the Nano 33 ble sense has everything I need for my project in terms of sensors and ble capability. Do you know of any other boards that have similar capabilities but don't have the annoying issue of being compatible with nothing?

@kriswiner
Copy link
Owner

kriswiner commented Jun 2, 2020 via email

@MrStashley
Copy link

There is, it's just that none of the calibration libraries are compatible with the board so I haven't been able to calibrate the sensors and I can't get the AHRS algorithm to spit out numbers that makes even close to sense. I've heard that the sensors on the nano 33 ble sense have coordinate planes in wierd directions and that the accelerometer and gyroscope have left handed coordinate planes and I think that might be contributing to my error as well, but I'm not really sure how to fix it.

@kriswiner
Copy link
Owner

kriswiner commented Jun 2, 2020 via email

@MrStashley
Copy link

ah okay thank you for that. I'll look into which axes I need to switch to get NED.

well except that the writeByte function causes the Mbed OS to crash. Do you think that it's an issue with the location of the registers on my board or an issue with the Wire functions?

@kriswiner
Copy link
Owner

kriswiner commented Mar 17, 2021 via email

@willyconcarne12
Copy link

https://github.com/kriswiner/LSM9DS1/blob/master/LSM9DS1_BasicAHRS_Nano33.ino doesn't work for you??

On Tue, Mar 16, 2021 at 8:33 AM willyconcarne12 @.***> wrote: Hey all - I have been grappling with getting this (calibrated, full orientation; quaternions) with the Arduino Nano 33 BLE as well. Does anyone have a library they were able to get all the way there? Brand new here and have spent the last 2 months trial + erroring, with some successes with pieces in isolation but can't get it all together. ANY help/guidance would be MUCH appreciated. — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#14 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABTDLKUKNE2LOZMJ67MJTLDTD525LANCNFSM4KWHMCKQ .

I got it to work! One of the main hang-ups was changing D1 & D2 (as others mentioned) to something like pinD1 & pinD2. The other half of my struggle was adjusting the code to feed into Unity and capture it on that side, which I finally got through. Next is dialing in sensitivity as well as orienting the w/x/y/z axis with that of the object.

But that is NOTHING compared to the work you put into this library. I don't know how to thank you! And am learning so much in the process. @j-mcc1993 @kriswiner

@StRob1980
Copy link

It might be inadequate calibration, sure. I followed the calibration routine in the code and also tried with increased number of samples. I did it on a wooden table far from any magnetic interference. I tried to get some other examples with hard and soft iron calibration to work with my Nano33BLE but with no luck unfortunately.
The BNO055 was, as I mentioned, just to see if my poor result was "as good as it gets" or if there still was something wrong with my implementation (or HW for that matter). And It clearly showed that it was my implementation. So I will return to this later on.
But as I know I would be helped of knowing that others did not get the sketch to work without some TLC it could be fair to tell others of what to expect to save them some time. Hope I didn't step on any toes, that was not my intention and I apologize for that.

@willyconcarne12 Did you get the heading/yaw responsive and somewhat accurate?

@willyconcarne12
Copy link

@StRob1980 I appreciate your take as I appear to be in a similar boat. I am still wading through the right levers to pull and what each one does specifically as well as reading the the side notes with each section. For example, GyroMeasError (264, 265) comments seem completely relevant to optimizing performance vs. accuracy ("...not fast enough for a quadcopter or robot car!" [without adjustments]).

Accuracy vs. response speed vs. sampling rates vs. bias, etc. levers to try to learn, pull and balance. Heck of a wrestling match going on over here! Thrilled I have the data now feeding and I'm moving the object, but the new brick wall has appeared in trying to get the model object oriented with the sensor and stable. No disconnects occurring so far which is great and when I lay it flat it will stabilize back to it's original position after a few seconds, but sense there's a lot more mountain to climb.

@StRob1980
Copy link

@willyconcarne12 I hope we can follow your progress here. I wish you a happy climbing. :)

@willyconcarne12
Copy link

Ok so still at it - making progress. It seems my calibration/bias is way off on:

  1. accelerometer, especially z-axis (bias 952)
  2. magnetometer

Calibration

I have tried toggling just about everything... any thoughts?

@kriswiner
Copy link
Owner

kriswiner commented Mar 31, 2021 via email

@willyconcarne12
Copy link

Well that's good on accel!

Hmmm... well here are the current initial params (I have been trying different things completely off the wall if you can't tell...)...

// Using the LSM9DS1+MS5611 Teensy 3.1 Add-On shield, ADO is set to 1
// Seven-bit device address of accel/gyro is 110101 for ADO = 0 and 110101 for ADO = 1
#define ADO 1
#if ADO
#define LSM9DS1XG_ADDRESS 0x6B // Device address when ADO = 1
#define LSM9DS1M_ADDRESS 0x1E // Address of magnetometer
#define MS5611_ADDRESS 0x77 // Address of altimeter
#else
#define LSM9DS1XG_ADDRESS 0x6A // Device address when ADO = 0
#define LSM9DS1M_ADDRESS 0x1D // Address of magnetometer
#define MS5611_ADDRESS 0x77 // Address of altimeter
#endif

#define SerialDebug true // set to true to get Serial output for debugging

// Set initial input parameters
enum Ascale { // set of allowable accel full scale settings
AFS_2G,
AFS_4G,
AFS_8G,
AFS_16G
};

enum Aodr { // set of allowable gyro sample rates
AODR_PowerDown,
AODR_10Hz,
AODR_50Hz,
AODR_119Hz,
AODR_238Hz = 1,
AODR_476Hz,
AODR_952Hz
};

enum Abw { // set of allowable accewl bandwidths
ABW_408Hz = 1,
ABW_211Hz,
ABW_105Hz,
ABW_50Hz
};

enum Gscale { // set of allowable gyro full scale settings
GFS_245DPS,
GFS_500DPS = 1,
GFS_NoOp,
GFS_2000DPS
};

enum Godr { // set of allowable gyro sample rates
GODR_PowerDown,
GODR_14_9Hz,
GODR_59_5Hz,
GODR_119Hz,
GODR_238Hz = 1,
GODR_476Hz,
GODR_952Hz
};

enum Gbw { // set of allowable gyro data bandwidths
GBW_low, // 14 Hz at Godr = 238 Hz, 33 Hz at Godr = 952 Hz
GBW_med, // 29 Hz at Godr = 238 Hz, 40 Hz at Godr = 952 Hz
GBW_high = 1, // 63 Hz at Godr = 238 Hz, 58 Hz at Godr = 952 Hz
GBW_highest // 78 Hz at Godr = 238 Hz, 100 Hz at Godr = 952 Hz
};

enum Mscale { // set of allowable mag full scale settings
MFS_4G,
MFS_8G,
MFS_12G,
MFS_16G
};

enum Mmode {
MMode_LowPower,
MMode_MedPerformance,
MMode_HighPerformance = 1,
MMode_UltraHighPerformance
};

enum Modr { // set of allowable mag sample rates
MODR_0_625Hz,
MODR_1_25Hz,
MODR_2_5Hz,
MODR_5Hz,
MODR_10Hz,
MODR_20Hz,
MODR_80Hz = 1
};

#define ADC_256 0x00 // define pressure and temperature conversion rates
#define ADC_512 0x02
#define ADC_1024 0x04
#define ADC_2048 0x06
#define ADC_4096 0x08
#define ADC_D1 0x40
#define ADC_D2 0x50

// Specify sensor full scale
uint8_t OSR = ADC_4096; // set pressure amd temperature oversample rate
uint8_t Gscale = GFS_500DPS; // gyro full scale
uint8_t Godr = GODR_238Hz; // gyro data sample rate
uint8_t Gbw = GBW_high; // gyro data bandwidth
uint8_t Ascale = AFS_8G; // accel full scale
uint8_t Aodr = AODR_238Hz; // accel data sample rate
uint8_t Abw = ABW_408Hz; // accel data bandwidth
uint8_t Mscale = MFS_8G; // mag full scale
uint8_t Modr = MODR_80Hz; // mag data sample rate
uint8_t Mmode = MMode_HighPerformance; // magnetometer operation mode
float aRes, gRes, mRes; // scale resolutions per LSB for the sensors

--

Also, does ADO have any effect on that?

@kriswiner
Copy link
Owner

kriswiner commented Mar 31, 2021 via email

@willyconcarne12
Copy link

Thanks for sending this - I tried out adding the mag_max and mag_min from the code included:
(int16_t mag_max[3] = {-32767, -32767, -32767}, mag_min[3] = {32767, 32767, 32767};)
instead of
(int16_t mag_max[3] = {0, 0, 0}, mag_min[3] = {0, 0, 0};)

Calibration Result:
Calibration 2

@kriswiner
Copy link
Owner

kriswiner commented Apr 1, 2021 via email

@j-mcc1993
Copy link
Contributor

One thing that hasn't come up here... have you all been adjusting the magnetic declination parameter? That parameter depends on your location, and unless you happen to live in the SF Bay Area, the parameter that's in the code won't be accurate to your location.

@willyconcarne12
Copy link

@kriswiner Doh! Yes I moved the sensor in a figure eight like it said (rotating it while also waving) while calibrating. Just tried it again with it sitting completely still the entire calibration and got these results.

Calibration 3

@j-mcc1993 great point - yes I had updated the location (using https://www.magnetic-declination.com/). I'm in Dallas so have +2.56 in there vs 13.3 for San Fran.

I'm hoping that once calibration is fixed, that should be the fix for my unstable axis when rotating the sensor (ex. x-axis pointing left from sensor start position --> perform twist motion with the sensor and return to start position --> x-axis is now pointing up or some other direction).

IMG_2490.mp4

@kriswiner
Copy link
Owner

kriswiner commented Apr 1, 2021 via email

@willyconcarne12
Copy link

Hate to sound helpless but I'm not sure what to pull to answer your question (and thank you so much for helping/any guidance! really trying to learn).

Should I send you my sketch?

@kriswiner
Copy link
Owner

kriswiner commented Apr 1, 2021 via email

@willyconcarne12
Copy link

Awesome. Your email is redacted on here (@.***) but I just sent you a private message on hackaday.io (if you still have access to that) w/ a link to the sketch.

@kriswiner
Copy link
Owner

kriswiner commented Apr 1, 2021 via email

@willyconcarne12
Copy link

Calibration 4

@kriswiner
Copy link
Owner

kriswiner commented Apr 1, 2021 via email

@kriswiner
Copy link
Owner

kriswiner commented Apr 1, 2021 via email

@willyconcarne12
Copy link

I have been pouring through... I wonder if it has to do with the Device address and/or Mag address. I am only using an Arduino Nano 33 BLE (i.e. NOT +MS5611 Teensy 3.1 Add-On shield). However, when I set #define ADO 1 to #define ADO 0, I can't get any data to come across the monitor.

// Using the LSM9DS1+MS5611 Teensy 3.1 Add-On shield, ADO is set to 1
// Seven-bit device address of accel/gyro is 110101 for ADO = 0 and 110101 for ADO = 1
#define ADO 1
#if ADO
#define LSM9DS1XG_ADDRESS 0x6B // Device address when ADO = 1
#define LSM9DS1M_ADDRESS 0x1E // Address of magnetometer
#define MS5611_ADDRESS 0x77 // Address of altimeter
#else
#define LSM9DS1XG_ADDRESS 0x6A // Device address when ADO = 0
#define LSM9DS1M_ADDRESS 0x1D // Address of magnetometer
#define MS5611_ADDRESS 0x77 // Address of altimeter
#endif

@kriswiner
Copy link
Owner

kriswiner commented Apr 2, 2021 via email

@willyconcarne12
Copy link

I apologize in advance if this is also not anywhere close to the solution, but trying to help. I do see a few differences looking at the sketch vs. the code snippet in the wiki you sent (https://github.com/kriswiner/MPU6050/wiki/Simple-and-Effective-Magnetometer-Calibration).

Sketch includes a "uint8_t data[6]"
uint8_t data[6]; // data array to hold mag x, y, z, data
uint16_t ii = 0, sample_count = 0;
int32_t mag_bias[3] = {0, 0, 0};
int16_t mag_max[3] = {-32767, -32767, -32767}, mag_min[3] = {32767, 32767, 32767};

vs. wiki snippet
uint16_t ii = 0, sample_count = 0;
int32_t mag_bias[3] = {0, 0, 0}, mag_scale[3] = {0, 0, 0};
int16_t mag_max[3] = {-32767, -32767, -32767}, mag_min[3] = {32767, 32767, 32767}, mag_temp[3] = {0, 0, 0};

I also see a dest 1 AND dest 2 in mag_scale of the snippet but only dest 1 in the sketch (or at least in the same section; may be located elsewhere or not needed?).

Sketch
void magcalLSM9DS1(float * dest1)

vs. snippet
void magcalMPU9250(float * dest1, float * dest2)
.
.
.
// Get soft iron correction estimate
mag_scale[0] = (mag_max[0] - mag_min[0])/2; // get average x axis max chord length in counts
mag_scale[1] = (mag_max[1] - mag_min[1])/2; // get average y axis max chord length in counts
mag_scale[2] = (mag_max[2] - mag_min[2])/2; // get average z axis max chord length in counts

float avg_rad = mag_scale[0] + mag_scale[1] + mag_scale[2];
avg_rad /= 3.0;

dest2[0] = avg_rad/((float)mag_scale[0]);
dest2[1] = avg_rad/((float)mag_scale[1]);
dest2[2] = avg_rad/((float)mag_scale[2]);

@kriswiner
Copy link
Owner

kriswiner commented Apr 2, 2021 via email

@willyconcarne12
Copy link

This might what these guys were wrestling with?

https://forum.arduino.cc/index.php?topic=663160.msg4467543#msg4467543

@kriswiner
Copy link
Owner

kriswiner commented Apr 2, 2021 via email

@willyconcarne12
Copy link

Thank you for that! Hate to ask another ignorant question, but is this the area of mag calibration adjustment once verifying?

the 32768.0 denominator here

void getMres() {
switch (Mscale)
{
// Possible magnetometer scales (and their register bit settings) are:
// 4 Gauss (00), 8 Gauss (01), 12 Gauss (10) and 16 Gauss (11)
case MFS_4G:
mRes = 4.0 / 32768.0;
break;
case MFS_8G:
mRes = 8.0 / 32768.0;
break;
case MFS_12G:
mRes = 12.0 / 32768.0;
break;
case MFS_16G:
mRes = 16.0 / 32768.0;
break;

and the correlating 32768 in the mag_max and mag_min scale here?

uint8_t data[6]; // data array to hold mag x, y, z, data
uint16_t ii = 0, sample_count = 0;
int32_t mag_bias[3] = {0, 0, 0};
int16_t mag_max[3] = {-32768, -32768, -32768}, mag_min[3] = {32768, 32768, 32768};

i.e. is 32768 "the number"?

@kriswiner
Copy link
Owner

kriswiner commented Apr 2, 2021 via email

jelsenburg added a commit to jelsenburg/seed_Euler that referenced this issue Sep 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests