chapter 9 code
This commit is contained in:
-5
@@ -1,12 +1,7 @@
|
|||||||
import board
|
import board
|
||||||
from digitalio import DigitalInOut
|
|
||||||
import time
|
|
||||||
import busio
|
import busio
|
||||||
import supervisor
|
import supervisor
|
||||||
|
|
||||||
led = DigitalInOut(board.LED)
|
|
||||||
led.switch_to_output()
|
|
||||||
|
|
||||||
# PICO UART pins? What am i not using?
|
# PICO UART pins? What am i not using?
|
||||||
uart = busio.UART(board.GP12,board.GP13,baudrate=9600, timeout=0.01)
|
uart = busio.UART(board.GP12,board.GP13,baudrate=9600, timeout=0.01)
|
||||||
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import board
|
||||||
|
import busio
|
||||||
|
|
||||||
|
uart = busio.UART(board.GP12, board.GP13, baudrate=9600)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
if uart.read(32) is not None:
|
||||||
|
uart.write("Hello, Bluetooth World!\n".encode())
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import board
|
||||||
|
import time
|
||||||
|
import busio
|
||||||
|
import robot
|
||||||
|
|
||||||
|
uart = busio.UART(board.GP12, board.GP13, baudrate=9600)
|
||||||
|
|
||||||
|
robot.left_distance.distance_mode = 1
|
||||||
|
robot.left_distance.start_ranging()
|
||||||
|
robot.right_distance.distance_mode = 1
|
||||||
|
robot.right_distance.start_ranging()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
if robot.left_distance.data_ready and robot.right_distance.data_ready:
|
||||||
|
sensor1 = robot.left_distance.distance
|
||||||
|
sensor2 = robot.right_distance.distance
|
||||||
|
uart.write(f"{sensor1},{sensor2}\n".encode())
|
||||||
|
robot.left_distance.clear_interrupt()
|
||||||
|
robot.right_distance.clear_interrupt()
|
||||||
|
time.sleep(0.05)
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
import board
|
|
||||||
import time
|
|
||||||
import busio
|
|
||||||
import robot
|
|
||||||
|
|
||||||
uart = busio.UART(board.GP12,board.GP13,baudrate=9600, timeout=0.01)
|
|
||||||
|
|
||||||
print("Initialising sensors")
|
|
||||||
robot.left_distance.distance_mode = 1
|
|
||||||
robot.left_distance.start_ranging()
|
|
||||||
robot.right_distance.distance_mode = 1
|
|
||||||
robot.right_distance.start_ranging()
|
|
||||||
|
|
||||||
print("Sending via UART...")
|
|
||||||
while True:
|
|
||||||
if robot.left_distance.data_ready and robot.right_distance.data_ready:
|
|
||||||
sensor1 = robot.left_distance.distance
|
|
||||||
sensor2 = robot.right_distance.distance
|
|
||||||
try:
|
|
||||||
sensor_packet = f"{sensor1},{sensor2},10,15,49.23,-3\n"
|
|
||||||
print(sensor_packet, end='')
|
|
||||||
uart.write(sensor_packet.encode('utf-8'))
|
|
||||||
finally:
|
|
||||||
robot.left_distance.clear_interrupt()
|
|
||||||
robot.right_distance.clear_interrupt()
|
|
||||||
time.sleep(0.05)
|
|
||||||
|
|
||||||
# installinig the bluetooth update ovre the air makes it work better with
|
|
||||||
# floating point, 6 channels, negatives all confirmed.
|
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import board
|
||||||
|
import busio
|
||||||
|
|
||||||
|
uart = busio.UART(board.GP12,board.GP13,baudrate=9600, timeout=0.01)
|
||||||
|
|
||||||
|
print("Waiting for bytes on UART...")
|
||||||
|
while True:
|
||||||
|
if uart.in_waiting:
|
||||||
|
print(uart.read(32))
|
||||||
@@ -7,39 +7,29 @@ import robot
|
|||||||
|
|
||||||
uart = busio.UART(board.GP12,board.GP13,baudrate=9600, timeout=0.01)
|
uart = busio.UART(board.GP12,board.GP13,baudrate=9600, timeout=0.01)
|
||||||
|
|
||||||
class RobotControl:
|
stop_at = 0
|
||||||
stop_at = 0
|
speed = 0.8
|
||||||
speed = 0.8
|
|
||||||
timeout = 3
|
|
||||||
|
|
||||||
def send_control(self, packet):
|
|
||||||
if packet.button == ButtonPacket.UP:
|
|
||||||
robot.set_left(self.speed)
|
|
||||||
robot.set_right(self.speed)
|
|
||||||
elif packet.button == ButtonPacket.DOWN:
|
|
||||||
robot.set_left(-self.speed)
|
|
||||||
robot.set_right(-self.speed)
|
|
||||||
elif packet.button == ButtonPacket.LEFT:
|
|
||||||
robot.set_left(-self.speed)
|
|
||||||
robot.set_right(self.speed)
|
|
||||||
elif packet.button == ButtonPacket.RIGHT:
|
|
||||||
robot.set_left(self.speed)
|
|
||||||
robot.set_right(-self.speed)
|
|
||||||
self.stop_at = time.time() + self.timeout
|
|
||||||
|
|
||||||
|
|
||||||
control = RobotControl()
|
|
||||||
|
|
||||||
print("Waiting for control signals...")
|
print("Waiting for control signals...")
|
||||||
while True:
|
while True:
|
||||||
data = uart.read(32)
|
if uart.in_waiting:
|
||||||
if data is not None and data.startswith(b'!B'):
|
packet = ButtonPacket.from_stream(uart)
|
||||||
packet = ButtonPacket.from_bytes(data)
|
if packet:
|
||||||
print("Button:", packet.button, "pressed:", packet.pressed)
|
if not packet.pressed:
|
||||||
if packet.pressed:
|
robot.stop()
|
||||||
control.send_control(packet)
|
elif packet.button == ButtonPacket.UP:
|
||||||
else:
|
robot.set_left(speed)
|
||||||
robot.stop()
|
robot.set_right(speed)
|
||||||
|
elif packet.button == ButtonPacket.DOWN:
|
||||||
|
robot.set_left(-speed)
|
||||||
|
robot.set_right(-speed)
|
||||||
|
elif packet.button == ButtonPacket.LEFT:
|
||||||
|
robot.set_left(-speed)
|
||||||
|
robot.set_right(speed)
|
||||||
|
elif packet.button == ButtonPacket.RIGHT:
|
||||||
|
robot.set_left(speed)
|
||||||
|
robot.set_right(-speed)
|
||||||
|
stop_at = time.time() + 3
|
||||||
|
|
||||||
if control.stop_at < time.time():
|
if time.time() > stop_at:
|
||||||
robot.stop()
|
robot.stop()
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import board
|
||||||
|
import time
|
||||||
|
import busio
|
||||||
|
from adafruit_bluefruit_connect.accelerometer_packet import AccelerometerPacket
|
||||||
|
import robot
|
||||||
|
|
||||||
|
|
||||||
|
uart = busio.UART(board.GP12,board.GP13,baudrate=9600, timeout=0.01)
|
||||||
|
|
||||||
|
stop_at = 0
|
||||||
|
speed = 0.8
|
||||||
|
|
||||||
|
print("Waiting for accelerometer signals...")
|
||||||
|
while True:
|
||||||
|
if uart.in_waiting:
|
||||||
|
packet = AccelerometerPacket.from_stream(uart)
|
||||||
|
speed = packet.z * 0.1
|
||||||
|
turning = packet.y * 0.1
|
||||||
|
if abs(speed) > 0.3 or abs(turning) > 0.1:
|
||||||
|
print("speed:", speed, "turning:", turning)
|
||||||
|
robot.set_left(speed + turning)
|
||||||
|
robot.set_right(speed - turning)
|
||||||
|
else:
|
||||||
|
robot.stop()
|
||||||
|
stop_at = time.time() + 1
|
||||||
|
|
||||||
|
if time.time() > stop_at:
|
||||||
|
robot.stop()
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
import rp2pio
|
||||||
|
import adafruit_pioasm
|
||||||
|
import array
|
||||||
|
|
||||||
|
program = """
|
||||||
|
; use the osr for count
|
||||||
|
; input pins c1 c2
|
||||||
|
|
||||||
|
set y, 0 ; clear y
|
||||||
|
mov osr, y ; and clear osr
|
||||||
|
read:
|
||||||
|
; x will be the old value
|
||||||
|
; y the new values
|
||||||
|
mov x, y ; store old Y in x
|
||||||
|
in null, 32 ; Clear ISR - using y
|
||||||
|
in pins, 2 ; read two pins into y
|
||||||
|
mov y, isr
|
||||||
|
jmp x!=y, different ; Jump if its different
|
||||||
|
jmp read ; otherwise loop back to read
|
||||||
|
|
||||||
|
different:
|
||||||
|
; x has old value, y has new.
|
||||||
|
; extract the upper bit of X.
|
||||||
|
in x, 31 ; get bit 31 - old p1 (remember which direction it came in)
|
||||||
|
in null, 31 ; keep only 1 bit
|
||||||
|
mov x, isr ; put this back in x
|
||||||
|
jmp !x, c1_old_zero
|
||||||
|
|
||||||
|
c1_old_not_zero:
|
||||||
|
jmp pin, count_up
|
||||||
|
jmp count_down
|
||||||
|
|
||||||
|
c1_old_zero:
|
||||||
|
jmp pin, count_down
|
||||||
|
; fall through
|
||||||
|
count_up:
|
||||||
|
; for a clockwise move - we'll add 1 by inverting
|
||||||
|
mov x, ~ osr ; store inverted OSR on x
|
||||||
|
jmp x--, fake ; use jump to take off 1
|
||||||
|
fake:
|
||||||
|
mov x, ~ x ; invert back
|
||||||
|
jmp send
|
||||||
|
count_down:
|
||||||
|
; for a clockwise move, just take one off
|
||||||
|
mov x, osr ; store osr in x
|
||||||
|
jmp x--, send ; dec and send
|
||||||
|
send:
|
||||||
|
; send x.
|
||||||
|
mov isr, x ; send it
|
||||||
|
push noblock ; put ISR into input FIFO
|
||||||
|
mov osr, x ; put X back in OSR
|
||||||
|
jmp read ; loop back
|
||||||
|
"""
|
||||||
|
|
||||||
|
assembled = adafruit_pioasm.assemble(program)
|
||||||
|
|
||||||
|
|
||||||
|
class QuadratureEncoder:
|
||||||
|
def __init__(self, first_pin, second_pin, reversed=False):
|
||||||
|
"""Encoder with 2 pins. Must use sequential pins on the board"""
|
||||||
|
self.sm = rp2pio.StateMachine(
|
||||||
|
assembled,
|
||||||
|
frequency=0,
|
||||||
|
first_in_pin=first_pin,
|
||||||
|
jmp_pin=second_pin,
|
||||||
|
in_pin_count=2,
|
||||||
|
)
|
||||||
|
self.reversed = reversed
|
||||||
|
self._buffer = array.array("i", [0])
|
||||||
|
|
||||||
|
def read(self):
|
||||||
|
while self.sm.in_waiting:
|
||||||
|
self.sm.readinto(self._buffer)
|
||||||
|
if self.reversed:
|
||||||
|
return -self._buffer[0]
|
||||||
|
else:
|
||||||
|
return self._buffer[0]
|
||||||
+43
@@ -0,0 +1,43 @@
|
|||||||
|
import board
|
||||||
|
import pwmio
|
||||||
|
import pio_encoder
|
||||||
|
|
||||||
|
motor_A1 = pwmio.PWMOut(board.GP17)
|
||||||
|
motor_A2 = pwmio.PWMOut(board.GP16)
|
||||||
|
motor_B1 = pwmio.PWMOut(board.GP18)
|
||||||
|
motor_B2 = pwmio.PWMOut(board.GP19)
|
||||||
|
|
||||||
|
right_motor = motor_A1, motor_A2
|
||||||
|
left_motor = motor_B1, motor_B2
|
||||||
|
|
||||||
|
right_encoder = pio_encoder.QuadratureEncoder(board.GP20, board.GP21, reversed=True)
|
||||||
|
left_encoder = pio_encoder.QuadratureEncoder(board.GP26, board.GP27)
|
||||||
|
|
||||||
|
|
||||||
|
def stop():
|
||||||
|
motor_A1.duty_cycle = 0
|
||||||
|
motor_A2.duty_cycle = 0
|
||||||
|
motor_B1.duty_cycle = 0
|
||||||
|
motor_B2.duty_cycle = 0
|
||||||
|
|
||||||
|
|
||||||
|
def set_speed(motor: tuple, speed: float):
|
||||||
|
# Swap motor pins if we reverse the speed
|
||||||
|
if speed < 0:
|
||||||
|
direction = motor[1], motor[0]
|
||||||
|
speed = -speed
|
||||||
|
else:
|
||||||
|
direction = motor
|
||||||
|
speed = min(speed, 1) # limit to 1.0
|
||||||
|
max_speed = 2 ** 16 - 1
|
||||||
|
|
||||||
|
direction[0].duty_cycle = int(max_speed * speed)
|
||||||
|
direction[1].duty_cycle = 0
|
||||||
|
|
||||||
|
|
||||||
|
def set_left(speed: float):
|
||||||
|
set_speed(left_motor, speed)
|
||||||
|
|
||||||
|
|
||||||
|
def set_right(speed: float):
|
||||||
|
set_speed(right_motor, speed)
|
||||||
Reference in New Issue
Block a user