TFmini s Lidar with Jetson Nano - Dieptranivsr/DroneIVSR GitHub Wiki

Required components:-
1.Jetson nano

2.Jumper Wires

Steps for connection:-
1) Connect Red wire of TFMini Lidar with jetson nano pin 2 (5v)*.

2) Connect Black wire of TFMini Lidar with jetson nano pin 6 (GND)*.

3) Connect WHITE wire of TFMini Lidar with jetson nano pin 8 (GPIO 14)*.

4) Connect Green wire of TFMini Lidar with jetson nano pin 10 (GPIO 15)*.

         TFMini Pinout     |    Jetson nano Pinout
       ==============================================
            Red            |        5V(Pin 2)
            Black          |       GND(Pin 6)
            White          |      GPIO 14(Pin 8)
            Green          |      GPIO 15(Pin10)
       ==============================================   
       
Command line procedure:-
    sudo nano “your file name”
    ls
    sudo python tfmini1.py

Code

# -*- coding: utf-8 -*
import serial
import time

ser = serial.Serial("/dev/ttyTHS1", 115200)

def getTFminiData():
    while True:
        #time.sleep(0.1)
        count = ser.in_waiting
        if count > 8:
            recv = ser.read(9)  
            ser.reset_input_buffer()  
            # type(recv), 'str' in python2(recv[0] = 'Y'), 'bytes' in python3(recv[0] = 89)
            # type(recv[0]), 'str' in python2, 'int' in python3 
            
            if recv[0] == 0x59 and recv[1] == 0x59:     #python3
                distance = recv[2] + recv[3] * 256
                strength = recv[4] + recv[5] * 256
                print('(', distance, ',', strength, ')')
                ser.reset_input_buffer()
                
            if recv[0] == 'Y' and recv[1] == 'Y':     #python2
                lowD = int(recv[2].encode('hex'), 16)      
                highD = int(recv[3].encode('hex'), 16)
                lowS = int(recv[4].encode('hex'), 16)      
                highS = int(recv[5].encode('hex'), 16)
                distance = lowD + highD * 256
                strength = lowS + highS * 256
                print(distance, strength)
                
            
            # you can also distinguish python2 and python3: 
            #import sys
            #sys.version[0] == '2'    #True, python2
            #sys.version[0] == '3'    #True, python3


if __name__ == '__main__':
    try:
        if ser.is_open == False:
            ser.open()
        getTFminiData()
    except KeyboardInterrupt:   # Ctrl+C
        if ser != None:
            ser.close()