Skip to content

Commit 8d0db9d

Browse files
Ilia O.Ilia O.
authored andcommitted
RPLIDAR A1 start fix
1 parent 5a69b1a commit 8d0db9d

35 files changed

+424
-74
lines changed

examples/all_lidars_lds02rr_esp32/all_lidars_lds02rr_esp32.ino

Lines changed: 76 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 KAIA.AI
1+
// Copyright 2023-2025 KAIA.AI
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -44,17 +44,26 @@ const uint8_t LIDAR_GPIO_PWM = 15;// ESP32 GPIO connected to Lidar PWM pin
4444
//#define YDLIDAR_X4_PRO
4545
//#define CAMSENSE_X1
4646

47+
// 4. UNCOMMENT debug option(s)
48+
// and increase SERIAL_MONITOR_BAUD to MAX possible
49+
//#define DEBUG_GPIO
50+
//#define DEBUG_PACKETS
51+
//#define DEBUG_SERIAL_IN
52+
//#define DEBUG_SERIAL_OUT
4753

4854
const uint32_t SERIAL_MONITOR_BAUD = 115200;
4955
const uint32_t LIDAR_PWM_FREQ = 10000;
5056
const uint8_t LIDAR_PWM_BITS = 11;
5157
const uint8_t LIDAR_PWM_CHANNEL = 2;
5258
const uint16_t LIDAR_SERIAL_RX_BUF_LEN = 1024;
59+
const uint16_t PRINT_EVERY_NTH_POINT = 20;
60+
const uint16_t HEX_DUMP_WIDTH = 16;
5361

5462
#include "lds_all_models.h"
5563

5664
HardwareSerial LidarSerial(1);
5765
LDS *lidar;
66+
uint16_t hex_dump_pos = 0;
5867

