-
Notifications
You must be signed in to change notification settings - Fork 5
/
42140.py
70 lines (59 loc) · 2.12 KB
/
42140.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
66
67
68
69
70
from pybricks.hubs import TechnicHub
from pybricks.pupdevices import *
from pybricks.parameters import *
from pybricks.tools import wait
#Thanks to unbrickme for supplying the port configuration
#Thanks to profinerd for testing
hub = TechnicHub()
motor1 = Motor(Port.B)
motor2 = Motor(Port.A)
remoteControl = Remote()
#main loop
while True:
#update pressed buttons information
pressed = remoteControl.buttons.pressed()
#update orientation of the hub
hubOrientation = hub.imu.up()
#commands to drive forwards or backwards
#they depend on the orientation of the hub
#for one side, motors are reversed and switched
if hubOrientation == Side.BOTTOM:
hub.light.on(Color.ORANGE)
remoteControl.light.on(Color.ORANGE)
if Button.LEFT_PLUS in pressed:
motor2.dc(100)
elif Button.LEFT_MINUS in pressed:
motor2.dc(-100)
else:
motor2.brake()
if Button.RIGHT_PLUS in pressed:
motor1.dc(-100)
elif Button.RIGHT_MINUS in pressed:
motor1.dc(100)
else:
motor1.brake()
elif hubOrientation == Side.TOP:
hub.light.on(Color.BLUE)
remoteControl.light.on(Color.BLUE)
if Button.LEFT_PLUS in pressed:
motor1.dc(100)
elif Button.LEFT_MINUS in pressed:
motor1.dc(-100)
else:
motor1.brake()
if Button.RIGHT_PLUS in pressed:
motor2.dc(-100)
elif Button.RIGHT_MINUS in pressed:
motor2.dc(100)
else:
motor2.brake()
#Continue to drive if buttons are still pressed (to complete rolling over)
#but stop the motors otherwise
else:
if (Button.LEFT_PLUS not in pressed) and (Button.LEFT_MINUS not in pressed) and (Button.RIGHT_PLUS not in pressed) and (Button.RIGHT_MINUS not in pressed):
motor1.brake()
motor2.brake()
#Turn the hub off. Shamelessly copied from profinerd
if (Button.CENTER in pressed):
hub.system.shutdown()
wait(100)