From 9fd379486d980cde19ccc79937e99db8d865ae47 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Thu, 14 Sep 2023 13:00:40 +0200 Subject: [PATCH] Adjusted preamble and airtime calculations --- Config.h | 18 +++++++++++------- RNode_Firmware.ino | 10 ++++++++-- Utilities.h | 12 ++++++++---- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/Config.h b/Config.h index 16b38fb..ccf1937 100644 --- a/Config.h +++ b/Config.h @@ -290,18 +290,23 @@ // MCU independent configuration parameters const long serial_baudrate = 115200; const int lora_rx_turnaround_ms = 50; + const int csma_slot_ms = lora_rx_turnaround_ms; // SX1276 RSSI offset to get dBm value from // packet RSSI register const int rssi_offset = 157; // Default LoRa settings - int lora_sf = 0; - int lora_cr = 5; - int lora_txp = 0xFF; - uint32_t lora_bw = 0; - uint32_t lora_freq = 0; - uint32_t lora_bitrate = 0; + int lora_sf = 0; + int lora_cr = 5; + int lora_txp = 0xFF; + uint32_t lora_bw = 0; + uint32_t lora_freq = 0; + uint32_t lora_bitrate = 0; + long lora_preamble_symbols = 6; + float lora_symbol_time_ms = 0.0; + float lora_symbol_rate = 0.0; + float lora_us_per_byte = 0.0; // Operational variables bool radio_locked = true; @@ -356,7 +361,6 @@ float longterm_channel_util = 0.0; float airtime = 0.0; float longterm_airtime = 0.0; - float us_per_byte = 0.0; #define current_airtime_bin(void) (millis()%AIRTIME_LONGTERM_MS)/AIRTIME_BINLEN_MS #endif float st_airtime_limit = 0.0; diff --git a/RNode_Firmware.ino b/RNode_Firmware.ino index 4b316af..ae34b72 100644 --- a/RNode_Firmware.ino +++ b/RNode_Firmware.ino @@ -318,6 +318,7 @@ bool startRadio() { setBandwidth(); setSpreadingFactor(); setCodingRate(); + setPreamble(); getFrequency(); LoRa.enableCrc(); @@ -403,12 +404,17 @@ void flushQueue(void) { queue_flushing = false; } +#define PHY_HEADER_LORA_SYMBOLS 8 void add_airtime(uint16_t written) { #if MCU_VARIANT == MCU_ESP32 - float ms_cost = ((float)written * us_per_byte)/1000.0; + float packet_cost_ms = 0.0; + float payload_cost_ms = ((float)written * lora_us_per_byte)/1000.0; + packet_cost_ms += payload_cost_ms; + packet_cost_ms += (lora_preamble_symbols+4.25)*lora_symbol_time_ms; + packet_cost_ms += PHY_HEADER_LORA_SYMBOLS * lora_symbol_time_ms; uint16_t cb = current_airtime_bin(); uint16_t nb = cb+1; if (nb == AIRTIME_BINS) { nb = 0; } - airtime_bins[cb] += ms_cost; + airtime_bins[cb] += packet_cost_ms; airtime_bins[nb] = 0; #endif } diff --git a/Utilities.h b/Utilities.h index 5c564bd..db02258 100644 --- a/Utilities.h +++ b/Utilities.h @@ -908,11 +908,10 @@ inline uint8_t packetSequence(uint8_t header) { void updateBitrate() { #if MCU_VARIANT == MCU_ESP32 if (radio_online) { - // self.bitrate = self.r_sf * ( (4.0/self.r_cr) / (math.pow(2,self.r_sf)/(self.r_bandwidth/1000)) ) * 1000 - // self.bitrate_kbps = round(self.bitrate/1000.0, 2) + lora_symbol_rate = (float)lora_bw/(float)(pow(2, lora_sf)); + lora_symbol_time_ms = (1.0/lora_symbol_rate)*1000.0; lora_bitrate = (uint32_t)(lora_sf * ( (4.0/(float)lora_cr) / ((float)(pow(2, lora_sf))/((float)lora_bw/1000.0)) ) * 1000.0); - us_per_byte = 1000000.0/((float)lora_bitrate/8.0); - + lora_us_per_byte = 1000000.0/((float)lora_bitrate/8.0); } else { lora_bitrate = 0; } @@ -929,6 +928,11 @@ void setCodingRate() { updateBitrate(); } +void setPreamble() { + if (radio_online) LoRa.setPreambleLength(lora_preamble_symbols); + updateBitrate(); +} + void set_implicit_length(uint8_t len) { implicit_l = len; if (implicit_l != 0) {