forked from xlcteam/RelativeHMC6352
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRelativeHMC6352.cpp
executable file
·55 lines (42 loc) · 1.01 KB
/
RelativeHMC6352.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
52
53
#include "RelativeHMC6352.h"
RelativeHMC6352::RelativeHMC6352()
{
_north = 0.0;
}
float RelativeHMC6352::angle_raw()
{
int HMC6352Address = 0x42;
int slaveAddress, i;
slaveAddress = HMC6352Address >> 1;
byte headingData[2];
int val;
Wire.beginTransmission(slaveAddress);
Wire.write("A"); // The "Get Data" command
Wire.endTransmission();
delay(10);
Wire.requestFrom(slaveAddress, 2);
i = 0;
while(Wire.available() && i < 2)
{
headingData[i] = Wire.read();
i++;
}
val = headingData[0]*256 + headingData[1];
return (int(val / 10) + float((val % 10) / 10.0));
}
float RelativeHMC6352::angle()
{
float relative_angle = fmod(RelativeHMC6352::angle_raw() - _north + 360, 360.0);
if (relative_angle < 0.0)
relative_angle += 360.0;
return relative_angle;
}
void RelativeHMC6352::set_north()
{
Wire.begin();
_north = RelativeHMC6352::angle_raw();
}
float RelativeHMC6352::real_north()
{
return _north;
}