Skip to content

Commit a3cca53

Browse files
authored
feat(robot): implement special effects actions (#34)
1 parent 29d1b2b commit a3cca53

File tree

2 files changed

+62
-31
lines changed

2 files changed

+62
-31
lines changed

sketches/robot/main.py

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66

77
from arduino_alvik import ArduinoAlvik
8-
from time import sleep_ms
8+
from time import sleep_ms, ticks_add, ticks_diff, ticks_ms
99

1010
try:
1111
from modulino import ModulinoPixels, ModulinoColor
@@ -14,16 +14,11 @@
1414
sys.exit(-1)
1515

1616
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
1820

1921

20-
STOP = 0
21-
GO_FORWARD = 1
22-
GO_BACKWARD = 2
23-
TURN_LEFT = 3
24-
TURN_RIGHT = 4
25-
LIFT = 5
26-
2722
# A WLAN interface must be active to send()/recv()
2823
sta = network.WLAN(network.STA_IF)
2924
sta.active(True)
@@ -36,7 +31,17 @@
3631
pixels = ModulinoPixels(a.i2c)
3732

3833

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+
4045

4146
def receiveAndExecuteFromEspNow():
4247
global lifState
@@ -52,9 +57,11 @@ def receiveAndExecuteFromEspNow():
5257
if int(msg_type) == STOP:
5358
a.drive(0, 0)
5459
elif int(msg_type) == GO_FORWARD:
55-
a.drive(-VELOCITY, 0)
60+
v = VELOCITY if isPlayingReverted else -VELOCITY
61+
a.drive(v, 0)
5662
elif int(msg_type) == GO_BACKWARD:
57-
a.drive(VELOCITY, 0)
63+
v = -VELOCITY if isPlayingReverted else VELOCITY
64+
a.drive(v, 0)
5865
elif int(msg_type) == TURN_LEFT:
5966
a.drive(0, ANGULAR_VELOCITY)
6067
elif int(msg_type) == TURN_RIGHT:
@@ -67,7 +74,8 @@ def receiveAndExecuteFromEspNow():
6774
liftDown()
6875
lifState = 0
6976
else:
70-
print("unknown command type ", msg_type)
77+
print("unknown command type ", msg_type)
78+
7179

7280
def liftUp():
7381
a.set_servo_positions(180, 0)
@@ -92,15 +100,7 @@ def liftDown():
92100
a.set_servo_positions(180, 0)
93101

94102

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():
104104
pixels.set_all_color(ModulinoColor.GREEN, 15)
105105
pixels.show()
106106

@@ -119,30 +119,60 @@ def showEndAnimation():
119119
sleep_ms(50)
120120

121121

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+
122128
STATE_INIT = 0
123129
STATE_PLAY = 1
124-
STATE_END = 2
125130

126131
state = STATE_INIT
127132

133+
deadline = 0
128134
while True:
129135
if state == STATE_INIT:
130136
a.drive(0, 0)
131137
showEndAnimation()
132138
if a.get_color_label() is not "BLACK":
133-
showReadyToPlayAnimation()
139+
showReadyToPlayLeds()
134140
state = STATE_PLAY
135141

136142
elif state == STATE_PLAY:
137143
receiveAndExecuteFromEspNow()
138144
color = a.get_color_label()
139145
if color == "BLACK":
140146
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
147177

148178
sleep_ms(50)

utilities/flash_firmware.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
# this is a patch to fix possible running threads on Alvik
55
from arduino_alvik import ArduinoAlvik
6+
67
alvik = ArduinoAlvik()
78
alvik.stop()
89

9-
update_firmware('./firmware_dev_1_0_3.bin')
10+
update_firmware("./firmware_dev_1_0_3.bin")
1011
# reset()

0 commit comments

Comments
 (0)