Skip to content

Commit a2ed0a8

Browse files
authored
Merge pull request #279 from sparkfun/TiltWork
Fix system reset upon tilt shutdown
2 parents bf4f2bc + ee978f0 commit a2ed0a8

File tree

4 files changed

+35
-60
lines changed

4 files changed

+35
-60
lines changed

Firmware/RTK_Everywhere/Begin.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void beginBoard()
139139
present.button_powerHigh = true; // Button is pressed when high
140140
present.beeper = true;
141141
present.gnss_to_uart = true;
142-
present.antennaReferencePoint_mm = 102.0;
142+
present.antennaReferencePoint_mm = 115.7;
143143
present.needsExternalPpl = true; // Uses the PointPerfect Library
144144

145145
#ifdef COMPILE_IM19_IMU

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 & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,9 +1150,6 @@ void tickerBluetoothLedUpdate()
11501150
void tickerGnssLedUpdate()
11511151
{
11521152
static uint8_t ledCallCounter = 0; // Used to calculate a 50% or 10% on rate for blinking
1153-
// static int gnssFadeLevel = 0; // Used to fade LED when needed
1154-
// static int gnssPwmFadeAmount = 255 / gnssTaskUpdatesHz; // Fade in/out with 20 steps, as limited by the ticker
1155-
// rate of 20Hz
11561153

11571154
ledCallCounter++;
11581155
ledCallCounter %= gnssTaskUpdatesHz; // Wrap to X calls per 1 second
@@ -1170,52 +1167,6 @@ void tickerGnssLedUpdate()
11701167
{
11711168
ledcWrite(ledGnssChannel, 0);
11721169
}
1173-
1174-
// // Solid during tilt corrected RTK fix
1175-
// if (tiltIsCorrecting() == true)
1176-
// {
1177-
// ledcWrite(ledGnssChannel, 255);
1178-
// }
1179-
// else
1180-
// {
1181-
// ledcWrite(ledGnssChannel, 0);
1182-
// }
1183-
1184-
// Fade on/off during RTK Fix
1185-
// else if (gnssIsRTKFix() == true)
1186-
// {
1187-
// // Fade in/out the GNSS LED during RTK Fix
1188-
// gnssFadeLevel += gnssPwmFadeAmount;
1189-
// if (gnssFadeLevel <= 0 || gnssFadeLevel >= 255)
1190-
// gnssPwmFadeAmount *= -1;
1191-
1192-
// if (gnssFadeLevel > 255)
1193-
// gnssFadeLevel = 255;
1194-
// if (gnssFadeLevel < 0)
1195-
// gnssFadeLevel = 0;
1196-
1197-
// ledcWrite(ledGnssChannel, gnssFadeLevel);
1198-
// }
1199-
1200-
// // Blink 2Hz 50% during RTK float
1201-
// else if (gnssIsRTKFloat() == true)
1202-
// {
1203-
// if (ledCallCounter <= (gnssTaskUpdatesHz / 2))
1204-
// ledcWrite(ledGnssChannel, 255);
1205-
// else
1206-
// ledcWrite(ledGnssChannel, 0);
1207-
// }
1208-
1209-
// // Blink a short PPS when GNSS 3D fixed
1210-
// else if (gnssIsFixed() == true)
1211-
// {
1212-
// if (ledCallCounter == (gnssTaskUpdatesHz / 10))
1213-
// {
1214-
// ledcWrite(ledGnssChannel, 255);
1215-
// }
1216-
// else
1217-
// ledcWrite(ledGnssChannel, 0);
1218-
// }
12191170
}
12201171
}
12211172

@@ -1384,7 +1335,7 @@ void buttonCheckTask(void *e)
13841335
// The user button only exits tilt mode
13851336
if ((singleTap || doubleTap) && (tiltIsCorrecting() == true))
13861337
{
1387-
tiltStop();
1338+
tiltRequestStop(); //Don't force the hardware off here as it may be in use in another task
13881339
}
13891340

13901341
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;
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)