LoRa Inter-message delay changes

This commit is contained in:
Jeff Lehman 2024-06-16 23:18:33 -05:00
parent b1c1cbc534
commit 1f455e7d43
2 changed files with 56 additions and 60 deletions

View File

@ -39,11 +39,12 @@ enum ping_t {
ping_reply
};
enum pingstate_t {
enum commstate_t {
stReady,
stInProcess,
stCrcMismatch,
stCrcMatch,
stInterMessageDelay,
stCompleted
};
@ -103,7 +104,7 @@ uint size;
};
struct Ping {
pingstate_t status = stReady;
commstate_t status = stReady;
unsigned long start;
uint timeout;
uint16_t address;

View File

@ -8,7 +8,7 @@
#define GLOBAL_LORA_RETRIES 2 // LoRa ACK automatic retries [0 - 3]
#define GLOBAL_LORA_TXPWR 17 // LoRa TX power in dBm (: +2dBm - +17dBm (for SX1276-7) +20dBm (for SX1278))
#define TXDELAYMS 300
#define INTERMSGDELAY 100
#define SPBUFFSIZE 10
#define LORASIZE ((250 - 15) / sizeof(DataReading)) // 250 bytes minus header/crc data
#define DRBUFFSIZE 100
@ -112,8 +112,8 @@ DRRingBuffer drBuff = {.dr = (DataReading*)calloc(DRBUFFSIZE,sizeof(DataReading)
SPRingBuffer spBuff = {.sp = (SystemPacket*)calloc(SPBUFFSIZE,sizeof(SystemPacket)), \
.address = (uint16_t*)calloc(SPBUFFSIZE,sizeof(uint16_t)), .startIdx = 0, .endIdx = 0, .size = SPBUFFSIZE};
int loraTxState = stReady;
int loraAckState = stReady;
commstate_t loraTxState = stReady;
commstate_t loraAckState = stReady;
#ifdef FDRS_GATEWAY
@ -145,6 +145,7 @@ unsigned long rxCountCrcOk = 0; // Number of total Lora packets with
unsigned long rxCountCrcBad = 0; // Number of total Lora packets with valid CRC
unsigned long txCountDR = 0; // Number of total LoRa DR packets transmitted
unsigned long txCountSP = 0; // Number of total LoRa SP packets transmitted
unsigned long lastTxComplete = 0; // Last time that a LoRa transmit was completed
extern time_t now;
time_t netTimeOffset = UINT32_MAX; // One direction of LoRa Ping time in units of seconds (1/2 full ping time)
unsigned long tx_start_time = 0;
@ -832,8 +833,8 @@ void handleLoRa()
static uint8_t len = 0;
static DataReading *data;
static uint16_t address;
static unsigned long lastTxtime = 0;
static unsigned long statsTime = 0;
static unsigned long lastTimeSourcePing = 0;
// check status of TX or RX operation
LoRaTxRxOperation();
@ -900,9 +901,6 @@ void handleLoRa()
// resend original packet again if retries are available
loraAckState = stReady;
}
if(loraTxState == stCompleted) {
loraTxState = stReady;
}
return;
}
@ -919,9 +917,6 @@ void handleLoRa()
BUFFINCSTART(spBuff);
}
// Time delay between consecutive transmits - TXDELAYMS
if(TDIFF(lastTxtime,(TXDELAYMS + random(0,50)))) {
// Start Transmit data from the DataReading queue
if((!ISBUFFEMPTY(drBuff) || (data != NULL)) && loraTxState == stReady && loraAckState == stReady)
{
@ -963,17 +958,12 @@ void handleLoRa()
}
// Ping LoRa time master to estimate time delay in radio link
if(timeSource.tmNetIf == TMIF_LORA) {
static unsigned long lastTimeSourcePing = 0;
// ping the time source every 10 minutes
if(TDIFFMIN(lastTimeSourcePing,10) || lastTimeSourcePing == 0) {
if(timeSource.tmNetIf == TMIF_LORA && (TDIFFMIN(lastTimeSourcePing,10) || lastTimeSourcePing == 0)) {
pingRequestLoRa(timeSource.tmAddress,4000);
lastTimeSourcePing = millis();
}
}
lastTxtime = millis();
}
// Print LoRa statistics
if(TDIFFSEC(statsTime,305) && (rxCountCrcOk + rxCountCrcBad) > 0) {
statsTime = millis();
@ -981,7 +971,12 @@ void handleLoRa()
}
// Change to ready at the end so only one transmit happens per function call
// also enforce LoRa inter-message transmit delays
if(loraTxState == stCompleted) {
loraTxState = stInterMessageDelay;
lastTxComplete = millis();
}
else if(loraTxState == stInterMessageDelay && TDIFF(lastTxComplete,(INTERMSGDELAY + random(0,50)))) {
loraTxState = stReady;
}
return;