]> git.tnoah.ca Git - lora-radio.git/commitdiff
Adjusted preamble and airtime calculations
authorMark Qvist <mark@unsigned.io>
Thu, 14 Sep 2023 11:00:40 +0000 (13:00 +0200)
committerMark Qvist <mark@unsigned.io>
Thu, 14 Sep 2023 11:00:40 +0000 (13:00 +0200)
Config.h
RNode_Firmware.ino
Utilities.h

index 16b38fbb62e780b2caafaaf7f4c0be7621297f34..ccf19378527438b7c5b0d5566d76974b1b253a58 100644 (file)
--- a/Config.h
+++ b/Config.h
        // 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;
                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;
index 4b316af0d0711645e1427576cdda2a7ce6ca2de5..ae34b725606a7d4ea733beeba44f2ebae917f169 100644 (file)
@@ -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
 }
index 5c564bd07b7f19fedfbf94265344f2e0564f37e9..db022580105a2b8d553c3907a605a31df47db6a3 100644 (file)
@@ -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) {