|
| 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 | + |
| 11 | +from devices import detectJoystick |
| 12 | +from joystick import JoyStick, BUTTON_A, BUTTON_X |
| 13 | + |
| 14 | +SPEED = 100 |
| 15 | +STEERING = 90 |
| 16 | + |
| 17 | +STATUS_STOPPED = 0 |
| 18 | +STATUS_FORWARD = 1 |
| 19 | +STATUS_BACKWARD = 2 |
| 20 | +STATUS_STEERING = 3 |
| 21 | + |
| 22 | +COLORS = (None, Color.GREEN, Color.RED, Color.YELLOW) |
| 23 | + |
| 24 | +class Driver(): |
| 25 | + def __init__(self, leftMotor, rightMotor, diameter, axle): |
| 26 | + self.driver = DriveBase(leftMotor, rightMotor, diameter, axle) |
| 27 | + self.x = 0 |
| 28 | + self.y = 0 |
| 29 | + self.speed = 0 |
| 30 | + self.steering = 0 |
| 31 | + |
| 32 | + def drive(self, speed, steering): |
| 33 | + self.speed = speed |
| 34 | + self.steering = steering |
| 35 | + if self.speed == 0: |
| 36 | + self.driver.stop() |
| 37 | + else: |
| 38 | + self.driver.drive(self.speed, self.steering) |
| 39 | + |
| 40 | +class Robot(): |
| 41 | + def __init__(self, leftMotor, rightMotor, topMotor, diameter, axle, maxSpeed=300, maxSteering=180, port=Port.S4): |
| 42 | + self.driver = Driver(leftMotor, rightMotor, diameter, axle) |
| 43 | + self.cannon = topMotor |
| 44 | + self.ultrasonic = UltrasonicSensor(port) |
| 45 | + self.speedStep = 32767 // maxSpeed |
| 46 | + self.steeringStep = 32767 // maxSteering |
| 47 | + self.active = True |
| 48 | + |
| 49 | + def drive(self, x, y): |
| 50 | + # map y (-32768 ~ +32767) to speed (+maxSpeed ~ -maxSpeed): |
| 51 | + speed = -y // self.speedStep |
| 52 | + # map x (-32768 ~ +32767) to steering (-maxSteering ~ +maxSteering): |
| 53 | + steering = x // self.steeringStep |
| 54 | + self.driver.drive(speed, steering) |
| 55 | + |
| 56 | + def target(self, x): |
| 57 | + self.cannon.run(-x // 327) |
| 58 | + |
| 59 | + def fire(self): |
| 60 | + brick.sound.file('cannon.wav') |
| 61 | + |
| 62 | + def inactive(self): |
| 63 | + self.active = False |
| 64 | + self.drive(0, 0) |
| 65 | + brick.sound.beep() |
| 66 | + |
| 67 | +def autoStopLoop(robot): |
| 68 | + while robot.active: |
| 69 | + if robot.ultrasonic.distance() < 200: |
| 70 | + robot.drive(0, 0) |
| 71 | + wait(100) |
| 72 | + |
| 73 | +def main(): |
| 74 | + brick.sound.beep() |
| 75 | + joystickEvent = detectJoystick(['Controller']) |
| 76 | + if joystickEvent: |
| 77 | + robot = Robot(Motor(Port.D), Motor(Port.A), Motor(Port.B), 55, 200) |
| 78 | + t = threading.Thread(target=autoStopLoop, args=(robot,)) |
| 79 | + t.start() |
| 80 | + |
| 81 | + def onButtonPressed(code): |
| 82 | + if code == BUTTON_X: |
| 83 | + robot.inactive() |
| 84 | + return False |
| 85 | + if code == BUTTON_A: |
| 86 | + robot.fire() |
| 87 | + return True |
| 88 | + |
| 89 | + def onLeftJoyChanged(x, y): |
| 90 | + robot.drive(x, y) |
| 91 | + |
| 92 | + def onRightJoyChanged(x, y): |
| 93 | + robot.target(x) |
| 94 | + |
| 95 | + joystick = JoyStick(joystickEvent) |
| 96 | + joystick.setButtonHandler(onButtonPressed) |
| 97 | + joystick.setJoyLeftHandler(onLeftJoyChanged) |
| 98 | + joystick.setJoyRightHandler(onRightJoyChanged) |
| 99 | + joystick.startLoop() |
| 100 | + else: |
| 101 | + brick.sound.beep() |
| 102 | + |
| 103 | +main() |
0 commit comments