forked from yomboprime/TFMiniArduinoTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest_LIDAR_TFMini.ino
175 lines (126 loc) · 3.48 KB
/
Test_LIDAR_TFMini.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* TFMini LIDAR Sensor test skecth for ESP32
*
* Juan Jose Luna Espinosa 2018
* https://github.com/yomboprime/TFMiniArduinoTest
*
* Released under public domain
*
*
* Connections:
* Green cable to RX in the ESP32 (UART 2, pin 16)
* Red cable to 5V
* Black cable to GND
* White cable is not needed.
*
* 9 byte frame of the sensor:
* Byte 0: Always is 0x59
* Byte 1: Always is 0x59
* Byte 2: Distance in cm, low byte
* Byte 3: Distance in cm, high byte
* Byte 4: Signal strength, low byte
* Byte 5: Signal strength, high byte
* Byte 6: Reserved byte
* Byte 7: Raw signal quality
* Byte 8: Checksum of the 8 previous bytes.
*/
HardwareSerial Serial1( 2 );
void setup() {
// Debug serial
Serial.begin( 115200 );
// Serial connected to LIDAR sensor
Serial1.begin( 115200 );
delay( 1000 );
Serial.println( "Starting..." );
}
void loop() {
// If a byte arrives from debug Serial, perform a readings per seconds test
if ( Serial.available() > 0 ) {
speedTest();
}
else {
// Perform one distance reading and show it on Serial
unsigned int distance = readLIDAR( 2000 );
if ( distance > 0 ) {
Serial.printf( "Distance (cm): %d\n", distance );
}
else {
Serial.println( "Timeout reading LIDAR" );
}
}
}
void speedTest() {
while ( Serial.available() > 0 ) {
Serial.read();
}
Serial.printf( "\n\nPerforming speed test...\n" );
long t0 = millis();
#define NUM_READINGS 1000
long accum = 0;
for ( int i = 0; i < NUM_READINGS; i++ ) {
accum += readLIDAR( 2000 );
}
long t1 = millis();
float readingsPerSecond = NUM_READINGS * 1000.0f / ( t1 - t0 );
float meanDistance = ((float)accum) / NUM_READINGS;
Serial.println( "\n\nSpeed test:" );
Serial.printf( "%f readings per second.\n", readingsPerSecond );
Serial.printf( "%f mean read distance.\n", meanDistance );
Serial.println( "\n\nHit another key to continue reading the sensor distance." );
while ( Serial.available() == 0 ) {
delay( 10 );
}
while ( Serial.available() > 0 ) {
Serial.read();
}
}
/*
* This function reads the Serial1 until a valid packet is found or timeout passed.
* Param timeout: Timeout in milliseconds.
* Returns distance in cm or 0 if timeout happened.
*/
unsigned int readLIDAR( long timeout ) {
unsigned char readBuffer[ 9 ];
long t0 = millis();
while ( Serial1.available() < 9 ) {
if ( millis() - t0 > timeout ) {
// Timeout
return 0;
}
delay( 10 );
}
for ( int i = 0; i < 9; i++ ) {
readBuffer[ i ] = Serial1.read();
}
while ( ! detectFrame( readBuffer ) ) {
if ( millis() - t0 > timeout ) {
// Timeout
return 0;
}
while ( Serial1.available() == 0 ) {
delay( 10 );
}
for ( int i = 0; i < 8; i++ ) {
readBuffer[ i ] = readBuffer[ i + 1 ];
}
readBuffer[ 8 ] = Serial1.read();
}
// Distance is in bytes 2 and 3 of the 9 byte frame.
unsigned int distance = ( (unsigned int)( readBuffer[ 2 ] ) ) |
( ( (unsigned int)( readBuffer[ 3 ] ) ) << 8 );
return distance;
}
bool detectFrame( unsigned char *readBuffer ) {
return readBuffer[ 0 ] == 0x59 &&
readBuffer[ 1 ] == 0x59 &&
(unsigned char)(
0x59 +
0x59 +
readBuffer[ 2 ] +
readBuffer[ 3 ] +
readBuffer[ 4 ] +
readBuffer[ 5 ] +
readBuffer[ 6 ] +
readBuffer[ 7 ]
) == readBuffer[ 8 ];
}