add another crc to make sure we don't get corrupt data

This commit is contained in:
2025-04-27 02:23:21 +02:00
parent 3457b76938
commit 0c71409c30
3 changed files with 84 additions and 62 deletions

View File

@@ -15,80 +15,83 @@
typedef struct __attribute__((packed)) typedef struct __attribute__((packed))
{ {
char syncPhrase[10]; char syncPhrase[10]; //10
uint32_t packetIndex; uint32_t packetIndex; //14
uint8_t packetType; uint8_t packetType; //15
uint32_t missionTimer; uint32_t missionTimer; //19
uint32_t CRCCheck;
} DownBoundPacket; } DownBoundPacket;
typedef struct __attribute__((packed)) typedef struct __attribute__((packed))
{ {
// MPU data // MPU data
int16_t accelerationX; int16_t accelerationX;//21
int16_t accelerationY; int16_t accelerationY; //23
int16_t accelerationZ; int16_t accelerationZ;//25
int16_t gyroX; int16_t gyroX;//27
int16_t gyroY; int16_t gyroY;//29
int16_t gyroZ; int16_t gyroZ;//31
int16_t magnetX; int16_t magnetX;//33
int16_t magnetY; int16_t magnetY;//35
int16_t magnetZ; int16_t magnetZ;//37
int16_t accelerometer_temperature; int16_t accelerometer_temperature;//39
// CCS data // CCS data
uint16_t eCO2; uint16_t eCO2;//41
uint16_t tvoc; uint16_t tvoc;//43
uint8_t currentCCS; uint8_t currentCCS;//44
uint16_t rawCCSData; uint16_t rawCCSData;//46
// INA data // INA data
uint16_t volts; uint16_t volts;//48
uint16_t current; uint16_t current;//50
uint16_t power; uint16_t power;//52
// BME DATA // BME DATA
uint32_t temperature; uint32_t temperature;//56
uint16_t humidity; uint16_t humidity;//58
uint32_t pressure; uint32_t pressure;//62
uint16_t gas; uint16_t gas;//64
bool gas_valid; bool gas_valid;//later
bool heater_stable; bool heater_stable;//later
uint8_t gas_range; uint8_t gas_range;//65
uint8_t gas_index; uint8_t gas_index;//66
float air_temperature; /*!< air temperature in degrees celsius */ float air_temperature; /*!< air temperature in degrees celsius */ //70
float relative_humidity; /*!< relative humidity in percent */ float relative_humidity; /*!< relative humidity in percent */ //74
float barometric_pressure; /*!< barometric pressure in hecto-pascal */ float barometric_pressure; /*!< barometric pressure in hecto-pascal */ //78
float gas_resistance; /*!< gas resistance in ohms */ float gas_resistance; /*!< gas resistance in ohms */ //82
uint16_t iaq_score; /*!< air quality index (0..500) */ uint16_t iaq_score; /*!< air quality index (0..500) */ //84
float temperature_score; float temperature_score; //88
float humidity_score; float humidity_score; //92
float gas_score; float gas_score; //96
// GPS DATA // GPS DATA
uint32_t time_seconds; // Seconds since start of day uint32_t time_seconds; // Seconds since start of day //100
int32_t latitude_centi_degrees; // Latitude * 10,000 int32_t latitude_centi_degrees; // Latitude * 10,000 //104
int32_t longitude_centi_degrees; // Longitude * 10,000 int32_t longitude_centi_degrees; // Longitude * 10,000 //108
int16_t altitude_centi_meters; // Altitude * 100 int16_t altitude_centi_meters; // Altitude * 100 //110
uint8_t fix_quality; uint8_t fix_quality; //111
uint8_t num_satellites; uint8_t num_satellites; //112
uint16_t date_yyddmm; // YYDDMM (from GPRMC) uint16_t date_yyddmm; // YYDDMM (from GPRMC) //114
uint16_t speed_centi_knots; // Speed * 100 (from GPRMC) uint16_t speed_centi_knots; // Speed * 100 (from GPRMC) //116
int32_t predicted_latitude_centi_degrees; // Latitude * 10,000 int32_t predicted_latitude_centi_degrees; // Latitude * 10,000 //120
int32_t predicted_longitude_centi_degrees; // Longitude * 10,000 int32_t predicted_longitude_centi_degrees; // Longitude * 10,000 //124
int16_t predicted_altitude_centi_meters; // Altitude * 100 int16_t predicted_altitude_centi_meters; // Altitude * 100 //126
// ADC DATA // ADC DATA
int32_t NH3; int32_t NH3; //130
int32_t CO; int32_t CO; //134
int32_t NO2; int32_t NO2; //138
int32_t UVC; int32_t UVC; //142
int16_t currentServoA; int16_t currentServoA; //144
int16_t targetServoA; int16_t targetServoA; //146
int16_t currentServoB; int16_t currentServoB; //148
int16_t targetServoB; int16_t targetServoB; //150
uint8_t telemetryIndex; //151
} TelemetryPacket; } TelemetryPacket;
@@ -97,6 +100,7 @@ typedef struct __attribute__((packed))
char syncPhrase[10]; char syncPhrase[10];
uint32_t packetIndex; uint32_t packetIndex;
uint8_t packetType; uint8_t packetType;
uint32_t CRCCheck;
} UplinkPacket; } UplinkPacket;
typedef struct __attribute__((packed)) typedef struct __attribute__((packed))

View File

