Hello everyone,
I am trying to connect an LD19 Lidar with arduino. Following the record trace I understand that the record header consists of 0x54+0x2c.
I have done many tests to try to get the record in the correct format but without success.
The sequence 0x54+0x2c. is rarely detected even when reading the bytes one at a time or inserting pauses or buffers.
One version of the tests done is this:
#define BUFFER_SIZE 70 // Double the size of the packet to handle partial buffer
const byte HEADER = 0x54;
const byte VER_LEN = 0x2C;
const int POINTS_PER_PACK = 12;
const int PACKET_LENGTH = 35; // Length of a packet
struct LidarPoint {
uint8_t distance;
uint8_t intensity;
};
struct LiDARFrame {
uint8_t header;
uint8_t ver_len;
uint16_t speed; // 2 bytes
uint16_t start_angle; // 2 bytes
LidarPoint points[POINTS_PER_PACK];
uint16_t end_angle; // 2 bytes
uint16_t timestamp; // 2 bytes
uint8_t crc8;
};
char buffer[BUFFER_SIZE];
int bufferIndex = 0;
void setup() {
Serial2.begin(240300); // Initialize Serial2 at 240300 baud
Serial.begin(115200); // Initialize USB serial for debugging
Serial.println("Initialization completed");
}
void loop() {
while (Serial2.available() > 0) {
int receivedByte = Serial2.read();
if (receivedByte != -1) {
// Add the byte to the buffer if there is space
if (bufferIndex < BUFFER_SIZE) {
buffer[bufferIndex++] = (char)receivedByte;
} else {
// If the buffer is full, shift the data to make space
memmove(buffer, buffer + 1, BUFFER_SIZE - 1);
buffer[BUFFER_SIZE - 1] = (char)receivedByte;
}
// Search for the header and ver_len byte in the buffer
if (bufferIndex >= 2 && buffer[bufferIndex - 2] == HEADER && buffer[bufferIndex - 1] == VER_LEN) {
// If found, read the complete packet
if (bufferIndex >= PACKET_LENGTH) {
LiDARFrame frame;
memcpy(&frame, buffer + bufferIndex - PACKET_LENGTH, PACKET_LENGTH);
// Print packet data
Serial.print("Speed: ");
Serial.println(frame.speed);
Serial.print("Start Angle: ");
Serial.println(frame.start_angle);
Serial.print("End Angle: ");
Serial.println(frame.end_angle);
Serial.print("Timestamp: ");
Serial.println(frame.timestamp);
for (int i = 0; i < POINTS_PER_PACK; i++) {
Serial.print("Point ");
Serial.print(i + 1);
Serial.print(" Distance: ");
Serial.print(frame.points[i].distance);
Serial.print(" mm, Intensity: ");
Serial.println(frame.points[i].intensity);
}
// Reset buffer
bufferIndex = 0;
}
}
}
}
//delay(10);
}
At the moment I have eliminated the CRC handling because it always comes back incorrect (will that mean something?).
Can anyone confirm the correct record structure or have a working example?
The lidar has the PWM pin grounded.
Many thanks in advance.
Translated with DeepL.com (free version)