[SOLVED] Obtaining Magnetic Declination

I have been working on a mobile project that needs to know the direction of true north.

I am using the Venus GPS from Sparkfun which according to the datasheet does not populate the declination field.

The alternative seems to be to try to compute the declination and then use a compass to find magnetic north. I own the Pololu LSM303DLH. I am using an Arduino Mega 2560 so I imagine I have enough room to do this quite accurately even with the rest of the code for the project. Does a library for computing magnetic declination exist for the Arduino?

Failing that I notice that there are libraries for this written in C, for example, NOAA's World Magnetic Model software. The code itself does not seem to vast, 4000 lines of mostly comments and printf statements along with a 2kb coefficient file. Would it be unrealistic to try to include this library directly in an Arduino sketch (perhaps with some wrapping)?

I'm really not sure the best way to approach this but I would like to be able to reasonably accurately compute magnetic declination in and around Europe and Northern Africa for the next 3 -5 years and in a way that will be easy to update in the future. Any guidance?

4000 lines of mostly comments and printf statements along with a 2kb coefficient file.

You only have 2k of data space in the uno so no chance. But the mega should be able to do this. You will need to strip out all the printfs and some /all the includes. You can't use stdio.h for example.

For the UK the difference between true and magnetic north is give on the OS maps and does not change over the 3/5 years you want.

Mark

If the GPS can be oriented in line with the vehicle's axis and a magnetometer/compass (is there a vehicle?) then heading bearings can be used to calibrate the declination. Assuming the compass has compensation for nearby iron masses...

You only have 2k of data space in the uno so no chance.

Wrong. You have 32kb of program flash and the PROGMEM macro, so if your program will fit in the 30k left, no problem.

-j

I suspect you mean magnetic deviation (difference between true and magnetic North)
declination is the angle the local magnetic field makes with the horizontal
(I theenk!)

Magnetic variation or declination is the difference between magnetic and true north at a particular place and time. Magnetic deviation is the difference between the magnetic field in a particular situation compared to the background field, caused by magnetic objects near to the observer including man-made magnetic materials and magnetic rocks in the crust.

Did you consider the option to use external storage for data like the cof file. (e.g. I2C EEPROM or SD CARD)

Advantage of the SD CARD is that you could log some readings too...

mmcp42:
I suspect you mean magnetic deviation (difference between true and magnetic North)
declination is the angle the local magnetic field makes with the horizontal
(I theenk!)

Magnetic declination is the angle between magnetic north and true north. Magnetic deviation is the error introduced in a compass by local magnetic fields (eg... by nearby iron or electromagnets). I was, as I originally stated, interested in declination.

MarkT:
If the GPS can be oriented in line with the vehicle's axis and a magnetometer/compass (is there a vehicle?) then heading bearings can be used to calibrate the declination. Assuming the compass has compensation for nearby iron masses...

The unit will be used on a vehicle although only when the vehicle is stationary. I suppose that the unit could detect when the vehicle is moving quickly in a straight line (using the GPS) then attempt to calibrate the compass. I hadn't really considered that. Quite a neat idea.

I have thought a bit more about this and I am starting to get cold feet. The project I am interested in is a vehicle mounted solar tracker that will be used in Europe and northern Africa to keep our leisure batteries topped up. Looking at the magnetic declination currently I see that from Agadir (western Morocco) to Istanbul (eastern Europe) and from Oslo (northern Europe) down to Nairobi (central Africa) the magnetic declination only varies from -8 deg to +4 deg. If we didn't correct for it at all our panel would be 8 degrees out in its positioning which would still provide very nearly cos(8 deg) = 0.990... of the energy falling on it.

On the other hand... it would be cool to just have an Arduino compatible magnetic declination library (especially something like a port of the NOAA library). In North America you have it a lot worse with declinations ranging from -14 deg to +18 deg, cos(18 deg) = 0.951... still not terrible actually but it would visually look like it was facing the wrong direction at nearly 20 degrees out!

Thanks so much for all these ideas and it does certainly look like creating such a library would be possible. I'm just not sure if I REALLY need it :~.

I believe magnetic declination is indeed the correct term.

this article on the HMC5843 three axis magnetometer may be helpful, as it mentions adjusting for magnetic declination.

The Ardupilot community deal with this stuff, so I suggest looking at their (extensive) libraries. Indeed they have an AP_Declination library on their github.

Nantonos, you gem.

The article you link to is a bit general and essentially tells you how to find out a single declination for where you are currently so that is not so helpful.

HOWEVER, the library you link to is based on this post (says so in the comments of the code). This guy has tried to do exactly what I was going to attempt. This didn't work so well so he wrote his own software that he can update using the NOAA tables. It looks like he has updated it since the last table was released so I should be good for the next 3 years. I am going to give this library a go.

Thank you so much!

Maybe the easiest thing to do is to get g different GPS that outputs the declination.

oops
sorry for mis-information
better to have everyone think you're a fool than to open your mouth and prove it :frowning:

Why not just find out what the magnetic declination is for your area and adjust for it in your code? For example: my location in Ohio is 8 degrees 15 minutes west of grid or true north so I would just add 8 to my azimuth to find grid north. Depending you exact your direction needs to be, you could use decimals. Don't forget to convert minutes to a decimal. multiply by 1/60