@@ -44,7 +44,7 @@ void setup_lora(void)
} }
} }
uint8_t spreadingFactor = 6; uint8_t spreadingFactor = 7;
uint8_t bandwidth = SX126X_LORA_BW_250_0; uint8_t bandwidth = SX126X_LORA_BW_250_0;
uint8_t codingRate = SX126X_LORA_CR_4_8; uint8_t codingRate = SX126X_LORA_CR_4_8;
uint16_t preambleLength = 8; uint16_t preambleLength = 8;
@@ -54,13 +54,14 @@ void setup_lora(void)
LoRaConfig(spreadingFactor, bandwidth, codingRate, preambleLength, 0, crcOn, invertIrq); LoRaConfig(spreadingFactor, bandwidth, codingRate, preambleLength, 0, crcOn, invertIrq);
} }
static void prepare_downbound_packet(DownBoundPacket *packet, uint8_t type, uint64_t missionTimer) static void prepare_downbound_packet(DownBoundPacket *packet, uint8_t type, uint64_t missionTimer, uint32_t crc)
{ {
memset(packet, 0, sizeof(DownBoundPacket)); memset(packet, 0, sizeof(DownBoundPacket));
packet->missionTimer = missionTimer; packet->missionTimer = missionTimer;
packet->packetIndex = packetIndexTX++; packet->packetIndex = packetIndexTX++;
ESP_LOGI(TAG_RADIO, "Sending downbound packet with index %ld", packetIndexTX - 1); ESP_LOGI(TAG_RADIO, "Sending downbound packet with index %ld", packetIndexTX - 1);
packet->packetType = type; packet->packetType = type;
packet->CRCCheck = crc;
strcpy(packet->syncPhrase, "PlechDole"); strcpy(packet->syncPhrase, "PlechDole");
} }
@@ -106,7 +107,8 @@ void prepare_and_send_telemetry(uint64_t missionTimer)
uint8_t bufOut[256] = {0}; uint8_t bufOut[256] = {0};
DownBoundPacket downboundPacket; DownBoundPacket downboundPacket;
prepare_downbound_packet(&downboundPacket, DownlinkPacketType_Telemetry, missionTimer); uint32_t crc = esp_rom_crc32_le(0,(uint8_t *) &telemetryPacket, sizeof(telemetryPacket));
prepare_downbound_packet(&downboundPacket, DownlinkPacketType_Telemetry, missionTimer, crc);
uint16_t offset = 0; uint16_t offset = 0;
memcpy(bufOut + offset, &downboundPacket, sizeof(downboundPacket)); memcpy(bufOut + offset, &downboundPacket, sizeof(downboundPacket));
@@ -122,14 +124,17 @@ static void build_and_send_ack(uint32_t ackIndex, uint32_t crc32Checksum, uint64
{ {
uint8_t bufOut[256] = {0}; uint8_t bufOut[256] = {0};
DownBoundPacket downboundPacket;
prepare_downbound_packet(&downboundPacket, DownlinkPacketType_ACK, missionTimer);
ACKPacket ackPacket = { ACKPacket ackPacket = {
.packetIndex = ackIndex, .packetIndex = ackIndex,
.crc32Checksum = crc32Checksum, .crc32Checksum = crc32Checksum,
}; };
uint32_t crc = esp_rom_crc32_le(0, (uint8_t *) &ackPacket, sizeof(ackPacket));
DownBoundPacket downboundPacket;
prepare_downbound_packet(&downboundPacket, DownlinkPacketType_ACK, missionTimer, crc);
uint16_t offset = 0; uint16_t offset = 0;
memcpy(bufOut + offset, &downboundPacket, sizeof(downboundPacket)); memcpy(bufOut + offset, &downboundPacket, sizeof(downboundPacket));
offset += sizeof(downboundPacket); offset += sizeof(downboundPacket);
@@ -169,8 +174,17 @@ void process_uplink_packet(uint8_t *data, uint8_t len, uint64_t missionTimer)
ESP_LOGI(TAG, "Got uplink packet of type %d, index %d", uplinkPacket.packetType, uplinkPacket.packetIndex); ESP_LOGI(TAG, "Got uplink packet of type %d, index %d", uplinkPacket.packetType, uplinkPacket.packetIndex);
uint8_t *payload = data + sizeof(UplinkPacket);
uint8_t payloadRXLen = len - sizeof(UplinkPacket); uint8_t payloadRXLen = len - sizeof(UplinkPacket);
uint32_t crc = esp_rom_crc32_le(0, payload, payloadRXLen);
if (crc != uplinkPacket.CRCCheck) {
ESP_LOGE(TAG, "Received BAD CRC for packet %d, crc is %ld, should be %ld", uplinkPacket.packetIndex, crc, uplinkPacket.CRCCheck);
return;
}
if (uplinkPacket.packetType == UplinkPacketType_ACK) if (uplinkPacket.packetType == UplinkPacketType_ACK)
{ {
ESP_LOGI(TAG, "Received ACK for packet %d", uplinkPacket.packetIndex); ESP_LOGI(TAG, "Received ACK for packet %d", uplinkPacket.packetIndex);

View File

@@ -8,6 +8,8 @@
static uint8_t s_led_state = 0; static uint8_t s_led_state = 0;
uint8_t telemetryIndex = 1;
uint8_t foundDevices[128]; uint8_t foundDevices[128];
uint8_t prevDevices[128]; uint8_t prevDevices[128];
@@ -261,6 +263,8 @@ void i2c_sensors_task(void *pvParameters)
telemetryPacket.currentServoA = servoState.currentServoA; telemetryPacket.currentServoA = servoState.currentServoA;
telemetryPacket.currentServoB = servoState.currentServoB; telemetryPacket.currentServoB = servoState.currentServoB;
telemetryPacket.telemetryIndex = telemetryIndex++;
} }
packetReadiness = 1; packetReadiness = 1;