From 0c71409c30477498aa628f99291a5206096509a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ryb=C3=A1rsky?= Date: Sun, 27 Apr 2025 02:23:21 +0200 Subject: [PATCH] add another crc to make sure we don't get corrupt data --- main/components/packets.h | 116 ++++++++++++++++++++------------------ main/components/radio.c | 26 +++++++-- main/components/sensors.c | 4 ++ 3 files changed, 84 insertions(+), 62 deletions(-) diff --git a/main/components/packets.h b/main/components/packets.h index f027309..748b8be 100644 --- a/main/components/packets.h +++ b/main/components/packets.h @@ -15,80 +15,83 @@ typedef struct __attribute__((packed)) { - char syncPhrase[10]; - uint32_t packetIndex; - uint8_t packetType; - uint32_t missionTimer; + char syncPhrase[10]; //10 + uint32_t packetIndex; //14 + uint8_t packetType; //15 + uint32_t missionTimer; //19 + uint32_t CRCCheck; } DownBoundPacket; typedef struct __attribute__((packed)) { // MPU data - int16_t accelerationX; - int16_t accelerationY; - int16_t accelerationZ; - int16_t gyroX; - int16_t gyroY; - int16_t gyroZ; - int16_t magnetX; - int16_t magnetY; - int16_t magnetZ; - int16_t accelerometer_temperature; + int16_t accelerationX;//21 + int16_t accelerationY; //23 + int16_t accelerationZ;//25 + int16_t gyroX;//27 + int16_t gyroY;//29 + int16_t gyroZ;//31 + int16_t magnetX;//33 + int16_t magnetY;//35 + int16_t magnetZ;//37 + int16_t accelerometer_temperature;//39 // CCS data - uint16_t eCO2; - uint16_t tvoc; - uint8_t currentCCS; - uint16_t rawCCSData; + uint16_t eCO2;//41 + uint16_t tvoc;//43 + uint8_t currentCCS;//44 + uint16_t rawCCSData;//46 // INA data - uint16_t volts; - uint16_t current; - uint16_t power; + uint16_t volts;//48 + uint16_t current;//50 + uint16_t power;//52 // BME DATA - uint32_t temperature; - uint16_t humidity; - uint32_t pressure; - uint16_t gas; - bool gas_valid; - bool heater_stable; - uint8_t gas_range; - uint8_t gas_index; + uint32_t temperature;//56 + uint16_t humidity;//58 + uint32_t pressure;//62 + uint16_t gas;//64 + bool gas_valid;//later + bool heater_stable;//later + uint8_t gas_range;//65 + uint8_t gas_index;//66 - float air_temperature; /*!< air temperature in degrees celsius */ - float relative_humidity; /*!< relative humidity in percent */ - float barometric_pressure; /*!< barometric pressure in hecto-pascal */ - float gas_resistance; /*!< gas resistance in ohms */ - uint16_t iaq_score; /*!< air quality index (0..500) */ - float temperature_score; - float humidity_score; - float gas_score; + float air_temperature; /*!< air temperature in degrees celsius */ //70 + float relative_humidity; /*!< relative humidity in percent */ //74 + float barometric_pressure; /*!< barometric pressure in hecto-pascal */ //78 + float gas_resistance; /*!< gas resistance in ohms */ //82 + uint16_t iaq_score; /*!< air quality index (0..500) */ //84 + float temperature_score; //88 + float humidity_score; //92 + float gas_score; //96 // GPS DATA - uint32_t time_seconds; // Seconds since start of day - int32_t latitude_centi_degrees; // Latitude * 10,000 - int32_t longitude_centi_degrees; // Longitude * 10,000 - int16_t altitude_centi_meters; // Altitude * 100 - uint8_t fix_quality; - uint8_t num_satellites; - uint16_t date_yyddmm; // YYDDMM (from GPRMC) - uint16_t speed_centi_knots; // Speed * 100 (from GPRMC) + uint32_t time_seconds; // Seconds since start of day //100 + int32_t latitude_centi_degrees; // Latitude * 10,000 //104 + int32_t longitude_centi_degrees; // Longitude * 10,000 //108 + int16_t altitude_centi_meters; // Altitude * 100 //110 + uint8_t fix_quality; //111 + uint8_t num_satellites; //112 + uint16_t date_yyddmm; // YYDDMM (from GPRMC) //114 + uint16_t speed_centi_knots; // Speed * 100 (from GPRMC) //116 - int32_t predicted_latitude_centi_degrees; // Latitude * 10,000 - int32_t predicted_longitude_centi_degrees; // Longitude * 10,000 - int16_t predicted_altitude_centi_meters; // Altitude * 100 + int32_t predicted_latitude_centi_degrees; // Latitude * 10,000 //120 + int32_t predicted_longitude_centi_degrees; // Longitude * 10,000 //124 + int16_t predicted_altitude_centi_meters; // Altitude * 100 //126 // ADC DATA - int32_t NH3; - int32_t CO; - int32_t NO2; - int32_t UVC; + int32_t NH3; //130 + int32_t CO; //134 + int32_t NO2; //138 + int32_t UVC; //142 - int16_t currentServoA; - int16_t targetServoA; - int16_t currentServoB; - int16_t targetServoB; + int16_t currentServoA; //144 + int16_t targetServoA; //146 + int16_t currentServoB; //148 + int16_t targetServoB; //150 + + uint8_t telemetryIndex; //151 } TelemetryPacket; @@ -97,6 +100,7 @@ typedef struct __attribute__((packed)) char syncPhrase[10]; uint32_t packetIndex; uint8_t packetType; + uint32_t CRCCheck; } UplinkPacket; typedef struct __attribute__((packed)) diff --git a/main/components/radio.c b/main/components/radio.c index b32816e..3ee901c 100644 --- a/main/components/radio.c +++ b/main/components/radio.c @@ -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 codingRate = SX126X_LORA_CR_4_8; uint16_t preambleLength = 8; @@ -54,13 +54,14 @@ void setup_lora(void) 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)); packet->missionTimer = missionTimer; packet->packetIndex = packetIndexTX++; ESP_LOGI(TAG_RADIO, "Sending downbound packet with index %ld", packetIndexTX - 1); packet->packetType = type; + packet->CRCCheck = crc; strcpy(packet->syncPhrase, "PlechDole"); } @@ -106,7 +107,8 @@ void prepare_and_send_telemetry(uint64_t missionTimer) uint8_t bufOut[256] = {0}; 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; 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}; - DownBoundPacket downboundPacket; - prepare_downbound_packet(&downboundPacket, DownlinkPacketType_ACK, missionTimer); - + ACKPacket ackPacket = { .packetIndex = ackIndex, .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; memcpy(bufOut + offset, &downboundPacket, 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); + uint8_t *payload = data + 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) { ESP_LOGI(TAG, "Received ACK for packet %d", uplinkPacket.packetIndex); diff --git a/main/components/sensors.c b/main/components/sensors.c index 26aac80..71d966d 100644 --- a/main/components/sensors.c +++ b/main/components/sensors.c @@ -8,6 +8,8 @@ static uint8_t s_led_state = 0; +uint8_t telemetryIndex = 1; + uint8_t foundDevices[128]; uint8_t prevDevices[128]; @@ -261,6 +263,8 @@ void i2c_sensors_task(void *pvParameters) telemetryPacket.currentServoA = servoState.currentServoA; telemetryPacket.currentServoB = servoState.currentServoB; + telemetryPacket.telemetryIndex = telemetryIndex++; + } packetReadiness = 1;