Add bluetooth module and pinout docs

master
Drew Bednar 3 years ago
parent 794869cfeb
commit 31d49b968f

Binary file not shown.

After

Width:  |  Height:  |  Size: 314 KiB

Binary file not shown.

Binary file not shown.

@ -0,0 +1,67 @@
"""
This modul implements a simple serial connection to the ZS-040 and will echo out to the console
any messages it receives. This board is a Bluetooth SPP (Serial Port Protocol) module.
When powered on the device will broadcast as BT-05 by default. Default passwords are usually 1234 or 0000.
"""
from select import select
from machine import Pin, UART
class ZS040:
def __init__(
self,
uart_id,
tx_pin,
rx_pin,
state_pin,
baudrate=38400,
bits=8,
stop=1,
parity=None,
):
self.uart = UART(
uart_id,
baudrate=baudrate,
bits=bits,
stop=stop,
parity=parity,
)
self.state_pin = Pin(state_pin, Pin.IN)
def read(self, nbytes=None):
return self.uart.read(nbytes)
def readline(self):
return self.uart.readline()
def write(self, buf):
self.uart.write(buf)
def any(self):
return self.uart.any()
@property
def state(self):
return self.state_pin.value()
@classmethod
def from_config(cls, pin_config):
return cls(
uart_id=pin_config["bluetooth"]["uart_id"],
tx_pin=pin_config["bluetooth"]["tx_pin"],
rx_pin=pin_config["bluetooth"]["rx_pin"],
state_pin=pin_config["bluetooth"]["state_pin"],
)
# while True:
# if BT_STATE_PIN.value():
# if uart.any():
# data = uart.read(1)
# print(data.decode("utf8"))
# else:
# print("No data. Sleeping")
# else:
# print("No Bluetooth device paired.")
# sleep_ms(500)

@ -0,0 +1 @@
__secret__ = "dirp"

@ -7,6 +7,7 @@ PIN_CONFIG = {
"encoder": {"cpr": 1320, "pin_a": 2, "pin_b": 3}, "encoder": {"cpr": 1320, "pin_a": 2, "pin_b": 3},
"motor": {"pwm": 19, "in1": 18, "in2": 17}, "motor": {"pwm": 19, "in1": 18, "in2": 17},
}, },
"bluetooth": {"uart_id": 8, "tx_pin": 35, "rx_pin": 34, "state_pin": 36},
} }
DUTY_MAX = 65535 DUTY_MAX = 65535

@ -12,27 +12,85 @@ class MotorController:
def __init__(self, left_motor, right_motor) -> None: def __init__(self, left_motor, right_motor) -> None:
self.left = left_motor self.left = left_motor
self.right = right_motor self.right = right_motor
self.speed = 65535 // 2
def forward(self): def forward(self):
"""Forward move"""
self.left.in1.value(1) self.left.in1.value(1)
self.left.in2.value(0) self.left.in2.value(0)
self.right.in2.value(1) self.right.in2.value(1)
self.right.in1.value(0) self.right.in1.value(0)
self.left.pwm.duty_u16(65535 // 2) self.left.pwm.duty_u16(self.speed)
self.right.pwm.duty_u16(65535 // 2) self.right.pwm.duty_u16(self.speed)
def reverse(self): def reverse(self):
"""Reverse move"""
self.left.in1.value(0) self.left.in1.value(0)
self.left.in2.value(1) self.left.in2.value(1)
self.right.in2.value(0) self.right.in2.value(0)
self.right.in1.value(1) self.right.in1.value(1)
self.left.pwm.duty_u16(65535 // 2) self.left.pwm.duty_u16(self.speed)
self.right.pwm.duty_u16(65535 // 2) self.right.pwm.duty_u16(self.speed)
def stop(self): def stop(self):
"""All stop"""
self.left.pwm.duty_u16(0) self.left.pwm.duty_u16(0)
self.right.pwm.duty_u16(0) self.right.pwm.duty_u16(0)
def right_forward(self):
"""Right turn while moving forward"""
self.left.in1.value(1)
self.left.in2.value(0)
self.right.in2.value(1)
self.right.in1.value(0)
self.left.pwm.duty_u16(self.speed)
self.right.pwm.duty_u16(self.speed // 2)
def right_spin(self):
"""Spins clockwise in place"""
self.left.in1.value(1)
self.left.in2.value(0)
self.right.in2.value(0)
self.right.in1.value(1)
self.left.pwm.duty_u16(self.speed)
self.right.pwm.duty_u16(self.speed // 2)
def right_reverse(self):
"""Right turn while moving in reverse"""
self.left.in1.value(0)
self.left.in2.value(1)
self.right.in2.value(0)
self.right.in1.value(1)
self.left.pwm.duty_u16(self.speed)
self.right.pwm.duty_u16(self.speed // 2)
def left_forward(self):
"""Right turn while moving forward"""
self.left.in1.value(1)
self.left.in2.value(0)
self.right.in2.value(1)
self.right.in1.value(0)
self.left.pwm.duty_u16(self.speed // 2)
self.right.pwm.duty_u16(self.speed)
def left_spin(self):
"""Spins counter-clockwise in place"""
self.left.in1.value(0)
self.left.in2.value(1)
self.right.in2.value(1)
self.right.in1.value(0)
self.left.pwm.duty_u16(self.speed)
self.right.pwm.duty_u16(self.speed)
def left_reverse(self):
"""Left turn while moving in reverse"""
self.left.in1.value(0)
self.left.in2.value(1)
self.right.in2.value(0)
self.right.in1.value(1)
self.left.pwm.duty_u16(self.speed // 2)
self.right.pwm.duty_u16(self.speed)
@classmethod @classmethod
def from_config(cls, pin_config): def from_config(cls, pin_config):
return cls( return cls(

Loading…
Cancel
Save