Michael Petoukhov
Published © GPL3+

miCube Alexa Voice Controlled LEGO EV3 Robot

An EV3 multitasking robot that cleans your mess, helps around the house and listens to you.

IntermediateFull instructions provided20 hours2,084

Things used in this project

Hardware components

Mindstorms EV3 Programming Brick / Kit
LEGO Mindstorms EV3 Programming Brick / Kit
This kit contains key parts to build the robot e.g. motors and EV3 brick, additional parts for the cume shape are Lego Technic parts
×1
Echo Dot
Amazon Alexa Echo Dot
This device supports Alexa gadget kit and provides voice capabilities to control the robot
×1
Flash Memory Card, MicroSDHC Card
Flash Memory Card, MicroSDHC Card
The Micro SD card is used to store EV3dev code and additional custom instructions
×1

Software apps and online services

VS Code
Microsoft VS Code
Alexa Skills Kit
Amazon Alexa Alexa Skills Kit
Alexa Gadgets Toolkit
Amazon Alexa Alexa Gadgets Toolkit

Story

Read more

Schematics

Stage

Building instruction for stage

Cube

Building instruction for Cube

Chassis

Building instruction for chassis

Sleigh

Building instruction for sleigh

Code

Model.json

JSON
Customised intents and voice commands
{
  "interactionModel": {
      "languageModel": {
          "invocationName": "mycube",
          "intents": [
              {
                  "name": "AMAZON.CancelIntent",
                  "samples": []
              },
              {
                  "name": "AMAZON.HelpIntent",
                  "samples": []
              },
              {
                  "name": "AMAZON.StopIntent",
                  "samples": []
              },
              {
                  "name": "AMAZON.NavigateHomeIntent",
                  "samples": []
              },
              {
                  "name": "MoveIntent",
                  "slots": [
                      {
                          "name": "Direction",
                          "type": "DirectionType"
                      },
                      {
                          "name": "Duration",
                          "type": "AMAZON.NUMBER"
                      }
                  ],
                  "samples": [
                      "{Direction} now",
                      "{Direction} {Duration} seconds",
                      "move {Direction} for {Duration} seconds"
                  ]
              },
              {
                  "name": "SetSpeedIntent",
                  "slots": [
                      {
                          "name": "Speed",
                          "type": "AMAZON.NUMBER"
                      }
                  ],
                  "samples": [
                      "set speed {Speed} percent",
                      "set {Speed} percent speed",
                      "set speed to {Speed} percent"
                  ]
              },
              {
                  "name": "SetCommandIntent",
                  "slots": [
                      {
                          "name": "Command",
                          "type": "CommandType"
                      }
                  ],
                  "samples": [
                      "activate {Command} mode",
                      "move in a {Command}",
                      "fire {Command}",
                      "activate {Command}"
                  ]
              }
          ],
          "types": [
              {
                  "name": "DirectionType",
                  "values": [
                      {
                          "name": {
                              "value": "brake"
                          }
                      },
                      {
                          "name": {
                              "value": "go backward"
                          }
                      },
                      {
                          "name": {
                              "value": "go forward"
                          }
                      },
                      {
                          "name": {
                              "value": "go right"
                          }
                      },
                      {
                          "name": {
                              "value": "go left"
                          }
                      },
                      {
                          "name": {
                              "value": "right"
                          }
                      },
                      {
                          "name": {
                              "value": "left"
                          }
                      },
                      {
                          "name": {
                              "value": "backwards"
                          }
                      },
                      {
                          "name": {
                              "value": "backward"
                          }
                      },
                      {
                          "name": {
                              "value": "forwards"
                          }
                      },
                      {
                          "name": {
                              "value": "forward"
                          }
                      }
                  ]
              },
              {
                  "name": "CommandType",
                  "values": [
                      {
                          "name": {
                              "value": "grabber close"
                          }
                      },
                      {
                          "name": {
                              "value": "take the present"
                          }
                      },
                      {
                          "name": {
                              "value": "find Santa"
                          }
                      },
                      {
                          "name": {
                              "value": "clean my mess"
                          }
                      },
                      {
                          "name": {
                              "value": "grab the object"
                          }
                      },
                      {
                          "name": {
                              "value": "bring me my drink"
                          }
                      },
                      {
                          "name": {
                              "value": "the password is hohoho"
                          }
                      },
                      {
                          "name": {
                              "value": "thank you for the cookies and milk"
                          }
                      },
                      {
                          "name": {
                              "value": "grabber open"
                          }
                      }
                  ]
              }
          ]
      }
  }
}

