LiDAR測距應(yīng)用越來越普及,例如行人檢測、車輛檢測、障礙物探測等自動駕駛應(yīng)用,以及道閘防砸、高度計等。本項目以北醒TFMini-S Micro LiDAR傳感器為例,演示LiDAR傳感器與Arduino的連接,以及測距結(jié)果及顯示。本項目BOM如下:
Arduino Nano開發(fā)板 1
TFMini-S LiDAR傳感器 1
JHD162A 16X2 LCD顯示器 1
10K電位器 1
面包板 1
跳線 22
基于激光的精確測距能力,LiDAR是一種精確的激光探測及測距器件。LiDAR工作原理與無線電雷達沒有區(qū)別,即由雷達發(fā)射系統(tǒng)發(fā)送一個信號,經(jīng)目標反射后被接收系統(tǒng)收集,通過測量反射光的運行時間而確定目標的距離。至于目標的徑向速度,可以由反射光的多普勒頻移來確定,也可以測量兩個或多個距離,并計算其變化率而求得速度。
?
項目采用的TFmini-S是一款基于TFmini升級的單點測距雷達,盲區(qū)降低為10cm,室外測距性能進一步提升,不同反射率的測距精度得到優(yōu)化,可以實現(xiàn)穩(wěn)定、精準、高靈敏和高速的距離測量。
連接TFMini-S LiDAR傳感器與Arduino
TFMini-S LiDAR可通過UART或I2C接口與Arduino連接。本項目采用UART。
?
將TFMini傳感器的VCC & GND引腳連接到Arduino開發(fā)板的5V & GND引腳,Tx、RX引腳連接到Arduino開發(fā)板的D2 & D3引腳。
當然,也可以把Tx and Rx連接在其他引腳上,只要支持軟件串口即可。
源代碼及例程庫
北醒官網(wǎng)、Github都提供有TFMini-S LiDAR傳感器庫文件和例程,具體如下:
include
#include "TFMini.h"
TFMini tfmini;
SoftwareSerial SerialTFMini(2, 3); //The only value that matters here is the first one, 2, Rx
void getTFminiData(int* distance, int* strength)
{
static char i = 0;
char j = 0;
int checksum = 0;
static int rx[9];
if (SerialTFMini.available())
{
rx[i] = SerialTFMini.read();
if (rx[0] != 0x59)
{
i = 0;
}
else if (i == 1 && rx[1] != 0x59)
{
i = 0;
}
else if (i == 8)
{
for (j = 0; j < 8; j++)
{
checksum += rx[j];
}
if (rx[8] == (checksum % 256))
{
*distance = rx[2] + rx[3] * 256;
*strength = rx[4] + rx[5] * 256;
}
i = 0;
}
else
{
i++;
}
}
}
void setup()
{
Serial.begin(115200); //Initialize hardware serial port (serial debug port)
while (!Serial); // wait for serial port to connect. Needed for native USB port only
Serial.println ("Initializing...");
SerialTFMini.begin(TFMINI_BAUDRATE); //Initialize the data rate for the SoftwareSerial port
tfmini.begin(&SerialTFMini); //Initialize the TF Mini sensor
}
void loop()
{
int distance = 0;
int strength = 0;
getTFminiData(&distance, &strength);
while (!distance)
{
getTFminiData(&distance, &strength);
if (distance)
{
Serial.print(distance);
Serial.print("cm\t");
Serial.print("strength: ");
Serial.println(strength);
}
}
delay(100);
}
這下,Serial monitor開始顯示距離和信號強度,把手在傳感器前面來回移動可觀察到距離在變化,盲區(qū)從30cm縮減到10cm。
?
如果希望通過LCD顯示測量結(jié)果,就需要在前述電路的面包板上加一個16x2 LCD顯示器,并通過10K電位器來調(diào)節(jié)LCD的對比度。這樣,本項目就實現(xiàn)了便攜式涉及,可以裝起來帶去戶外體驗了。
#include
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
#include
#include "TFMini.h"
TFMini tfmini;
SoftwareSerial SerialTFMini(2, 3); //The only value that matters here is the first one, 2, Rx
void getTFminiData(int* distance, int* strength)
{
static char i = 0;
char j = 0;
int checksum = 0;
static int rx[9];
if (SerialTFMini.available())
{
rx[i] = SerialTFMini.read();
if (rx[0] != 0x59)
{
i = 0;
}
else if (i == 1 && rx[1] != 0x59)
{
i = 0;
}
else if (i == 8)
{
for (j = 0; j < 8; j++)
{
checksum += rx[j];
}
if (rx[8] == (checksum % 256))
{
*distance = rx[2] + rx[3] * 256;
*strength = rx[4] + rx[5] * 256;
}
i = 0;
}
else
{
i++;
}
}
}
void setup()
{
lcd.begin(16, 2);
Serial.begin(115200); //Initialize hardware serial port (serial debug port)
while (!Serial); // wait for serial port to connect. Needed for native USB port only
Serial.println ("Initializing...");
SerialTFMini.begin(TFMINI_BAUDRATE); //Initialize the data rate for the SoftwareSerial port
tfmini.begin(&SerialTFMini); //Initialize the TF Mini sensor
}
void loop()
{
int distance = 0;
int strength = 0;
getTFminiData(&distance, &strength);
while (!distance)
{
getTFminiData(&distance, &strength);
if (distance)
{
Serial.print(distance);
Serial.print("cm\t");
Serial.print("strength: ");
Serial.println(strength);
lcd.setCursor(0, 0);
lcd.print("Dis: ");
lcd.print(distance);
lcd.print(" cm");
lcd.setCursor(0, 1);
lcd.print("Str: ");
lcd.print(strength);
}
}
delay(500);
lcd.clear();
}
結(jié)果顯示,TFMini-S LiDAR傳感器最大測距為12m,由于戶外障礙物比室內(nèi)少很多,測距準確度高達90%。
審核編輯:湯梓紅
-
傳感器
+關(guān)注
關(guān)注
2550文章
51035瀏覽量
753063 -
激光測距
+關(guān)注
關(guān)注
9文章
157瀏覽量
27544 -
Arduino
+關(guān)注
關(guān)注
188文章
6468瀏覽量
186951 -
LIDAR
+關(guān)注
關(guān)注
10文章
326瀏覽量
29411
發(fā)布評論請先 登錄
相關(guān)推薦
評論