I'm a dad to two boys, 9 and 11 years old, and as usual with boys, they have several toy blasters (e.g. Nerf and others) and use any of their other toys or props as targets. It's good family fun! But often the most time spent is on setting up the targets over and over. I help them at times so they can focus on the fun part but there's got to be a better way!
I've searched the market and found nothing available similar to what professionals use in the real world. I've watched some videos online for inspiration, gathered our LEGO MINDSTORMS sets, and started building the project powered by Alexa.
SetupGet your EV3 development environment setup by following the setup instructions detailed in the following link:
https://www.hackster.io/alexagadgets/lego-mindstorms-voice-challenge-setup-17300f
Connect the EV3 Brick to a compatible Echo deviceFollow the Mission 1 instructions detailed in the following link to get your EV3 Brick connected to your Echo device and reacting to your Alexa wake word.
https://www.hackster.io/alexagadgets/lego-mindstorms-voice-challenge-mission-1-f31925
Build the target practice gadget with LEGOFollow the attached custom LEGO build instructions to build the target practice gadget.
Required components:
- LEGO MINDSTORMS EV3 Brick (1)
- LEGO MINDSTORMS EV3 Large Motor (4)
- LEGO MINDSTORMS EV3 Touch Sensor (2)
- LEGO MINDSTORMS EV3 Cables -.5m (2),.35m (2),.25m (2)
- LEGO Technic pieces (most come from any of these LEGO MINDSTORMS sets - 31313, 45544, 45560)
Optional components:
- LEGO plates used for the targets (you can use what you have available to make suitable alternatives)
- Tape or rubber bands (to secure the targets in place)
Follow the Mission 3 general instructions detailed in the following link to make your gadget react to your voice commands using the Alexa Skill you create.
https://www.hackster.io/alexagadgets/lego-mindstorms-voice-challenge-mission-3-4ed812
Then follow the Mission 4 general instructions detailed in the following link to make your Alexa skill react to custom events sent from the gadget.
https://www.hackster.io/alexagadgets/lego-mindstorms-voice-challenge-mission-4-392a2e
Once you've got Mission 1, Mission 3, and Mission 4 examples working, you can now proceed and replace the Alexa Skill and EV3 gadget code with those attached to this project.
The skill will react to your voice commands to set the mode of game play, start, stop, restart the game. The skill will also react to custom events sent by the gadget whenever targets get hit or when the game is over and there's a winner.
Implementing the targetsThe 4 targets use the large motors to switch it between sides. When a target gets hit, the position changes by a few degrees and this is what the code checks for continuously while the game is on. Below is the corresponding code snippet:
def _target_thread(self, target, index):
"""
Performs the side switch when the target gets hit
"""
while True:
while self.game_started:
curr_position = target.position
target_on_red = curr_position < 90
if target.position != curr_position and abs(target.position - curr_position) < 5:
target_string = "Target {} is hit!".format(index)
self._send_event(EventName.HIT, {'speechOut': target_string})
print(target_string, file=sys.stderr)
target.on_to_position(SpeedPercent(35), 180 if target_on_red else 0, brake=True, block=True)
target_on_red = not target_on_red
self.targets_on_red[index-1] = target_on_red
time.sleep(1)
curr_position = target.position
self.targets_is_hit[index-1] = True
Two additional bonus targets use the touch sensors. Below is the corresponding code snippet:
def _bonus_target_thread(self, target, index):
"""
Performs the score when the bonus target gets hit
"""
while True:
while self.game_started:
target.wait_for_bump()
target_string = "Bonus target {} is hit!".format(index)
self._send_event(EventName.BONUS, {'speechOut': target_string})
print(target_string, file=sys.stderr)
Implementing the gameplayThe gadget has three modes of gameplay - duel, solo, and unlimited. On duel play, the game is over when all of the regular targets are on either the left or right side of the gadget. On solo play, all targets start on one side, must be hit to switch to the other side, then be hit again to go back to the starting side. On unlimited play, the game will go continuously until you stop. Below is the corresponding code snippet:
def _game_thread(self):
"""
Watches the targets to determine when the game is over and who is the winner
"""
while True:
while self.game_started:
game_over = False
if self.unlimited_mode:
game_over = False
elif not(self.solo_mode):
# In duel mode, the game is won when you've hit all targets and they are all on the opponent's side
if (self.targets_is_hit[0] or self.targets_is_hit[1] or self.targets_is_hit[2] or self.targets_is_hit[3]) and self.targets_on_red[0] and self.targets_on_red[1] and self.targets_on_red[2] and self.targets_on_red[3]:
winner = "Blue"
game_over = True
elif (self.targets_is_hit[0] or self.targets_is_hit[1] or self.targets_is_hit[2] or self.targets_is_hit[3]) and not(self.targets_on_red[0]) and not(self.targets_on_red[1]) and not(self.targets_on_red[2]) and not(self.targets_on_red[3]):
winner = "Red"
game_over = True
else:
# In solo mode, the game is won when you've hit all targets on one side then hit them all again on the other side
if self.targets_is_hit[0] and self.targets_is_hit[1] and self.targets_is_hit[2] and self.targets_is_hit[3] and self.targets_on_red[0] and self.targets_on_red[1] and self.targets_on_red[2] and self.targets_on_red[3]:
winner = "Red"
game_over = True
if game_over:
self.game_started = False
target_string = "{} won!".format(winner) if not(self.solo_mode) else ""
self._send_event(EventName.WINNER, {'speechOut': target_string, 'winner': winner})
print(target_string, file=sys.stderr)
time.sleep(1)
Handling custom eventsThe gadget sends custom events to the Alexa Skill on certain events such as, the game is ready, target gets hit, the game is over. Below is the corresponding code snippet:
handle(handlerInput) {
console.log("== Received Custom Event ==");
let customEvent = handlerInput.requestEnvelope.request.events[0];
let payload = customEvent.payload;
let name = customEvent.header.name;
let speechOutput;
if (name === 'Ready') {
speechOutput = "<amazon:emotion name=\"excited\" intensity=\"high\">Targets ready, go shoot!</amazon:emotion>";
} else if (name === 'Hit') {
speechOutput = "<audio src=\"soundbank://soundlibrary/alarms/beeps_and_bloops/bell_01\"/>";
} else if (name === 'Bonus') {
speechOutput = "<say-as interpret-as=\"interjection\">kapow!</say-as>";
} else if (name === 'Winner') {
let speechOutput = "<say-as interpret-as=\"interjection\">woo hoo!</say-as><amazon:emotion name=\"excited\" intensity=\"high\">" + payload.speechOut + "Would you like to play again?</amazon:emotion>";
return handlerInput.responseBuilder
.speak(speechOutput, "REPLACE_ALL")
.withShouldEndSession(false)
.getResponse();
} else if (name === 'Speech') {
speechOutput = payload.speechOut;
} else {
speechOutput = "Hmmm. I don't know how to handle that...";
}
return handlerInput.responseBuilder
.speak(speechOutput + BG_MUSIC, "REPLACE_ALL")
.getResponse();
}
When a game is over, the Alexa skill asks if you want to play again. You can either say yes, play a different mode, or no to quit target practice.
Working DemonstrationYou can watch the video here for a working demonstration of the gadget working with Alexa.
Recap1) Turn on the LEGO MINDSTORMS target practice gadget loaded with the Python code.
2) Run the Python code and make sure it is connected to your compatible Amazon Echo device via Bluetooth.
3) Place the gadget on a stable desk or on the floor, and make sure the gadget has at least a foot of clearance in the back (to prevent the targets from hitting something when it switches sides).
4) Move back at least 6 feet away from the gadget, load up your toy blasters with ammo, and get all shooters ready.
5) Say "Alexa, launch target practice"
6) The Alexa Skill will be launched, welcomes you to target practice, and asks you what mode would you like to play.
7) Say "Alexa, play duel" (or "play solo" or "play unlimited")
8) The Alexa Skill will confirm to you the mode of play, and trigger the gadget to set the targets to their starting positions.
9) When ready, the gadget will send the READY event to the Alexa Skill and Alexa will announce "Targets ready, go shoot!"
10) When you hear this, the game is on and you can start shooting your blasters.
11) Each time any of the targets get hit, the gadget will send the HIT event to the Alexa Skill and you'll hear a bell sound.
12) Each time any of the bonus targets get hit, the gadget will send the BONUS event to the Alexa Skill and Alexa will say a random interjection (e.g. "bam!", "kaboom!", "pow!").
13) On duel and solo modes, when a game is over, the gadget will send the WINNER event to the Alexa Skill and Alexa will announce the winner like "Woo hoo! Red won." and asks "Would you like to play again?"
14) Say "Yes" to play the same mode again, the targets will reset back to their starting positions, then Alexa will announce when to commence fire.
15) Or say "Play solo" (or any different mode of play), the targets will reset to their starting positions, then Alexa will again announce when the game starts and you can start shooting.
16) Or say "No" to end target practice and Alexa will say "Goodbye".
So stay where you are, reload, and have continuous family fun! Alexa makes this all easy and possible.
DisclaimersAmazon Alexa, Echo Dot, and all related logos and motion marks are trademarks of Amazon.com, Inc. or its affiliates.
LEGO® and LEGO MINDSTORMS® is a trademark of the LEGO Group of companies.
Comments