forked from amperka/Troyka-IMU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LPS331.cpp
177 lines (145 loc) · 4.69 KB
/
LPS331.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <LPS331.h>
#include <Wire.h>
#include <GOST4401_81.h>
// Defines ///////////////////////////////////////////////////////////
// The Arduino two-wire interface uses a 7-bit number for the address,
// and sets the last bit correctly based on reads and writes
#define LPS331AP_ADDRESS_SA0_LOW 0b1011100
#define LPS331AP_ADDRESS_SA0_HIGH 0b1011101
// Constructors //////////////////////////////////////////////////////
LPS331::LPS331(void)
{
// Pololu board pulls SA0 high, so default assumption is that it is
// high
address = LPS331AP_ADDRESS_SA0_LOW;
}
// Public Methods ////////////////////////////////////////////////////
// sets or detects slave address; returns bool indicating success
void LPS331::begin()
{
Wire.begin();
writeReg(LPS331_CTRL_REG1, 0b11100000);
delay(100);
}
// writes register
void LPS331::writeReg(byte reg, byte value)
{
Wire.beginTransmission(address);
Wire.write(reg);
Wire.write(value);
Wire.endTransmission();
}
// reads register
byte LPS331::readReg(byte reg)
{
byte value;
Wire.beginTransmission(address);
Wire.write(reg);
Wire.endTransmission(false); // restart
Wire.requestFrom(address, (byte)1);
value = Wire.read();
Wire.endTransmission();
return value;
}
// read pressure in pascals
float LPS331::readPressurePascals(void)
{
return (float)readPressureRaw() / 40.96;
}
// reads pressure in millibars (mbar)/hectopascals (hPa)
float LPS331::readPressureMillibars(void)
{
return (float)readPressureRaw() / 4096;
}
// reads pressure in inches of mercury (inHg)
float LPS331::readPressureInchesHg(void)
{
return (float)readPressureRaw() / 138706.5;
}
// reads pressure in millimeters of mercury (mmHg)
float LPS331::readPressureMillimetersHg(void)
{
// 1 mbar * 0,75006168270417 = 1 mmHg
return (float)(readPressureRaw()) * 0.75006375541921 / 4096.0;
}
// reads pressure and returns raw 24-bit sensor output
int32_t LPS331::readPressureRaw(void)
{
Wire.beginTransmission(address);
// assert MSB to enable register address auto-increment
Wire.write(LPS331_PRESS_OUT_XL | (1 << 7));
Wire.endTransmission();
Wire.requestFrom(address, (byte)3);
while (Wire.available() < 3);
uint8_t pxl = Wire.read();
uint8_t pl = Wire.read();
uint8_t ph = Wire.read();
// combine bytes
return (int32_t)(int8_t)ph << 16 | (uint16_t)pl << 8 | pxl;
}
// reads temperature in degrees K
float LPS331::readTemperatureK(){
return readTemperatureC() + LPS331_CELSIUS_TO_KELVIN_OFFSET;
}
// reads temperature in degrees C
float LPS331::readTemperatureC(void)
{
return 42.5 + (float)readTemperatureRaw() / 480;
}
// reads temperature in degrees F
float LPS331::readTemperatureF(void)
{
return 108.5 + (float)readTemperatureRaw() / 480 * 1.8;
}
// reads temperature and returns raw 16-bit sensor output
int16_t LPS331::readTemperatureRaw(void)
{
Wire.beginTransmission(address);
// assert MSB to enable register address auto-increment
Wire.write(LPS331_TEMP_OUT_L | (1 << 7));
Wire.endTransmission();
Wire.requestFrom(address, (byte)2);
while (Wire.available() < 2);
uint8_t tl = Wire.read();
uint8_t th = Wire.read();
// combine bytes
return (int16_t)(th << 8 | tl);
}
// Calculates altitude in meters above MSL using GOST4401-81
// atmosphere model from the given pressure in pascals
// The model implemented for height up to 51km
//
float LPS331::GOST4401_altitude(float pressure_pascals){
return GOST4401_getAltitude(pressure_pascals);
}
// converts pressure in mbar to altitude in meters, using 1976 US
// Standard Atmosphere model (note that this formula only applies to a
// height of 11 km, or about 36000 ft)
// If altimeter setting (QNH, barometric pressure adjusted to sea
// level) is given, this function returns an indicated altitude
// compensated for actual regional pressure; otherwise, it returns
// the pressure altitude above the standard pressure level of 1013.25
// mbar or 29.9213 inHg
float LPS331::pressureToAltitudeMeters(float pressure_mbar, float altimeter_setting_mbar)
{
return (1 - pow(pressure_mbar / altimeter_setting_mbar, 0.190263)) * 44330.8;
}
// converts pressure in inHg to altitude in feet; see notes above
float LPS331::pressureToAltitudeFeet(float pressure_inHg, float altimeter_setting_inHg)
{
return (1 - pow(pressure_inHg / altimeter_setting_inHg, 0.190263)) * 145442;
}
// Private Methods ///////////////////////////////////////////////////
bool LPS331::autoDetectAddress(void)
{
// try each possible address and stop if reading WHO_AM_I returns the expected response
address = LPS331AP_ADDRESS_SA0_LOW;
if (testWhoAmI()) return true;
address = LPS331AP_ADDRESS_SA0_HIGH;
if (testWhoAmI()) return true;
return false;
}
bool LPS331::testWhoAmI(void)
{
return (readReg(LPS331_WHO_AM_I) == 0xBB);
}