Source

python-socket-examples / can_controlled_car.py

Full commit
import threading
import queue

from real_raw_can import filtered_receiver
from bluetooth_car import BluetoothCar

def can_drive_car(stop, q):
    car = BluetoothCar()
    duration = 0.1
    commands = {
        'up': car.forwards,
        'down': car.reverse,
        'left': car.left,
        'right': car.right
        }
    
    while not stop.is_set():
        command = q.get().decode()
        if command in commands:
            commands[command](duration)


if __name__ == "__main__":
    stop = threading.Event()
    q = queue.Queue()
    # Start a direction message monitor
    threading.Thread(target=filtered_receiver, args=(stop, 0x0042, q)).start()
    can_drive_car(stop, q)