Skip to content

Commit 575d7f0

Browse files
committed
Set handles to null after deleting task. Avoid Strings in batt check.
1 parent e83896e commit 575d7f0

File tree

1 file changed

+72
-66
lines changed

1 file changed

+72
-66
lines changed

Firmware/RTK_Surveyor/System.ino

Lines changed: 72 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
//This allows multiple units to be on at same time
33
bool startBluetooth()
44
{
5+
//Shutdown any previous WiFi
6+
caster.stop();
7+
WiFi.mode(WIFI_OFF);
8+
radioState = RADIO_OFF;
9+
10+
btStart();
11+
512
if (digitalRead(baseSwitch) == HIGH)
613
{
714
//Rover mode
@@ -14,13 +21,23 @@ bool startBluetooth()
1421
}
1522

1623
if (SerialBT.begin(deviceName) == false)
24+
{
25+
Serial.println(F("An error occurred initializing Bluetooth"));
26+
radioState = RADIO_OFF;
27+
digitalWrite(bluetoothStatusLED, LOW);
1728
return (false);
29+
}
30+
1831
Serial.print(F("Bluetooth broadcasting as: "));
1932
Serial.println(deviceName);
2033

34+
radioState = BT_ON_NOCONNECTION;
35+
digitalWrite(bluetoothStatusLED, HIGH);
36+
lastBluetoothLEDBlink = millis();
37+
2138
//Start the tasks for handling incoming and outgoing BT bytes to/from ZED-F9P
2239
//Reduced stack size from 10,000 to 1,000 to make room for WiFi/NTRIP server capabilities
23-
xTaskCreate(F9PSerialReadTask, "F9Read", 1000, NULL, 0, &F9PSerialReadTaskHandle);
40+
xTaskCreate(F9PSerialReadTask, "F9Read", 1000, NULL, 0, &F9PSerialReadTaskHandle);
2441
xTaskCreate(F9PSerialWriteTask, "F9Write", 1000, NULL, 0, &F9PSerialWriteTaskHandle);
2542

2643
SerialBT.setTimeout(1);
@@ -33,9 +50,15 @@ bool endBluetooth()
3350
{
3451
//Kill tasks if running
3552
if (F9PSerialReadTaskHandle != NULL)
53+
{
3654
vTaskDelete(F9PSerialReadTaskHandle);
55+
F9PSerialReadTaskHandle = NULL;
56+
}
3757
if (F9PSerialWriteTaskHandle != NULL)
58+
{
3859
vTaskDelete(F9PSerialWriteTaskHandle);
60+
F9PSerialWriteTaskHandle = NULL;
61+
}
3962

4063
//SerialBT.end();
4164
customBTstop(); //Gracefully turn off Bluetooth so we can turn it back on if needed
@@ -46,31 +69,31 @@ bool endBluetooth()
4669
//Starting and restarting BT is a problem. See issue: https://github.com/espressif/arduino-esp32/issues/2718
4770
//To work around the bug without modifying the core we create our own btStop() function with
4871
//the patch from github
49-
bool customBTstop(){
50-
if(esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE){
51-
return true;
52-
}
53-
if(esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED){
54-
if (esp_bt_controller_disable()) {
55-
log_e("BT Disable failed");
56-
return false;
57-
}
58-
while(esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED);
72+
bool customBTstop() {
73+
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) {
74+
return true;
75+
}
76+
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED) {
77+
if (esp_bt_controller_disable()) {
78+
log_e("BT Disable failed");
79+
return false;
5980
}
60-
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED)
81+
while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED);
82+
}
83+
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED)
84+
{
85+
log_i("inited");
86+
if (esp_bt_controller_deinit())
6187
{
62-
log_i("inited");
63-
if (esp_bt_controller_deinit())
64-
{
65-
log_e("BT deint failed");
66-
return false;
67-
}
68-
while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED)
69-
;
70-
return true;
88+
log_e("BT deint failed");
89+
return false;
7190
}
72-
log_e("BT Stop failed");
73-
return false;
91+
while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED)
92+
;
93+
return true;
94+
}
95+
log_e("BT Stop failed");
96+
return false;
7497
}
7598

