Ch-6 clean
This commit is contained in:
@@ -14,13 +14,9 @@ pio_input = """
|
||||
|
||||
assembled = adafruit_pioasm.assemble(pio_input)
|
||||
|
||||
sm = rp2pio.StateMachine(
|
||||
assembled,
|
||||
frequency=2000,
|
||||
first_in_pin=board.GP20
|
||||
)
|
||||
sm = rp2pio.StateMachine(assembled, frequency=2000, first_in_pin=board.GP20)
|
||||
|
||||
buffer = array.array('I', [0])
|
||||
buffer = array.array("I", [0])
|
||||
|
||||
while True:
|
||||
# read data from the fifo
|
||||
|
||||
@@ -15,13 +15,10 @@ assembled = adafruit_pioasm.assemble(pio_input)
|
||||
|
||||
## set up a statemachine
|
||||
sm = rp2pio.StateMachine(
|
||||
assembled,
|
||||
frequency=2000,
|
||||
first_in_pin=board.GP20,
|
||||
in_pin_count=2
|
||||
assembled, frequency=2000, first_in_pin=board.GP20, in_pin_count=2
|
||||
)
|
||||
|
||||
buffer = bytearray(1) # an array of bytes to read into - we are just asking for a byte
|
||||
buffer = bytearray(1) # an array of bytes to read into - we are just asking for a byte
|
||||
|
||||
while True:
|
||||
# read data from the fifo
|
||||
|
||||
@@ -34,7 +34,7 @@ sm = rp2pio.StateMachine(
|
||||
frequency=2000,
|
||||
)
|
||||
|
||||
buffer = array.array('I', [0])
|
||||
buffer = array.array("I", [0])
|
||||
|
||||
# read the data
|
||||
sm.readinto(buffer)
|
||||
|
||||
@@ -30,14 +30,12 @@ sm = rp2pio.StateMachine(
|
||||
)
|
||||
|
||||
|
||||
buffer = array.array('I', [0])
|
||||
buffer = array.array("I", [0])
|
||||
|
||||
sm.write(array.array('I', [0b01101000_00000000_00000000_00000000]))
|
||||
sm.write(array.array("I", [0b01101000_00000000_00000000_00000000]))
|
||||
# read the data
|
||||
sm.readinto(buffer)
|
||||
print("Initial Y: {0} 0b{0:032b} 0x{0:x}".format(buffer[0]))
|
||||
|
||||
sm.readinto(buffer)
|
||||
print("Y bit extracted: {0} 0b{0:032b} 0x{0:x}".format(buffer[0]))
|
||||
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import rp2pio
|
||||
import adafruit_pioasm
|
||||
import array
|
||||
|
||||
|
||||
def scenario(test_data, bit_to_extract, expected):
|
||||
program = f"""
|
||||
.program extract_bit
|
||||
@@ -41,7 +42,7 @@ def scenario(test_data, bit_to_extract, expected):
|
||||
assembled,
|
||||
frequency=2000,
|
||||
) as sm:
|
||||
buffer = array.array('I', [0])
|
||||
buffer = array.array("I", [0])
|
||||
|
||||
print("source bits: {0} 0b{0:032b} 0x{0:x}".format(test_data))
|
||||
sm.readinto(buffer)
|
||||
@@ -55,6 +56,7 @@ def scenario(test_data, bit_to_extract, expected):
|
||||
print("Y bit expected: {0} 0b{0:032b} 0x{0:x}".format(expected))
|
||||
print("")
|
||||
|
||||
|
||||
def pull_scenario(test_data, bit_to_extract, expected):
|
||||
program = f"""
|
||||
.program extract_bit_pull
|
||||
@@ -92,8 +94,8 @@ def pull_scenario(test_data, bit_to_extract, expected):
|
||||
assembled,
|
||||
frequency=2000,
|
||||
) as sm:
|
||||
sm.write(array.array('I', [test_data]))
|
||||
buffer = array.array('I', [0])
|
||||
sm.write(array.array("I", [test_data]))
|
||||
buffer = array.array("I", [0])
|
||||
|
||||
print("source bits: {0} 0b{0:032b} 0x{0:x}".format(test_data))
|
||||
sm.readinto(buffer)
|
||||
@@ -105,7 +107,8 @@ def pull_scenario(test_data, bit_to_extract, expected):
|
||||
if buffer[0] != expected:
|
||||
print("Didn't get what I expected")
|
||||
print("Y bit expected: {0} 0b{0:032b} 0x{0:x}".format(expected))
|
||||
print("")
|
||||
print("")
|
||||
|
||||
|
||||
scenario(0b101, 1, 0)
|
||||
scenario(0b110, 1, 1)
|
||||
@@ -126,4 +129,3 @@ pull_scenario(0b10111000_00000000_00000000_00000000, 30, 0)
|
||||
# v
|
||||
pull_scenario(0b10111000_00000000_00000000_00000000, 31, 1)
|
||||
pull_scenario(0b01111000_00000000_00000000_00000000, 31, 0)
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ sm = rp2pio.StateMachine(
|
||||
frequency=2000,
|
||||
)
|
||||
|
||||
buffer = array.array('i', [0])
|
||||
buffer = array.array("i", [0])
|
||||
|
||||
# read the data
|
||||
sm.readinto(buffer)
|
||||
|
||||
@@ -26,7 +26,7 @@ sm = rp2pio.StateMachine(
|
||||
frequency=2000,
|
||||
)
|
||||
|
||||
buffer = array.array('I', [0])
|
||||
buffer = array.array("I", [0])
|
||||
|
||||
# read the data
|
||||
sm.readinto(buffer)
|
||||
|
||||
@@ -28,7 +28,7 @@ sm = rp2pio.StateMachine(
|
||||
frequency=2000,
|
||||
)
|
||||
|
||||
buffer = array.array('I', [0])
|
||||
buffer = array.array("I", [0])
|
||||
|
||||
# read the data
|
||||
sm.readinto(buffer)
|
||||
|
||||
@@ -19,7 +19,7 @@ assembled = adafruit_pioasm.assemble(program)
|
||||
## set up a statemachine
|
||||
sm = rp2pio.StateMachine(assembled, frequency=2000)
|
||||
|
||||
buffer = array.array('I', [0])
|
||||
buffer = array.array("I", [0])
|
||||
|
||||
# read the data
|
||||
sm.readinto(buffer)
|
||||
|
||||
@@ -26,7 +26,7 @@ sm = rp2pio.StateMachine(
|
||||
frequency=2000,
|
||||
)
|
||||
|
||||
buffer = array.array('I', [0])
|
||||
buffer = array.array("I", [0])
|
||||
|
||||
# read the data
|
||||
sm.readinto(buffer)
|
||||
|
||||
@@ -29,7 +29,7 @@ sm = rp2pio.StateMachine(
|
||||
frequency=2000,
|
||||
)
|
||||
|
||||
buffer = array.array('I', [0])
|
||||
buffer = array.array("I", [0])
|
||||
|
||||
# read the data
|
||||
sm.readinto(buffer)
|
||||
|
||||
@@ -36,7 +36,7 @@ sm = rp2pio.StateMachine(
|
||||
frequency=2000,
|
||||
)
|
||||
|
||||
buffer = array.array('I', [0])
|
||||
buffer = array.array("I", [0])
|
||||
|
||||
# read the data
|
||||
sm.readinto(buffer)
|
||||
|
||||
@@ -25,13 +25,10 @@ assembled = adafruit_pioasm.assemble(program)
|
||||
|
||||
## set up a statemachine
|
||||
sm = rp2pio.StateMachine(
|
||||
assembled,
|
||||
frequency=20000,
|
||||
first_in_pin=board.GP20,
|
||||
in_pin_count=2
|
||||
assembled, frequency=20000, first_in_pin=board.GP20, in_pin_count=2
|
||||
)
|
||||
|
||||
buffer = array.array('I', [0])
|
||||
buffer = array.array("I", [0])
|
||||
|
||||
while True:
|
||||
sm.readinto(buffer)
|
||||
|
||||
@@ -25,26 +25,20 @@ assembled = adafruit_pioasm.assemble(program)
|
||||
|
||||
## set up a statemachine
|
||||
left_enc = rp2pio.StateMachine(
|
||||
assembled,
|
||||
frequency=0,
|
||||
first_in_pin=board.GP20,
|
||||
in_pin_count=2
|
||||
assembled, frequency=0, first_in_pin=board.GP20, in_pin_count=2
|
||||
)
|
||||
|
||||
right_enc = rp2pio.StateMachine(
|
||||
assembled,
|
||||
frequency=0,
|
||||
first_in_pin=board.GP26,
|
||||
in_pin_count=2
|
||||
)
|
||||
assembled, frequency=0, first_in_pin=board.GP26, in_pin_count=2
|
||||
)
|
||||
|
||||
buffer = array.array('I', [0])
|
||||
buffer = array.array("I", [0])
|
||||
|
||||
left_data = 0
|
||||
right_data = 0
|
||||
|
||||
while True:
|
||||
# read data from the fifo
|
||||
# read data from the fifo
|
||||
if left_enc.in_waiting:
|
||||
left_enc.readinto(buffer)
|
||||
left_data = buffer[0]
|
||||
|
||||
@@ -59,28 +59,20 @@ assembled = adafruit_pioasm.assemble(program)
|
||||
|
||||
## set up a statemachine
|
||||
left_enc = rp2pio.StateMachine(
|
||||
assembled,
|
||||
frequency=0,
|
||||
first_in_pin=board.GP20,
|
||||
jmp_pin=board.GP21,
|
||||
in_pin_count=2
|
||||
assembled, frequency=0, first_in_pin=board.GP20, jmp_pin=board.GP21, in_pin_count=2
|
||||
)
|
||||
|
||||
right_enc = rp2pio.StateMachine(
|
||||
assembled,
|
||||
frequency=0,
|
||||
first_in_pin=board.GP26,
|
||||
jmp_pin=board.GP27,
|
||||
in_pin_count=2
|
||||
)
|
||||
assembled, frequency=0, first_in_pin=board.GP26, jmp_pin=board.GP27, in_pin_count=2
|
||||
)
|
||||
|
||||
buffer = array.array('i', [0])
|
||||
buffer = array.array("i", [0])
|
||||
|
||||
left_data = 0
|
||||
right_data = 0
|
||||
|
||||
while True:
|
||||
# read data from the fifo
|
||||
# read data from the fifo
|
||||
if left_enc.in_waiting:
|
||||
left_enc.readinto(buffer)
|
||||
left_data = buffer[0]
|
||||
|
||||
@@ -12,13 +12,13 @@ last_y = 0
|
||||
|
||||
print("Entering main loop")
|
||||
while True:
|
||||
x = robot.left_encoder.read()
|
||||
y = robot.right_encoder.read()
|
||||
x = robot.left_encoder.read()
|
||||
y = robot.right_encoder.read()
|
||||
|
||||
x_diff = x - last_x
|
||||
y_diff = y - last_y
|
||||
x_diff = x - last_x
|
||||
y_diff = y - last_y
|
||||
|
||||
last_x = x
|
||||
last_y = y
|
||||
last_x = x
|
||||
last_y = y
|
||||
|
||||
mouse.move(x=x_diff, y=y_diff)
|
||||
mouse.move(x=x_diff, y=y_diff)
|
||||
|
||||
+20
-20
@@ -54,24 +54,24 @@ send:
|
||||
|
||||
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]
|
||||
|
||||
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]
|
||||
|
||||
@@ -13,12 +13,14 @@ 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, speed):
|
||||
# Swap motor pins if we reverse the speed
|
||||
if speed < 0:
|
||||
@@ -26,15 +28,16 @@ def set_speed(motor, speed):
|
||||
speed = -speed
|
||||
else:
|
||||
direction = motor
|
||||
speed = min(speed, 1) # limit to 1.0
|
||||
max_speed = 2**16-1
|
||||
|
||||
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):
|
||||
set_speed(left_motor, speed)
|
||||
|
||||
|
||||
def set_right(speed):
|
||||
set_speed(right_motor, speed)
|
||||
|
||||
@@ -6,8 +6,8 @@ robot.set_right(0.8)
|
||||
target = 4000
|
||||
|
||||
while robot.left_encoder.read() < target or robot.right_encoder.read() < target:
|
||||
if robot.left_encoder.read() >= target:
|
||||
robot.set_left(0)
|
||||
if robot.right_encoder.read() >= target:
|
||||
robot.set_right(0)
|
||||
print(robot.left_encoder.read(), robot.right_encoder.read())
|
||||
if robot.left_encoder.read() >= target:
|
||||
robot.set_left(0)
|
||||
if robot.right_encoder.read() >= target:
|
||||
robot.set_right(0)
|
||||
print(robot.left_encoder.read(), robot.right_encoder.read())
|
||||
|
||||
Reference in New Issue
Block a user