This is the replacement for the purple compass, which is no longer manufactured. It plugs into the compass connector exactly the same as the purple one, but note that the x-axis points towards the top of the compass as seen in the photo above. This should be set up pointing towards the front of the robot if you want the compass reading to tell you the direction the robot is facing.
Also, this compass has its own library, different from the purple compass.
You can find it here: LIS3MDL library
The link will take you to a page on Github, which is a code storing website. Below is a screen shot of the page.
Click on the green Code button and download the zip file.
Move the zip folder from your Downloads folder into the Libraries folder, which is in the Arduino folder in your Documents folder.!!!
The library should be installed next time you start Arduino.
To check, in the Arduino program, go File - Examples and scroll right down to the bottom where you should find an example called LIS3MDL, and inside it there should be a program called Calibrate. Use this to check if the compass is working.
Connect the compass to the pins on the shield as shown in the photo on the right. The pins are labelled on the compass, so you should be able to see which ones go where.
Run the Calibrate program and you should get a print out on the Serial Monitor of a whole lot of numbers.
Slowly rotate the compass until the numbers stop changing and you will know that the compass is working OK. Those numbers won't mean much to you so download a better version of the Calibrate program using this link:
Link to better Calibrate program
Run the program and rotate the robot slowly until the numbers settle down and stop changing. This program now gives you two new numbers Xmid and Ymid, which are in the middle, or half way between the max and min values. Keep these two numbers and put them in this program:
Open this LIS3MDL_heading program and you will see where you can insert the values Xmid and Ymid. Put in the values you got in the Calibrate program and run it. You should now get a number printed out which tells you which way the robot is pointing.
Keep these two programs in a safe place. You will need them if you have to recalibrate your compass again.
Now you can tell which way the robot is pointing, you can start making it turn to point the way you want it to go.
Before you can do that you need to understand the headin() function in this program.
heading() is an Arduino function I made, that tells you which way the compass is pointing measured between the earth's field and the x-axis marked on the compass. (see diagram on right)
Angles are measured clockwise or anticlockwise from the x-axis marked on the compass.
The diagram on the right shows the the robot at an angle Θ clockwise from the earth's field. Clockwise directions are shown with positive numbers. Anti-clockwise ones are shown with negative numbers.
Normally you would place the compass on the robot so the x-axis points towards the front of the robot. Then the heading() function tells you how far away from North the robot is pointing.
You don't have to worry about this too much because you can just place the robot on the map facing the way you want it to go and read the heading. Whatever it says, that's the heading that you want the robot to travel on.
What you do have to do is work out how to turn the robot to get it pointing the way you want it to go.
Say the robot's current direction, C, is 43° clockwise from north, and it wants to turn to a target position, T, which is 25° clockwise from north. It needs to turn anti-clockwise. 43- 25 = 18° - a positive number.
From this we can come up with a rule: C - T > 0 means turn anti-clockwise. And we come up with a second rule: T - C > 0 means turn clockwise. (You can check if you want to)
There is one thing bad about these rules: the robot will never stop turning because T - C will never be exactly zero.
So we have a little buffer zone 3 degrees either side of zero: C-T > 3 means turn anticlockwise. T-C > 3 means turn clockwise.
If the robot is within 3° of the target direction, it is near enough and can stop turning. This region where the robot does not turn is called the dead zone. If you want the robot to follow a direction really accurately, you make the dead zone smaller. If you don't worry about accuracy so much, you make the dead zone bigger. We don't need a really small dead zone because the green lines we are looking for are quite long, and even though the red spots are smaller, by the time we get to the green lines, we are quite close to the red spots.
Here is some pseudo-code for making the robot travel on a set heading:
(T is the target heading, and C is the current heading.)
read heading
if (C-T) >3
turn anticlockwise
elseif (T-C ) > 3
turn clockwise
else
go fowards
This code needs to be made into a function, which we could call travel(). We can tell the function the direction we want to travel in by passing that direction to the function when we call it.
We would use something like this:
travel (int T) (T is the target direction. It is passed to the function as we call it. e.g. travel(72) means travel on a bearing
of 72°. Now you finally know why functions have ( ) at the end. It is for passing values to the function.
{
int C = heading(); //this reads the compass
if(C -T) > 3 //this code just turns the robot to face the right way or lets it move forward
turn anti-clockwise;
elseif (T - C) > 3
turn clockwise;
else go forwards;
)
Normally the travel() function would be in a while loop that stops when a colour target is reached:
while (colour is not green)
travel(45); // this would make the robot travel on a bearing of 45° until it hit a green line.