电子说
LiDAR测距应用越来越普及,例如行人检测、车辆检测、障碍物探测等自动驾驶应用,以及道闸防砸、高度计等。本项目以北醒TFMini-S Micro LiDAR传感器为例,演示LiDAR传感器与Arduino的连接,以及测距结果及显示。本项目BOM如下:
Arduino Nano开发板 1
TFMini-S LiDAR传感器 1
JHD162A 16X2 LCD显示器 1
10K电位器 1
面包板 1
跳线 22
基于激光的精确测距能力,LiDAR是一种精确的激光探测及测距器件。LiDAR工作原理与无线电雷达没有区别,即由雷达发射系统发送一个信号,经目标反射后被接收系统收集,通过测量反射光的运行时间而确定目标的距离。至于目标的径向速度,可以由反射光的多普勒频移来确定,也可以测量两个或多个距离,并计算其变化率而求得速度。
项目采用的TFmini-S是一款基于TFmini升级的单点测距雷达,盲区降低为10cm,室外测距性能进一步提升,不同反射率的测距精度得到优化,可以实现稳定、精准、高灵敏和高速的距离测量。
TFMini-S LiDAR可通过UART或I2C接口与Arduino连接。本项目采用UART。
将TFMini传感器的VCC & GND引脚连接到Arduino开发板的5V & GND引脚,Tx、RX引脚连接到Arduino开发板的D2 & D3引脚。
当然,也可以把Tx and Rx连接在其他引脚上,只要支持软件串口即可。
北醒官网、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开始显示距离和信号强度,把手在传感器前面来回移动可观察到距离在变化,盲区从30cm缩减到10cm。
如果希望通过LCD显示测量结果,就需要在前述电路的面包板上加一个16x2 LCD显示器,并通过10K电位器来调节LCD的对比度。这样,本项目就实现了便携式涉及,可以装起来带去户外体验了。
#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();
}
结果显示,TFMini-S LiDAR传感器最大测距为12m,由于户外障碍物比室内少很多,测距准确度高达90%。
审核编辑:汤梓红
全部0条评论
快来发表一下你的评论吧 !