Note: this page works for the purple compass, not the green/blue one. I am still trying to find information about how that one is set up.
This page shows you where to find the information needed to work the digital compass shown in the photo below.
This information was originally for a Sparkfun compass which is why the pcbs are red. However, it works for the purple Duinotech compass as well since this uses exactly the same magnetometer chip.
Open the Hookup guide from this address:
https://learn.sparkfun.com/tutorials/mag3110-magnetometer-hookup-guide-
You can use this Hookup Guide to find the Sparkfun MAG3110 Magnetometer Breakout Board Library. Download the zipped library from the Hookup Guide. Start Arduino and click on Sketch - Manage Libraries - Add ZIP library. The library will be installed automatically.
To check if it has installed properly, look in Files - Examples and scroll down you will find a folder called Sparkfun MAG3100 Magnetometer Breakout Library. It contains several example programs that show you how to use the compass.
I used Sparkfun_MAG3110_Calibrated to get it going. It is one of the example programs provided in the magnetometer library. To get this working you need open Arduino and go File - Examples - Sparkfun MAG3110 Magnetometer etc and open the program which has the word Calibrated in it. Upload it into the Arduino and then open the Serial Monitor and spin the robot back and forth through 360 degrees for a while. Eventually once the robot has calibrated itself, it will start printing out the heading (or direction it is pointing in. Directions are given as positive numbers from zero to 180 turning anti-clockwise from north, and negative numbers from zero to -180 turning clockwise from north. The x-axis printed on the compass should point towards the front of the robot, if you want the compass readings to tell you which way the robot is facing.
The movie below shows a robot turning until it points north and then stopping. The compass is mounted on a stick to get it away from the magnetic fields produced by the electric motors. This seems to be successful because, as you can see from the real compass on the table, the robot is stopping almost exactly when it points north.
Permanent Calibration
Avoid the pain of having to calibrate every time you switch it on.
The calibration process is used to set the values of four variables called x- and y-scale and x- and y-offsets. You can avoid calibrating the compass every time it is turned on by finding out the values that are used to calibrate it, and typing them into the setup part of your program.
The program below allows you to calibrate the compass, and then when it finishes, it prints out the values of the calibration variables. You can then copy them and put them into your own program so you don't need to calibrate it any more.
Click here to get program for finding calibration values
Note: This file is a zip file, so you will need to extract it and put it somewhere safe on your hard drive before you use it. Make sure that you end up with a folder called obtain_calibration and an .ino file inside that folder also called obtain_calibration.
The code below shows how to build those calibration variables into setup() in a program that uses the compass. Notice that the only command you need in the loop() part of the program is
mag.readHeading();
Spin to face target direction
This is the first step towards travelling in a chosen direction. The drawing shows the desired direction of travel in black and the current direction the robot is facing in red.
Here is some pseudo-code for spinning to face the way you want to go:
read heading.
if (present heading - desired heading ) >3 //more than 3 degrees from desired direction
spin clockwise
else if( desired heading - present heading) > 3 //more than 3 degrees from desired direction
spin anticlockwise
else
stop //robot stops when it is pointing the right way
}
This code brings the robot around until it is within 3 degrees of the desired direction. You could maybe make it 2 or 1 degrees if the compass is working that accurately.
If you are having problems figuring out how to write this code look here:
Code to make robot spin to face a chosen direction
Travel in a Direction
To make the robot travel in a chosen direction, all you need to do is modify the above code so that, at the end, instead of stopping, it moves forwards. That means that whenever it gets off-course, it will correct itself and keep going.
Finally, you need to make a function called travel(target) which will make the robot travel in a target direction. You will use this function, together with the shaft encoder or colour sensor to make the robot find the checkpoints around the labyrinth.
If you are having trouble creating a function to travel in a chosen direction, look here:
Code to create function for travelling in a chosen direction
Cool! If you have reached here you have got the compass working properly.
Now you have got the compass working, it is time to get the colour sensor or the shaft encoder working.