tarask
6 days ago 532005c6573d95199ce0ffbc33df4c7a0a4c3ef9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "modules/my_altimeter/NRA15.h"
#include "modules/my_task_creator/TaskCreator.h"
 
// Protocol constants
#define START_SEQUENCE 0xAA  // same for L and H bit
#define END_SEQUENCE 0x55    // same for L and H bit
 
// Message IDs
#define MSG_TARGET_INFO 0x070C
 
// Initializing the static variables
double NRA15::distance = 0.0;
Stream* NRA15::serial = nullptr;
 
#ifndef NRA15_USE_HARDWARE_SERIAL
    SoftwareSerial* NRA15::softSerial = nullptr;
#endif
 
// Destructor
NRA15::~NRA15() {
#ifndef NRA15_USE_HARDWARE_SERIAL
    if (softSerial) {
        delete softSerial;
        softSerial = nullptr;
    }
#endif
}
 
void NRA15::NRA15RxTask(void *pvParameters)
{
    log_i("NRA15 Task Started"); // Logging task start
    
    // Initialize Serial (Hardware or Software)
#ifdef NRA15_USE_HARDWARE_SERIAL
    // Using HardwareSerial (Serial0)
    Serial.begin(115200, SERIAL_8N1, NRA15_RX_PIN, NRA15_TX_PIN);
    serial = &Serial;
    log_i("NRA15: Using HardwareSerial @ 115200 on GPIO%d/%d", NRA15_RX_PIN, NRA15_TX_PIN);
#else
    // Using SoftwareSerial
    softSerial = new SoftwareSerial(NRA15_RX_PIN, NRA15_TX_PIN);
    softSerial->begin(115200);
    serial = softSerial;
    log_i("NRA15: Using SoftwareSerial @ 115200 on GPIO%d/%d", NRA15_RX_PIN, NRA15_TX_PIN);
#endif
    
    // Getting TaskCreator object
    TaskCreator *mCreator = (TaskCreator *)pvParameters;
    NRA15 *obj = &(mCreator->mNRA15); // Getting NRA15 object from TaskCreator
    MavlinkTransmitter *transmitter = &(mCreator->mTransmitter);
    
    vTaskDelay(50); // Delay to allow initialization
    
    uint32_t lastMavlinkTime = xTaskGetTickCount();
    uint32_t lastValidDataTime = xTaskGetTickCount();
    
    // Local variables for packet assembly
    uint8_t buffer[14]; // Setting buffer size for UART communication
    uint8_t bufferIndex = 0;
    bool isValid = false;
    
    for (;;)
    {
        // Read data from Serial 
        while (serial->available()) {
            uint8_t byte = serial->read();
            
            if (bufferIndex < 2) {
                if (byte == START_SEQUENCE) {
                    buffer[bufferIndex++] = byte;
                } else {
                    bufferIndex = 0; // Reset
                }
            }
            else if (bufferIndex >= 2 && bufferIndex < 14) {
                buffer[bufferIndex++] = byte;
                
                // Complete packet received
                if (bufferIndex == 14) {
                    // Validate end sequence
                    if (buffer[12] == END_SEQUENCE && buffer[13] == END_SEQUENCE) {
                        // Get Message ID
                        uint16_t msgId = buffer[2] | (buffer[3] << 8);
                        
                        if (msgId == MSG_TARGET_INFO) {
                            // Parse target information
                            uint8_t rangeH = buffer[6];
                            uint8_t rangeL = buffer[7];
                            
                            // Calculate distance in meters
                            obj->distance = (rangeH * 256 + rangeL) * 0.01;
                            isValid = true;
                            lastValidDataTime = xTaskGetTickCount();
                            log_v("NRA15: %.2fm", obj->distance);
                        }
                    } else {
                        log_v("NRA15: Invalid end sequence");
                    }
                    
                    bufferIndex = 0; // Reset for next packet
                }
            }
        }
        
        
        uint32_t currentTime = xTaskGetTickCount();
 
        //Timeout check
        if ((currentTime - lastMavlinkTime) > pdMS_TO_TICKS(500)) {
            if (isValid) {
                isValid = false;
                log_w("NRA15: no target (timeout)");
            }
        }
        
        // Send MAVLink distance sensor message every 100ms
        if ((currentTime - lastMavlinkTime) >= pdMS_TO_TICKS(100)) {
            transmitter->SendDistanceSensor(
                obj->distance,
                isValid, 
                (float)RANGEFINDER_NRA15_MAX_DISTANCE
            );
            lastMavlinkTime = currentTime;
        }
        
        vTaskDelay(pdMS_TO_TICKS(10)); // 10ms delay
    }
}