-
Notifications
You must be signed in to change notification settings - Fork 2
/
hard_uart_miniS_esp32.ino
119 lines (108 loc) · 3.76 KB
/
hard_uart_miniS_esp32.ino
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
// This code is wrriten to read data from Benewake mini-S LiDAR through UART physical interface.
// The code is tested with ESP32. It can also be used with ESP8266 by making slight modifications
// by Ibrahim Technical Support Engineer: [email protected]
// Note the format for setting a serial port is as follows: Serial2.begin(baud-rate, protocol, RX pin, TX pin);
#define RXD2 16
#define TXD2 17
int dist; /*----actual distance measurements of LiDAR---*/
int strength; /*----signal strength of LiDAR----------------*/
float temprature;
unsigned char check; /*----save check value------------------------*/
int i;
unsigned char uart[9]; /*----save data measured by LiDAR-------------*/
const int HEADER=0x59; /*----frame header of data package------------*/
int rec_debug_state = 0x01;//receive state for frame
void setup() {
delay(2000);
Serial.begin(115200);
Serial.println("\nBenewake TFmini-S UART LiDAR Program");
Serial2.begin(115200);
}
void loop() {
Get_Lidar_data();
}
void Get_Lidar_data(){
if (Serial2.available()) //check if serial port has data input
{
if(rec_debug_state == 0x01)
{ //the first byte
uart[0]=Serial2.read();
if(uart[0] == 0x59)
{
check = uart[0];
rec_debug_state = 0x02;
}
}
else if(rec_debug_state == 0x02)
{//the second byte
uart[1]=Serial2.read();
if(uart[1] == 0x59)
{
check += uart[1];
rec_debug_state = 0x03;
}
else{
rec_debug_state = 0x01;
}
}
else if(rec_debug_state == 0x03)
{
uart[2]=Serial2.read();
check += uart[2];
rec_debug_state = 0x04;
}
else if(rec_debug_state == 0x04)
{
uart[3]=Serial2.read();
check += uart[3];
rec_debug_state = 0x05;
}
else if(rec_debug_state == 0x05)
{
uart[4]=Serial2.read();
check += uart[4];
rec_debug_state = 0x06;
}
else if(rec_debug_state == 0x06)
{
uart[5]=Serial2.read();
check += uart[5];
rec_debug_state = 0x07;
}
else if(rec_debug_state == 0x07)
{
uart[6]=Serial2.read();
check += uart[6];
rec_debug_state = 0x08;
}
else if(rec_debug_state == 0x08)
{
uart[7]=Serial2.read();
check += uart[7];
rec_debug_state = 0x09;
}
else if(rec_debug_state == 0x09)
{
uart[8]=Serial2.read();
if(uart[8] == check)
{
dist = uart[2] + uart[3]*256;//the distance
strength = uart[4] + uart[5]*256;//the strength
temprature = uart[6] + uart[7] *256;//calculate chip temprature
temprature = temprature/8 - 256;
Serial.print("dist = ");
Serial.print(dist); //output measure distance value of LiDAR
Serial.print('\n');
Serial.print("strength = ");
Serial.print(strength); //output signal strength value
Serial.print('\n');
Serial.print("\t Chip Temprature = ");
Serial.print(temprature);
Serial.println(" celcius degree"); //output chip temperature of Lidar
while(Serial2.available()){Serial2.read();} // This part is added becuase some previous packets are there in the buffer so to clear serial buffer and get fresh data.
delay(100);
}
rec_debug_state = 0x01;
}
}
}