def run(self): self.tank_drive.on(SpeedPercent(30), SpeedPercent(30)) while True: colorNr = self.cs.color if colorNr == 1: # black line log("border") self.tank_drive.stop() self.reverseRotations(1) self.rotateDegrees(150) self.tank_drive.on(SpeedPercent(30), SpeedPercent(30)) elif colorNr == 2 or colorNr == 4 or colorNr == 5: # blue or yellow or red print("lake with color={color}".format(color=colorNr)) log("lake") self.tank_drive.stop() self.rotateDegrees(90) self.tank_drive.on(SpeedPercent(30), SpeedPercent(30)) elif self.checkCollision(): log("collision") self.tank_drive.stop() # self.s.speak("blyat.wav") self.reverseRotations(1) self.rotateDegrees(180) self.tank_drive.on(SpeedPercent(30), SpeedPercent(30))
# initialize # ------------ # initialize left and right motor as tank combo tankDrive = ev3.MoveTank(ev3.OUTPUT_A, ev3.OUTPUT_D) # initialize some constants SPEED_FORWARD = ev3.SpeedPercent(30) # set speed to 30% of maximum speed SPEED_BACKWARD = ev3.SpeedPercent(-30) # backward with same speed as forward SPEED_ZERO = ev3.SpeedPercent(0) # stop motor (speed is zero) TURN_TIME = 0.62 # main loop # ----------- log("drive forward") tankDrive.on_for_seconds(SPEED_FORWARD, SPEED_FORWARD, 2) log("turn right") tankDrive.on_for_seconds(SPEED_FORWARD, SPEED_BACKWARD, TURN_TIME) log("drive forward") tankDrive.on_for_seconds(SPEED_FORWARD, SPEED_FORWARD, 3) log("turn right") tankDrive.on_for_seconds(SPEED_FORWARD, SPEED_BACKWARD, TURN_TIME) log("drive forward") tankDrive.on_for_seconds(SPEED_FORWARD, SPEED_FORWARD, 2) log("turn right")
from time import sleep from ev3dev2 import sound mySound = sound.Sound() # import basic log function from logging library specially for ev3 from ev3devlogging import log # import basic log function which als logs time #from ev3devlogging import timedlog as log # play beep sound mySound.beep() log("sleep") sleep(3) # print to screen, doesn't get logged in file print("starwars") # print to log file, not shown on EV3 screen log("starwars song") mySound.tone([(392, 350, 100), (392, 350, 100), (392, 350, 100), (311.1, 250, 100), (466.2, 25, 100), (392, 350, 100), (311.1, 250, 100), (466.2, 25, 100), (392, 700, 100), (587.32, 350, 100), (587.32, 350, 100), (587.32, 350, 100), (622.26, 250, 100), (466.2, 25, 100), (369.99, 350, 100), (311.1, 250, 100), (466.2, 25, 100), (392, 700, 100), (784, 350, 100), (392, 250, 100),
# initialize #------------ # initialize color sensor colorSensor = ev3.ColorSensor(ev3.INPUT_2) # initialize left and right motor as tank combo tankDrive = ev3.MoveTank(ev3.OUTPUT_A, ev3.OUTPUT_D) # initialize some constants SPEED_FORWARD = ev3.SpeedPercent(30) # set speed to 30% of maximum speed SPEED_BACKWARD = ev3.SpeedPercent(-30) # backward with same speed as forward SPEED_ZERO = ev3.SpeedPercent(0) # stop motor (speed is zero) # main loop #----------- log("drive forward") tankDrive.on(SPEED_FORWARD, SPEED_FORWARD) while True: color = colorSensor.color if color == colorSensor.COLOR_BLACK: # hit black border line print("border!") log("detect black") # immediately stop log("stop") tankDrive.stop() # drive backwards for the duration of 1 second log("drive backwards") tankDrive.on_for_seconds(SPEED_BACKWARD, SPEED_BACKWARD, 1) # rotate right for 1 second by only running the left motor forward (keep right motor off) log("turn right") tankDrive.on_for_seconds(SPEED_FORWARD, SPEED_ZERO, 1)
degrees, brake=False) def checkCollision(): return touchSensorLeft.is_pressed or touchSensorRight.is_pressed # main loop # ----------- tankDrive.on(ev3.SpeedPercent(30), ev3.SpeedPercent(30)) while True: color = colorSensor.color if color == colorSensor.COLOR_BLACK: # black line log("border") tankDrive.stop() reverseSmallDistance() rotateDegrees(150) tankDrive.on(ev3.SpeedPercent(30), ev3.SpeedPercent(30)) elif color == colorSensor.COLOR_BLUE or color == colorSensor.COLOR_YELLOW or color == colorSensor.COLOR_RED: print("lake with color={color}".format(color=color)) log("lake") tankDrive.stop() rotateDegrees(90) tankDrive.on(ev3.SpeedPercent(30), ev3.SpeedPercent(30)) elif checkCollision(): log("collision") tankDrive.stop() reverseSmallDistance() rotateDegrees(180)