Skip to content

Commit 29d1b2b

Browse files
authored
deathmatch: dead or alive (#33)
1 parent 14226a0 commit 29d1b2b

File tree

2 files changed

+42
-102
lines changed

2 files changed

+42
-102
lines changed

sketches/controller/main.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from modulino import ModulinoKnob, ModulinoButtons
77

8-
ALVIK_MAC = "74:4d:bd:a0:49:e8"
8+
ALVIK_MAC = "74:4d:bd:a2:08:74"
99

1010
sta = network.WLAN(network.STA_IF) # Or network.AP_IF
1111
sta.active(True)
@@ -45,36 +45,36 @@ def mac_str_to_bytes(mac_str):
4545

4646

4747
def stop():
48-
e.send(_mac_peer, struct.pack("BHH", STOP), True)
48+
e.send(_mac_peer, struct.pack("B", STOP), True)
4949

5050

5151
def go_forward():
52-
e.send(_mac_peer, struct.pack("BHH", GO_FORWARD), True)
52+
e.send(_mac_peer, struct.pack("B", GO_FORWARD), True)
5353

5454

5555
def go_backward():
56-
e.send(_mac_peer, struct.pack("BHH", GO_BACKWARD), True)
56+
e.send(_mac_peer, struct.pack("B", GO_BACKWARD), True)
5757

5858

5959
def turn_left():
60-
e.send(_mac_peer, struct.pack("BHH", TURN_LEFT), True)
60+
e.send(_mac_peer, struct.pack("B", TURN_LEFT), True)
6161

6262

6363
def turn_right():
64-
e.send(_mac_peer, struct.pack("BHH", TURN_RIGHT), True)
64+
e.send(_mac_peer, struct.pack("B", TURN_RIGHT), True)
6565

6666

6767
def lift():
68-
e.send(_mac_peer, struct.pack("BHH", LIFT), True)
68+
e.send(_mac_peer, struct.pack("B", LIFT), True)
6969

7070

7171
buttons.on_button_a_press = lambda: go_forward()
7272
buttons.on_button_b_press = lambda: stop()
7373
buttons.on_button_c_press = lambda: go_backward()
7474

7575
knob.on_press = lambda: lift()
76-
knob.on_rotate_clockwise = lambda steps, value: turn_left()
77-
knob.on_rotate_counter_clockwise = lambda steps, value: turn_right()
76+
knob.on_rotate_clockwise = lambda steps, value: turn_right()
77+
knob.on_rotate_counter_clockwise = lambda steps, value: turn_left()
7878

7979
while True:
8080
knob.update()

sketches/robot/main.py

Lines changed: 33 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
print("ImportError: ModulinoPixels not installed")
1414
sys.exit(-1)
1515

16-
LIVES = 8
1716
VELOCITY = 13 # cm/s (max is 13cm/s)
1817
ANGULAR_VELOCITY = 320 # (max 320.8988764044944)
1918

@@ -41,14 +40,14 @@
4140

4241
def receiveAndExecuteFromEspNow():
4342
global lifState
44-
host, msg = e.recv(
43+
_, msg = e.recv(
4544
timeout_ms=0
4645
) # TODO: See ESPNow.irecv() for a memory-friendly alternative.
4746
if msg is None:
4847
return
49-
if len(msg) < 6: # discard garbage
48+
if len(msg) < 1: # discard garbage
5049
return
51-
unpacked_message = struct.unpack("BHH", msg)
50+
unpacked_message = struct.unpack("B", msg)
5251
msg_type = unpacked_message[0]
5352
if int(msg_type) == STOP:
5453
a.drive(0, 0)
@@ -67,6 +66,8 @@ def receiveAndExecuteFromEspNow():
6766
else:
6867
liftDown()
6968
lifState = 0
69+
else:
70+
print("unknown command type ", msg_type)
7071

7172
def liftUp():
7273
a.set_servo_positions(180, 0)
@@ -91,118 +92,57 @@ def liftDown():
9192
a.set_servo_positions(180, 0)
9293

9394

94-
def lostLifeAnimation():
95-
a.drive(0, 0)
96-
97-
for i in range(3):
98-
a.left_led.set_color(1, 0, 0)
99-
a.right_led.set_color(1, 0, 0)
100-
sleep_ms(200)
101-
a.left_led.set_color(0, 0, 0)
102-
a.right_led.set_color(0, 0, 0)
103-
sleep_ms(200)
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)
104103

105-
pixels.clear_all()
106-
color = ModulinoColor.RED
107-
if LIVES >= 6:
108-
color = ModulinoColor.GREEN
109-
elif LIVES >= 3 and LIVES < 6:
110-
color = ModulinoColor(255, 156, 0)
111-
else:
112-
color = ModulinoColor.RED
113-
pixels.set_range_color(0, LIVES - 1, color, 70)
104+
pixels.set_all_color(ModulinoColor.GREEN, 15)
114105
pixels.show()
115106

116107

117-
def checkModulinoPixels():
118-
ok_pixel = False
119-
retries = 3
120-
for x in range(retries):
121-
if pixels.connected:
122-
ok_pixel = True
123-
break
124-
else:
125-
a.left_led.set_color(1, 0, 0)
126-
a.right_led.set_color(1, 0, 0)
127-
sleep_ms(500)
128-
return ok_pixel
129-
130-
131108
def showEndAnimation():
132-
for j in range(0, 3):
133-
for i in range(0, 8):
134-
pixels.clear_all()
135-
pixels.set_rgb(i, 255, 0, 0, 100)
136-
pixels.show()
137-
sleep_ms(50)
109+
for i in range(0, 8):
110+
pixels.clear_all()
111+
pixels.set_rgb(i, 255, 0, 0, 15)
112+
pixels.show()
113+
sleep_ms(50)
138114

139-
for i in range(7, -1, -1):
140-
pixels.clear_all()
141-
pixels.set_rgb(i, 255, 0, 0, 100)
142-
pixels.show()
143-
sleep_ms(50)
115+
for i in range(7, -1, -1):
116+
pixels.clear_all()
117+
pixels.set_rgb(i, 255, 0, 0, 15)
118+
pixels.show()
119+
sleep_ms(50)
144120

145121

146122
STATE_INIT = 0
147123
STATE_PLAY = 1
148-
STATE_LOOSE_LIFE = 2
149-
STATE_END = 3
150-
STATE_ERROR = -1
124+
STATE_END = 2
151125

152126
state = STATE_INIT
153127

154128
while True:
155129
if state == STATE_INIT:
156-
if checkModulinoPixels():
130+
a.drive(0, 0)
131+
showEndAnimation()
132+
if a.get_color_label() is not "BLACK":
133+
showReadyToPlayAnimation()
157134
state = STATE_PLAY
158-
else:
159-
state = STATE_ERROR
160135

161136
elif state == STATE_PLAY:
162-
print("PLAY")
137+
receiveAndExecuteFromEspNow()
163138
color = a.get_color_label()
164-
print(color)
165-
166139
if color == "BLACK":
167-
LIVES -= 1
168-
lostLifeAnimation()
169-
if LIVES > 0:
170-
state = STATE_LOOSE_LIFE
171-
elif LIVES == 0:
172-
state = STATE_END
173-
140+
state = STATE_INIT
174141
elif color == "RED":
175142
# random spin
176143
a.rotate(
177144
random.choice([30.0, 45.0, 90.0, 130.0, 150.0, 180.0, 275.0, 360.0]),
178145
"deg",
179-
)
146+
)
180147

181-
receiveAndExecuteFromEspNow()
182-
183-
elif state == STATE_LOOSE_LIFE:
184-
print("LOOSE LIFE")
185-
186-
receiveAndExecuteFromEspNow()
187-
188-
if a.get_color_label() is not "BLACK":
189-
state = STATE_PLAY
190-
else:
191-
state = STATE_LOOSE_LIFE
192-
193-
elif state == STATE_END:
194-
a.drive(0, 0)
195-
showEndAnimation()
196-
197-
elif state == STATE_ERROR:
198-
a.drive(0, 0)
199-
while True:
200-
# Blink the LEDs forever
201-
a.left_led.set_color(1, 0, 0)
202-
a.right_led.set_color(1, 0, 0)
203-
sleep_ms(200)
204-
a.left_led.set_color(0, 0, 0)
205-
a.right_led.set_color(0, 0, 0)
206-
sleep_ms(200)
207-
208-
sleep_ms(100)
148+
sleep_ms(50)

0 commit comments

Comments
 (0)