@@ -72,7 +72,7 @@ typedef enum
72
72
TILT_STARTED,
73
73
TILT_INITIALIZED,
74
74
TILT_CORRECTING,
75
-
75
+ TILT_REQUEST_STOP,
76
76
} TiltState;
77
77
TiltState tiltState = TILT_DISABLED;
78
78
@@ -141,7 +141,7 @@ void tiltUpdate()
141
141
// Check to see if tilt compensation is active
142
142
if (tiltSensor->isCorrecting ())
143
143
{
144
- beepMultiple (2 , 500 , 500 ); // Number of beeps, length of beep ms, length of quiet ms
144
+ beepMultiple (2 , 500 , 500 ); // Number of beeps, length of beep ms, length of quiet ms
145
145
146
146
lastTiltBeepMs = millis ();
147
147
@@ -207,6 +207,10 @@ void tiltUpdate()
207
207
}
208
208
209
209
break ;
210
+
211
+ case TILT_REQUEST_STOP:
212
+ tiltStop (); // Changes state to TILT_OFFILINE
213
+ break ;
210
214
}
211
215
}
212
216
@@ -343,7 +347,8 @@ void beginTilt()
343
347
// Set the overall length of the GNSS setup in meters: rod length 1800mm + internal length 96.45mm + antenna
344
348
// POC 19.25mm = 1915.7mm
345
349
char clubVector[strlen (" CLUB_VECTOR=0,0,1.916" ) + 1 ];
346
- float arp_m = present.antennaReferencePoint_mm / 1000.0 ; // Convert mm to m. antennaReferencePoint_mm assigned in begin()
350
+ float arp_m =
351
+ present.antennaReferencePoint_mm / 1000.0 ; // Convert mm to m. antennaReferencePoint_mm assigned in begin()
347
352
348
353
snprintf (clubVector, sizeof (clubVector), " CLUB_VECTOR=0,0,%0.3f" , settings.tiltPoleLength + arp_m);
349
354
result &= tiltSensor->sendCommand (clubVector);
@@ -369,9 +374,9 @@ void beginTilt()
369
374
370
375
// Enable magnetic field mode
371
376
// 'it is recommended to use the magnetic field initialization mode to speed up the initialization process'
372
- result &= tiltSensor->sendCommand (" AHRS=ENABLE" );
377
+ result &= tiltSensor->sendCommand (" AHRS=ENABLE" );
373
378
374
- result &= tiltSensor->sendCommand (" MAG_AUTO_SAVE=ENABLE" );
379
+ result &= tiltSensor->sendCommand (" MAG_AUTO_SAVE=ENABLE" );
375
380
376
381
if (result == true )
377
382
{
@@ -388,19 +393,37 @@ void beginTilt()
388
393
389
394
void tiltStop ()
390
395
{
396
+ // Gracefully stop the UART before freeing resources
397
+ while (SerialForTilt->available ())
398
+ SerialForTilt->read ();
399
+
400
+ SerialForTilt->end ();
401
+
391
402
// Free the resources
392
- delete tiltSensor;
393
- tiltSensor = nullptr ;
403
+ if (tiltSensor != nullptr )
404
+ {
405
+ delete tiltSensor;
406
+ tiltSensor = nullptr ;
407
+ }
394
408
395
- delete SerialForTilt;
396
- SerialForTilt = nullptr ;
409
+ if (SerialForTilt != nullptr )
410
+ {
411
+ delete SerialForTilt;
412
+ SerialForTilt = nullptr ;
413
+ }
397
414
398
415
if (tiltState == TILT_CORRECTING)
399
416
beepDurationMs (1000 ); // Indicate we are going offline
400
417
401
418
tiltState = TILT_OFFLINE;
402
419
}
403
420
421
+ // Called by other tasks. Prevents stopping serial port while within a library transaction.
422
+ void tiltRequestStop ()
423
+ {
424
+ tiltState = TILT_REQUEST_STOP;
425
+ }
426
+
404
427
bool tiltIsCorrecting ()
405
428
{
406
429
if (tiltState == TILT_CORRECTING)
0 commit comments