# Compensating for Magnetic Declination mathematically or programmatically

My goal: to calculate a car's orientation with respect to true north. The car will be traveling thousands of miles across both Australia and North America (without stopping), so simple adding the offset for where I typically am is not an option, as per various other forums suggestions. I was attempting to avoid using the GPS sensor to calculate orientation using direction, because the accuracy of this data is rather important

What I've found: Using a Magnetosensor seems to be a very good way to find magnetic north, and there are ways of finding the offset needed to point to true north (judging by the various magnetic calculators found online such as NOAA). https://www.ngdc.noaa.gov/geomag-web/#declination

Complications that have arisen: I stumbled across various world maps of magnetic declination, and the magnetic field doesn't seem to follow any pattern or continuity (dang physics, why u no easy), meaning that a mathematic approach to this is (apparently) impossible.

Where I went from there: My next intention was to combine data taken from a magneto sensor and from a GPS module to lookup, in a table, the relative offset needed to counteract the magnetic declination of the car at its current latitude and longitude. But finding a table of various different lat,long coordinates and their declination proved unsuccessful.

Ideas? Opinions? Help? Where should I go from here. Is it even worth compensating for Magnetic North? Or should I simply leave all data as-is and disregard the drift that may arise. Is there a mathematic approach I can take to this, or should I consider dedicating onboard space to housing the offset table? Or is there another approach entirely I did not consider.

The Earth's magnetic field is quite uniform and the magnetic declination changes slowly from place to place, but also with time, as the poles wander.

Maps showing the local magnetic declination may be out of date. The best you can do is get as many estimates as possible across your path, and fit a smooth function to them.

Note that magnetometers are extremely sensitive to their environment and will be profoundly distracted by the metal and magnetic fields due to electric currents in an automobile.

It will be quite a feat to drive thousands of miles without stopping. How do you plan for refueling?

okay I guess I should spill the project. It's going to be used for telemetry for a solar car. Refueling is not an issue of course.

It seems that the best option is a large lookup table for the car to refer to along its path? Looking at a map like this: http://www.solarpathfinder.com/images/spf/World_Magnetic_Declination.jpg it seems like going from one end of Australia to the other, we will need to adjust by several degrees over the course of the race, meaning that after 2000 miles, we will be ~15 degrees off if we don't account for it?

Also, if the car is electric. How far away will we need to put the sensor from the motor to prevent electrical interference? The car is about 14 feet long, and the motor is almost directly in the center, so we have about 7ft of play here.

get as many estimates as possible across your path, and fit a smooth function to them.

If you know the exact route you will be taking, this is the best way to go. By analyzing the declinations along the route (one dimension), you can pick a few "critical" points along the route where the declination changes "the most". Everything between those points can be a linear (quadratic, cubic...) interpolation of that value.

This makes the table fairly small, even to cover few thousand miles of a one-dimensional line (i.e., your path).

BTW, if you are trying to squeeze a lot of code into a small space, be sure the check out my NeoGPS library. Although it starts out as the smallest, fastest, most reliable and most accurate GPS library, it has many configuration options the can make it even smaller and faster. The Installation page has tips for picking the pins for the GPS device (and associated libraries), and the Troubleshooting page has lots of other tips for GPS programs.

Cheers, /dev

many many thanks for your help dev. So you recommend picking out 3 or 4 lat/long points, and finding the offset at that point? say (45,45) is 8 degrees, (45,57) is 10 degrees, and (46,64) is 13 degrees. Then creating a mathematic scale between each point, then applying the resulting function on the output from the sensor? So clever, why didn’t I think of it myself… (ps your code is very impressive for NeoGPS. I think I’ll see if I can implement it. Thank you)

So you recommend picking out 3 or 4 lat/long points, and finding the offset at that point? say (45,45) is 8 degrees, (45,57) is 10 degrees, and (46,64) is 13 degrees.

Yes. To be really thorough, I would sample 100 points on your route (20 miles apart?), sorted by odometer distance from the starting point. Then get the declination for those points. Last, get one of your mathematically-inclined mates to analyze that data and pick the (1) the data points and (2) the interpolation function through those points.

