簡體   English   中英

如何在 I2C 中使用 Arduino 從 LIDAR 的正確地址讀取?

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

我需要使用 Arduino Nano 從 I2C 中的激光雷達讀取距離數據。 目前,這是我編寫的代碼。

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
}

但是我感覺我從錯誤的數據地址請求',因為我沒有得到我期望的結果(即不同的距離數據)。 此外,我使用了 I2C 掃描儀,它 100% 確認激光雷達位於(默認地址)0x10 上。

(TF02 Pro) 數據表: https ://www.unmannedtechshop.co.uk/wp-content/uploads/2020/01/TF02-Pro-Product-Manual-Alpha.pdf

問題:我是否從正確的數據地址讀取任何幫助將不勝感激。

根據數據表,正確的讀取命令是五個字節: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