-
Notifications
You must be signed in to change notification settings - Fork 0
/
wafflebot.py
285 lines (211 loc) · 9.88 KB
/
wafflebot.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env python3
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# You may not use this file except in compliance with the terms and conditions
# set forth in the accompanying LICENSE.TXT file.
#
# THESE MATERIALS ARE PROVIDED ON AN "AS IS" BASIS. AMAZON SPECIFICALLY DISCLAIMS, WITH
# RESPECT TO THESE MATERIALS, ALL WARRANTIES, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
# THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
import os
import sys
import time
import logging
import json
import random
import threading
from enum import Enum
from agt import AlexaGadget
from ev3dev2.led import Leds
from ev3dev2.sound import Sound
from ev3dev2.motor import OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D, SpeedPercent, LargeMotor
from ev3dev2.sensor.lego import ColorSensor
# Set the logging level to INFO to see messages from AlexaGadget
logging.basicConfig(level=logging.INFO, stream=sys.stdout, format='%(message)s')
logging.getLogger().addHandler(logging.StreamHandler(sys.stderr))
logger = logging.getLogger(__name__)
class EventName(Enum):
"""
The list of custom event name sent from this gadget
"""
SPEECH = "Speech"
class MindstormsGadget(AlexaGadget):
"""
A Mindstorms gadget that can perform bi-directional interaction with an Alexa skill.
"""
def __init__(self):
"""
Performs Alexa Gadget initialization routines and ev3dev resource allocation.
"""
super().__init__()
# Robot state
# Connect large motors to A,B,C,D.
self.forkArm = LargeMotor(OUTPUT_B)
self.lidArm = LargeMotor(OUTPUT_A)
self.sensorArm = LargeMotor(OUTPUT_C)
self.dispenser = LargeMotor(OUTPUT_D)
self.colorSensor = ColorSensor()
self.leds = Leds()
self.sound = Sound()
# Start threads
def on_connected(self, device_addr):
"""
Gadget connected to the paired Echo device.
:param device_addr: the address of the device we connected to
"""
self.leds.set_color("LEFT", "GREEN")
self.leds.set_color("RIGHT", "GREEN")
logger.info("{} connected to Echo device".format(self.friendly_name))
self._resetRobot()
def on_disconnected(self, device_addr):
"""
Gadget disconnected from the paired Echo device.
:param device_addr: the address of the device we disconnected from
"""
#reset the robot in the event it was in the middle of cooking
self._resetRobot()
self.leds.set_color("LEFT", "BLACK")
self.leds.set_color("RIGHT", "BLACK")
logger.info("{} disconnected from Echo device".format(self.friendly_name))
def on_custom_mindstorms_gadget_control(self, directive):
"""
Handles the Custom.Mindstorms.Gadget control directive.
:param directive: the custom directive with the matching namespace and name
"""
try:
payload = json.loads(directive.payload.decode("utf-8"))
print("Control payload: {}".format(payload), file=sys.stderr)
control_type = payload["type"]
if control_type == "makeWaffles":
# Expected params: [desiredWaffles,cookTimeMinutes,cookTimeSeconds,dispenseTimeSeconds]
self._makeWaffles(payload["desiredWaffles"],payload["cookTimeMinutes"],payload["cookTimeSeconds"],payload["dispenseTimeSeconds"])
except KeyError:
print("Missing expected parameters: {}".format(directive), file=sys.stderr)
def _makeWaffles(self, desiredWaffles, cookTimeMinutes, cookTimeSeconds, dispenseTimeSeconds):
"""
Handles makeWaffles commands from the directive.
:param desiredWaffles: desired number of waffles as an integer.
"""
print("Make Waffles command: ({},{},{},{})".format(desiredWaffles,cookTimeMinutes,cookTimeSeconds,dispenseTimeSeconds), file=sys.stderr)
madeWaffles = 0
self._closeLid()
if self._checkHeatLight() == False:
if self._heatUpWaffleIron() == False:
self._send_event(EventName.SPEECH, {'speechOut': "Waffle bot encountered a problem heating up the waffle iron. Please check the waffle iron and try your request again."})
#can't cook waffles without a hot iron.
return False
self._openLid()
while madeWaffles < int(desiredWaffles):
self._dispenseBatter(int(dispenseTimeSeconds))
self._closeLid()
totalCookTimeSeconds = ((int(cookTimeMinutes) * 60) + int(cookTimeSeconds))
print("totalCookTimeSeconds: {}".format(totalCookTimeSeconds), file=sys.stderr)
time.sleep(totalCookTimeSeconds)
self._openLid()
#attempt two pickup/delivery attempts
self._pickUpWaffle()
self._deliverWaffle()
self._pickUpWaffle()
self._deliverWaffle()
if self._checkWaffleIron() == False:
#attempt two additional pickup/delivery attempts
self._pickUpWaffle()
self._deliverWaffle()
self._pickUpWaffle()
self._deliverWaffle()
if self._checkWaffleIron() == False:
#if obstruction is still present, send event back to alexa
self._send_event(EventName.SPEECH, {'speechOut': "Waffle bot has detected an obstruction on the cooking surface. Please clear the obstruction and try your request again."})
#exit loop
break
madeWaffles = madeWaffles + 1
def _openLid(self):
self.lidArm.on_for_degrees(SpeedPercent(-20),95)
def _closeLid(self):
self.lidArm.on_for_degrees(SpeedPercent(20),95)
def _pickUpWaffle(self):
self.forkArm.on_for_degrees(SpeedPercent(50),1600)
def _deliverWaffle(self):
self.forkArm.on_for_degrees(SpeedPercent(-50),1600)
def _dispenseBatter(self,dispenseTimeSeconds):
#dispense batter
self.dispenser.on_for_degrees(SpeedPercent(50),140)
time.sleep(dispenseTimeSeconds)
self.dispenser.on_for_degrees(SpeedPercent(-50),140)
#give batter time to settle
time.sleep(3)
def _heatUpWaffleIron(self):
self._send_event(EventName.SPEECH, {'speechOut': "Waffle bot needs to heat up the waffle iron. Your request will continue when the waffle iron is ready."})
checkAttempts = 0
while checkAttempts <= 30:
time.sleep(10)
if self._checkHeatLight() == False:
checkAttempts = checkAttempts + 1
else:
self._send_event(EventName.SPEECH, {'speechOut': "The waffle iron is ready. Waffle bot can continue your request."})
return True
return False
def _checkHeatLight(self):
#a detected ambient light intensity greater than or equal to 4 indicates the heat light is on
print("checking heat light")
self.sensorArm.on_for_degrees(SpeedPercent(20),70)
time.sleep(.5)
ambientLightIntensity = self.colorSensor.ambient_light_intensity
self.sensorArm.on_for_degrees(SpeedPercent(-20),70)
print("heat light ambient light intensity: {}".format(ambientLightIntensity), file=sys.stderr)
if ambientLightIntensity >= 4:
return False
else:
return True
def _checkWaffleIron(self):
print("checking waffle iron")
#anything with a reflected light intensity greater than 5 is considered an obstruction
self.sensorArm.on_for_degrees(SpeedPercent(20),95)
time.sleep(.5)
reflectedLightIntensity = self.colorSensor.reflected_light_intensity
self.sensorArm.on_for_degrees(SpeedPercent(-20),95)
print("waffle iron reflected light intensity: {}".format(reflectedLightIntensity), file=sys.stderr)
if reflectedLightIntensity > 5:
return False
else:
return True
def _resetRobot(self):
print("resetting robot")
self.forkArm.on(SpeedPercent(-20))
self.forkArm.wait(overloadedOrStalled)
self.forkArm.reset()
self.forkArm.on_for_degrees(SpeedPercent(20),90)
self.sensorArm.on(SpeedPercent(-20))
self.sensorArm.wait(overloadedOrStalled)
self.sensorArm.reset()
self.sensorArm.on_for_degrees(SpeedPercent(20),10)
self.lidArm.on(SpeedPercent(-20))
self.lidArm.wait(overloadedOrStalled)
self.lidArm.reset()
self.lidArm.on_for_degrees(SpeedPercent(20),10)
def _send_event(self, name: EventName, payload):
"""
Sends a custom event to trigger a sentry action.
:param name: the name of the custom event
:param payload: the event JSON payload
"""
self.send_custom_event('Custom.Mindstorms.Gadget', name.value, payload)
def overloadedOrStalled(motorState):
print("checking motorState: ({})".format(motorState), file=sys.stderr)
return (('overloaded' in motorState) or ('stalled' in motorState))
if __name__ == '__main__':
gadget = MindstormsGadget()
# Set LCD font and turn off blinking LEDs
os.system('setfont Lat7-Terminus12x6')
gadget.leds.set_color("LEFT", "BLACK")
gadget.leds.set_color("RIGHT", "BLACK")
# Startup sequence
gadget.sound.play_song((('C4', 'e'), ('D4', 'e'), ('E5', 'q')))
gadget.leds.set_color("LEFT", "GREEN")
gadget.leds.set_color("RIGHT", "GREEN")
# Gadget main entry point
gadget.main()
# Shutdown sequence
gadget.sound.play_song((('E5', 'e'), ('C4', 'e')))
gadget.leds.set_color("LEFT", "BLACK")
gadget.leds.set_color("RIGHT", "BLACK")