Benewake(北醒) TF-LC02 (TTL) 雷达不使用TTL转USB转接板在Arduino Uno上的运用

前言

本例程仅用作参考

Benewake(北醒) TF-LC02产品简要说明

性能参数
在这里插入图片描述产品图片及尺寸
在这里插入图片描述

Arduino开发板介绍

参考链接:常用Arduino板介绍

Benewake(北醒) TF-LC02 接口及通讯协议说明

接口定义

在这里插入图片描述

串口协议说明

在这里插入图片描述在这里插入图片描述

通讯协议说明

在这里插入图片描述

功能码说明

在这里插入图片描述在这里插入图片描述

接线示意图

在这里插入图片描述
:线路颜色仅供参考,具体参照实际线路颜色定义

例程说明

配置软硬串口

  • 引入软串口连接雷达,用硬串口来打印雷达收到并处理后的数据
#include <SoftwareSerial.h>        //软串口头文件
int ledPin = 13; // 设置LED的引脚
SoftwareSerial Port_Debug(2, 3);   //定义软件口名称和PIN2为RX PIN3为TX
void setup() {
/********************************
-  TOF串口协议:TTL
-  波特率:115200
-  数据位:8
-  停止位:1
-  奇偶校验:无
-  流控:无
**************************************/
 Serial.begin(115200);       //通过硬件串口来获取实时性要求比较高的雷达数据,软串口容易出现掉帧的情况   
 Port_Debug.begin(115200);
 pinMode(ledPin, OUTPUT); // 将LED引脚设置为输出模式
}

定义获取TOF数据的结构

  • 设置获取距离的指令
u8 cmd[5] = {0x55, 0xAA, 0x81, 0x00, 0xFA};  //获取距离指令
typedef struct {
 int distance;
 u8 ErrorCode; //TOF错误码请参考使用说明书
 boolean receiveComplete;
} TF;
TF Lidar = {0,0,false};

获取雷达距离数据的协议解析

/***************************************
 -  通讯协议:
 -  2 byte : 帧头 0x55 0xAA
 -  1 byte : 功能码 (详细参考使用说明) 例:0x81 获取距离值 单位:mm
 -  1 byte : 后面参数的长度
 -  N byte : 设定参数
 -  1 byte : 帧尾 0xFA
 -  ***************************************
 -  例:获取距离值
 -  Arduino 发送:55 AA 81 00 FA
 -  TOF模组回复 : 55 AA 81 03 01 55 00 FA
 **************************************/
void getLidarData(TF* lidar) {
  static char i = 0;
  static int rx[8];
  while (Port_Debug.available())
  {
    rx[i] = Port_Debug.read();
    if (rx[0] != 0x55)
    {
      i = 0;
    } else if (i == 1 && rx[1] != 0xAA)
    {
      i = 0;
    } else if (i == 7)
    {
      i = 0;
      if (rx[7] == 0xFA)
      {
        lidar->distance = rx[5] + rx[4] * 256;
        lidar->ErrorCode  = rx[6];
        lidar->receiveComplete = true;
      }
    } else
    {
      i++;
    }
  }
}

通过主循环发送获取距离指令,并打印结果(可根据需求打印结果)

  • 当雷达测距距离小于50cm时,LED灯灭;当大于50cm时,LED灯亮
void loop() {
  getLidarData(&Lidar);
  if (!Lidar.receiveComplete)
  {
    Port_Debug.write(cmd, 5);
  } else
  {
    Port_Print_Ascii(&Lidar);          // Ascii 打印输出结果
    
    if(Lidar.distance < 500)
    {
      digitalWrite(ledPin, HIGH); // 将LED引脚设置为高电平,LED亮起来
    }
    else if(Lidar.distance > 500)
    {
      digitalWrite(ledPin, LOW); // 将LED引脚设置为低电平,LED熄灭
    }
    //Port_Print_Benewake_9Byte(&Lidar);   // 北醒通用9Byte打印
    Lidar.receiveComplete = false;
    delay(33);                       //延时33ms,雷达探测速率最快33ms
  }
}

