chapter 9 code
This commit is contained in:
@@ -7,39 +7,29 @@ import robot
|
||||
|
||||
uart = busio.UART(board.GP12,board.GP13,baudrate=9600, timeout=0.01)
|
||||
|
||||
class RobotControl:
|
||||
stop_at = 0
|
||||
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()
|
||||
stop_at = 0
|
||||
speed = 0.8
|
||||
|
||||
print("Waiting for control signals...")
|
||||
while True:
|
||||
data = uart.read(32)
|
||||
if data is not None and data.startswith(b'!B'):
|
||||
packet = ButtonPacket.from_bytes(data)
|
||||
print("Button:", packet.button, "pressed:", packet.pressed)
|
||||
if packet.pressed:
|
||||
control.send_control(packet)
|
||||
else:
|
||||
robot.stop()
|
||||
if uart.in_waiting:
|
||||
packet = ButtonPacket.from_stream(uart)
|
||||
if packet:
|
||||
if not packet.pressed:
|
||||
robot.stop()
|
||||
elif packet.button == ButtonPacket.UP:
|
||||
robot.set_left(speed)
|
||||
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():
|
||||
robot.stop()
|
||||
if time.time() > stop_at:
|
||||
robot.stop()
|
||||
|
||||
Reference in New Issue
Block a user