简体   繁体   English

如何在 I2C 中使用 Arduino 从 LIDAR 的正确地址读取?

[英]How do I read from a correct address of LIDAR using Arduino in I2C?

I need to read the distance data from a lidar in I2C using an Arduino Nano.我需要使用 Arduino Nano 从 I2C 中的激光雷达读取距离数据。 Currently, this is the code I've written.目前,这是我编写的代码。

unsigned int readDistance()
{
  unsigned int dist = 0 ; // LiDAR actually measured distance value. static so we can return previous dist
  
  // step 1: instruct sensor to read echoes
  Wire.beginTransmission(0x10) ; // transmit to device 0x10
  Wire.write(2) ; // sets distance data address (addr)
  Wire.write(3) ; // sets distance data address (addr)
  Wire.endTransmission() ; // stop transmitting
  
  // step 2: wait for readings to happen
  delay(100) ; // datasheet suggests at least 100ms
  
  // step 3: request reading from sensor
  Wire.requestFrom(0x10, 2) ; // request 2 bytes (DIST_L, DIST_H) from slave device #0x10
  dist = Wire.read() ; dist += Wire.read() << 8; // calculate distance value, bit shift high distance
  return dist ; // return updated dist
}

But I have a feeling I'm requesting from the wrong data address', since I'm not getting the results I expect (ie. varying distance data).但是我感觉我从错误的数据地址请求',因为我没有得到我期望的结果(即不同的距离数据)。 Furthermore, I used an I2C scanner which 100% confirms the lidar is on the (default address) of 0x10.此外,我使用了 I2C 扫描仪,它 100% 确认激光雷达位于(默认地址)0x10 上。

Datasheet of (TF02 Pro): https://www.unmannedtechshop.co.uk/wp-content/uploads/2020/01/TF02-Pro-Product-Manual-Alpha.pdf (TF02 Pro) 数据表: https ://www.unmannedtechshop.co.uk/wp-content/uploads/2020/01/TF02-Pro-Product-Manual-Alpha.pdf

Question: am i reading from the right data address Any help would be appreciated.问题:我是否从正确的数据地址读取任何帮助将不胜感激。

According to the data sheet the correct read command is five bytes: 5A 05 00 01 60. It doesn't work like a typically i2c device.根据数据表,正确的读取命令是五个字节:5A 05 00 01 60。它不像典型的 i2c 设备那样工作。

unsigned int readDistance()
{
  unsigned int dist = 0 ; // LiDAR actually measured distance value
  
  // step 1: instruct sensor to read echoes
  Wire.beginTransmission(0x10) ; // transmit to device 0x10
  Wire.write(0x5A) ; // some read command.
  Wire.write(0x05) ;
  Wire.write(0x00) ;
  Wire.write(0x01) ;
  Wire.write(0x60) ;
  Wire.endTransmission() ; // stop transmitting
  
  // step 2: wait for readings to happen
  delay(100) ; // datasheet suggests at least 100ms
  
  // step 3: request reading from sensor
  Wire.requestFrom(0x10, 4) ; // request first 4 bytes from slave device #0x10
  Wire.read() ; Wire.read() ; //ignore header bytes
  // remaining two bytes for distance (DIST_L, DIST_H) 
  dist = Wire.read() ; dist += Wire.read() << 8 ; // calculate distance value, bit shift high distance
  return dist ; // return updated dist
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM