ankit sachdev
Created December 30, 2019

Teacher, teacher!

A fun interactive EV3 based robot which helps a child to learn how to write alphabet numbers and draw shapes powered with Alexa.

IntermediateProtip5 hours24
Teacher, teacher!

Things used in this project

Story

Read more

Schematics

brick_two_C4g1xvTwYc.png

brick_one_vPuII3fM6E.png

Code

model.json

JSON
{
    "interactionModel": {
        "languageModel": {
            "invocationName": "let's learn",
            "intents": [
                {
                    "name": "AMAZON.CancelIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.HelpIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.StopIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.NavigateHomeIntent",
                    "samples": []
                },
                {
                    "name": "LessonIntent",
                    "slots": [
                        {
                            "name": "number",
                            "type": "AMAZON.NUMBER",
                            "samples": [
                                "lesson five",
                                "open lesson {number}",
                                "{number} lesson",
                                "lesson {number}",
                                "lesson three",
                                "lesson two",
                                "lesson one",
                                "lesson"
                            ]
                        }
                    ],
                    "samples": [
                        "ask water bottle to open lesson one",
                        "open lesson {number}",
                        "{number} lesson",
                        "lesson {number}",
                        "lesson three",
                        "lesson two",
                        "lesson one",
                        "lesson"
                    ]
                }
            ],
            "types": []
        },
        "dialog": {
            "intents": [
                {
                    "name": "LessonIntent",
                    "confirmationRequired": false,
                    "prompts": {},
                    "slots": [
                        {
                            "name": "number",
                            "type": "AMAZON.NUMBER",
                            "confirmationRequired": false,
                            "elicitationRequired": true,
                            "prompts": {
                                "elicitation": "Elicit.Slot.1368406247135.116705719181"
                            }
                        }
                    ]
                }
            ],
            "delegationStrategy": "ALWAYS"
        },
        "prompts": [
            {
                "id": "Elicit.Slot.1368406247135.116705719181",
                "variations": [
                    {
                        "type": "PlainText",
                        "value": "lesson two selected"
                    },
                    {
                        "type": "PlainText",
                        "value": "lesson one selected"
                    },
                    {
                        "type": "PlainText",
                        "value": "Lesson {number} selected"
                    }
                ]
            }
        ]
    }
}

Mindstorms code

Scratch
Used for understanding what is written
No preview (download only).

python code

Python
#!/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, MoveTank, SpeedPercent, MediumMotor

# 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 MindstormsGadget(AlexaGadget):
    """
    A Mindstorms gadget that performs movement based on voice commands.
    Two types of commands are supported, directional movement and preset.
    """

    def __init__(self):
        """
        Performs Alexa Gadget initialization routines and ev3dev resource allocation.
        """
        super().__init__()

        # Gadget state
        self.patrol_mode = False

        # Ev3dev initialization
        self.leds = Leds()
        self.sound = Sound()
        self.drive = MoveTank(OUTPUT_B, OUTPUT_C)
        self.weapon = MediumMotor(OUTPUT_A)

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

    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 == "lesson":

                # Expected params: [number]
                self._lesson(payload["number"])

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

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

        if number in one:
            self.weapon.on_for_rotations(SpeedPercent(20), .25)
            wait(cond, timeout=2000)
            self.weapon.on_for_rotations(SpeedPercent(-20), .25)
         
        if number in two:
            self.weapon.on_for_rotations(SpeedPercent(20), .5)
            wait(cond, timeout=2000)
            self.weapon.on_for_rotations(SpeedPercent(-20), .5)

            
        if number in three:
            self.weapon.on_for_rotations(SpeedPercent(20), .75)
            wait(cond, timeout=2000)
            self.weapon.on_for_rotations(SpeedPercent(-20), .75)

class lesson(Enum):
    """
    The list of directional commands and their variations.
    These variations correspond to the skill slot values.
    """
    ALPHABET = ['lesson one', 'ABCD', 'Alphabets', 'one']
    NUMBERS = ['lesson two', 'one two three four', 'numbers', 'two']
    SHAPES = ['lesson three', 'Shape','three']

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 = ['stop', 'brake']
    ALPHABET = ['lesson one', 'ABCD', 'Alphabets']
    NUMBERS = ['lesson two', 'one two three four', 'numbers']
    SHAPES = ['lesson three', 'Shape']
class Command(Enum):
    """
    The list of preset commands and their invocation variation.
    These variations correspond to the skill slot values.
    """
    MOVE_CIRCLE = ['circle', 'spin']
    MOVE_SQUARE = ['square']
    PATROL = ['patrol', 'guard mode', 'sentry mode']
    FIRE_ONE = ['cannon', '1 shot', 'one shot']
    FIRE_ALL = ['all shot']
               
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")

Credits

ankit sachdev

ankit sachdev

1 project • 1 follower

Comments