Lambda folder

JSON
Contains files for Alexa Development Console
No preview (download only).

miCude main program code

Python
Python code
#!/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
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, MoveTank, SpeedPercent, MediumMotor
from ev3dev2.sensor.lego import UltrasonicSensor, 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 Direction(Enum):
    """
    The list of directional commands and their variations.
    These variations correspond to the skill slot values.
    """
    FORWARD = ['forward', 'forwards', 'go forward']
    BACKWARD = ['back', 'backward', 'backwards', 'go backward']
    LEFT = ['left', 'go left']
    RIGHT = ['right', 'go right']
    STOP = ['brake', 'halt']


class Command(Enum):
    """
    The list of preset commands and their invocation variation.
    These variations correspond to the skill slot values.
    """
    
    OBJECT_GRABBER = ['grab the object']
    GRABBER_OPEN = ['grabber open']
    GRABBER_CLOSE = ['grabber close']
    CAN_GRABBER = ['bring me my drink']
    MESS = ['clean my mess']
    FIND_SANTA = ['find Santa']
    PRESENT = ['take the present']
    PASSWORD = ['the password is hohoho']
    COOKIES = ['thank you for the cookies and milk']

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__()

        # Connect two large motors on output ports B and C
        # Connect two Medium motors on output ports A and D
        # Connect Ultrasonic and Colour sensors into any ports

        self.drive = MoveTank(OUTPUT_B, OUTPUT_C)
        self.plateLifter = MediumMotor(OUTPUT_A)
        self.grabber = MediumMotor(OUTPUT_D)
        self.sound = Sound()
        self.leds = Leds()
        self.us = UltrasonicSensor()
        self.cs = ColorSensor()

    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))

    def on_disconnected(self, device_addr):
        """
        Gadget disconnected from the paired Echo device.
        :param device_addr: the address of the device we disconnected from
        """
        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 == "move":

                # Expected params: [direction, duration, speed]
                self._move(payload["direction"], int(payload["duration"]), int(payload["speed"]))

            if control_type == "command":
                # Expected params: [command]
                self._activate(payload["command"])

        except KeyError:
            print("Missing expected parameters: {}".format(directive), file=sys.stderr)

    def _move(self, direction, duration: int, speed: int, is_blocking=False):
        """
        Handles move commands from the directive.
        Right and left movement can under or over turn depending on the surface type.
        :param direction: the move direction
        :param duration: the duration in seconds
        :param speed: the speed percentage as an integer
        :param is_blocking: if set, motor run until duration expired before accepting another command
        """
        print("Move command: ({}, {}, {}, {})".format(direction, speed, duration, is_blocking), file=sys.stderr)
        if direction in Direction.FORWARD.value:
            self.drive.on_for_seconds(SpeedPercent(speed), SpeedPercent(speed), duration, block=is_blocking)

        if direction in Direction.BACKWARD.value:
            self.drive.on_for_seconds(SpeedPercent(-speed), SpeedPercent(-speed), duration, block=is_blocking)

        if direction in (Direction.RIGHT.value + Direction.LEFT.value):
            self._turn(direction, speed)
            self.drive.on_for_seconds(SpeedPercent(speed), SpeedPercent(speed), duration, block=is_blocking)

        if direction in Direction.STOP.value:
            self.drive.off()
            self.patrol_mode = False

    def _activate(self, command, speed=50):
        """
        Handles preset commands.
        :param command: the preset command
        :param speed: the speed if applicable
        """

        #Variable assignment for the various functions
        forwardSpeed = 20
        reverseSpeed = -20
        proxCount1 = 0
        proxCount2 = 0
        proxCount3 = 0
        rotationsGrabberOpenPresentFully = 9
        rotationsGrabberClosePresentFully = -9
        rotationsGrabberClosePresent = -4
        rotationsGrabberOpenPresent = 4
        rotationsPO = 24
        rotationsPC = -24
        speedP = 60
        Rotate180SpeedLeft = 20
        Rotate180SpeedRight =  -20
        self.cs.mode='COL-COLOR'
        colors=('unknown','black','blue','green','yellow','red','white','brown')
        colourCount1 = 0
        colourCount2 = 0
        colourCount3 = 0
        loopCount = 0
        speedG = 40
        rotationsGrabberOpenFull = 13
        rotationsGrabberCloseFull = -13
        self.us.mode='US-DIST-CM'
        units = self.us.units
        distance = self.us.value()/10
        rotationsGrabberCanOpen = 8
        rotationsGrabberCanClose = 6
        rotationsGrabberObjectOpen = 9
        rotationsGrabberObjectClose = 6

        print("Activate command: ({}, {})".format(command, speed), file=sys.stderr)

        if command in Command.GRABBER_OPEN.value:
            #opens the front grabber 
            self.grabber.on_for_degrees(SpeedPercent(speedG),360*rotationsGrabberOpenFull)
        
        if command in Command.GRABBER_CLOSE.value:
            #closes the front grabber
            self.grabber.on_for_degrees(SpeedPercent(speedG),360*rotationsGrabberCloseFull)
            
        if command in Command.OBJECT_GRABBER.value:
            #grabs an object on the stage
            self.grabber.on_for_degrees(SpeedPercent(speedG), 360*rotationsGrabberObjectOpen) #open grabber
            self.drive.on_for_degrees(SpeedPercent(forwardSpeed), SpeedPercent(forwardSpeed), 360) #drive forward
            self.grabber.on_for_degrees(SpeedPercent(speedG), 360*rotationsGrabberObjectClose) #close the grabber
            self.drive.on_for_degrees(SpeedPercent(reverseSpeed), SpeedPercent(reverseSpeed), 360) #reverse
            self.drive.on_for_degrees(SpeedPercent(Rotate180SpeedLeft), SpeedPercent(Rotate180SpeedRight), 360-25) #turn 180 degrees
            self.drive.on_for_degrees(SpeedPercent(forwardSpeed), SpeedPercent(forwardSpeed), 360) #drive forward
            self.grabber.on_for_degrees(SpeedPercent(speedG), 360*-rotationsGrabberObjectClose) #open grabber 6 rotations
            self.drive.on_for_degrees(SpeedPercent(reverseSpeed), SpeedPercent(reverseSpeed), 360) #reverse
            self.grabber.on_for_degrees(SpeedPercent(speedG), 360*-rotationsGrabberObjectOpen) #close grabber completely
            self._send_event(EventName.SPEECH, {'speechOut': "My master here is your object"})

        if command in Command.CAN_GRABBER.value:
            #grabs a drink from the stage using a colour sensor and predefined distance

            ###COLOUR SENSE FUNCTIONS###
            while colourCount1 == 0:
                if colors[self.cs.value()] != 'white' and colourCount1 == 0:
                    self.drive.on(SpeedPercent(forwardSpeed), SpeedPercent(forwardSpeed))
    
                else:
                    colourCount1 = 1
                    self.drive.on(SpeedPercent(0), SpeedPercent(0))
                    self.drive.on_for_degrees(SpeedPercent(Rotate180SpeedRight), SpeedPercent(Rotate180SpeedLeft), (360-25)/2)
                    self.grabber.on_for_degrees(SpeedPercent(speedG), 360*rotationsGrabberCanOpen)
                    self.drive.on_for_degrees(SpeedPercent(forwardSpeed), SpeedPercent(forwardSpeed), 360) #drive forward
                    self.grabber.on_for_degrees(SpeedPercent(speedG), 360*-rotationsGrabberCanClose)
                    self.drive.on_for_degrees(SpeedPercent(reverseSpeed), SpeedPercent(reverseSpeed), 360) #drive back
                    self.drive.on_for_degrees(SpeedPercent(Rotate180SpeedRight), SpeedPercent(Rotate180SpeedLeft), (360-25)/2)


            while colourCount2 == 0:
                if colors[self.cs.value()] != 'white' and colourCount2 == 0:
                    self.drive.on(SpeedPercent(forwardSpeed), SpeedPercent(forwardSpeed))
                else:
                    colourCount2 = 1
                    self.drive.on(SpeedPercent(0), SpeedPercent(0))
                    self.grabber.on_for_degrees(SpeedPercent(speedG), 360*rotationsGrabberCanClose)
                    self.drive.on_for_degrees(SpeedPercent(reverseSpeed), SpeedPercent(reverseSpeed), 360) #drive back
                    self.grabber.on_for_degrees(SpeedPercent(speedG), 360*-rotationsGrabberCanOpen)
                    self._send_event(EventName.SPEECH, {'speechOut': "My master, here is your drink"})

        if command in Command.MESS.value:
            #cleans the mess using the ultrasonic sensor and the colour sensor
            while loopCount == 0:
                    while colourCount1 == 0:
                        if colors[self.cs.value()] != 'white' and colourCount1 == 0:
                            self.drive.on(SpeedPercent(forwardSpeed), SpeedPercent(forwardSpeed))
                        else:
                            colourCount1 = 1
                            self.drive.on(SpeedPercent(0), SpeedPercent(0))
                            self.grabber.on_for_degrees(SpeedPercent(speedG), 360*rotationsGrabberOpenFull)

                    while proxCount1 == 0:
                        self.us.mode='US-DIST-CM'
                        units = self.us.units
                        distance = self.us.value()/10
                        if distance > 5 and proxCount1 == 0:
                            self.drive.on(SpeedPercent(forwardSpeed), SpeedPercent(forwardSpeed))
                        else:
                            proxCount1 = 1
                            self.drive.on(SpeedPercent(0), SpeedPercent(0))
                    while colourCount2 == 0:
                        if colors[self.cs.value()] != 'white' and colourCount2 == 0:
                            self.drive.on(SpeedPercent(reverseSpeed), SpeedPercent(reverseSpeed))
                        else:
                            colourCount2 = 1
                            self.drive.on(SpeedPercent(0), SpeedPercent(0))
                            self.drive.on_for_degrees(SpeedPercent(Rotate180SpeedLeft), SpeedPercent(Rotate180SpeedRight), (360-25)/2)
                    while proxCount2 == 0:
                        self.us.mode='US-DIST-CM'
                        units = self.us.units
                        distance = self.us.value()/10
                        if distance > 23 and proxCount2 == 0:
                            self.drive.on(SpeedPercent(forwardSpeed), SpeedPercent(forwardSpeed))
                        else:
                            proxCount2 =1
                            self.drive.on(SpeedPercent(0), SpeedPercent(0))
                            self.drive.on_for_degrees(SpeedPercent(Rotate180SpeedRight), SpeedPercent(Rotate180SpeedLeft), (360-25)/2)
                    while proxCount3 == 0:
                        self.us.mode='US-DIST-CM'
                        units = self.us.units
                        distance = self.us.value()/10
                        if distance > 5 and proxCount3 == 0:
                           self.drive.on(SpeedPercent(forwardSpeed), SpeedPercent(forwardSpeed))
                        else:
                            proxCount3 =1
                            self.drive.on(SpeedPercent(0), SpeedPercent(0))
                    while colourCount3 == 0:
                        if colors[self.cs.value()] != 'white' and colourCount3 == 0:
                            self.drive.on(SpeedPercent(reverseSpeed), SpeedPercent(reverseSpeed))
                        else:
                            colourCount3 = 1
                            self.drive.on(SpeedPercent(0), SpeedPercent(0))
                            self.grabber.on_for_degrees(SpeedPercent(speedG), 360*rotationsGrabberCloseFull)
                            self.drive.on_for_degrees(SpeedPercent(Rotate180SpeedRight), SpeedPercent(Rotate180SpeedLeft), 360-25)
                            loopCount = 1

            self._send_event(EventName.SPEECH, {'speechOut': "I cleaned your mess and i am ready for the next task"})
        
        if command in Command.FIND_SANTA.value:
            #finds "santa" using the ultrasonic sensor and some predefined movements
            proxCount1 = 0
            while proxCount1 == 0:
                self.us.mode='US-DIST-CM'
                units = self.us.units
                distance = self.us.value()/10
                if distance > 22 and proxCount1 == 0:
                    self.drive.on(SpeedPercent(forwardSpeed), SpeedPercent(forwardSpeed))
                else:
                    proxCount1 =1
                    self.drive.on(SpeedPercent(0), SpeedPercent(0))
                    self.grabber.on_for_degrees(SpeedPercent(speedG), 360*rotationsGrabberOpenPresentFully)
                    self._send_event(EventName.SPEECH, {'speechOut': "Hi santa. Do you have a present for me?"})

        if command in Command.PRESENT.value:
            #grabs the present using the ultrasonic sensor and puts it under the tree with predefined movements
            proxCount2 = 0
            while proxCount2 == 0:
                self.us.mode='US-DIST-CM'
                units = self.us.units
                distance = self.us.value()/10
                if distance > 15 and proxCount2 == 0:
                    self.drive.on(SpeedPercent(forwardSpeed), SpeedPercent(forwardSpeed))
                else:
                    proxCount2 = 1
                    self.drive.on(SpeedPercent(0), SpeedPercent(0))
                    self.grabber.on_for_degrees(SpeedPercent(speedG), 360*rotationsGrabberClosePresent)
                    self.drive.on_for_degrees(SpeedPercent(reverseSpeed), SpeedPercent(reverseSpeed), 360)
                    self.drive.on_for_degrees(SpeedPercent(Rotate180SpeedLeft), SpeedPercent(Rotate180SpeedRight), (360-25)/2)
                    self.drive.on_for_degrees(SpeedPercent(forwardSpeed), SpeedPercent(forwardSpeed), 400)
                    self.grabber.on_for_degrees(SpeedPercent(speedG), 360*rotationsGrabberOpenPresent)
                    self.drive.on_for_degrees(SpeedPercent(reverseSpeed), SpeedPercent(reverseSpeed), 400)
                    self.grabber.on_for_degrees(SpeedPercent(speedG), 360*rotationsGrabberClosePresentFully)
                    self.drive.on_for_degrees(SpeedPercent(Rotate180SpeedRight), SpeedPercent(Rotate180SpeedLeft), (360-25)/2)
            self._send_event(EventName.SPEECH, {'speechOut': "What is your password?"})

        if command in Command.PASSWORD.value:
            #opens the hidden compartment and lifts the plate with cookies and milk
            self.plateLifter.on_for_degrees(SpeedPercent(speedP), 360*rotationsPC)

        if command in Command.COOKIES.value:
            #closes the compartment, says goodbye to "santa" and drives away
            self.plateLifter.on_for_degrees(SpeedPercent(speedP), 360*rotationsPO)
            self.drive.on_for_degrees(SpeedPercent(Rotate180SpeedRight), SpeedPercent(Rotate180SpeedLeft), 360-25)
            self._send_event(EventName.SPEECH, {'speechOut': "Goodbye santa. see you next year"})
            self.drive.on_for_degrees(SpeedPercent(forwardSpeed), SpeedPercent(forwardSpeed), 360*2)

    def _turn(self, direction, speed):
        """
        Turns based on the specified direction and speed.
        Calibrated for hard smooth surface.
        :param direction: the turn direction
        :param speed: the turn speed
        """
        if direction in Direction.LEFT.value:
            self.drive.on_for_seconds(SpeedPercent(0), SpeedPercent(speed), 2)

        if direction in Direction.RIGHT.value:
            self.drive.on_for_seconds(SpeedPercent(speed), SpeedPercent(0), 2)

    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 sentry JSON payload
        """
        self.send_custom_event('Custom.Mindstorms.Gadget', name.value, payload)


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")

INI File

Python
micube Ini configuration file
# 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.

[GadgetSettings]
amazonId = YOUR_GADGET_AMAZON_ID
alexaGadgetSecret = YOUR_GADGET_SECRET

[GadgetCapabilities]
Custom.Mindstorms.Gadget = 1.0

Credits

Michael Petoukhov

Michael Petoukhov

1 project • 1 follower
I am a Lego Technic fan and YouTuber. YouTube helps me share my custom built projects.

Comments