-
Notifications
You must be signed in to change notification settings - Fork 0
/
LS_Adafruit_Sensor_Calibration_EEPROM.cpp
122 lines (100 loc) · 3.06 KB
/
LS_Adafruit_Sensor_Calibration_EEPROM.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
#include "LS_Adafruit_Sensor_Calibration.h"
/**************************************************************************/
/*!
@brief Initializes Flash and filesystem
@returns False if any failure to initialize flash or filesys
*/
/**************************************************************************/
bool Adafruit_Sensor_Calibration_EEPROM::begin(uint8_t eeprom_addr) {
ee_addr = 0;
#if defined(ESP8266) || defined(ESP32)
EEPROM.begin(512);
#endif
return true;
}
bool Adafruit_Sensor_Calibration_EEPROM::saveCalibration(void) {
Serial.println("Save Cal");
uint8_t buf[EEPROM_CAL_SIZE];
memset(buf, 0, EEPROM_CAL_SIZE);
buf[0] = 0x75;
buf[1] = 0x54;
float offsets[16];
memcpy(offsets, accel_zerog, 12); // 3 x 4-byte floats
memcpy(offsets + 3, gyro_zerorate, 12); // 3 x 4-byte floats
memcpy(offsets + 6, mag_hardiron, 12); // 3 x 4-byte floats
offsets[9] = mag_field;
offsets[10] = mag_softiron[0];
offsets[11] = mag_softiron[4];
offsets[12] = mag_softiron[8];
offsets[13] = mag_softiron[1];
offsets[14] = mag_softiron[2];
offsets[15] = mag_softiron[5];
memcpy(buf + 2, offsets, 16 * 4);
uint16_t crc = 0xFFFF;
for (uint16_t i = 0; i < EEPROM_CAL_SIZE - 2; i++) {
crc = crc16_update(crc, buf[i]);
}
Serial.print("CRC: ");
Serial.println(crc, HEX);
buf[EEPROM_CAL_SIZE - 2] = crc & 0xFF;
buf[EEPROM_CAL_SIZE - 1] = crc >> 8;
for (uint16_t a = 0; a < EEPROM_CAL_SIZE; a++) {
EEPROM.write(a + ee_addr, buf[a]);
}
#if defined(ESP8266) || defined(ESP32)
EEPROM.commit();
#endif
return true;
}
bool Adafruit_Sensor_Calibration_EEPROM::loadCalibration(void) {
uint8_t buf[EEPROM_CAL_SIZE];
uint16_t crc = 0xFFFF;
for (uint16_t a = 0; a < EEPROM_CAL_SIZE; a++) {
buf[a] = EEPROM.read(a + ee_addr);
crc = crc16_update(crc, buf[a]);
}
if (crc != 0 || buf[0] != 0x75 || buf[1] != 0x54) {
Serial.print("CRC: ");
Serial.println(crc, HEX);
return false;
}
float offsets[16];
memcpy(offsets, buf + 2, 16 * 4);
accel_zerog[0] = offsets[0];
accel_zerog[1] = offsets[1];
accel_zerog[2] = offsets[2];
gyro_zerorate[0] = offsets[3];
gyro_zerorate[1] = offsets[4];
gyro_zerorate[2] = offsets[5];
mag_hardiron[0] = offsets[6];
mag_hardiron[1] = offsets[7];
mag_hardiron[2] = offsets[8];
mag_field = offsets[9];
mag_softiron[0] = offsets[10];
mag_softiron[1] = offsets[13];
mag_softiron[2] = offsets[14];
mag_softiron[3] = offsets[13];
mag_softiron[4] = offsets[11];
mag_softiron[5] = offsets[15];
mag_softiron[6] = offsets[14];
mag_softiron[7] = offsets[15];
mag_softiron[8] = offsets[12];
return true;
}
bool Adafruit_Sensor_Calibration_EEPROM::printSavedCalibration(void) {
delay(30000);
Serial.println(F("------------"));
for (uint16_t a = ee_addr; a < ee_addr + EEPROM_CAL_SIZE; a++) {
uint8_t c = EEPROM.read(a);
Serial.print("0x");
if (c < 0x10)
Serial.print('0');
Serial.print(c, HEX);
Serial.print(", ");
if ((a - ee_addr) % 16 == 15) {
Serial.println();
}
}
Serial.println(F("\n------------"));
return true;
}