forked from bluerobotics/BlueRobotics_MS5837_Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMS5837.cpp
More file actions
293 lines (243 loc) · 6.76 KB
/
Copy pathMS5837.cpp
File metadata and controls
293 lines (243 loc) · 6.76 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include "MS5837.h"
#include <Wire.h>
#include "math.h"
#define MS5837_ADDR 0x76
#define MS5837_RESET 0x1E
#define MS5837_ADC_READ 0x00
#define MS5837_PROM_READ 0xA0
// Pressure Conversion
#define MS5837_CONVERT_D1_256 0x40
#define MS5837_CONVERT_D1_512 0x42
#define MS5837_CONVERT_D1_1024 0x44
#define MS5837_CONVERT_D1_2048 0x46
#define MS5837_CONVERT_D1_4096 0x48
#define MS5837_CONVERT_D1_8192 0x4A
// Temperature Conversion
#define MS5837_CONVERT_D2_256 0x50
#define MS5837_CONVERT_D2_512 0x52
#define MS5837_CONVERT_D2_1024 0x54
#define MS5837_CONVERT_D2_2048 0x56
#define MS5837_CONVERT_D2_4096 0x58
#define MS5837_CONVERT_D2_8192 0x5A
const float MS5837::Pa = 100.0f;
const float MS5837::bar = 0.001f;
const float MS5837::mbar = 1.0f;
const uint8_t MS5837::MS5837_30BA = 0;
const uint8_t MS5837::MS5837_02BA = 1;
MS5837::MS5837() {
fluidDensity = 1029;
}
bool MS5837::init() {
// Reset the MS5837, per datasheet
Wire.beginTransmission(MS5837_ADDR);
Wire.write(MS5837_RESET);
Wire.endTransmission();
// Wait for reset to complete
delay(10);
// Read calibration values and CRC
for ( uint8_t i = 0 ; i < 7 ; i++ ) {
Wire.beginTransmission(MS5837_ADDR);
Wire.write(MS5837_PROM_READ+i*2);
Wire.endTransmission();
Wire.requestFrom(MS5837_ADDR,2);
C[i] = (Wire.read() << 8) | Wire.read();
}
// Verify that data is correct with CRC
uint8_t crcRead = C[0] >> 12;
uint8_t crcCalculated = crc4(C);
if ( crcCalculated == crcRead ) {
return true; // Initialization success
}
return false; // CRC fail
}
void MS5837::setModel(uint8_t model) {
_model = model;
}
void MS5837::setFluidDensity(float density) {
fluidDensity = density;
}
void MS5837::setOverSampling(MS5837_CONVERT_OVERSAMPLING oversampling){
_oversampling = (int)oversampling;
}
void MS5837::readNonBlocking()
{
if (_active_request == 0)
{
Wire.beginTransmission(MS5837_ADDR);
Wire.write(MS5837_CONVERT_D1_256 + 2*_oversampling);
Wire.endTransmission();
_active_request = 1;
_microsecond_counter = 0;
Serial.println("sent d1");
}
else if (_active_request == 1)
{
if (_microsecond_counter >= delay_lookup[_oversampling])
{
// we've waited enough, so ask for response
Wire.beginTransmission(MS5837_ADDR);
Wire.write(MS5837_ADC_READ);
Wire.endTransmission();
Wire.requestFrom(MS5837_ADDR,3);
D1 = 0;
D1 = Wire.read();
D1 = (D1 << 8) | Wire.read();
D1 = (D1 << 8) | Wire.read();
_active_request = 2;
_microsecond_counter = 0;
// Request D2 conversion
Wire.beginTransmission(MS5837_ADDR);
Wire.write(MS5837_CONVERT_D2_256 + 2*_oversampling);
Wire.endTransmission();
Serial.println("got d1");
Serial.println("sent d2");
}
}
else if (_active_request == 2)
{
if (_microsecond_counter >= delay_lookup[_oversampling])
{
// we've waited enough, so ask for response
Wire.beginTransmission(MS5837_ADDR);
Wire.write(MS5837_ADC_READ);
Wire.endTransmission();
Wire.requestFrom(MS5837_ADDR,3);
D2 = 0;
D2 = Wire.read();
D2 = (D2 << 8) | Wire.read();
D2 = (D2 << 8) | Wire.read();
calculate();
Serial.println(pressure());
_active_request = 0;
_microsecond_counter = 0;
Serial.println("got d2");
}
}
}
void MS5837::incrementTime(uint16_t increment_value)
{
_microsecond_counter += increment_value;
}
void MS5837::read() {
// Request D1 conversion
Wire.beginTransmission(MS5837_ADDR);
Wire.write(MS5837_CONVERT_D1_256 + 2*_oversampling);
Wire.endTransmission();
delayMicroseconds(delay_lookup[_oversampling]);
Wire.beginTransmission(MS5837_ADDR);
Wire.write(MS5837_ADC_READ);
Wire.endTransmission();
Wire.requestFrom(MS5837_ADDR,3);
D1 = 0;
D1 = Wire.read();
D1 = (D1 << 8) | Wire.read();
D1 = (D1 << 8) | Wire.read();
// Request D2 conversion
Wire.beginTransmission(MS5837_ADDR);
Wire.write(MS5837_CONVERT_D2_256 + 2*_oversampling);
Wire.endTransmission();
delayMicroseconds(delay_lookup[_oversampling]);
Wire.beginTransmission(MS5837_ADDR);
Wire.write(MS5837_ADC_READ);
Wire.endTransmission();
Wire.requestFrom(MS5837_ADDR,3);
D2 = 0;
D2 = Wire.read();
D2 = (D2 << 8) | Wire.read();
D2 = (D2 << 8) | Wire.read();
calculate();
}
void MS5837::calculate() {
// Given C1-C6 and D1, D2, calculated TEMP and P
// Do conversion first and then second order temp compensation
int32_t dT = 0;
int64_t SENS = 0;
int64_t OFF = 0;
int32_t SENSi = 0;
int32_t OFFi = 0;
int32_t Ti = 0;
int64_t OFF2 = 0;
int64_t SENS2 = 0;
// Terms called
dT = D2-uint32_t(C[5])*256l;
if ( _model == MS5837_02BA ) {
SENS = int64_t(C[1])*65536l+(int64_t(C[3])*dT)/128l;
OFF = int64_t(C[2])*131072l+(int64_t(C[4])*dT)/64l;
P = (D1*SENS/(2097152l)-OFF)/(32768l);
} else {
SENS = int64_t(C[1])*32768l+(int64_t(C[3])*dT)/256l;
OFF = int64_t(C[2])*65536l+(int64_t(C[4])*dT)/128l;
P = (D1*SENS/(2097152l)-OFF)/(8192l);
}
// Temp conversion
TEMP = 2000l+int64_t(dT)*C[6]/8388608LL;
//Second order compensation
if ( _model == MS5837_02BA ) {
if((TEMP/100)<20){ //Low temp
Ti = (11*int64_t(dT)*int64_t(dT))/(34359738368LL);
OFFi = (31*(TEMP-2000)*(TEMP-2000))/8;
SENSi = (63*(TEMP-2000)*(TEMP-2000))/32;
}
} else {
if((TEMP/100)<20){ //Low temp
Ti = (3*int64_t(dT)*int64_t(dT))/(8589934592LL);
OFFi = (3*(TEMP-2000)*(TEMP-2000))/2;
SENSi = (5*(TEMP-2000)*(TEMP-2000))/8;
if((TEMP/100)<-15){ //Very low temp
OFFi = OFFi+7*(TEMP+1500l)*(TEMP+1500l);
SENSi = SENSi+4*(TEMP+1500l)*(TEMP+1500l);
}
}
else if((TEMP/100)>=20){ //High temp
Ti = 2*(dT*dT)/(137438953472LL);
OFFi = (1*(TEMP-2000)*(TEMP-2000))/16;
SENSi = 0;
}
}
OFF2 = OFF-OFFi; //Calculate pressure and temp second order
SENS2 = SENS-SENSi;
TEMP = (TEMP-Ti);
if ( _model == MS5837_02BA ) {
P = (((D1*SENS2)/2097152l-OFF2)/32768l);
} else {
P = (((D1*SENS2)/2097152l-OFF2)/8192l);
}
}
float MS5837::pressure(float conversion) {
if ( _model == MS5837_02BA ) {
return P*conversion/100.0f;
}
else {
return P*conversion/10.0f;
}
}
float MS5837::temperature() {
return TEMP/100.0f;
}
float MS5837::depth() {
return (pressure(MS5837::Pa)-101300)/(fluidDensity*9.80665);
}
float MS5837::altitude() {
return (1-pow((pressure()/1013.25),.190284))*145366.45*.3048;
}
uint8_t MS5837::crc4(uint16_t n_prom[]) {
uint16_t n_rem = 0;
n_prom[0] = ((n_prom[0]) & 0x0FFF);
n_prom[7] = 0;
for ( uint8_t i = 0 ; i < 16; i++ ) {
if ( i%2 == 1 ) {
n_rem ^= (uint16_t)((n_prom[i>>1]) & 0x00FF);
} else {
n_rem ^= (uint16_t)(n_prom[i>>1] >> 8);
}
for ( uint8_t n_bit = 8 ; n_bit > 0 ; n_bit-- ) {
if ( n_rem & 0x8000 ) {
n_rem = (n_rem << 1) ^ 0x3000;
} else {
n_rem = (n_rem << 1);
}
}
}
n_rem = ((n_rem >> 12) & 0x000F);
return n_rem ^ 0x00;
}