Created
August 21, 2022 17:17
-
-
Save Dhruvacube/5305c65dd47e3b73976e5baa312769f8 to your computer and use it in GitHub Desktop.
Thr RC CAR in ESP32 written in micropython
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from machine import UART, Pin, lightsleep | |
from micropython import const | |
#Instruction class | |
class _Instruction: | |
left = 'L' | |
right = 'R' | |
backward = 'B' | |
forward = 'F' | |
stop = 'S' | |
back_left = 'H' | |
back_right = 'J' | |
forward_left = 'G' | |
forward_right = 'I' | |
INSTRUCTION = _Instruction() | |
#Values of PWM signals | |
class _PWM: | |
HIGH = 1 | |
LOW=0 | |
PWM = _PWM() | |
class _motor_pins: | |
#motor pins no | |
motorleftpinno2 = const(11) #left motor pin 2 (counter clockwise) | |
motorleftpinno1 = const(10) #left motor pin 1 (clockwise) | |
motorrightpinno2 = const(13) #right motor pin 2 (counter clockwise) | |
motorrightpinno1 = const(12) #right motor pin 1 (clockwise) | |
def __init__(self, *args, **kwargs): | |
#motor pins variable | |
self.motorleftpin2 = Pin(self.motorleftpinno2, Pin.OUT) #left motor pin 2 (counter clockwise) | |
self.motorleftpin1 = Pin(self.motorleftpinno1, Pin.OUT) #left motor pin 1 (clockwise) | |
self.motorrightpin2 = Pin(self.motorrightpinno2, Pin.OUT) #right motor pin 2 (counterclockwise) | |
self.motorrightpin1 = Pin(self.motorrightpinno1, Pin.OUT) #right motor pin 1 (clockwise) | |
#setting the pins to low | |
self.stop() | |
def set_moter_clockwise(self, left = PWM.HIGH, right = PWM.HIGH): | |
if left == right: | |
self.set_moter_counter_clockwise(not left, not right) | |
self.motorleftpin1.value(left) | |
self.motorrightpin1.value(right) | |
def set_moter_counter_clockwise(self, left = PWM.HIGH, right = PWM.HIGH): | |
if left == right: | |
self.set_moter_clockwise(not left, not right) | |
self.motorleftpin2.value(left) | |
self.motorrightpin2.value(right) | |
def stop(self): | |
self.motorleftpin2.value(PWM.LOW) | |
self.motorleftpin1.value(PWM.LOW) | |
self.motorrightpin2.value(PWM.LOW) | |
self.motorrightpin1.value(PWM.LOW) | |
MOTOR_PINS = _motor_pins() | |
uart = UART(2, const(9600)) | |
state = 0 #bluetooth state | |
while True: | |
if not bool(uart.any()): | |
continue | |
state = uart.read().decode('ascii').upper() | |
if state == INSTRUCTION.left: | |
uart.write('Moving Left') | |
MOTOR_PINS.set_moter_clockwise(left=PWM.LOW) | |
MOTOR_PINS.set_moter_counter_clockwise(right=PWM.LOW) | |
elif state == INSTRUCTION.right: | |
uart.write('Moving Right') | |
MOTOR_PINS.set_moter_clockwise(right=PWM.LOW) | |
MOTOR_PINS.set_moter_counter_clockwise(left=PWM.LOW) | |
elif state == INSTRUCTION.backward: | |
uart.write('Moving Backwards') | |
MOTOR_PINS.set_moter_counter_clockwise() | |
elif state == INSTRUCTION.forward: | |
uart.write('Moving Backwards') | |
MOTOR_PINS.set_moter_clockwise() | |
elif state == INSTRUCTION.back_left: | |
state = INSTRUCTION.right | |
continue | |
elif state == INSTRUCTION.back_right: | |
state = INSTRUCTION.left | |
continue | |
elif state == INSTRUCTION.forward_left: | |
state = INSTRUCTION.left | |
continue | |
elif state == INSTRUCTION.forward_right: | |
state = INSTRUCTION.right | |
continue | |
elif state == INSTRUCTION.stop: | |
uart.write('Stopping') | |
MOTOR_PINS.stop() | |
state=0 | |
lightsleep(100) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment