#!/usr/bin/env python3 # imports # ------------ # import log function from ev3devlogging import timedlog as log # import ev3 API from ev3dev2 import auto as ev3 # 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")
def reverseSmallDistance(): numberOfMotorRotationsBackwards = 1 tankDrive.on_for_rotations(ev3.SpeedPercent(-35), ev3.SpeedPercent(-35), numberOfMotorRotationsBackwards, brake=False)
def rotateDegrees(degrees): degrees = degrees * 2 tankDrive.on_for_degrees(ev3.SpeedPercent(40), ev3.SpeedPercent(0), degrees, brake=False)
def rotateDegrees(degrees): degrees = degrees * 2 tankDrive.on_for_degrees(ev3.SpeedPercent(40), ev3.SpeedPercent(0), 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():