Raspberry Pi · code
TF-Luna with Raspberry Pi UART: code example
Setup, connection, configuration and code guidance for TF-Luna with Raspberry Pi. The original files are available in Materials and downloads. Documented interface: UART.
Applicable setup
TF-Luna · Raspberry Pi · UART
Technical instructions and steps
# -- coding: utf-8 - # This script for Linux environment import time import serial
# Checked with TFLuna
# ser = serial.Serial("/dev/ttyUSB1", 115200)
ser = serial.Serial("/dev/ttyAMA0", 115200)# ser = serial.Serial("/dev/tty0", 115200) # ser = serial.Serial("COM14", 115200)
# we define a new function that will get the data from LiDAR and publish it def readdata(): while True: counter = ser.inwaiting # count the number of bytes of the serial port if counter > 8: bytesserial = ser.read(9) ser.resetinput_buffer()
if bytesserial[0] == 0x59 and bytesserial[1] == 0x59: # python3 print("TF-Luna python3 portion") distance = bytesserial[2] + bytesserial[3]256 strength = bytesserial[4] + bytesserial[5]256 temperature = bytesserial[6] + bytesserial[7]*256 # For TFLuna temperature = (temperature/8) - 256 print("Distance:"+ str(distance)) print("Strength:" + str(strength)) if temperature != 0: print("Chip Temperature:" + str(temperature)) ser.resetinputbuffer()
if bytes_serial[0] == "Y" and bytes_serial[1] == "Y":
distL = int(bytes_serial[2].encode("hex"), 16)
distH = int(bytes_serial[3].encode("hex"), 16)
stL = int(bytes_serial[4].encode("hex"), 16)
stH = int(bytes_serial[5].encode("hex"), 16)
distance = distL + distH*256
strength = stL + stH*256
tempL = int(bytes_serial[6].encode("hex"), 16)
tempH = int(bytes_serial[7].encode("hex"), 16)
temperature = tempL + tempH*256
temperature = (temperature/8) - 256
print("TF-Luna python2 portion")
print("Distance:"+ str(distance) + "\n")
print("Strength:" + str(strength) + "\n")
print("Chip Temperature:" + str(temperature) + "\n")
ser.reset_input_buffer()if name == "main": try: if ser.isOpen() == False: ser.open() read_data() except KeyboardInterrupt(): # ctrl + c in terminal. if ser != None: ser.close() print("program interrupted by the user")
Use and verification
The technical content follows the readable source material. Confirm the product hardware revision, flight-stack or software version, and field safety boundary against the original material and current platform documentation.
Materials and downloads
- -*- coding: utf-8 -* · English source (PY)Complete inquiry to download
