Sumobot Jack
unknown
python
a month ago
4.0 kB
5
Indexable
#!/usr/bin/env pybricks-micropython from pybricks.hubs import EV3Brick from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor) from pybricks.parameters import Port, Stop, Direction, Button, Color from pybricks.tools import wait, StopWatch, DataLog from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile import time #define the main ev3 brick used ev3 = EV3Brick() #define each of the things attached to the ev3 brick and their respective ports leftMotor = Motor(Port.B) rightMotor = Motor(Port.D) weapon = Motor(Port.A) colourSensor = ColorSensor(Port.S1) distanceSensor = UltrasonicSensor(Port.S3) button1 = TouchSensor(Port.S2) button2 = TouchSensor(Port.S4) #define the drivebase to control all the motors efficiently with their own functions (turn(), drive(), ect) robot = DriveBase(leftMotor, rightMotor, 55.5, 195) #beep when ready ev3.speaker.beep() ev3.speaker.beep() #press to start while (button1.pressed() == False) and (button2.pressed() == False): pass #define distance for ultrasonic sensor to trigger, and delay for the wait time between moving back and turning distance = 200 delay = 100 weapon_speed = 300 #create a dictionary for the different if functions for each sensor sensor_types = { "colour": colourSensor.color() != Color.BLACK, "distance": distanceSensor.distance() > distance, "bumper1": button1.pressed() == False, "bumper2": button2.pressed() == False } #define functions, then access them multiple times throughout the code with different variables being assigned to "type" def sensor(Type): #define dictionary locally, #because when the code looks for the dict, it first checks the function it is in, then the rest of the code, #resulting in a more efficient response types = { "colour": colourSensor.color() != Color.BLACK, "distance": distanceSensor.distance() > distance, "bumper1": button1.pressed() == False, "bumper2": button2.pressed() == False } #if all need to be checked, if Type == "all": #check if each return (all([types["colour"], types["distance"], types["bumper1"], types["bumper2"]])) print("all") #if the type is not all, the if function will be passed and will return the result of the function (true or false) else: if types[Type]: return True else: return False def spin(): #spin and swing weapon until you see something print("spin") while sensor("all"): robot.drive(0, 300) #when something is detected, return the sensor that detected it if sensor_types["colour"] == False: return sensor_types["colour"] if sensor_types["bumper1"] == False: return sensor_types["bumper1"] if sensor_types["bumper2"] == False: return sensor_types["bumper2"] if sensor_types["distance"] == False: return sensor_types["distance"] #if nothing is detected, return as none (should not happen) print("failed") return "none" def backitup(): #swing weapon while going backwards for a second print("backup") robot.straight(-150) wait(delay) def charge(): print("charge") while sensor("all"): if sensor("bumper1") and not sensor("bumper2"): weapon.run(weapon_speed) robot.drive(500, -10) elif sensor("bumper2") and not sensor("bumper1"): weapon.run(weapon_speed) robot.drive(500, 10) else: weapon.run(weapon_speed) robot.drive(500, 0) return #keep spinning until you see something, if its the black line then go back a bit, else charge at opponent while True: x = spin() if x == "colour": backitup() else: charge()
Editor is loading...
Leave a Comment