This project was created for the LEGO MINDSTORMS Voice Challenge: Powered by Alexa. The project enables battles between LEGO MINDSTORMS robots using Amazon Alexa as the gamemaster.
InspirationMy friends and I enjoy playing with LEGOs. Whenever we get together, we normally end up battling with our LEGOs. Last spring I was in a LEGO class where we made LEGO battlebots, but there were severe limitations, like not being able to keep track of the score, not having an impartial way of knowing when a "hit" was made, and not being able to easily customize your game parameters. So this fall, when my Robotics/Engineering classes decided to work on the Voice Challenge, I knew I wanted to add Alexa voice integration to my EV3 robots to allow for greater functionality in my LEGO battles.
With Alexa you can easily:
- set your Robot's Infrared (IR) channel
- set your Robot's initial health (number of lives)
- start battles
- request an update on your current health status (number of lives)
- hear an alarm and number of lives remaining when your touch sensor registers being hit
- hear an end game alarm and announcement when you are out of lives
Before getting into the details, here is a video demo of my working creation. It takes a minute to set up the initial game parameters, but then you can use it for multiple battles. I hope the video encourages you to view the rest of my project and then try it out yourself.
Building Your RobotsYou can build a robot of your own design. Its requirements are:
- two Large EV3 motors for movement, in ports B (left motor) and C (right motor).
- an IR sensor in port 4 to communicate with the IR remote
- a touch sensor in port 1 to indicate when you have been "hit"
- a Medium EV3 motor in port A. This could be used for a hammer or other type of weapon
If you do not want to build your own robot, you can easily customize the EV3 TRACK3R using the LEGO MINDSTORMS home edition kit. The instructions for building this base robot can be found on the LEGO website:
https://www.lego.com/cdn/cs/set/assets/blt5703aa5eb10dfc68/31313_TRACK3R_2016.pdf
Build pages 4-35 for the base robot and IR sensor (you can skip the attachment on pages 28-31) and pages 68-82 for the hammer attachment. At this point your robot will look like this:
Then you will need to build a touch sensor attachment for registering hits. The building instructions for the one I designed can be found below in Schematics. If you have built the TRACK3R, this attachment can be built with the remaining pieces in your MINDSTORMS kit. It can then be attached as shown below:
You can also build a similar base robot using the LEGO MINDSTORMS EV3 Education Core Set plus Expansion Set. However, you will also need to have an IR sensor and remote since they don't come in the education kits. The build instructions for the Tank Bot can be found on the LEGO Education website:
At this point your robot will look like this:
Then you will need to attach an IR sensor and the hammer and the touch sensor target, which can be built as shown in the previous robot's instructions. All these pieces are included in the education kits (except the IR sensor as noted previously). They can be attached as shown below:
To code your EV3 brick you will need ev3dev for programming your EV3 Brick in Python 3 and Visual Studio Code for editing code and uploading to your brick. For in-depth instructions go the Voice Challenge tutorial in the link below:
https://www.hackster.io/alexagadgets/lego-mindstorms-voice-challenge-setup-17300f#overview
Then go to this tutorial to connect your EV3 brick to Alexa:
You can find my complete Python 3 code for the EV3 brick in the Code section. Below are some of the key snippets.
This is the code for associating the Infrared (IR) buttons with motor actions.
This is the code for associating IR Channel 1 to the above actions. It is replicated for channels 2, 3, and 4.
I then have a thread to allow the IR channel the player selected to control the motors when there are lives left (i.e., when self.gameon is True).
The thread shown below is used to monitor the touch sensor and keep track of the number of "hits." It sends an event to Alexa after each touch and when you are out of lives it will shut off your motors.
The code shown below allows the EV3 brick to receive a directive from Alexa to start a new game. Alexa also passes parameters for the number of lives and the IR channel.
To enable the LEGO robot battles skill, you will need an Amazon developer account. In-depth instructions on how to make an Alexa skill can be found in the Voice Challenge tutorial at this link:
My Alexa Skill uses four custom intents. The SetHealthIntent allows you to use voice interaction to set your health (i.e, number of lives) using an utterance such as "Set heath to three" or "Set lives to three." The SetIrChannelIntent allows you to set your Infrared channel using an utterance such as "Set IR channel to one." The HealthStatusIntent allows you to get your current lives left using an utterance such as "What's my health status?" The StartGameIntent sends a directive to the EV3 brick to start a game (i.e., battle) using an utterance such as "Game on" or "Begin a new battle." Other sample utterances can be found in the model.json file.
Below is the code for the StartGameIntent handler. It sends the number of lives and IR Channel if set by the player. It will default to 3 lives if it hasn't been set by the player.
The EventsReceivedRequest handler contains the following code for when the EV3 brick sends an event when the touch sensor is hit. Alexa will play a unique alarm based on your IR channel and announce the number of lives left. If you have no lives left, Alexa will play an end game sound and announce "Zero health remaining. Game Over."
You can find my complete Alexa Skill code in the Code section below.
Comments