|
2 | 2 | Base.ino |
3 | 3 | =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/ |
4 | 4 |
|
5 | | -// This function gets called when an RTCM packet passes parser check in processUart1Message() task |
6 | | -// Pass data along to NTRIP Server, or ESP-NOW radio |
7 | | -void processRTCM(uint8_t *rtcmData, uint16_t dataLength) |
| 5 | +// Enough storage for two rounds of RTCM 1074,1084,1094,1124,1005 |
| 6 | +// To help prevent the "no increase in file size" and "due to lack of RTCM" glitch: |
| 7 | +// RTCM is stored in PSRAM by the processUart1Message task and written by sendRTCMToConsumers |
| 8 | +const uint8_t rtcmConsumerBufferEntries = 16; |
| 9 | +const uint16_t rtcmConsumerBufferEntrySize = 1032; // RTCM can be up to 1024 + 6 bytes |
| 10 | +uint16_t rtcmConsumerBufferLengths[rtcmConsumerBufferEntries]; |
| 11 | +volatile uint8_t rtcmConsumerBufferHead; |
| 12 | +volatile uint8_t rtcmConsumerBufferTail; |
| 13 | +uint8_t *rtcmConsumerBufferPtr = nullptr; |
| 14 | + |
| 15 | +//---------------------------------------- |
| 16 | +// Allocate and initialize the rtcmConsumerBuffer |
| 17 | +//---------------------------------------- |
| 18 | +bool rtcmConsumerBufferAllocated() |
8 | 19 | { |
9 | | - // Give this byte to the various possible transmission methods |
10 | | - for (int x = 0; x < dataLength; x++) |
| 20 | + if (rtcmConsumerBufferPtr == nullptr) |
11 | 21 | { |
12 | | - for (int serverIndex = 0; serverIndex < NTRIP_SERVER_MAX; serverIndex++) |
13 | | - ntripServerProcessRTCM(serverIndex, rtcmData[x]); |
| 22 | + rtcmConsumerBufferPtr = (uint8_t *)rtkMalloc( |
| 23 | + (size_t)rtcmConsumerBufferEntrySize * (size_t)rtcmConsumerBufferEntries, |
| 24 | + "ntripBuffer"); |
| 25 | + for (uint8_t i = 0; i < rtcmConsumerBufferEntries; i++) |
| 26 | + rtcmConsumerBufferLengths[i] = 0; |
| 27 | + rtcmConsumerBufferHead = 0; |
| 28 | + rtcmConsumerBufferTail = 0; |
| 29 | + } |
| 30 | + if (rtcmConsumerBufferPtr == nullptr) |
| 31 | + { |
| 32 | + systemPrintln("rtcmConsumerBuffer malloc failed!"); |
| 33 | + return false; |
14 | 34 | } |
15 | 35 |
|
16 | | - for (int x = 0; x < dataLength; x++) |
17 | | - espNowProcessRTCM(rtcmData[x]); |
| 36 | + return true; |
| 37 | +} |
18 | 38 |
|
19 | | - loraProcessRTCM(rtcmData, dataLength); |
| 39 | +//---------------------------------------- |
| 40 | +// Check how many RTCM buffers contain data |
| 41 | +//---------------------------------------- |
| 42 | +uint8_t rtcmBuffersInUse() |
| 43 | +{ |
| 44 | + if (!rtcmConsumerBufferAllocated()) |
| 45 | + return 0; |
| 46 | + |
| 47 | + uint8_t buffersInUse = rtcmConsumerBufferHead - rtcmConsumerBufferTail; |
| 48 | + if (buffersInUse >= rtcmConsumerBufferEntries) // Wrap if Tail is > Head |
| 49 | + buffersInUse += rtcmConsumerBufferEntries; |
| 50 | + return buffersInUse; |
| 51 | +} |
| 52 | + |
| 53 | +//---------------------------------------- |
| 54 | +// Check if any RTCM data is available |
| 55 | +//---------------------------------------- |
| 56 | +bool rtcmDataAvailable() |
| 57 | +{ |
| 58 | + return (rtcmBuffersInUse() > 0); |
| 59 | +} |
| 60 | + |
| 61 | +//---------------------------------------- |
| 62 | +// Store each RTCM message in a PSRAM buffer |
| 63 | +// This function gets called as each complete RTCM message comes in |
| 64 | +// The messages are written to the servers by sendRTCMToConsumers |
| 65 | +//---------------------------------------- |
| 66 | +void storeRTCMForConsumers(uint8_t *rtcmData, uint16_t dataLength) |
| 67 | +{ |
| 68 | + if (!rtcmConsumerBufferAllocated()) |
| 69 | + return; |
| 70 | + |
| 71 | + // Check if a buffer is available |
| 72 | + if (rtcmBuffersInUse() < (rtcmConsumerBufferEntries - 1)) |
| 73 | + { |
| 74 | + uint8_t *dest = rtcmConsumerBufferPtr; |
| 75 | + dest += (size_t)rtcmConsumerBufferEntrySize * (size_t)rtcmConsumerBufferHead; |
| 76 | + memcpy(dest, rtcmData, dataLength); // Store the RTCM |
| 77 | + rtcmConsumerBufferLengths[rtcmConsumerBufferHead] = dataLength; // Store the length |
| 78 | + rtcmConsumerBufferHead = rtcmConsumerBufferHead + 1; // Increment the Head |
| 79 | + rtcmConsumerBufferHead = rtcmConsumerBufferHead % rtcmConsumerBufferEntries; // Wrap |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + if (settings.debugNtripServerRtcm && (!inMainMenu)) |
| 84 | + systemPrintln("rtcmConsumerBuffer full. RTCM lost"); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +//---------------------------------------- |
| 89 | +// Send the stored RTCM to consumers: ntripServer, LoRa and ESP-NOW |
| 90 | +//---------------------------------------- |
| 91 | +void sendRTCMToConsumers() |
| 92 | +{ |
| 93 | + if (!rtcmConsumerBufferAllocated()) |
| 94 | + return; |
| 95 | + |
| 96 | + while (rtcmConsumerBufferHead != rtcmConsumerBufferTail) |
| 97 | + { |
| 98 | + uint8_t *dest = rtcmConsumerBufferPtr; |
| 99 | + dest += (size_t)rtcmConsumerBufferEntrySize * (size_t)rtcmConsumerBufferTail; |
| 100 | + |
| 101 | + // NTRIP Server |
| 102 | + for (int serverIndex = 0; serverIndex < NTRIP_SERVER_MAX; serverIndex++) |
| 103 | + ntripServerSendRTCM(serverIndex, dest, rtcmConsumerBufferLengths[rtcmConsumerBufferTail]); |
| 104 | + |
| 105 | + // LoRa |
| 106 | + loraProcessRTCM(dest, rtcmConsumerBufferLengths[rtcmConsumerBufferTail]); |
| 107 | + |
| 108 | + // ESP-NOW |
| 109 | + for (uint16_t x = 0; x < rtcmConsumerBufferLengths[rtcmConsumerBufferTail]; x++) |
| 110 | + espNowProcessRTCM(dest[x]); |
| 111 | + |
| 112 | + rtcmConsumerBufferTail = rtcmConsumerBufferTail + 1; // Increment the Tail |
| 113 | + rtcmConsumerBufferTail = rtcmConsumerBufferTail % rtcmConsumerBufferEntries; // Wrap |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +//---------------------------------------- |
| 118 | +// Store data ready to be passed along to NTRIP Server, or ESP-NOW radio |
| 119 | +// This function gets called when an RTCM packet passes parser check in processUart1Message() task |
| 120 | +//---------------------------------------- |
| 121 | +void processRTCM(uint8_t *rtcmData, uint16_t dataLength) |
| 122 | +{ |
| 123 | + storeRTCMForConsumers(rtcmData, dataLength); |
20 | 124 |
|
21 | 125 | rtcmLastPacketSent = millis(); |
22 | 126 | rtcmPacketsSent++; |
|
0 commit comments