Raspberry Pi · guide
TF-Luna 接入 树莓派 CAN / UART · Raspberry Pi:接入与配置指南
TF-Luna 接入 树莓派 的准备、连接、配置与代码要点。原始文件可在资料下载区获取。涉及接口:CAN、UART。
适用范围
TF-Luna · 树莓派 · CAN、UART · Raspberry Pi
原始英文技术说明
Interfacing TF-Luna Plus with Raspberry Pi 4B
Written by: Ibrahim (FAE)
TF-Luna interfacing with Raspberry Pi Benewake (Beijing) Co. Ltd.This article explains how to interface TF-Luna with Raspberry Pi 4B but it should work with all other versions of Raspberry Pi as long as the right pins are used. The following assumptions are made while writing this article:
1. The end-user has Raspberry Pi and TF-Luna in hands
2. The operating system (tested on Raspbian version 10 buster) is installed on Raspberry Pi 3. The serial port is already enabled 4. Python2 or 3 is installed 5. Python editor is required for writing the script. I have Thonny installed on my RasPi.
NOTE: If you have installed full Raspbian, all the required packages are included in full version.
TF-Luna interfacing with Raspberry Pi Benewake (Beijing) Co. Ltd.Pin configuration of Raspberry Pi:
The following image shows the pin out of Raspberry Pi board:
Figure 1: Pin-outs
We are using the serial pins for connecting TF-Luna with Raspberry Pi. Pin-8 (TXD) and Pin-10 (RXD) as enclosed in red rectangle. Pin 2 and 6 or 4 and 6 can be used as power source because the operating voltage of TF-Luna is 5V.
TF-Luna interfacing with Raspberry Pi Benewake (Beijing) Co. Ltd.Wire sequence of TF-Luna:
Figure 2 and table-I show all the details about pin description and the wiring sequence of TF- Luna.
Figure 2: Wiring sequence of TF-Luna
Table-I: Pin description
TF-Luna interfacing with Raspberry Pi Benewake (Beijing) Co. Ltd.Customized cable needed for proper connection:
If we look at the connector of TF-Luna, it is GH1.25-4P connector. On the other hand Raspberry Pi has pin headers. So we need a cable which has 4P 1.55MM bar connector (also called horizontal patch socket) on one side and female DuPont connector on other side for inserting it into Raspberry Pi. The following figure shows customized cable for this purpose.
Figure 3: GH1.25 4P to DuPont cable
Schematic diagram:
The next image show connection diagram of TF-Luna with Raspberry Pi.
Figure 4: Schematic
TF-Luna interfacing with Raspberry Pi Benewake (Beijing) Co. Ltd.It should be noted that if you are connecting more devices to your raspberry Pi, then it is highly recommended to use a high current power supply because the default adapter has only 2A current capacity. So choose your power supply depending upon the current requirement of the whole system. The actual connection settings can be seen in figure 5.
Figure 5: Actual connection
TF-Luna interfacing with Raspberry Pi Benewake (Beijing) Co. Ltd.Python code:
In this section we will discuss the main parts of python code that are used for reading the data from TF-Luna. The following tables show the serial protocol used by TF-Luna, and data format:
Table II: Communication protocol
Table III: Data format
TF-Luna interfacing with Raspberry Pi Benewake (Beijing) Co. Ltd.Following the above sequence of data, we will get the bytes from TF-Luna using Python. First of all we need to install the serial port library which can be done using pip installer (works with both Python2 and 3):
After successfully installing the library, it’s time to write the script and import the serial port library.
import serial
Create the object of class Serial and pass the necessary parameters (serial port and baud rate):
ser = serial.Serial("/dev/ttyAMA0", 115200)The main function starts from here and it searches if there the port is open and call the function read_data():
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")
The function read_data() checks the bytes, make some necessary shifting of bytes and then prints the data in terminal:
TF-Luna interfacing with Raspberry Pi Benewake (Beijing) Co. Ltd.# 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()
TF-Luna interfacing with Raspberry Pi Benewake (Beijing) Co. Ltd.This code has two parts; first part is compatible with Python3 (upper part) and second is compatible with Python2 (lower section). For different byte positions, please refer to Table-III. The distance measured by LIDAR can be observed in the following figure.
Figure 6: Distance measured by LiDAR
使用说明
按原始资料可读取的技术内容编排;具体的产品硬件版本、飞控/软件版本和现场安全边界仍应以原始资料及目标平台当前文档为准。
资料与下载
- Interfacing TF-Luna Plus with Raspberry · 英文原文(PDF)填写信息后下载
