-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompass.cpp
51 lines (41 loc) · 1.87 KB
/
compass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "compass.h"
Compass::Compass() : compass(Adafruit_HMC5883_Unified(12345)) {
/*
if(!compass.begin()) {
// There was a problem detecting the HMC5883 ... check your connections
lcd->setCursor(1,0);
lcd->print("Cannot find the compass");
}
*/
}
void Compass::checkCompass() {
sensors_event_t event;
compass.getEvent(&event);
/*
// Display the results (magnetic vector values are in micro-Tesla (uT))
Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print(" ");
Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print(" ");
Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print(" ");Serial.println("uT");
// Hold the module so that Z is pointing 'up' and you can measure the heading with x&y
// Calculate heading when the magnetometer is level, then correct for signs of axis.
float heading = atan2(event.magnetic.y, event.magnetic.x);
// Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
// Find yours here: http://www.magnetic-declination.com/
// Mine is: -13* 2' W, which is ~13 Degrees, or (which we need) 0.22 radians
// If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
float declinationAngle = 0.22;
heading += declinationAngle;
// Correct for when signs are reversed.
if(heading < 0)
heading += 2*PI;
// Check for wrap due to addition of declination.
if(heading > 2*PI)
heading -= 2*PI;
// Convert radians to degrees for readability.
float headingDegrees = heading * 180/M_PI;
Serial.print("Heading (degrees): "); Serial.println(headingDegrees);
*/
}
void Compass::registerTripMaster(TripMaster* _tripMaster) {
tripMaster = _tripMaster;
}