Search code examples
pythonpython-3.xraspberry-piraspberry-pi4lidar

Lidar and Servo on Raspberry pi 4


I've two separate code bases for Tf-mini(Lidar) and servo motor. I want the both code to run at the same time. I tried to combine them in to a single code, But only one code runs at a time. Please help to rewrite or edit the code.

Code for servo motor here:

    import RPi.GPIO as GPIO
    import time
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(7, GPIO.OUT)
    p = GPIO.PWM(7, 100)
    t = 0.007
    r = 20 
    p.start(r)
    while True:
    for i in range(5,r):
            p.ChangeDutyCycle(i)
            print(i)
            time.sleep(t)
    for i in range(r,5,-1):
            p.ChangeDutyCycle(i)
            print(i)
            time.sleep(t)`

Code for lidar here:

import time
import serial


ser = serial.Serial("/dev/ttyAMA0", 115200)
def read_data():
while True:
    counter = ser.in_waiting 
    if counter > 8:
        bytes_serial = ser.read(9)
        ser.reset_input_buffer()

        if bytes_serial[0] == 0x59 and bytes_serial[1] == 0x59: # this portion is for python3
            print("Printing python3 portion")            
            distance = bytes_serial[2] + bytes_serial[3]*256 # multiplied by 256, because the binary 
        data is shifted by 8 to the left (equivalent to "<< 8").                                              
        # Dist_L, could simply be added resulting in 16-bit data of Dist_Total.
            strength = bytes_serial[4] + bytes_serial[5]*256
            temperature = bytes_serial[6] + bytes_serial[7]*256
            temperature = (temperature/8) - 256
            print("Distance:"+ str(distance))
            print("Strength:" + str(strength))
            if temperature != 0:
                print("Temperature:" + str(temperature))
            ser.reset_input_buffer()

        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("Printing python2 portion")
            print("Distance:"+ str(distance) + "\n")
            print("Strength:" + str(strength) + "\n")
            print("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")

Please help me to combine these two code to run at the same time.


Solution

  • I think I see your problem. Both segments of code are sequentially driven and hence it will create problems. Here is what you need to do (pseudo code):

    loop forever:
         t1=start_time
         change duty cycle #remove the wait code
         poll the serial port #the ladar code
         t2=end_time
         if t2 - t1 < t:
            wait (t - (t2-t1))
    

    Being an old Arduino fan myself, I see the struggles of handling the servo motor. I have a simple library to do most of the dirty work for me. You are most welcome to use my library via any of the two links: https://github.com/vikramdayal/RaspberryMotors or https://pypi.org/project/RaspberryMotors/#description