5968
void setupLidar() {
6069

@@ -115,11 +124,14 @@ void setup() {
115124

116125
setupLidar();
117126

118-
//Serial.print("LiDAR model ");
119-
//Serial.println(lidar->getModelName());
120-
121-
//Serial.print("Lidar baud rate ");
122-
//Serial.println(lidar->getSerialBaudRate());
127+
Serial.print("LiDAR model ");
128+
Serial.print(lidar->getModelName());
129+
Serial.print(", baud rate ");
130+
Serial.print(lidar->getSerialBaudRate());
131+
Serial.print(", TX GPIO ");
132+
Serial.print(LIDAR_GPIO_TX);
133+
Serial.print(", RX GPIO ");
134+
Serial.println(LIDAR_GPIO_RX);
123135

124136
LDS::result_t result = lidar->start();
125137
Serial.print("startLidar() result: ");
@@ -129,25 +141,52 @@ void setup() {
129141
// lidar->setScanTargetFreqHz(5.0f);
130142
}
131143

144+
void printByteAsHex(uint8_t b) {
145+
if (b < 16)
146+
Serial.print('0');
147+
Serial.print(b, HEX);
148+
Serial.print(' ');
149+
}
150+
151+
void printBytesAsHex(const uint8_t * buffer, uint16_t length) {
152+
if (length == 0)
153+
return;
154+
155+
for (uint16_t i = 0; i < length; i++) {
156+
printByteAsHex(buffer[i]);
157+
}
158+
}
159+
132160
int lidar_serial_read_callback() {
133-
return LidarSerial.read();
134-
/*
161+
#ifdef DEBUG_SERIAL_IN
135162
int ch = LidarSerial.read();
136-
if (ch != -1)
137-
Serial.println(ch);
163+
if (ch != -1) {
164+
if (hex_dump_pos++ % HEX_DUMP_WIDTH == 0)
165+
Serial.println();
166+
printByteAsHex(ch);
167+
}
138168
return ch;
139-
*/
169+
#else
170+
return LidarSerial.read();
171+
#endif
140172
}
141173

142174
size_t lidar_serial_write_callback(const uint8_t * buffer, size_t length) {
175+
#ifdef DEBUG_SERIAL_OUT
176+
Serial.println();
177+
Serial.print('>');
178+
printBytesAsHex(buffer, length);
179+
Serial.println();
180+
#endif
181+
143182
return LidarSerial.write(buffer, length);
144183
}
145184

146185
void lidar_scan_point_callback(float angle_deg, float distance_mm, float quality,
147186
bool scan_completed) {
148187
static int i=0;
149188

150-
if (i % 20 == 0) {
189+
if (i % PRINT_EVERY_NTH_POINT == 0) {
151190
Serial.print(i);
152191
Serial.print(' ');
153192
Serial.print(distance_mm);
@@ -167,16 +206,16 @@ void lidar_motor_pin_callback(float value, LDS::lds_pin_t lidar_pin) {
167206

168207
int pin = (lidar_pin == LDS::LDS_MOTOR_EN_PIN) ? LIDAR_GPIO_EN : LIDAR_GPIO_PWM;
169208

170-
if (value <= (float)LDS::DIR_INPUT) {
209+
#ifdef DEBUG_GPIO
210+
Serial.print("GPIO ");
211+
Serial.print(pin);
212+
Serial.print(' ');
213+
Serial.print(lidar->pinIDToString(lidar_pin));
214+
Serial.print(" mode set to ");
215+
Serial.println(lidar->pinStateToString((LDS::lds_pin_state_t) int(value)));
216+
#endif
171217

172-
/*
173-
Serial.print("GPIO ");
174-
Serial.print(pin);
175-
Serial.print(' ');
176-
Serial.print(lidar->pinIDToString(lidar_pin));
177-
Serial.print(" mode set to ");
178-
Serial.println(lidar->pinStateToString(int(pin_mode)));
179-
*/
218+
if (value <= (float)LDS::DIR_INPUT) {
180219

181220
// Configure pin direction
182221
if (value == (float)LDS::DIR_OUTPUT_PWM) {
@@ -199,18 +238,11 @@ void lidar_motor_pin_callback(float value, LDS::lds_pin_t lidar_pin) {
199238
// TODO invert PWM as needed
200239
digitalWrite(pin, (value == (float)LDS::VALUE_HIGH) ? HIGH : LOW);
201240

202-
/*
203-
Serial.print("GPIO ");
204-
Serial.print(pin);
205-
Serial.print(' ');
206-
Serial.print(lidar->pinIDToString(lidar_pin));
207-
Serial.print(" value set to ");
208-
Serial.println(lidar->pinStateToString(int(value)));
209-
*/
210-
211241
} else {
212-
//Serial.print("PWM value set to ");
213-
//Serial.print(value);
242+
#ifdef DEBUG_GPIO
243+
Serial.print("PWM value set to ");
244+
Serial.print(value);
245+
#endif
214246

215247
// set PWM duty cycle
216248
#ifdef INVERT_PWM_PIN
@@ -225,8 +257,10 @@ void lidar_motor_pin_callback(float value, LDS::lds_pin_t lidar_pin) {
225257
ledcWriteChannel(LIDAR_PWM_CHANNEL, pwm_value);
226258
#endif
227259

228-
//Serial.print(' ');
229-
//Serial.println(pwm_value);
260+
#ifdef DEBUG_GPIO
261+
Serial.print(' ');
262+
Serial.println(pwm_value);
263+
#endif
230264
}
231265
}
232266

@@ -245,6 +279,14 @@ void lidar_error_callback(LDS::result_t code, String aux_info) {
245279
}
246280

247281
void lidar_packet_callback(uint8_t * packet, uint16_t length, bool scan_completed) {
282+
#ifdef DEBUG_PACKETS
283+
Serial.println();
284+
Serial.print("Packet callback, length=");
285+
Serial.print(length);
286+
Serial.print(", scan_completed=");
287+
Serial.println(scan_completed);
288+
#endif
289+
248290
return;
249291
}
250292

0 commit comments

Comments
 (0)