-
Notifications
You must be signed in to change notification settings - Fork 0
/
dps310.cpp
86 lines (64 loc) · 1.86 KB
/
dps310.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "dps310.h"
// the baro, the myth, the legend
Adafruit_DPS310 dps310;
void initDPS310()
{
if (!dps310.begin_I2C(DPS310_I2CADDR_DEFAULT))
{
// TODO print to both screen and serial (and log?)
Serial.println("Unable to contact barometer on 0x77.");
return;
//return ErrorCode::barometerCouldNotConnect;
}
// configure
dps310.configurePressure(DPS310_64HZ, DPS310_64SAMPLES);
dps310.configureTemperature(DPS310_64HZ, DPS310_64SAMPLES);
}
bool getBaroValues(float* temperature, float* pressure)
{
if (!dps310.temperatureAvailable() || !dps310.pressureAvailable())
{
// TODO print to serial and to log, maybe to screen too
return false;
}
sensors_event_t temperatureEvent;
sensors_event_t pressureEvent;
dps310.getEvents(&temperatureEvent, &pressureEvent);
if (temperatureEvent.temperature == NAN || pressureEvent.pressure == NAN)
{
// TODO maybe print error?
return false;
}
*temperature = temperatureEvent.temperature;
*pressure = pressureEvent.pressure;
return true;
}
float calcAltitude(float pressure, float temperature)
{
return (pow((SEA_LEVEL_PRESSURE / pressure), 0.190223) - 1.0) * (temperature + 273.15) / 0.0065;
}
float calibrateGroundAltitude()
{
Array<float, NUM_GROUND_ALT_READINGS_TO_TAKE> groundAltReadings;
uint numGroundAltReadingsTaken = 0;
float baroTemp;
float baroPres;
while (numGroundAltReadingsTaken < NUM_GROUND_ALT_READINGS_TO_TAKE)
{
if (getBaroValues(&baroTemp, &baroPres))
{
groundAltReadings[numGroundAltReadingsTaken] = calcAltitude(baroPres, baroTemp);
numGroundAltReadingsTaken++;
// TODO log readings to somewhere maybe
// TODO log progress somewhere maybe
}
}
float groundAlt = 0;
for (uint i = 0; i < NUM_GROUND_ALT_READINGS_TO_TAKE; i++)
{
groundAlt += groundAltReadings[i];
}
groundAlt /= float(NUM_GROUND_ALT_READINGS_TO_TAKE);
// TODO print result?
return groundAlt;
}