7699
//If the phone has any new data (NTRIP RTCM, etc), read it in over Bluetooth and pass along to ZED
@@ -627,80 +650,63 @@ void updateBattLEDs()
627650
{
628651
if (millis() - lastBattUpdate > 5000)
629652
{
630-
lastBattUpdate += 5000;
653+
lastBattUpdate = millis();
631654

632655
checkBatteryLevels();
633656
}
634657
}
635658

636659
//When called, checks level of battery and updates the LED brightnesses
637-
//And outputs a serial message to USB and BT
660+
//And outputs a serial message to USB
638661
void checkBatteryLevels()
639662
{
640-
String battMsg = "";
641-
642-
battLevel = lipo.getSOC();
643-
644-
battMsg += "Batt (";
645-
battMsg += battLevel;
646-
battMsg += "%): ";
647-
648-
battMsg += "Voltage: ";
649-
battMsg += lipo.getVoltage();
650-
battMsg += "V";
651-
652-
if (lipo.getChangeRate() > 0)
653-
battMsg += " Charging: ";
663+
//long startTime = millis();
664+
665+
//Check I2C semaphore
666+
// if (xSemaphoreTake(xI2CSemaphore, i2cSemaphore_maxWait) == pdPASS)
667+
// {
668+
battLevel = lipo.getSOC();
669+
battVoltage = lipo.getVoltage();
670+
battChangeRate = lipo.getChangeRate();
671+
// xSemaphoreGive(xI2CSemaphore);
672+
// }
673+
//Serial.printf("Batt time to update: %d\n", millis() - startTime);
674+
675+
Serial.printf("Batt (%d%%): Voltage: %0.02fV", battLevel, battVoltage);
676+
677+
char tempStr[25];
678+
if (battChangeRate > 0)
679+
sprintf(tempStr, "C");
654680
else
655-
battMsg += " Discharging: ";
656-
battMsg += lipo.getChangeRate();
657-
battMsg += "%/hr ";
681+
sprintf(tempStr, "Disc");
682+
Serial.printf(" %sharging: %0.02f%%/hr ", tempStr, battChangeRate);
658683

659684
if (battLevel < 10)
660685
{
661-
battMsg += "RED uh oh!";
686+
sprintf(tempStr, "RED uh oh!");
662687
ledcWrite(ledRedChannel, 255);
663688
ledcWrite(ledGreenChannel, 0);
664689
}
665690
else if (battLevel < 50)
666691
{
667-
battMsg += "Yellow ok";
692+
sprintf(tempStr, "Yellow ok");
668693
ledcWrite(ledRedChannel, 128);
669694
ledcWrite(ledGreenChannel, 128);
670695
}
671696
else if (battLevel >= 50)
672697
{
673-
battMsg += "Green all good";
698+
sprintf(tempStr, "Green all good");
674699
ledcWrite(ledRedChannel, 0);
675700
ledcWrite(ledGreenChannel, 255);
676701
}
677702
else
678703
{
679-
battMsg += "No batt";
704+
sprintf(tempStr, "No batt");
680705
ledcWrite(ledRedChannel, 10);
681706
ledcWrite(ledGreenChannel, 0);
682707
}
683-
battMsg += "\n\r";
684-
SerialBT.print(battMsg);
685-
Serial.print(battMsg);
686-
687-
}
688-
689-
//Configure the on board MAX17048 fuel gauge
690-
void beginFuelGauge()
691-
{
692-
// Set up the MAX17048 LiPo fuel gauge
693-
if (lipo.begin() == false)
694-
{
695-
Serial.println(F("MAX17048 not detected. Continuing."));
696-
return;
697-
}
698-
699-
//Always use hibernate mode
700-
if (lipo.getHIBRTActThr() < 0xFF) lipo.setHIBRTActThr((uint8_t)0xFF);
701-
if (lipo.getHIBRTHibThr() < 0xFF) lipo.setHIBRTHibThr((uint8_t)0xFF);
702708

703-
Serial.println(F("MAX17048 configuration complete"));
709+
Serial.printf("%s\n", tempStr);
704710
}
705711

706712
//Ping an I2C device and see if it responds

0 commit comments

Comments
 (0)