We are programming a Lego Spike vehicle in Python. The wheels move based on 2 motors. What the vehicle is supposed to do, according to my code, is pair the motors, move forward for 3 seconds, move backwards for 3 seconds, and then turn slightly.
What actually happens is that it moves backwards for around a second, and then stops.
My code:
from hub import port
import motor_pair, motor, color_sensor, color
class motorWheels:
def __init__(self, speed, time):
self.speed = speed
self.time = time
def setSpeed(self, speed): #Changes the speed
self.speed = speed
def setTime(self, time): #Changes the time
self.time = time
def pairMotors(self):
motor_pair.pair(motor_pair.PAIR_1, port.A, port.B) #Pairing the motors on port A, and port B together
async def forward(self, speed, time): #Main motors (A,B), speed between -1100 and 1100 #Time is in ms
motor_pair.move_for_time(motor_pair.PAIR_1, time, speed)
def reverse(self, speed, time): #Main motors (A,B), speed between -1100 and 1100 #Time is in ms
motor_pair.move_for_time(motor_pair.PAIR_1,time, -1*speed)
def turnDegrees(self, degrees): #Må testes for å finne hva "degrees" er i virkeligheten
motor.run_for_degrees(port.A, degrees, 400)
motor.run_for_degrees(port.B, -1*degrees, 400)
def runUntilColor(self, num): #Will run for 1 second, each time in the loop.
if num == 1:
while color_sensor.color(port.C) != color.RED:
motor_pair.move_for_time(motor_pair.PAIR_1, 1000, 1000)
if num == 2:
while color_sensor.color(port.C) != color.BLUE:
motor_pair.move_for_time(motor_pair.PAIR_1, 1000, 1000)
if num == 3:
while color_sensor.color(port.C) != color.GREEN:
motor_pair.move_for_time(motor_pair.PAIR_1, 1000, 1000)
class Crane:
def __init__(self, height):
self.height = height
def crane(self, speed, time):
motor.run_for_time(port.D, time, speed)
drive = motorWheels(0, 0)
drive.pairMotors()
drive.forward(1000, 3000)
drive.reverse(1000,3000)
drive.turnDegrees(360)