-
Notifications
You must be signed in to change notification settings - Fork 6
/
sensors.ino
65 lines (51 loc) · 1.6 KB
/
sensors.ino
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
54
55
56
57
58
59
60
61
62
63
64
65
boolean mpuInit()
{
boolean out;
accelgyro.initialize();
if(accelgyro.testConnection()) out = true;
else out = false;
return out;
}
boolean hmcInit()
{
boolean out;
compass = HMC5883L();
//return out;
int scaleError = 0, measurementError = 0;
scaleError = compass.SetScale(8.1); // Set the scale of the compass.
measurementError = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous
if(scaleError == 512 && measurementError == 512) out = true;
else out = false;
return out;
}
void mpuRead()
{
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
pitchRad = -atan2(az,ay);
rollRad = atan2(ax,ay); // check this!
// Computing average
static int pitchDeg1, pitchDeg2, pitchDeg3, pitchDeg4, pitchDeg5;
pitchDeg5 = pitchDeg4;
pitchDeg4 = pitchDeg3;
pitchDeg3 = pitchDeg2;
pitchDeg2 = pitchDeg1;
pitchDeg1 = pitchRad*180.00/M_PI; // conversion to degrees
pitchDegMean = (pitchDeg1 + pitchDeg2 + pitchDeg3 + pitchDeg4 + pitchDeg5)/5;
}
void hmcRead()
{
MagnetometerScaled mag = compass.ReadScaledAxis();
// compensation
float xc = mag.XAxis * cos(rollRad) - 0.8 * mag.YAxis * sin(rollRad);
float zc = mag.ZAxis * cos(pitchRad) + 0.6 * mag.YAxis * sin(pitchRad); // perfect pitch compensation
// averaging disabled
headingDegMean = atan2(xc,zc)*180.00/PI; // conversion to degrees
headingDegMean += 180+28; // offset
// range adjustement
if(headingDegMean < 0) headingDegMean += 360;
if(headingDegMean >= 360) headingDegMean -= 360;
}
void batRead()
{
bat = map(analogRead(batPin),0,486,0,7950);
}