Remote Controlled Car - Code

From RoboWiki
Revision as of 11:27, 6 June 2024 by Robot (talk | contribs) (Created page with "Return back to project page: Remote Controlled Car - Jakub Vojtek Python code for the Line Following Car project: <syn...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Return back to project page: Remote Controlled Car - Jakub Vojtek

Python code for the Line Following Car project:

 
from bluedot import BlueDot
from buildhat import Motor
import serial
import time

left_wheel = Motor('A')
right_wheel = Motor('B')
bd = BlueDot(cols=2, rows=1)
bd[1, 0].color = "red"
bd[1, 0].square = True

ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
time.sleep(2)

def ambulance_mode_toggle():
    ser.write(b'A')

def forward():
    left_wheel.start(-30)
    right_wheel.start(30)

def backward():
    left_wheel.start(30)
    right_wheel.start(-30)

def stop():
    left_wheel.stop()
    right_wheel.stop()

def right():
    left_wheel.start(-30)
    right_wheel.stop()

def left():
    left_wheel.stop()
    right_wheel.start(30)

def move(pos):
    if (0.5 > pos.x > -0.5) and (pos.y > 0.5):
        forward()
    elif (0.5 > pos.x > -0.5) and (pos.y < -0.5):
        backward()
    elif pos.x < -0.5 and (0.5 > pos.y > -0.5):
        left()
    elif pos.x > 0.5 and (0.5 > pos.y > -0.5):
        right()
    elif pos.x > 0.5 and pos.y > 0.5:
        left_wheel.start(-30)
        right_wheel.start(10)
    elif pos.x < -0.5 and pos.y > 0.5:
        left_wheel.start(-10)
        right_wheel.start(30)
    elif pos.x > 0.5 and pos.y < -0.5:
        left_wheel.start(30)
        right_wheel.start(-10)
    elif pos.x < -0.5 and pos.y < -0.5:
        left_wheel.start(10)
        right_wheel.start(-30)

bd[0, 0].when_pressed = move
bd[0, 0].when_moved = move
bd[0, 0].when_released = stop

bd[1, 0].when_pressed = ambulance_mode_toggle

if __name__ == '__main__':
    try:
        while True:
            bd.wait_for_press()
    finally:
        ser.close()