打印通用Ascii 码结果

void Port_Print_Ascii(TF* lidar)
{
  Serial.print("Dist = ");
  Serial.println(lidar->distance);
  if(lidar->ErrorCode)
  {
    Serial.print("ErrorCode = ");
    Serial.println(lidar->ErrorCode,HEX);
  }
}

串口助手显示

在这里插入图片描述

完整例程分析

#include <SoftwareSerial.h>        //软串口头文件

int ledPin = 13; // 设置LED的引脚
SoftwareSerial Port_Debug(2, 3);   //定义软件口名称和PIN2为RX PIN3为TX

u8 cmd[5] = {0x55, 0xAA, 0x81, 0x00, 0xFA};  //获取距离指令

typedef struct {
  int distance;
  u8 ErrorCode; //TOF错误码请参考使用说明书
  boolean receiveComplete;
} TF;
TF Lidar = {0,0,false};

void getLidarData(TF* lidar) {
  static char i = 0;
  static int rx[8];
  while (Port_Debug.available())
  {
    rx[i] = Port_Debug.read();
    if (rx[0] != 0x55)
    {
      i = 0;
    } else if (i == 1 && rx[1] != 0xAA)
    {
      i = 0;
    } else if (i == 7)
    {
      i = 0;
      if (rx[7] == 0xFA)
      {
        lidar->distance = rx[5] + rx[4] * 256;
        lidar->ErrorCode  = rx[6];
        lidar->receiveComplete = true;
      }
    } else
    {
      i++;
    }
  }
}

void setup() {
  Serial.begin(115200);       //通过硬件串口来获取实时性要求比较高的雷达数据,软串口容易出现掉帧的情况   
  Port_Debug.begin(115200);
  pinMode(ledPin, OUTPUT); // 将LED引脚设置为输出模式
}

void loop() {
  getLidarData(&Lidar);
  if (!Lidar.receiveComplete)
  {
    Port_Debug.write(cmd, 5);
    delay(33);
  } else
  {
    Port_Print_Ascii(&Lidar);          // Ascii 打印输出结果

    if(Lidar.distance < 500)
    {
      digitalWrite(ledPin, HIGH); // 将LED引脚设置为高电平,LED亮起来
    }
    else if(Lidar.distance > 500)
    {
      digitalWrite(ledPin, LOW); // 将LED引脚设置为低电平,LED熄灭
    }
    
    //Port_Print_Benewake_9Byte(&Lidar);   // 北醒通用9Byte打印
    Lidar.receiveComplete = false;
    delay(33);                       //延时33ms,雷达探测速率最快33ms
  }
}
void Port_Print_Ascii(TF* lidar)
{
  Serial.print("Dist = ");
  Serial.println(lidar->distance);
  if(lidar->ErrorCode)
  {
    Serial.print("ErrorCode = ");
    Serial.println(lidar->ErrorCode,HEX);
  }
}



void Port_Print_Benewake_9Byte(TF* lidar)
{
  u8 i = 0;
  u8 CheckSum = 0;
  u8 B_9Byte[9];
  B_9Byte[0] = 0x59;
  B_9Byte[1] = 0x59;
  B_9Byte[2] = lidar->distance & 0xFF;
  B_9Byte[3] = lidar->distance >> 8;
  B_9Byte[4] = 0x00;
  B_9Byte[5] = 0x00;
  B_9Byte[6] = 0x00;
  B_9Byte[7] = 0x00;
  for(i=0;i<7;i++){
    CheckSum += B_9Byte[i];
  }
  B_9Byte[8] = CheckSum & 0xFF;
  Port_Debug.write(B_9Byte,9);
}

//void serialEvent() {
 // getLidarData(&Lidar);
//}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值