enable_uart=1
1. 개요
TF-Luna는 근적외선 레이저를 사용하는 거리센서이다. 가격이 비교적 저렴하고 아두이노나 라즈베리파이 관련 자료를 쉽게 찾을 수 있어 편리하다. 라즈베리파이에서 TF-Luna를 사용해 본다.
https://github.com/makerportal/tfluna-python
https://makersportal.com/blog/distance-detection-with-the-tf-luna-lidar-and-raspberry-pi
2. 아두이노에서 사용하기
Arduion IDE에서 sketch>Include Library>Manage Libraries를 선택하고 검색창에서 TFLI2C 검색하고 라이브러리를 설치를 한다. 전원과 UART pins (RXD/TXD)에 각각 연결한다. TF-Luna는 UART(Universal Asynchronous Receiver-Transmitter) 직렬 포트를 통해 Raspberry Pi와 통신한다.
3. Raspberty Pi4에서 실행하기
전원과 UART pins (RXD/TXD)에 각각 연결한다. 여기서 사용할 포트는 GPIO 핀 14/15(물리적 핀 8/10)에 연결된 미니 UART이다. 먼저 RPi의 부팅 구성 파일을 통해 포트를 활성화해야 한다.
> sudo nano /boot/config.txt
설정파일 맨 하단에 다음을 추가한다. 재부팅한다.
enable_uart=1
아래의 파이썬 코드는 baudrate 115200 을 사용하는 테스트 코드이다(상기 링크에서 받아 온 것이다).
import serial,time
import numpy as np
ser = serial.Serial("/dev/serial0", 115200,timeout=0) # mini UART serial device
def read_tfluna_data():
print('read ToF data from TF-Luna')
while True:
counter = ser.in_waiting # count the number of bytes of the serial port
if counter > 8:
bytes_serial = ser.read(9) # read 9 bytes
ser.reset_input_buffer() # reset buffer
if bytes_serial[0] == 0x59 and bytes_serial[1] == 0x59: # check first two bytes
distance = bytes_serial[2] + bytes_serial[3]*256 # distance in next two bytes
strength = bytes_serial[4] + bytes_serial[5]*256 # signal strength in next two bytes
temperature = bytes_serial[6] + bytes_serial[7]*256 # temp in next two bytes
temperature = (temperature/8.0) - 256.0 # temp scaling and offset
return distance/100.0,strength,temperature
if __name__ == '__main__':
if ser.isOpen() == False:
ser.open() # open serial port if not open
distance,strength,temperature = read_tfluna_data() # read values
print('Distance: {0:2.2f} m, Strength: {1:2.0f} / 65535 (16-bit), Chip Temperature: {2:2.1f} C'.\
format(distance,strength,temperature)) # print sample data
ser.close() # close serial port