Adjusted preamble and airtime calculations

This commit is contained in:
Mark Qvist 2023-09-14 13:00:40 +02:00
parent eb7b5c6c26
commit 9fd379486d
3 changed files with 27 additions and 13 deletions

View File

@ -290,18 +290,23 @@
// MCU independent configuration parameters // MCU independent configuration parameters
const long serial_baudrate = 115200; const long serial_baudrate = 115200;
const int lora_rx_turnaround_ms = 50; const int lora_rx_turnaround_ms = 50;
const int csma_slot_ms = lora_rx_turnaround_ms;
// SX1276 RSSI offset to get dBm value from // SX1276 RSSI offset to get dBm value from
// packet RSSI register // packet RSSI register
const int rssi_offset = 157; const int rssi_offset = 157;
// Default LoRa settings // Default LoRa settings
int lora_sf = 0; int lora_sf = 0;
int lora_cr = 5; int lora_cr = 5;
int lora_txp = 0xFF; int lora_txp = 0xFF;
uint32_t lora_bw = 0; uint32_t lora_bw = 0;
uint32_t lora_freq = 0; uint32_t lora_freq = 0;
uint32_t lora_bitrate = 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 // Operational variables
bool radio_locked = true; bool radio_locked = true;
@ -356,7 +361,6 @@
float longterm_channel_util = 0.0; float longterm_channel_util = 0.0;
float airtime = 0.0; float airtime = 0.0;
float longterm_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 #define current_airtime_bin(void) (millis()%AIRTIME_LONGTERM_MS)/AIRTIME_BINLEN_MS
#endif #endif
float st_airtime_limit = 0.0; float st_airtime_limit = 0.0;

View File

@ -318,6 +318,7 @@ bool startRadio() {
setBandwidth(); setBandwidth();
setSpreadingFactor(); setSpreadingFactor();
setCodingRate(); setCodingRate();
setPreamble();
getFrequency(); getFrequency();
LoRa.enableCrc(); LoRa.enableCrc();
@ -403,12 +404,17 @@ void flushQueue(void) {
queue_flushing = false; queue_flushing = false;
} }
#define PHY_HEADER_LORA_SYMBOLS 8
void add_airtime(uint16_t written) { void add_airtime(uint16_t written) {
#if MCU_VARIANT == MCU_ESP32 #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 cb = current_airtime_bin();
uint16_t nb = cb+1; if (nb == AIRTIME_BINS) { nb = 0; } 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; airtime_bins[nb] = 0;
#endif #endif
} }

View File

@ -908,11 +908,10 @@ inline uint8_t packetSequence(uint8_t header) {
void updateBitrate() { void updateBitrate() {
#if MCU_VARIANT == MCU_ESP32 #if MCU_VARIANT == MCU_ESP32
if (radio_online) { if (radio_online) {
// self.bitrate = self.r_sf * ( (4.0/self.r_cr) / (math.pow(2,self.r_sf)/(self.r_bandwidth/1000)) ) * 1000 lora_symbol_rate = (float)lora_bw/(float)(pow(2, lora_sf));
// self.bitrate_kbps = round(self.bitrate/1000.0, 2) 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); 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 { } else {
lora_bitrate = 0; lora_bitrate = 0;
} }
@ -929,6 +928,11 @@ void setCodingRate() {
updateBitrate(); updateBitrate();
} }
void setPreamble() {
if (radio_online) LoRa.setPreambleLength(lora_preamble_symbols);
updateBitrate();
}
void set_implicit_length(uint8_t len) { void set_implicit_length(uint8_t len) {
implicit_l = len; implicit_l = len;
if (implicit_l != 0) { if (implicit_l != 0) {