|
| 1 | +#!/usr/bin/env pybricks-micropython |
| 2 | + |
| 3 | +import struct, threading |
| 4 | + |
| 5 | +from pybricks import ev3brick as brick |
| 6 | +from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor) |
| 7 | +from pybricks.parameters import (Port, Stop, Direction, Button, Color, SoundFile, ImageFile, Align) |
| 8 | +from pybricks.tools import print, wait, StopWatch |
| 9 | +from pybricks.robotics import DriveBase |
| 10 | +from devices import detectJoystick |
| 11 | + |
| 12 | +class Robot(): |
| 13 | + def __init__(self): |
| 14 | + self.motor = Motor(Port.B) |
| 15 | + self.ultrasonic = UltrasonicSensor(Port.S4) |
| 16 | + self.active = True |
| 17 | + self.speed = 0 |
| 18 | + self.colors = [None, Color.GREEN, Color.YELLOW, Color.RED] |
| 19 | + |
| 20 | + def setSpeed(self, acc): |
| 21 | + if acc < 0: |
| 22 | + self.speed = max(-3, self.speed - 1) |
| 23 | + elif acc > 0: |
| 24 | + self.speed = min(3, self.speed + 1) |
| 25 | + else: |
| 26 | + self.speed = 0 |
| 27 | + if self.speed != 0: |
| 28 | + self.motor.run(self.speed * 90) |
| 29 | + else: |
| 30 | + self.motor.stop() |
| 31 | + brick.light(self.colors[abs(self.speed)]) |
| 32 | + |
| 33 | + def inactive(self): |
| 34 | + self.active = False |
| 35 | + self.setSpeed(0) |
| 36 | + brick.sound.beep() |
| 37 | + |
| 38 | +def autoStopLoop(robot): |
| 39 | + while robot.active: |
| 40 | + if robot.speed > 0 and robot.ultrasonic.distance() < 200: |
| 41 | + robot.setSpeed(0) |
| 42 | + wait(100) |
| 43 | + |
| 44 | +def joystickLoop(robot, eventFile): |
| 45 | + FORMAT = 'llHHI' |
| 46 | + EVENT_SIZE = struct.calcsize(FORMAT) |
| 47 | + with open(eventFile, 'rb') as infile: |
| 48 | + while True: |
| 49 | + event = infile.read(EVENT_SIZE) |
| 50 | + _, _, t, c, v = struct.unpack(FORMAT, event) |
| 51 | + # button A, B: |
| 52 | + if t == 1 and v == 1: |
| 53 | + if c == 305: |
| 54 | + # press A: |
| 55 | + robot.setSpeed(1) |
| 56 | + elif c == 304: |
| 57 | + # press B: |
| 58 | + robot.setSpeed(-1) |
| 59 | + elif c == 307: |
| 60 | + # press X: |
| 61 | + return robot.inactive() |
| 62 | + elif t == 3: |
| 63 | + if c == 1: |
| 64 | + # Left stick & verticle: |
| 65 | + speed = 0 |
| 66 | + if v < 32768: |
| 67 | + # up: |
| 68 | + speed = 1 |
| 69 | + elif v > 32768: |
| 70 | + # down: |
| 71 | + speed = -1 |
| 72 | + robot.setSpeed(speed) |
| 73 | + |
| 74 | +def buttonLoop(robot): |
| 75 | + while True: |
| 76 | + if not any(brick.buttons()): |
| 77 | + wait(10) |
| 78 | + else: |
| 79 | + if Button.LEFT in brick.buttons(): |
| 80 | + robot.setSpeed(-1) |
| 81 | + elif Button.RIGHT in brick.buttons(): |
| 82 | + robot.setSpeed(1) |
| 83 | + elif Button.CENTER in brick.buttons(): |
| 84 | + robot.setSpeed(0) |
| 85 | + elif Button.UP in brick.buttons(): |
| 86 | + return robot.inactive() |
| 87 | + wait(500) |
| 88 | + |
| 89 | +def main(): |
| 90 | + brick.sound.beep() |
| 91 | + joystickEvent = detectJoystick(['Controller']) |
| 92 | + robot = Robot() |
| 93 | + t = threading.Thread(target=autoStopLoop, args=(robot,)) |
| 94 | + t.start() |
| 95 | + if joystickEvent: |
| 96 | + joystickLoop(robot, joystickEvent) |
| 97 | + else: |
| 98 | + buttonLoop(robot) |
| 99 | + |
| 100 | +main() |
0 commit comments