Skip to content

Commit ee978f0

Browse files
committed
Don't stop Tilt from within external task
1 parent faacf28 commit ee978f0

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

Firmware/RTK_Everywhere/Developer.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ void tiltUpdate() {}
177177
void tiltStop() {}
178178
void tiltSensorFactoryReset() {}
179179
bool tiltIsCorrecting() {return(false);}
180+
void tiltRequestStop() {}
180181

181182
#endif // COMPILE_IM19_IMU
182183

Firmware/RTK_Everywhere/Tasks.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,7 @@ void buttonCheckTask(void *e)
13351335
// The user button only exits tilt mode
13361336
if ((singleTap || doubleTap) && (tiltIsCorrecting() == true))
13371337
{
1338-
tiltStop();
1338+
tiltRequestStop(); //Don't force the hardware off here as it may be in use in another task
13391339
}
13401340

13411341
else if (doubleTap)

Firmware/RTK_Everywhere/Tilt.ino

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ typedef enum
7272
TILT_STARTED,
7373
TILT_INITIALIZED,
7474
TILT_CORRECTING,
75-
75+
TILT_REQUEST_STOP,
7676
} TiltState;
7777
TiltState tiltState = TILT_DISABLED;
7878

@@ -141,7 +141,7 @@ void tiltUpdate()
141141
// Check to see if tilt compensation is active
142142
if (tiltSensor->isCorrecting())
143143
{
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
145145

146146
lastTiltBeepMs = millis();
147147

@@ -207,6 +207,10 @@ void tiltUpdate()
207207
}
208208

209209
break;
210+
211+
case TILT_REQUEST_STOP:
212+
tiltStop(); // Changes state to TILT_OFFILINE
213+
break;
210214
}
211215
}
212216

@@ -343,7 +347,8 @@ void beginTilt()
343347
// Set the overall length of the GNSS setup in meters: rod length 1800mm + internal length 96.45mm + antenna
344348
// POC 19.25mm = 1915.7mm
345349
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()
347352

348353
snprintf(clubVector, sizeof(clubVector), "CLUB_VECTOR=0,0,%0.3f", settings.tiltPoleLength + arp_m);
349354
result &= tiltSensor->sendCommand(clubVector);
@@ -369,9 +374,9 @@ void beginTilt()
369374

370375
// Enable magnetic field mode
371376
// '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");
373378

374-
result &= tiltSensor->sendCommand("MAG_AUTO_SAVE=ENABLE");
379+
result &= tiltSensor->sendCommand("MAG_AUTO_SAVE=ENABLE");
375380

376381
if (result == true)
377382
{
@@ -388,19 +393,37 @@ void beginTilt()
388393

389394
void tiltStop()
390395
{
396+
// Gracefully stop the UART before freeing resources
397+
while (SerialForTilt->available())
398+
SerialForTilt->read();
399+
400+
SerialForTilt->end();
401+
391402
// Free the resources
392-
delete tiltSensor;
393-
tiltSensor = nullptr;
403+
if (tiltSensor != nullptr)
404+
{
405+
delete tiltSensor;
406+
tiltSensor = nullptr;
407+
}
394408

395-
delete SerialForTilt;
396-
SerialForTilt = nullptr;
409+
if (SerialForTilt != nullptr)
410+
{
411+
delete SerialForTilt;
412+
SerialForTilt = nullptr;
413+
}
397414

398415
if (tiltState == TILT_CORRECTING)
399416
beepDurationMs(1000); // Indicate we are going offline
400417

401418
tiltState = TILT_OFFLINE;
402419
}
403420

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+
404427
bool tiltIsCorrecting()
405428
{
406429
if (tiltState == TILT_CORRECTING)

0 commit comments

Comments
 (0)