GithubHelp home page GithubHelp logo

nliaudat / magnetometer_calibration Goto Github PK

View Code? Open in Web Editor NEW
21.0 4.0 2.0 142 KB

Ellipsoid fitting using python numpy to calibrate magnetometers

Python 100.00%
magnetometer magnetometer-calibration esphome hmc5883l numpy ellipsoid-fit

magnetometer_calibration's Introduction

Magnetometer calibration

Ellipsoid fitting using python numpy to calibrate magnetometers

alt text

Usage :

  1. output you raw data from sensor and name it mag_out.txt in format x,y,z (uT)

  2. change the "MField" value (line 42) in calibrate.py according to your sensor value (calculation available at begining of calibrate.py)

  3. run script and get "Hard Iron Bias" and "Soft Iron bias" matrixes (and c++ according code)

    // Taken from calibrate.py or magcal
     float hard_iron_bias_x =  41.45884154873271 ;
     float hard_iron_bias_y =  -87.79628696573607 ;
     float hard_iron_bias_z =  569.4171225039286 ;
    
    
     double soft_iron_bias_xx =  0.5823136909144911 ;
     double soft_iron_bias_xy =  0.007124620314368133 ;
     double soft_iron_bias_xz =  -0.024442807568982334 ;
    
    
     double soft_iron_bias_yx =  0.00712462031436818 ;
     double soft_iron_bias_yy =  0.5906868599676302 ;
     double soft_iron_bias_yz =  0.005356720947343228 ;
    
    
     double soft_iron_bias_zx =  -0.024442807568982372 ;
     double soft_iron_bias_zy =  0.005356720947343263 ;
     double soft_iron_bias_zz =  0.7210550285247264 ;
     
     // get values x,y,z and subtract the hard iron offset
     float xm_off = id(hmc5883l_x).state - hard_iron_bias_x;
     float ym_off = id(hmc5883l_y).state - hard_iron_bias_y;
     float zm_off = id(hmc5883l_z).state - hard_iron_bias_z;
     
     // multiply by the inverse soft iron offset 
     float xm_cal = xm_off *  soft_iron_bias_xx + ym_off *  soft_iron_bias_yx  + zm_off *  soft_iron_bias_zx;
     float ym_cal = xm_off *  soft_iron_bias_xy + ym_off *  soft_iron_bias_yy + zm_off *  soft_iron_bias_zy;
     //not needed : float zm_cal = xm_off *  soft_iron_bias_xz + ym_off *  soft_iron_bias_yz  + zm_off *  soft_iron_bias_zz;
     
     //float heading = atan2(ym_cal, xm_cal);
     float heading = atan2(0 - xm_cal, ym_cal);
     
     //heading += id(magnetic_declination); 
     
     if(id(enable_magnetometer_serial_output) == true){
     ESP_LOGD("main", "%.1f,%.1f,%.1f", id(hmc5883l_x).state, id(hmc5883l_y).state, id(hmc5883l_z).state);
     }
     // Correct for when signs are reversed.
     if (heading < 0) {
     heading += 2*PI;
     }
     // Check for wrap due to addition of declination.
     if (heading > 2*PI) {
     //heading -= 2*PI;
     }
     float headingDegrees = heading * 180/M_PI; // Convert radians to degrees.
     return headingDegrees;
    

If you run esphome, a sample code is included to output raw data and get corrected values.

Working demo

If you want a sample or a working demo, look at https://github.com/nliaudat/weatherstation

Requirements :

calibrate.py requires numpy,scipy and optional matplotlib to plot the graph

Thanks :

John zhang12300 for issuing the bug

jremington for fixing the bug

Sources :

    -  https://teslabs.com/articles/magnetometer-calibration/      
    -  https://www.best-microcontroller-projects.com/hmc5883l.html

magnetometer_calibration's People

Contributors

nliaudat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

87091988 ivantk

magnetometer_calibration's Issues

v_1[3] corresponds to the item xy?

I amd phD candinate John. Recently I studied your code, and I found that there is some unconsistency between the description in https://teslabs.com/articles/magnetometer-calibration/ and your code .
In the decription, the fourth of the v_1, i.e., v_1[3] corresponds to the item yz. However, in your code, v_1[3] corresponds to the item xy. It confused me a lot. Hopefully you can check it and looking forward to receiving your reply soon. Thank you!
M = np.array([[v_1[0], v_1[3], v_1[4]],
[v_1[3], v_1[1], v_1[5]],
[v_1[4], v_1[5], v_1[2]]])

Raw Gravitation Field Value

Hi,
I'm using Wheeltec N100 IMU for the sensor, but i can't find gain value that the sensor use
Is there any chance that i can find Raw Gravitation Field Value (MField) without gain?
or i could find the gain value myself?

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.