This will use your ip address to give your magnetic declination:

Nantonos: That library looks like the best way to go. I haven't had chance to try it out yet and as it happens I'm probably not going to get time for at least 2-5 days. I will update when I have had chance.

wwbrown:
Maybe the easiest thing to do is to get g different GPS that outputs the declination.

Easiest and most accurate, true. I've already soldered up my GPS unit and it wasn't too cheap. If I was to start out again I'd probably go for a unit that can output declination.

mmcp42:
oops
sorry for mis-information
better to have everyone think you're a fool than to open your mouth and prove it :frowning:

Hardly! I've done far worse things :stuck_out_tongue_closed_eyes:.

TeslaIaint:
Why not just find out what the magnetic declination is for your area and adjust for it in your code? For example: my location in Ohio is 8 degrees 15 minutes west of grid or true north so I would just add 8 to my azimuth to find grid north. Depending you exact your direction needs to be, you could use decimals. Don't forget to convert minutes to a decimal. multiply by 1/60

This will use your ip address to give your magnetic declination:
http://magnetic-declination.com/

The unit will be mounted on a vehicle without internet access and toured all over Europe. As I said earlier though: "Looking at the magnetic declination currently I see that from Agadir (western Morocco) to Istanbul (eastern Europe) and from Oslo (northern Europe) down to Nairobi (central Africa) the magnetic declination only varies from -8 deg to +4 deg. If we didn't correct for it at all our panel would be 8 degrees out in its positioning which would still provide very nearly cos(8 deg) = 0.990... of the energy falling on it."

me again!

+12 degrees in Northern Finland
Botswana -11 degrees

trouble is magnetic substrata can have a massive local effect

I recall seeing aeronautical isocline charts for Scandinavia with loops and whorls like a fingerprint

Bob101:
I have been working on a mobile project that needs to know the direction of true north.

The project I am interested in is a vehicle mounted solar tracker that will be used in Europe and northern Africa to keep our leisure batteries topped up.

I don't understand why you're trying to deal with magnetic declination and so on. It seems that the objective is to predict the position of the sun so you can point a solar collector at it. To do that you need to know the lat/log, time and direction of North. But just how accurately do you need to know which way is North? Or put another way, just how directional is your solar collector? If it's a flat PV panel, it's not very directional at all. Surely, a GPS position and time fix plus a bog standard uncorrected magnetic compass is going to get you better accuracy than you need. If you don't think it is, then what sort of accuracy are you aiming for, and why?

Well 2-5 days passed quickly :P... ok, ok, I'm ahead of schedule here. I grabbed a copy of the library and made some tiny changes (so I didn't have the baggage of AP).

Specifically I changed AP_Declination.h

#ifndef AP_Declination_h
#define AP_Declination_h
#include <Arduino.h> // added this
#include <avr/pgmspace.h>
#include <math.h>

AP_Declination.cpp

//#include <FastSerial.h> // removed this
//#include <AP_Common.h> // removed this
#include <Arduino.h> // added this
#include <AP_Declination.h>
#include <avr/pgmspace.h>
#include <math.h>

Then I changed prinf to print in AP_Declination_test and removed "FastSerialPort(Serial, 0);". All compiles well and runs flawlessly in my Arduino Mega. Passed all of their self-tests and for the few points I tried was within 1 degree of magnetic-declination.com. Yay!

mmcp42:
me again!

+12 degrees in Northern Finland
Botswana -11 degrees

trouble is magnetic substrata can have a massive local effect

I recall seeing aeronautical isocline charts for Scandinavia with loops and whorls like a fingerprint

Super job! Thanks for finding outliers for me to check. Just compared them against the library and it agrees with magnetic-declination.com to within a degree for all the places I checked. Happy :).

PeterH:
I don't understand why you're trying to deal with magnetic declination and so on. It seems that the objective is to predict the position of the sun so you can point a solar collector at it. To do that you need to know the lat/log, time and direction of North. But just how accurately do you need to know which way is North? Or put another way, just how directional is your solar collector? If it's a flat PV panel, it's not very directional at all. Surely, a GPS position and time fix plus a bog standard uncorrected magnetic compass is going to get you better accuracy than you need. If you don't think it is, then what sort of accuracy are you aiming for, and why?

You are right that a PV panel isn't that directional at all. The energy collected will probably fall off roughly as the cosine of the angle between the panel normal and the direction of the sun. I suppose 10 degrees off (due to declination) wouldn't be the end of the project and it would be very reasonable to just neglect it.

I am a bit of a perfectionist, however, and I am hoping some of what I learn/cobble together can be used in other projects such as heliostats where precision could be more important. I have been working with Gabriel over at cerebralmeltdown.com and he has managed to create a library that can track the sun with better than degree accuracy at a rate of one calculation per second. I have built/adapted a machine that can move a panel to better than 1 degree of accuracy. It seemed natural to me to just try to maintain this accuracy provided it didn't take up all my time. I wondered how easy it would be and thanks to this thread I think I'm there now in just 1 day! Yay!

Output from AP_Declination_Test is attached.

If anybody smarter than me wants to check specific values against NOAA or their favourite calculator please let me know if the test was successful!

Thanks guys/girls!

AP_Declination_test.txt (149 KB)