Best Way to Sample a Magnetic Field with Magnetometer of Pololu 3Pi Plus

I have a Pololu 3Pi 32U4. I am using the magnetometer for accurate 90 degree turns. This method works well given that the magnetic field is consistent, has low variability, and no obstructions. It is critical to sample the magnetic field of the local area properly for accurate turns. I have a track enclosed in a box that looks like this:


What is the best way to sample this magnetic field? I want to sample it so that my robot can learn enough of the magnetic field to turn accurately at any point of this track. One idea I had was having the robot sample each of the four corners of the magnetic field. This can be implemented in code.

What are some other more efficient and less work strategies to get a good sample of the magnetic field in the area of this track?


Unfortunately, we do not have any specific recommendations about the best way to do that in your particular application, though your suggestion of sampling each of the four corners sounds like it might be okay.

- Patrick

I got a new strategy. Before each turn, spend 2 seconds sampling the magnetic field. How is this strategy?

That might work, although I’m not sure if sampling in a single orientation will give you enough information to compensate for local magnetic fields.

- Patrick

Good point. Perhaps my robot can use the gyroscope to turn 360 degrees while sampling. What do you think?

Is it possible to use the magnetometer for turning while sampling?

A gyro is typically good for tracking relative rotation over short timescales. In general, the best approach for long-term or absolute measurements will probably involve combining the outputs from all three of the sensors in the IMU (accelerometer, gyro, and magnetometer).

There is nothing preventing you from taking magnetometer measurements while the robot is turning, though the current driving the motors could influence the results.

- Patrick

Question: How would I use the accelerometer for turning?

There are probably different approaches for how you could use different IMU sensor measurements to help with turning, but here is a link to an example program in our LIS3MDL Arduino library that demonstrates how you might use magnetometer and accelerometer data together to generate a tilt-compensated magnetic heading.

- Patrick