You could use a spreadsheet to pick the best coefficients for a polynomial interpolation: 1st-order (a line), 2nd-order (a quadratic), 3rd-order (cubic), etc. Splines and Bezier curves are potential functions. The International Geomagnetic Reference Field is a 12th-order 2-dimensional approximation, but you don't need that.

You should also analyze the error of the interpolator, so you know how far off it can get. Some points will be perfect, others will be not-so-perfect. :) That's how you decide to use 3 points or 20 points: the decision should be based on the maximum error you can accept and the most code space/execution time you can spend doing the calculation.

It's going to be used for telemetry for a solar car

Why do you need to locate true north for telemetry? Do you mean you want to log the car's direction of travel relative to true north? Presumably, this car will be on a road whose orientation is well known, even if you have to use Google Earth ahead of time to prepare the required information. All you need to know is exactly where you are on that road and from that you'll know where true north lies. It seems to me that using a magnetosensor to measure magnetic declination is a very roundabout way to find true north. GPS looks like the best option. Apart from telling you exactly where you are, it is reasonably easy to calculate your true heading from successive GPS reports.

the accuracy of this data is rather important

OK, what accuracy is required and why do you think that you can't obtain it from using info from a GPS?

Pete

jremington: The Earth's magnetic field is quite uniform and the magnetic declination changes slowly from place to place, but also with time, as the poles wander.

Maps showing the local magnetic declination may be out of date. The best you can do is get as many estimates as possible across your path, and fit a smooth function to them.

Note that magnetometers are extremely sensitive to their environment and will be profoundly distracted by the metal and magnetic fields due to electric currents in an automobile.

It will be quite a feat to drive thousands of miles without stopping. How do you plan for refueling?

This is true in theory, but practically, on the ground, the local environment will have a great effect, particularly in volcanic areas. I have seen compasses do several 360 degree turns in less than a mile. I am sure declination is also effected.

Paul

@Dev I'll do that and see what results I get.

@El_Supremo, we have an issue with our steering system that is leading to a suspicion that our wheels are turning at a different speeds (probably due to misalignment or maybe a bad system in there somewhere (idk I'm not mech-E)). As a result we will be monitoring the rotation rate of each wheel. The difference between them will tell us when the wheels are misaligned. The problem is that when you turn around corners, your wheels will also turn at different rates, and we need to be able to differentiate between that data and the situations we are interested in, hence the high precision of reading.

@Paul: do you think the interference will be enough that this option will be out of the question

Maybe measure the steering wheel's position and use that to compensate for the difference in rate. Perhaps all you really need to know is if the steering wheel is such that the car is driving straight. Then, if the wheels differ in speed, there's a problem.

Pete

I have seen compasses do several 360 degree turns in less than a mile. I am sure declination is also effected.

Yes, near iron ore deposits especially. And, as I hinted, the vehicle's magnetic field disturbances will be readily detectable many meters away. I use a magnetometer to sense cars passing through our driveway gate.

But no maps will show you the correct declination in any of those areas, so if you were really interested, you would have to drive the entire route and record the local variations.

I agree with el_supremo in questioning the wisdom of or need for this approach.

Well the wisdom is lacking, it was just a first concept. Looks like this isn’t gonna be the best approach. I’ll look at the idea of using a pot on the steering wheel to sense rotation

GPS already has the database you're looking for. The most-common data sentence emitted by a GPS is \$GPRMC, which contains the magnetic variation. Now that field may always be zero or empty for your specific GPS but it's part of the specification and it should not be hard to find a GPS which does show the real data.

But a car drives with its wheels on the ground most of the time. You don't have any crosswinds or currents that will make your track different to your heading. Just use the GPS-derived track (also in \$GPRMC) to approximate heading. It will only ever differ from the actual heading if you're in a skid.

If you really need short-term yaw rate (and not heading at all) then a gyro such as a MPU6050 chip will do very well too. That's what most car manufacturers use for the anti-skid protection system - it looks at the commanded turn rate from the steering wheel and the actual turn rate from the gyro and if they're different, it tries to correct the difference.