/
robot.py
65 lines (47 loc) · 1.08 KB
/
robot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import time
import nxt
FORWARD_SPEED = 50
TURN_SPEED = 50
LEFT = -1
RIGHT = 1
brick = nxt.find_one_brick()
motors = [nxt.Motor(brick, nxt.PORT_B),
nxt.Motor(brick, nxt.PORT_C)]
def stop(secs=0.2):
for motor in motors:
motor.idle()
if secs:
time.sleep(secs)
def forward(secs=0.2):
for motor in motors:
motor.run(FORWARD_SPEED, regulated=True)
if secs:
time.sleep(secs)
stop(0)
def turn(direction, secs=0.2):
motors[0].run(-direction*TURN_SPEED, regulated=True)
motors[1].run(direction*TURN_SPEED, regulated=True)
if secs:
time.sleep(secs)
stop(0)
def action(word):
if word == "forward":
forward()
elif word == "left":
turn(LEFT)
elif word == "right":
turn(RIGHT)
elif word == "stop":
stop()
if __name__ == "__main__":
i = ""
while i != "q":
i = input()
if i == "w":
forward()
elif i == "s":
stop()
elif i == "a":
turn(LEFT)
elif i == "d":
turn(RIGHT)