-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBearingMonitor.cpp
375 lines (301 loc) · 8.83 KB
/
BearingMonitor.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
* BearingMonitor.cpp
*
* Created on: 16 abr. 2017
* Author: Sergio
*/
#include "BearingMonitor.h"
//#include "GPSport.h" // Include this library to output to DEBUG_PORT
#ifdef BNO055_INTERNAL_FUSION
#include "DevBNO055Int.h"
#endif
#ifdef BNO055_EXTERNAL_FUSION
#include "DevBNO055Ext.h"
#endif
#ifdef MINIMU9V5
#include "DevMinIMU9V5.h"
#endif
#ifdef ICM20948
#include "DevICM20948.h"
#endif
BearingMonitor::BearingMonitor(float headingDev = 0) {
setHeadingDev( headingDev);
}
BearingMonitor::~BearingMonitor() {
// TODO Auto-generated destructor stub
_IMU_status = NOT_DETECTED;
}
e_IMU_status BearingMonitor::IMU_setup(long EE_address){
#ifdef SHIP_SIM
_IMU_status = SIMULATED;
_internalIMU_status= INT_NOT_DETECTED;
return _IMU_status;
#endif
#ifdef BNO055_INTERNAL_FUSION
_imuDevice= (DevBNO055Int *) IMUDevice::createIMUDevice(); // create the imu_device object
#endif
#ifdef BNO055_EXTERNAL_FUSION
_imuDevice= (DevBNO055Ext *) IMUDevice::createIMUDevice(); // create the imu_device object
#endif
#ifdef MINIMU9V5
_imuDevice= (DevMinIMU9V5 *) IMUDevice::createIMUDevice(); // create the imu_device object
#endif
#ifdef ICM20948
_imuDevice= (DevICM20948 *) IMUDevice::createIMUDevice(); // create the imu_device object
#endif
if (_imuDevice->IMU_setup(EE_address)) {
_internalIMU_status= INT_DETECTED;
_IMU_status = CAL_MODE;
_IMU_cal_status = CAL_START;
}else {
_IMU_status = NOT_DETECTED;
_internalIMU_status= INT_NOT_DETECTED;
}
IBIT();
return _IMU_status;
}
void BearingMonitor::IBIT(){
_imuDevice->IBIT();
}
// Recurrent loop for calibration
//CAL_START-->CAL_INPROGRESS-->(CAL_RESULT_NOT_CALIBRATED; CAL_RESULT_RECALIBRATED)
//CAL_RESULT_RECALIBRATED-->completeCal?-->CHECK_ONGOING-->CHECK_FINISHED
//return true-->process ongoing
//return false-->process finished / IMU in operational mode again
e_IMU_cal_status BearingMonitor::EEload_Calib(long int &eeaddress){
return (_imuDevice->EEload_Calib(eeaddress)?CAL_RESULT_RECALIBRATED:CAL_RESULT_NOT_CALIBRATED);
}
bool BearingMonitor::EEsave_Calib( long int &eeaddress){
return (_imuDevice->EEsave_Calib(eeaddress));
}
bool BearingMonitor::compute_Cal_IMU(char sensor) {
bool ret=false;
//No calibration when IMU is in external mode.
if (_IMU_status==EXTERNAL_IMU) return false;
switch (_IMU_cal_status) {
case CAL_NOT_STARTED:
ret=false;
break;
case CAL_START:
ret=IMU_startCalibration(sensor);
break;
case CAL_INPROGRESS:
ret=IMU_Cal_Loop();
break;
//No action for other status
default:
break;
}
switch (_IMU_cal_status) {
case CAL_RESULT_NOT_CALIBRATED:
DEBUG_print(F("\nCalibration time out!\n"));
DEBUG_print(F("WARNING: Calibration failed. Bearing values might be inaccurate.\n"));
ret=false;
break;
case CAL_RESULT_RECALIBRATED:
// Check calibration status
// completeCal=true performs complete initial calibration + check (system==3 required)
// completeCal=false ensures minimum recalibration after each power-on ( as long as mag and gyro are 3, data is realiable)
if (_IMU_check == CHECK_NOT_STARTED) {
//if (!_imuDevice->isExternalCalibration())
//DEBUG_print(F("\nCalibrated! Ok\n"));
// Heading value is not received until a slight movement is detected by IMU
// Practically speaking this is not an issue, but some info is provided to user
//DEBUG_print(F("Move slightly to start receiving IMU data\n"));
//updateHeading();
ret=false;
}
//TODO: Check calibration quality
if (false) {
ret=compute_check_IMU();
}
break;
//No action for other status
default:
break;
}
if (ret == false and _IMU_status != OPERATIONAL) {
_IMU_status = OPERATIONAL;
_IMU_cal_status = CAL_NOT_STARTED;
_IMU_check = CHECK_NOT_STARTED;
}
return ret;
}
//return true-->process ongoing
//return false-->process finished
bool BearingMonitor::compute_check_IMU(void) {
switch (_IMU_check) {
case CHECK_NOT_STARTED:
//start check
return IMU_startCalCheck();
break;
case CHECK_ONGOING:
return IMU_CalCheck_Loop();
break;
case CHECK_FINISHED:
return false;
break;
}
return (_IMU_check ==CHECK_ONGOING);
}
bool BearingMonitor::IMU_startCalibration(char sensor) {
//_cal_iter = 0;
_IMU_cal_status = CAL_INPROGRESS;
//First iteration only
//DEBUG_print(F("Start IMU Calibration...\n"));
return _imuDevice->IMU_startCalibration(sensor);
}
void BearingMonitor::reset_calibration (long &eeAddress) {
DEBUG_print(F("***reset calib\n"));
//ID
long ID =0;
EEPROM.put(eeAddress, ID);
}
bool BearingMonitor::getCheckXYZ (uint16_t &x, int8_t &y, uint8_t &z) {
if (_IMU_check == CHECK_ONGOING) {
x= _x;
y= _y;
z= _z;
return true;
} else {
x= 0;
y= 0;
z= 0;
return false; //fn only return valid values when check is ongoing.
}
return false;
}
bool BearingMonitor::getCheckSGAM(uint8_t &S, uint8_t &G, uint8_t &A, uint8_t &M){
//if (_IMU_status != CAL_INPROGRESS) return false; //fn only return valid values when check is ongoing.
S=_calSys;
G=_calGyro;
A=_calAccel;
M=_calMagn;
return true;
}
void BearingMonitor::displayCalStatus(void)
{
DEBUG_print(F("\t"));
if (!_calSys)
{
DEBUG_print(F("! "));
}
/* Display the individual values */
sprintf(DEBUG_buffer,"Sys:%i G:%i A:%i M:%i\n", _calSys, _calGyro, _calAccel, _calMagn);
DEBUG_print(DEBUG_buffer);
DEBUG_flush();
}
e_IMU_status BearingMonitor::updateHeading(bool changeSourceEnabled, bool validExternal, float HDMExternal){
#ifdef SHIP_SIM
_heading = _SIMheading;
_heading_isValid = true;
_heading_isFrozen = false;
return SIMULATED;
#else
//decide if changing IMU status and source
if (changeSourceEnabled and _IMU_status!=CAL_MODE) {
if (validExternal) {
_IMU_status = EXTERNAL_IMU;
} else {
if (_internalIMU_status == INT_DETECTED) {
_IMU_status = OPERATIONAL;
} else {
_IMU_status = NOT_DETECTED;
}
}
}
//update heading based on IMU status
switch (_IMU_status) {
case OPERATIONAL:
case CAL_MODE:
updateHeading();
break;
case EXTERNAL_IMU:
updateHeading(validExternal, HDMExternal);
break;
case NOT_DETECTED:
break;
//case SIMULATED:
// break;
}
return _IMU_status;
#endif
}
void BearingMonitor::updateHeading(void){
if (_IMU_status == EXTERNAL_IMU) return;
_heading_isFrozen=false;
_heading_isValid=true;
float yaw = _heading;
float raw = _imuDevice->updateHeading();
//delta entre ángulos (-180, 180)
float delta_yaw_raw = raw-yaw;
if (delta_yaw_raw<-180) delta_yaw_raw+=360;
if (delta_yaw_raw>180) delta_yaw_raw-=360;
// Apply low pass filter to the IMU results
yaw = reduce360 (yaw*_heading_alfa + (yaw+delta_yaw_raw)* (1-_heading_alfa));
_heading = yaw;
}
e_IMU_status BearingMonitor::updateHeading(bool valid, float HDM){
_heading_isValid = valid;
_heading_isFrozen = false; //in external mode, there is no info of heading quality
if (valid) _heading = HDM;
return _IMU_status;
}
bool BearingMonitor::IMU_Cal_Loop(void){
// Exit if calibration is not in progress
if (_IMU_cal_status != CAL_INPROGRESS) return false;
bool ret = _imuDevice->IMU_Cal_Loop();
if (ret==false) _IMU_cal_status=CAL_RESULT_RECALIBRATED;
return ret;
}
void BearingMonitor::Cal_NextSensor(void){
_imuDevice->Cal_NextSensor();
}
bool BearingMonitor::isExternalCalibration(void){
return _imuDevice->isExternalCalibration();
}
bool BearingMonitor::IMU_startCalCheck(void) {
//_cal_iter = max_loop;
_IMU_check = CHECK_ONGOING;
return true;
}
bool BearingMonitor::IMU_CalCheck_Loop(void){
//Check funcionality not implemented
_IMU_check = CHECK_FINISHED;
return false;
}
void BearingMonitor::refreshCalStatus(void)
{
// DEBUG_print(F("refreshCalStatus\n"));
// /* Get the four calibration values (0..3) */
// /* Any sensor data reporting 0 should be ignored, */
// /* 3 means 'fully calibrated" */
uint8_t system, gyro, accel, mag;
system = gyro = accel = mag = 0;
_imuDevice->getCalibrationStatus(system, gyro, accel, mag);
_calSys = system;
_calGyro = gyro;
_calAccel = accel;
_calMagn = mag;
}
bool BearingMonitor::getCalibrationStatus(void) {
refreshCalStatus();
return (_calSys==3);
}
// calibrate.py external programme to calibrate A and M
bool BearingMonitor::set_calibrate_py_offsets(float B[3], float Ainv[3][3], char sensor) {
//ICM20948AHRS_setOffsets(G_offset[3]);
_imuDevice->set_calibrate_py_offsets(B, Ainv, sensor);
return true;
}
//FUNCTIONAL MODULE:SHIP SIMULATOR
void BearingMonitor::SIM_updateShip(int tillerAngle) {
static unsigned long DelayCalcStart = millis();
if ((millis() -DelayCalcStart) < deltaT) return;
float unstat= random(unstat0)-unstat0/2;
float alfa = (tillerAngle-unstat)*alfaMax/(sqrt(sq(float(tillerAngle-unstat))+softFactor));
//_SIMheading = (tillerAngle<40? _SIMheading : _SIMheading *intertia + (1-intertia)*(_SIMheading + deltaT * alfa));
_SIMheading = _SIMheading *intertia + (1-intertia)*(_SIMheading + deltaT * alfa);
_SIMheading = reduce360(_SIMheading);
DelayCalcStart = millis();
}