本文主要是介绍Benewake(北醒) TFmini-S\TFmini Plus\TF-Luna\TF02-Pro 串口版本雷达在Arduino Nano上的例程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
- TFLidar_Arduino_HardwareSerial_Polling
- 接线定义
- 代码
- 输出
- TFLidar_Arduino_HardwareSerial_Interrupt
- 接线定义
- 代码
- 输出
- TFLidar_Arduino_SoftwareSerial
- 接线定义
- 代码
- 输出
- TFLidar_Arduino_SoftwareSerial_Multiple
- 接线定义
- 代码
- 输出
- TFLidar_Arduino_SoftwareSerial_Multiple_Frequency
- 接线定义
- 代码
- 输出
本文介绍了北醒单点系列雷达在Arduino上的一些例子,并使用Nano板连接TFmini-S测试
例程下载:
链接: https://pan.baidu.com/s/1_Xb-k6ygCmWTFEEwkDEwXw
提取码: 6yw6
TFLidar_Arduino_HardwareSerial_Polling
接线定义
TFmini | Arduino |
---|---|
5V(红) | 5V |
GND(黑) | GND |
TX(绿) | D0(RX) |
使用硬件串口、采用轮询方式
注:使用硬件串口时,烧录程序过程中雷达绿色线不要插入Nano板D0
代码
typedef struct {int distance;int strength;int temp;boolean receiveComplete;
} TF;
TF Lidar = {0, 0, 0, false};void getLidarData(TF* lidar)
{static char i = 0;char j = 0;int checksum = 0;static int rx[9];if (Serial.available()) {rx[i] = Serial.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)) {lidar->distance = rx[2] + rx[3] * 256;lidar->strength = rx[4] + rx[5] * 256;lidar->temp = (rx[6] + rx[7] * 256) / 8 - 256;lidar->receiveComplete = true;}i = 0;} else {i++;}}
}/*首先将雷达输出的9位数据按顺序存放入‘rx[]’数组中,随后计算输出的距离、信号强度、温度*/void setup() {Serial.begin(115200);
}void loop()
{getLidarData(&Lidar);if (Lidar.receiveComplete) {Lidar.receiveComplete = false;Serial.print("Distance: ");Serial.print(Lidar.distance);Serial.print("cm\t");Serial.print("Strength: ");Serial.print(Lidar.strength);Serial.print("\t");Serial.print("Temp: ");Serial.println(Lidar.temp);}
}
输出
点击“工具-串口监视器”即可观察输出结果
TFLidar_Arduino_HardwareSerial_Interrupt
接线定义
TFmini | Arduino |
---|---|
5V(红) | 5V |
GND(黑) | GND |
TX(绿) | D0(RX) |
使用硬件串口、采用中断方式
注:使用硬件串口时,烧录程序过程中雷达绿色线不要插入Nano板D0
代码
typedef struct {int distance;int strength;int temp;boolean receiveComplete;
} TF;
TF Lidar = {0, 0, 0, false};void getLidarData(TF* lidar) {static char i = 0;char j = 0;int checksum = 0;static int rx[9];if (Serial.available()) {rx[i] = Serial.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)) {lidar->distance = rx[2] + rx[3] * 256;lidar->strength = rx[4] + rx[5] * 256;lidar->temp = (rx[6] + rx[7] * 256) / 8 - 256;lidar->receiveComplete = true;}i = 0;} else {i++;}}
}void setup() {Serial.begin(115200);
}void loop() {if (Lidar.receiveComplete) {Lidar.receiveComplete = false;Serial.print("Distance: ");Serial.print(Lidar.distance);Serial.print("cm\t");Serial.print("Strength: ");Serial.print(Lidar.strength);Serial.print("\t");Serial.print("Temp: ");Serial.println(Lidar.temp);}
}void serialEvent() {getLidarData(&Lidar);
}
输出
TFLidar_Arduino_SoftwareSerial
接线定义
TFmini | Arduino |
---|---|
5V(红) | 5V |
GND(黑) | GND |
TX(绿) | D2(RX) |
使用软件件串口、采用轮询方式
代码
#include <SoftwareSerial.h> //header file of software serial portSoftwareSerial Serial1(2, 3); //define software serial port name as Serial1 and define pin2 as RX and pin3 as TXtypedef struct {int distance;int strength;int temp;boolean receiveComplete;
} TF;
TF Lidar = {0, 0, 0, false};void setup() {Serial.begin(115200); //set bit rate of serial port connecting Arduino with computerSerial1.begin(115200); //set bit rate of serial port connecting LiDAR with Arduino
}void loop() {getLidarData(&Lidar);if (Lidar.receiveComplete) {Lidar.receiveComplete = false;Serial.print("Distance: ");Serial.print(Lidar.distance);Serial.print("cm\t");Serial.print("Strength: ");Serial.print(Lidar.strength);Serial.print("\t");Serial.print("Temp: ");Serial.println(Lidar.temp);}
}void getLidarData(TF* lidar) {static char i = 0;char j = 0;int checksum = 0;static int rx[9];if (Serial1.available()) {rx[i] = Serial1.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)) {lidar->distance = rx[2] + rx[3] * 256;lidar->strength = rx[4] + rx[5] * 256;lidar->temp = (rx[6] + rx[7] * 256) / 8 - 256;lidar->receiveComplete = true;}i = 0;} else {i++;}}
}
输出
TFLidar_Arduino_SoftwareSerial_Multiple
接线定义
TFmini | Arduino |
---|---|
5V(红) | 5V |
GND(黑) | GND |
LIDAR1 TX(绿) | D2(RX) |
LIDAR2TX(绿) | D3(RX) |
使用软件件串口、采用轮询方式
代码
#include <SoftwareSerial.h>//SoftwareSerial port(TX, RX);
SoftwareSerial portOne(2, 2);
SoftwareSerial portTwo(3, 3);typedef struct {int distance;int strength;int temp;boolean receiveComplete;
} TF;
TF Lidar1 = {0, 0, 0, false};
TF Lidar2 = {0, 0, 0, false};void getLidarData(SoftwareSerial* port, TF* Lidar) {static char i = 0;char j = 0;int checksum = 0;static int rx[9];port->listen();if (port->available()) {rx[i] = port->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)) {Lidar->distance = rx[2] + rx[3] * 256;Lidar->strength = rx[4] + rx[5] * 256;Lidar->temp = (rx[6] + rx[7] * 256) / 8 - 256;Lidar->receiveComplete = true;}i = 0;} else {i++;}}
}void setup() {Serial.begin(115200);portOne.begin(115200);portTwo.begin(115200);
}void loop() {while (!Lidar1.receiveComplete) {getLidarData(&portOne, &Lidar1);if (Lidar1.receiveComplete) {Serial.print("Lidar1 Mesure: ");Serial.print("Distance: ");Serial.print(Lidar1.distance);Serial.print("cm\t");Serial.print("Strength: ");Serial.print(Lidar1.strength);Serial.print("\t");Serial.print("Temp: ");Serial.println(Lidar1.temp);}}Lidar1.receiveComplete = false;while (!Lidar2.receiveComplete) {getLidarData(&portTwo, &Lidar2);if (Lidar2.receiveComplete) {Serial.print("Lidar2 Mesure: ");Serial.print("Distance: ");Serial.print(Lidar2.distance);Serial.print("cm\t");Serial.print("Strength: ");Serial.print(Lidar2.strength);Serial.print("\t");Serial.print("Temp: ");Serial.println(Lidar2.temp);}}Lidar2.receiveComplete = false;
}
输出
TFLidar_Arduino_SoftwareSerial_Multiple_Frequency
接线定义
TFmini | Arduino |
---|---|
5V(红) | 5V |
GND(黑) | GND |
LIDAR1 TX(绿) | D2(RX) |
LIDAR2TX(绿) | D3(RX) |
使用软件件串口、采用轮询方式、添加频率显示
代码
#include <SoftwareSerial.h>
typedef struct {int distance;int strength;int temp;boolean receiveComplete;
} TF;
TF Lidar1 = {0, 0, 0, false};
TF Lidar2 = {0, 0, 0, false};//SoftwareSerial port(RX, TX);
SoftwareSerial portOne(2, 2);
SoftwareSerial portTwo(3, 3);void getLidarData(SoftwareSerial* port, TF* lidar) {port->listen();while (lidar->receiveComplete == false){static char i = 0;char j = 0;int checksum = 0;static int rx[9];if (port->available()) {rx[i] = port->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)) {lidar->distance = rx[2] + rx[3] * 256;lidar->strength = rx[4] + rx[5] * 256;lidar->temp = (rx[6] + rx[7] * 256) / 8 - 256;lidar->receiveComplete = true;}i = 0;} else {i++;}}}
}void setup() {Serial.begin(115200);portOne.begin(115200);portTwo.begin(115200);
}void loop() {static unsigned long lastTime = millis();static unsigned int count = 0;static unsigned int frequency = 0;getLidarData(&portOne, &Lidar1);getLidarData(&portTwo, &Lidar2);if (Lidar1.receiveComplete == true && Lidar2.receiveComplete == true) {++count;if (millis() - lastTime > 999) {lastTime = millis();frequency = count;count = 0;}Serial.print("Lidar1 Mesure: ");Serial.print("Distance: ");Serial.print(Lidar1.distance);Serial.print("cm\t");Serial.print("Strength: ");Serial.print(Lidar1.strength);Serial.print("\t");Serial.print("Temp: ");Serial.print(Lidar1.temp);Serial.print("\t");Serial.print("Lidar2 Mesure: ");Serial.print("Distance: ");Serial.print(Lidar2.distance);Serial.print("cm\t");Serial.print("Strength: ");Serial.print(Lidar2.strength);Serial.print("\t");Serial.print("Temp: ");Serial.print(Lidar2.temp);Serial.print("\t");Serial.print(frequency); //40~70Hz, It maybe higher if we don't print other thing.Serial.println("Hz");Lidar1.receiveComplete = false;Lidar2.receiveComplete = false;}}
输出
这篇关于Benewake(北醒) TFmini-S\TFmini Plus\TF-Luna\TF02-Pro 串口版本雷达在Arduino Nano上的例程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!