Fix UWB init and range parsing

This commit is contained in:
martin 2025-08-25 19:46:51 +02:00
commit 31a75c8783
3 changed files with 164 additions and 61 deletions

View file

@ -51,30 +51,57 @@ void setup() {
showStartupScreen();
// Initialize UWB
// Initialize UWB serial
uwbSerial.begin(115200, SERIAL_8N1, IO_RXD2, IO_TXD2);
// Reset UWB module
// Hardware reset UWB module (critical for reliable operation)
pinMode(RESET_PIN, OUTPUT);
digitalWrite(RESET_PIN, HIGH);
delay(1000);
// Initialize UWB
if (!uwb.begin()) {
Serial.println("UWB initialization failed!");
return;
}
Serial.println("Configuring UWB module...");
// Configure device based on type
Serial.printf("Configuring as %s...\n", deviceConfig.deviceName);
uwb.configureDevice(deviceConfig.deviceId, deviceConfig.isAnchor);
uwb.setCapacity(deviceConfig.capacityTags, deviceConfig.capacityTimeSlot, deviceConfig.capacityExtMode);
uwb.setNetwork(NETWORK_ID);
uwb.enableReporting(true);
uwb.saveConfiguration();
delay(1000);
uwb.restartDevice();
delay(2000);
// Complete UWB setup sequence following AT command protocol
uwb.sendCommand("AT?", 2000);
delay(500);
uwb.sendCommand("AT+RESTORE", 5000);
delay(2000); // Wait for factory reset to complete
// Configure device role and parameters
String configCmd = "AT+SETCFG=" + String(deviceConfig.deviceId) + ",";
configCmd += deviceConfig.isAnchor ? "1" : "0"; // 1=Anchor, 0=Tag
configCmd += ",1,1"; // 6.8Mbps mode, range filtering enabled
uwb.sendCommand(configCmd, 2000);
delay(500);
// Set device capacity
String capacityCmd = "AT+SETCAP=" + String(deviceConfig.capacityTags) + ",";
capacityCmd += String(deviceConfig.capacityTimeSlot) + ",";
capacityCmd += String(deviceConfig.capacityExtMode);
uwb.sendCommand(capacityCmd, 2000);
delay(500);
// Enable automatic reporting and set network
uwb.sendCommand("AT+SETRPT=1", 2000);
delay(500);
uwb.sendCommand("AT+SETPAN=" + String(NETWORK_ID), 2000);
delay(500);
// Save configuration and restart
uwb.sendCommand("AT+SAVE", 2000);
delay(1000); // Wait for save to complete
uwb.sendCommand("AT+RESTART", 2000);
delay(3000); // Wait longer for restart to complete
// Verify configuration
Serial.println("Verifying configuration...");
String configResponse = uwb.sendCommand("AT+GETCFG?", 2000);
Serial.printf("Configuration verification: %s\n", configResponse.c_str());
// Additional verification commands
uwb.sendCommand("AT+GETPAN?", 2000);
uwb.sendCommand("AT+GETRPT?", 2000);
// Initialize device data
#if DEVICE_TYPE == DEVICE_ANCHOR
@ -147,7 +174,7 @@ void processUWBData() {
Serial.println("Received: " + response);
#if DEVICE_TYPE == DEVICE_ANCHOR
if (uwb.parseRangeData(response, tags, UWB_TAG_COUNT)) {
if (uwb.parseRangeData(response, tags, UWB_TAG_COUNT, true)) {
// Successfully parsed data - log active tags
for (int i = 0; i < UWB_TAG_COUNT; i++) {
if (tags[i].active && (millis() - tags[i].lastUpdate < 1000)) {
@ -157,7 +184,7 @@ void processUWBData() {
}
}
#elif DEVICE_TYPE == DEVICE_TAG
if (uwb.parseRangeData(response, anchors, MAX_ANCHORS)) {
if (uwb.parseRangeData(response, anchors, MAX_ANCHORS, false)) {
// Successfully parsed data - log active anchors
for (int i = 0; i < MAX_ANCHORS; i++) {
if (anchors[i].active && (millis() - anchors[i].lastUpdate < 1000)) {