0

$USD

$USD
TUTORIALS RoboticsArduino

How to Play With MiniQ 2WD Complete Robot Kit v2.0- Lesson 8 (Compass)

DFRobot Mar 23 2017 1134


Key Points

Electric compass can be used in many situations of navigation. And it is so easy to get the direction value thanks to the development of science. Just look back to the great sailing ships era, people navigate according to the sun, the stars and life experiences, but they often make wrong judgments.
So we live in a high technology century. And we can make a compass enabled technology robots ourselves!

You will:
1. Learn how to use the compass sensor/chip
2. Display the direction in computer


Upload the Code

Install the library: HMC5883L,

1. Library Route

 

Open hmc.ino, and upload the code:

2.upload the code

Open the serial monitor:

3. Value printed through serial monitor

4. The relationship between direction and value

From the image upon we can know that if we get 0, it is north, and if we get 90, it is east. And be careful of the sensor direction in the robot, the x axis is nor as the same as the headstock, so if you get 90, it is 90-90=0, or if you get 270, it is 270-90=180, thus you can know the direction of you headstock.

5. The directionof every axis

6. The direction when robot towards west

OK, now rotate your robot and you have made a compass.


Tips:
1. Cause the compass sensor gets the value through geomagnetism, so you’d better not put any magnet goods near the sensor. And if you feel the value had anything wrong, check the object around.
2. And you must put your robot flatiy, or the value will be inaccuracy.

7 .Rotate your Smartphone

Some of the smartphoe alse have this problem, because the application developers didn’t do any intelligent tilt, if you rotate your smartphone like the picture above, maybe you will find the value is not correct.
4. If I need direction, can I just use a magnetoresistive sensor without a acceleration sensor.
Yes, of course. But you need to make your sensor in horizontal, that means, the pitch angle and the roll angle are both zaro.
5. Why I need a acceleration sensor?


If you want your sensor gives a right value no matter wheather it is in horizontal or not, you need a sensor to correct the error taken by tilt, so you need to know the titlt value, thus you need a acceleration sensor. Or Honeywell gives us a good solution, you can search HMC6343 which has acceleration and geomagnetic sensor inside to give a value with intelligent tilt.

Code Analysis

Import the libraries:
#include <Wire.h>//add 2.0C library
#include <HMC5883L.h>//add hmc5883 library

Defines:
HMC5883L compass;// Store our compass as a variable.
Init HMC5883
compass = HMC5883L(); // Construct a new HMC5883 compass.
compass.SetScale(1.3);
compass.SetMeasurementMode(Measurement_Continuous);// Set the measurement mode to Continuous

Get the value
MagnetometerRaw raw = compass.ReadRawAxis();// Retrive the raw values from the compass
MagnetometerScaled scaled = compass.ReadScaledAxis();// Retrived the scaled values from the compass (scaled to the configured scale).

Deal with the value get (calculate the direction by the components of two axis)
float xHeading = atan2(scaled.YAxis, scaled.XAxis);
float yHeading = atan2(scaled.ZAxis, scaled.XAxis);
float zHeading = atan2(scaled.ZAxis, scaled.YAxis);

Deal with the value (invert the value into 0-2PI)
if(xHeading < 0) xHeading += 2*PI;
if(xHeading > 2*PI) xHeading -= 2*PI;
if(yHeading < 0) yHeading += 2*PI;
if(yHeading > 2*PI) yHeading -= 2*PI;
if(zHeading < 0) zHeading += 2*PI;
if(zHeading > 2*PI) zHeading -= 2*PI;

Change the value from radian into angle
   float xDegrees = xHeading * 180/M_PI; 
   float yDegrees = yHeading * 180/M_PI; 
   float zDegrees = zHeading * 180/M_PI; 

The library of HMC5883 helps do every to get the direction value, so you don’t need to do much complex things.

 

Circuit Design

HMC5883 is an integrated solution,so we don’t need to design a lot to make it work.

8. HMC5883

miniQ only connect SCL and SDA, the only two resistors are used for pull-up. So if you want to design HMC5883 module yourself, just use this circuit and it can work.

Continue Reading

1. Make your robot go straightly toward east. And bypass objects.
2. If you have a 3-axis acceleration sensor, try to use it together with HMC5883 to get a correct value of direction when your HMC5883 is not horizontal.


Related Category: Robotic > Robot Platform

Last Arduino MiniQ 2WD Tutorial: MiniQ 2WD Complete Robot Kit v2.0- Lesson 7(Encoder)
Next Arduino MiniQ 2WD Tutorial: MiniQ 2WD Complete Robot Kit v2.0- Lesson 9(Application Design: Catch the Ball)