Код: Выделить всё
FORWARD = "forward"
REVERSE = "reverse"
class setup_motor():
def __init__(self, motor, direction):
self.motor = motor
self.direction = direction
self.full_executed = False
self.half_executed = False
def push_down(self, degrees, time_after_start, length):
time_elapsed = time.time() - start_time
if time_elapsed > time_after_start and not self.half_executed:
# spin motor
self.half_executed = True
elif time_elapsed > time_after_start + length and not self.full_executed:
# spin motor
self.full_executed = True
Код: Выделить всё
L3.push_down(pressed_degrees, 1*multiplier, 0.1*multiplier)
L2.push_down(pressed_degrees, 1*multiplier, 0.1*multiplier)
L1.push_down(pressed_degrees, 1*multiplier, 0.1*multiplier)
Они создаются следующим образом:
Код: Выделить всё
L1 = setup_motor("L1", FORWARD)
L2 = setup_motor("L2", REVERSE)
L3 = setup_motor("L3", FORWARD)
start_time — это просто время запуска сценария.
Код: Выделить всё
start_time = time.time() #start of script time
Он выводит:
Код: Выделить всё
Motor: L1, time after start: 0, length: 0.3
Time: 0.002918720245361328
Motor: L1, time after start: 0, length: 0.3
Time: 0.30016064643859863
Motor: L3, time after start: 0.6, length: 0.2
Time: 0.6007165908813477
Motor: L3, time after start: 0.6, length: 0.2
Time: 0.8000619411468506
Motor: L2, time after start: 1, length: 0.1
Time: 1.0007250308990479
Motor: L2, time after start: 1, length: 0.1
Time: 1.1002442836761475
Цикл выглядит следующим образом (те, у которых есть #skiped, — это те, которые не выполняются — см. вывод ):
Код: Выделить всё
multiplier = 1
while True:
#motor_name.pushdown(degrees, time after start of script, length)
L1.push_down(pressed_degrees, 0*multiplier, 0.3*multiplier)
L1.push_down(pressed_degrees, 0.5*multiplier, 0.1*multiplier) #skipped
L3.push_down(pressed_degrees, 0.6*multiplier, 0.2*multiplier)
L3.push_down(pressed_degrees, 1*multiplier, 0.1*multiplier) #skipped
L2.push_down(pressed_degrees, 1*multiplier, 0.1*multiplier)
L1.push_down(pressed_degrees, 1*multiplier, 0.1*multiplier) #skipped
Подробнее здесь: https://stackoverflow.com/questions/793 ... g-executed
Мобильная версия