User avatar
Davespice
Forum Moderator
Forum Moderator
Posts: 1662
Joined: Fri Oct 14, 2011 8:06 pm
Location: The Netherlands
Contact: Twitter

How to calibrate the Magnetometer

Fri May 01, 2015 10:33 am

EDIT: I am leaving this post intact as a reference, but a lot of it is no applicable. Please skip to this post:
viewtopic.php?f=104&t=109064&p=750616#p810193
Steps 8 to 19 are still relevant.


Hi all, okay so Astro Pi kits are imminently going to be landing on doorsteps so I thought I should get this information online asap!

You'll probably have noticed the file RTIMULib.ini appearing in the same folder as your python programs. This is because the Astro Pi python module uses a another library behind the scenes called RTIMU.

The ini file mentioned above is used to store the Magnetometer calibration data. By default your ini files will be empty of calibration data though. Without this data you'll find that the compass functions don't behave in the way you expect them to.

For example I expect many of you have found that the compass.py example program shows a blue pixel that doesn't move very much?

To fix this we need to do two things:
  1. Run the calibration program. This will then create a new copy of RTIMULib.ini containing the Magnetometer calibration data.
  2. Copy the new RTIMULib.ini file to the folder where your python programs are.
Step by step instructions:
  1. Log in to your Astro Pi in the usual way and from the command prompt (or a Terminal window) and enter the following command:

    Code: Select all

    cd ~/RTIMULib/Linux/RTIMULibCal/
  2. Then enter the command:

    Code: Select all

    make
  3. If you see the following error...

    Code: Select all

    RTIMU.cpp:(.text+0x1c8): undefined reference to `RTIMULSM9DS1::RTIMULSM9DS1(RTIMUSettings*)'
    collect2: ld returned 1 exit status
    Makefile:102: recipe for target 'Output/RTIMULibCal' failed
    make: *** [Output/RTIMULibCal] Error 1
    
    ...then do the following steps, otherwise go straight to step 4.
    I know this looks complex but it's easy, you're just editing a text file and running the make command again. Easy as pie!

    Code: Select all

    nano Makefile
    Locate the following line:

    Code: Select all

        $(RTIMULIBPATH)/IMUDrivers/RTIMULSM9DS0.h \
    Place your cursor at the start of the line and press ALT-6 followed by CTRL-U and this should duplicate the line.
    Edit the DS0 at the end of the new line so that it says DS1 like this:

    Code: Select all

        $(RTIMULIBPATH)/IMUDrivers/RTIMULSM9DS0.h \
        $(RTIMULIBPATH)/IMUDrivers/RTIMULSM9DS1.h \
    Then locate this line further down:

    Code: Select all

        objects/RTIMULSM9DS0.o \
    Place your cursor at the start of the line and press ALT-6 followed by CTRL-U and this should duplicate the line.
    Edit the DS0 at the end of the new line so that it says DS1 like this:

    Code: Select all

        objects/RTIMULSM9DS0.o \
        objects/RTIMULSM9DS1.o \
    Press CTRL-O followed by Enter to save.
    Then press CTRL-X to quit nano.
    Now run make again and it should work.

    Code: Select all

    make
  4. Now run the ls command and you should see that two folders were created, objects and Output.

    Code: Select all

    ls
  5. Go into the Output folder using the following commands:

    Code: Select all

    cd Output
    ls
  6. The file RTIMULibCal should show in green text. This is the calibration program we need to run.
  7. Run the program with this command:

    Code: Select all

    sudo ./RTIMULibCal
  8. You will then see this menu:

    Code: Select all

    Options are:
    
      m - calibrate magnetometer with min/max
      e - calibrate magnetometer with ellipsoid (do min/max first)
      a - calibrate accelerometers
      x - exit
    
    Enter option:
    
  9. Press lower case m
  10. This message will then show, press any key to start.

    Code: Select all

    Magnetometer min/max calibration
    --------------------------------
    Waggle the IMU chip around, ensuring that all six axes
    (+x, -x, +y, -y and +z, -z) go through their extrema.
    When all extrema have been achieved, enter 's' to save, 'r' to reset
    or 'x' to abort and discard the data.
    
    Press any key to start...
    
  11. After it starts you will see this kind of thing scrolling up the screen.

    Code: Select all

    Min x:  51.60  min y:  69.39  min z:  65.91
    Max x:  53.15  max y:  70.97  max z:  67.97
  12. Focus on the two lines at the very bottom of the screen as these are the most recently posted measurements from the program.
  13. Now you have to move the Astro Pi around in every possible way you can think of.
    It helps if you unplug all non essential cables so you don't create a birds nest :)
  14. Try and get a complete circle in each of the pitch, roll and yaw axes.
  15. TAKE CARE TO NOT ACCIDENTALLY EJECT THE SD CARD WHEN YOU'RE DOING THIS.
  16. Spend a few minutes doing this and stop when you find that the numbers are not changing any more.
  17. Now press lower case s
  18. Now press lower case x to exit the program.
  19. If you run the ls command now you'll see a new RTIMULib.ini file has been created.

    Code: Select all

    ls
  20. This is the file we need to copy. You can copy this to wherever your python programs are saved to make use of the calibration data.
  21. If your python programs are saved in the home folder then use this command to copy it:

    Code: Select all

    sudo cp ./RTIMULib.ini ~
  22. Or if you want to try out the compass.py example with the calibration data then use this command to copy it:

    Code: Select all

    sudo cp ./RTIMULib.ini ~/astro-pi-hat/examples/
  23. Then to run the compass demo you would use these commands:

    Code: Select all

    cd ~/astro-pi-hat/examples/
    sudo ./compass.py
    If you rotate the Astro Pi around like a compass now the blue pixel should chase around the edge.
Please post below if you have any difficulties further with the Magnetometer calibration.
Thanks for reading.

P.S. ShiftPlusOne may reply with further information on how to do the ellipsoid calibration in due course, which is a more advanced form of calibration for the best possible results.

jbelshaw24
Posts: 1
Joined: Fri May 01, 2015 10:56 am

Re: How to calibrate the Magnetometer

Fri May 01, 2015 11:00 am

Thanks Dave,

I expect to get the astro-pi quite soon and will help Hannah get the magnetometer up and running.

Johnny5C
Posts: 82
Joined: Thu Apr 18, 2013 3:32 pm

Re: How to calibrate the Magnetometer

Sat May 02, 2015 10:48 am

Thanks Dave. Worked like a charm.

Had to do the full version, but it's not difficult if you follow the well written steps!

Cheers

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5369
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: How to calibrate the Magnetometer

Sat May 02, 2015 2:13 pm

Quick heads up that if you find that you needed to modify the make file, it's probably a good idea to update from https://github.com/XECDesign/RTIMULib/tree/astro-pi

Code: Select all

git clone https://github.com/XECDesign/RTIMULib.git
cd RTIMULib
git checkout astro-pi
cd Linux/python
python setup.py build
sudo python setup.py install
We'll be trying to get the necessary changes back upstream and packaging RTIMULib for simple 'apt-get' installation. There's a chance that the code won't be accepted upstream, since things like humidity are a little outside of the scope of an IMU library, but in that case, we'll just package a patched version.[/s]

Edit: No longer relevant.

theirry
Posts: 42
Joined: Sat Jul 20, 2013 6:08 pm

Re: How to calibrate the Magnetometer

Sat May 16, 2015 6:52 pm

Looks like my astro pi cant find it:

bash: cd: /root/RTIMULib/Linux/RTIMULibCal/: No such file or directory

User avatar
Davespice
Forum Moderator
Forum Moderator
Posts: 1662
Joined: Fri Oct 14, 2011 8:06 pm
Location: The Netherlands
Contact: Twitter

Re: How to calibrate the Magnetometer

Sat May 16, 2015 8:58 pm

theirry wrote:Looks like my astro pi cant find it:

bash: cd: /root/RTIMULib/Linux/RTIMULibCal/: No such file or directory
Hi again, not sure where you're getting /root from?
Davespice wrote:

Code: Select all

cd ~/RTIMULib/Linux/RTIMULibCal/
The ~ in this path just means the home folder, so if you're logged in as the pi user this is the same as /home/pi so this should also work:

Code: Select all

cd /home/pi/RTIMULib/Linux/RTIMULibCal/

theirry
Posts: 42
Joined: Sat Jul 20, 2013 6:08 pm

Re: How to calibrate the Magnetometer

Sun May 17, 2015 8:54 am

Ah, it works now! thanks :D I have tried to use cd before on my ubuntu computer and it tends to be quite frustrating trying to get the right case sensitive directory names.

theirry
Posts: 42
Joined: Sat Jul 20, 2013 6:08 pm

Re: How to calibrate the Magnetometer

Sun May 17, 2015 9:04 am

I think you should change 22. for my pi it is like this: /home/pi/astro-pi-hat/examples

It has the 'pi' bit too.

User avatar
Davespice
Forum Moderator
Forum Moderator
Posts: 1662
Joined: Fri Oct 14, 2011 8:06 pm
Location: The Netherlands
Contact: Twitter

Re: How to calibrate the Magnetometer

Sun May 17, 2015 5:35 pm

theirry wrote:I think you should change 22. for my pi it is like this: /home/pi/astro-pi-hat/examples

It has the 'pi' bit too.
Sorry no, 22 is correct as it is. The ~ symbol just means the home folder of the logged on user (usually /home/pi on Raspbian).
See here: http://stackoverflow.com/questions/3488 ... t-of-paths

Try it out like this (you change to /tmp just to be some place different than the home folder):

Code: Select all

cd /home/pi
pwd
cd /tmp
pwd
cd ~
pwd

theirry
Posts: 42
Joined: Sat Jul 20, 2013 6:08 pm

Re: How to calibrate the Magnetometer

Sun May 17, 2015 6:05 pm

Ah ok, Thanks again!

User avatar
Davespice
Forum Moderator
Forum Moderator
Posts: 1662
Joined: Fri Oct 14, 2011 8:06 pm
Location: The Netherlands
Contact: Twitter

Re: How to calibrate the Magnetometer

Thu Jun 25, 2015 10:11 am

Folks, a slight change to the above instructions. As of version 1.1.5 of the Astro Pi python module you can copy the RTIMULib.ini file produced as part of the calibration to /etc and this will then be found and used by the Astro Pi python module, see notes here.

This replaces step 21 and 22 above:

Code: Select all

rm -r ~/.config/astro_pi
sudo cp RTIMULib.ini /etc

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5369
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: How to calibrate the Magnetometer

Wed Sep 02, 2015 12:05 pm

Another update, if you would like to do your own full calibration, including the ellipsoid fit.

Code: Select all

sudo apt-get install octave
cd
cp /usr/share/librtimulib-utils/RTEllipsoidFit ./ -a
cd RTEllipsoidFit
RTIMULibCal
Follow the on-screen instructions.
Refer to steps 8 to 19 in the original post.
In addition to those steps, you can also do the ellipsoid fit.

When you're done, copy the resulting RTIMULib.ini to /etc/ and remove the local copy in ~/.config/sense_hat/

Code: Select all

rm ~/.config/sense_hat/RTIMULib.ini
sudo cp RTIMULib.ini /etc

mattmiller
Posts: 1966
Joined: Thu Feb 05, 2015 11:25 pm

Re: How to calibrate the Magnetometer

Wed Sep 02, 2015 2:02 pm

I'd also recommend

Code: Select all

sudo rm ./root/.config/sense_hat/RTIMULib.ini
(it will get re-created on next use of Sense lib with updated settings)

PeetjeT
Posts: 2
Joined: Fri Nov 20, 2015 12:48 pm

Re: How to calibrate the Magnetometer

Fri Nov 20, 2015 12:55 pm

Hi, I just acquired a Pi 2B with a Sense Hat.
When i try to calibrate the unit using the "m" option and test it the compass only varies about 60 degrees, no more.

When i try to calibrate with the Ellipsoid i get the following errors, what can be the cause of this?

---: 199 +--: 338 -+-: 283 ++-: 346
--+: 391 +-+: 247 -++: 621 +++: 387
Failed to open ellipsoid fit raw data file


Processing ellipsoid fit data...


Client is not authorized to connect to Serverwarning: unable to open X11 DISPLAY
error: fscanf: invalid stream number = -1
error: called from:
error: /home/pi/RTEllipsoidFit.m at line 41, column 15

Ellipsoid fit returned 256 - aborting.

Thank you for helping out.

doc75
Posts: 1
Joined: Fri Dec 25, 2015 9:51 pm

Re: How to calibrate the Magnetometer

Fri Dec 25, 2015 9:55 pm

Hello,

I also got the error:

Code: Select all

Failed to open ellipsoid fit raw data file
Indeed, the ellipsoid calibration tool tries ot create the magRaw.dta file in ELLIPSOID_FIT_DIR which value is defined at "../RTEllipsoidFit/"

So either you create this directory from your current directory location or you start the calibration to ensure this directory exist.

Hope I am clear.

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5369
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: How to calibrate the Magnetometer

Sat Dec 26, 2015 12:05 am

doc75 wrote: Indeed, the ellipsoid calibration tool tries ot create the magRaw.dta file in ELLIPSOID_FIT_DIR which value is defined at "../RTEllipsoidFit/"

So either you create this directory from your current directory location or you start the calibration to ensure this directory exist.
Correct, that's why the instructions were written the way they were:
ShiftPlusOne wrote:

Code: Select all

cd
cp /usr/share/librtimulib-utils/RTEllipsoidFit ./ -a
cd RTEllipsoidFit
RTIMULibCal

grahame
Posts: 1
Joined: Sat Feb 06, 2016 3:01 pm

Re: How to calibrate the Magnetometer

Sat Feb 06, 2016 3:05 pm

I have a Pi 2 with Sense Hat. When I run RTIMULib it fails to open I2C bus 1 SPI bus 0 select 0,1 and says No IMU detected.
The Sense Hat board seems to work OK otherwise.
Can anyone help?

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5369
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: How to calibrate the Magnetometer

Sat Feb 06, 2016 9:00 pm

grahame wrote:I have a Pi 2 with Sense Hat. When I run RTIMULib it fails to open I2C bus 1 SPI bus 0 select 0,1 and says No IMU detected.
The Sense Hat board seems to work OK otherwise.
Can anyone help?
Could you install i2c-detect and provide the output of the following commands:

Code: Select all

uname -a
vcgencmd version
i2cdetect -y 1

User avatar
ecaheti
Posts: 140
Joined: Tue Aug 14, 2012 3:06 pm
Location: Normandie, France
Contact: Website

Re: How to calibrate the Magnetometer

Thu Apr 07, 2016 6:18 pm

I've work on the compass, using a stepper motor to make repeatable rotation, and I still have bad results...
You'll find attached acquisition of a 360° rotation from north to north, and I don't even know how I can exploit this results =/
Image
I have made some calibration (min/max+ellipsoid), but has you can see, nothing really change.

Any idea ?

User avatar
ecaheti
Posts: 140
Joined: Tue Aug 14, 2012 3:06 pm
Location: Normandie, France
Contact: Website

Re: How to calibrate the Magnetometer

Fri Apr 08, 2016 7:40 am

I also deleted the calibration file in the root folder (/root/.config/sense_hat/RTIMULib.ini).

Now the results are much better ! Don't forget to delete it

Return to “Astro Pi”