5
5
import sys
6
6
7
7
from arduino_alvik import ArduinoAlvik
8
- from time import sleep_ms
8
+ from time import sleep_ms , ticks_add , ticks_diff , ticks_ms
9
9
10
10
try :
11
11
from modulino import ModulinoPixels , ModulinoColor
14
14
sys .exit (- 1 )
15
15
16
16
VELOCITY = 13 # cm/s (max is 13cm/s)
17
- ANGULAR_VELOCITY = 320 # (max 320.8988764044944)
17
+ ANGULAR_VELOCITY = 250 # (max 320.8988764044944)
18
+ FREEZE_FOR_SECONDS = 5
19
+ REVERT_CONTROLLER_FOR_SECONDS = 10
18
20
19
21
20
- STOP = 0
21
- GO_FORWARD = 1
22
- GO_BACKWARD = 2
23
- TURN_LEFT = 3
24
- TURN_RIGHT = 4
25
- LIFT = 5
26
-
27
22
# A WLAN interface must be active to send()/recv()
28
23
sta = network .WLAN (network .STA_IF )
29
24
sta .active (True )
36
31
pixels = ModulinoPixels (a .i2c )
37
32
38
33
39
- lifState = 0 # 0 = down, 1 = up
34
+ STOP = 0
35
+ GO_FORWARD = 1
36
+ GO_BACKWARD = 2
37
+ TURN_LEFT = 3
38
+ TURN_RIGHT = 4
39
+ LIFT = 5
40
+
41
+
42
+ isPlayingReverted = False # if true the controller action are reverted
43
+ lifState = 0 # 0 = down, 1 = up
44
+
40
45
41
46
def receiveAndExecuteFromEspNow ():
42
47
global lifState
@@ -52,9 +57,11 @@ def receiveAndExecuteFromEspNow():
52
57
if int (msg_type ) == STOP :
53
58
a .drive (0 , 0 )
54
59
elif int (msg_type ) == GO_FORWARD :
55
- a .drive (- VELOCITY , 0 )
60
+ v = VELOCITY if isPlayingReverted else - VELOCITY
61
+ a .drive (v , 0 )
56
62
elif int (msg_type ) == GO_BACKWARD :
57
- a .drive (VELOCITY , 0 )
63
+ v = - VELOCITY if isPlayingReverted else VELOCITY
64
+ a .drive (v , 0 )
58
65
elif int (msg_type ) == TURN_LEFT :
59
66
a .drive (0 , ANGULAR_VELOCITY )
60
67
elif int (msg_type ) == TURN_RIGHT :
@@ -67,7 +74,8 @@ def receiveAndExecuteFromEspNow():
67
74
liftDown ()
68
75
lifState = 0
69
76
else :
70
- print ("unknown command type " , msg_type )
77
+ print ("unknown command type " , msg_type )
78
+
71
79
72
80
def liftUp ():
73
81
a .set_servo_positions (180 , 0 )
@@ -92,15 +100,7 @@ def liftDown():
92
100
a .set_servo_positions (180 , 0 )
93
101
94
102
95
- def showReadyToPlayAnimation ():
96
- for _ in range (2 ):
97
- pixels .set_all_color (ModulinoColor .GREEN , 15 )
98
- pixels .show ()
99
- sleep_ms (250 )
100
- pixels .clear_all ()
101
- pixels .show ()
102
- sleep_ms (250 )
103
-
103
+ def showReadyToPlayLeds ():
104
104
pixels .set_all_color (ModulinoColor .GREEN , 15 )
105
105
pixels .show ()
106
106
@@ -119,30 +119,60 @@ def showEndAnimation():
119
119
sleep_ms (50 )
120
120
121
121
122
+ def map_value (value , from_low , from_high , to_low , to_high ):
123
+ return int (
124
+ (value - from_low ) * (to_high - to_low ) / (from_high - from_low ) + to_low
125
+ )
126
+
127
+
122
128
STATE_INIT = 0
123
129
STATE_PLAY = 1
124
- STATE_END = 2
125
130
126
131
state = STATE_INIT
127
132
133
+ deadline = 0
128
134
while True :
129
135
if state == STATE_INIT :
130
136
a .drive (0 , 0 )
131
137
showEndAnimation ()
132
138
if a .get_color_label () is not "BLACK" :
133
- showReadyToPlayAnimation ()
139
+ showReadyToPlayLeds ()
134
140
state = STATE_PLAY
135
141
136
142
elif state == STATE_PLAY :
137
143
receiveAndExecuteFromEspNow ()
138
144
color = a .get_color_label ()
139
145
if color == "BLACK" :
140
146
state = STATE_INIT
141
- elif color == "RED" :
142
- # random spin
143
- a .rotate (
144
- random .choice ([30.0 , 45.0 , 90.0 , 130.0 , 150.0 , 180.0 , 275.0 , 360.0 ]),
145
- "deg" ,
146
- )
147
+ elif color == "YELLOW" :
148
+ pixels .set_all_color (ModulinoColor .YELLOW , 15 )
149
+ pixels .show ()
150
+ deg = random .choice ([30.0 , 45.0 , 90.0 , 130.0 , 150.0 , 180.0 , 275.0 , 360.0 ])
151
+ a .rotate (deg , "deg" )
152
+ showReadyToPlayLeds ()
153
+ elif color == "BLUE" :
154
+ a .drive (0 , 0 )
155
+ for x in range (0 , FREEZE_FOR_SECONDS ):
156
+ sleep_ms (500 )
157
+ pixels .set_all_color (ModulinoColor .BLUE , 15 )
158
+ pixels .show ()
159
+ sleep_ms (500 )
160
+ pixels .clear_all ()
161
+ pixels .show ()
162
+ showReadyToPlayLeds ()
163
+ elif color == "GREEN" or color == "LIGHT GREEN" :
164
+ if not isPlayingReverted :
165
+ deadline = ticks_add (ticks_ms (), REVERT_CONTROLLER_FOR_SECONDS * 1000 )
166
+ isPlayingReverted = True
167
+
168
+ if isPlayingReverted :
169
+ elapsed = ticks_diff (deadline , ticks_ms ())
170
+ mapped = map_value (elapsed , 0 , REVERT_CONTROLLER_FOR_SECONDS * 1000 , 0 , 7 )
171
+ pixels .clear_all ()
172
+ pixels .set_range_color (0 , mapped , ModulinoColor .VIOLET )
173
+ pixels .show ()
174
+ if elapsed < 0 :
175
+ showReadyToPlayLeds ()
176
+ isPlayingReverted = False
147
177
148
178
sleep_ms (50 )
0 commit comments