Andrei CiobanuDorin Suba
Published © CC BY

Color Learn3r

Teach your children the colors using Lego EV3 and Amazon Alexa

IntermediateFull instructions provided4 hours711

Things used in this project

Hardware components

Mindstorms EV3 Programming Brick / Kit
LEGO Mindstorms EV3 Programming Brick / Kit
×1
wireless adapter
optional
×1
Micro SDHC card
for installing ev3dev
×1

Software apps and online services

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

Story

Read more

Code

NodeJS Lambda function

JavaScript
This is used by Alexa to communicate with EV3
const Alexa = require('ask-sdk-core');
const Https = require('https');

const TARGET = process.env.npm_lifecycle_event;

// The namespace of the custom directive to be sent by this skill
const NAMESPACE = 'Custom.Mindstorms.Gadget';

// The name of the custom directive to be sent this skill
const NAME_CONTROL = 'control';

const COLORS = {
    'red': 0,
    'green': 60,
    'yellow': 120,
    'blue': 180,
    'white': 240,
    'black': 300
}

const HINTS = {
    'red': 'a tomato',
    'green': 'the grass',
    'yellow': 'the Sun',
    'blue': 'the ocean',
    'white': 'the snow',
    'black': `Santa's boots`
}

const STORIES = {
    'red': `Once upon a time an apple and a tomato argued who's the reddest fruit in the whole world. The tomato got so mad that it exploded and this is how the ketchup was invented.`,
    'green': `Things that are green: grass, aligator, lettuce, lime, cactus.`,
    'yellow': `Yellow... yellow Sun... yellow banana... yellow baby chick. We all live in a yellow submarine. Yellow submarine, yellow submarine...`,
    'blue': `Blue is the sky, blue is the ocean, the jeans are blue.`,
    'white': `There are lots of things that are W. H. I. T. E.: milk, clouds, snow, Santa's beard.`,
    'black': `Bats, scary cats are black. Santas boots are black.`
}

const ModeEnum = {
    'SHOW': 1,
    'STORY': 2,
    'QUIZ': 3
}
const ModeKey = 'Mode';
const ColorKey = 'Color';
const QuizListKey = 'QuizList';
const QuizIndexKey = 'QuizIndex';

const LaunchRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
    },
    handle: async function (handlerInput) {

        let request = handlerInput.requestEnvelope;
        let { apiEndpoint, apiAccessToken } = request.context.System;
        console.log("Checking endpoint");
        let apiResponse = await getConnectedEndpoints(apiEndpoint, apiAccessToken);
        console.log("v1/endpoints response: " + JSON.stringify(apiResponse));
        if ((apiResponse.endpoints || []).length === 0) {
            return handlerInput.responseBuilder
                .speak(`I couldn't find an EV3 Brick connected to this Echo device. Please check to make sure your EV3 Brick is connected, and try again.`)
                .getResponse();
        }
        // Store the gadget endpointId to be used in this skill session
        let endpointId = apiResponse.endpoints[0].endpointId || [];
        putSessionAttribute(handlerInput, 'endpointId', endpointId);

        return handlerInput.responseBuilder
            .speak("Welcome to color learner. How can I help?")
            .reprompt("How can I help?")
            .getResponse();
    }
};

const ShowColorRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'ShowColorIntent';
    },
    handle: async function (handlerInput) {
        const attributesManager = handlerInput.attributesManager;
        // Set skill duration to 1 minute (2 30-seconds interval)
        putSessionAttribute(handlerInput, 'duration', 2);
        // Set the token to track the event handler
        const token = handlerInput.requestEnvelope.request.requestId;
        putSessionAttribute(handlerInput, 'token', token);
        const endpointId = attributesManager.getSessionAttributes().endpointId || [];
        let color = Alexa.getSlotValue(handlerInput.requestEnvelope, ColorKey);
        color = color.toLowerCase();
        if (color in COLORS) {
            putSessionAttribute(handlerInput, ColorKey, color);
            putSessionAttribute(handlerInput, ModeKey, ModeEnum.SHOW);
            return handlerInput.responseBuilder
                .speak('<audio src="soundbank://soundlibrary/ui/gameshow/amzn_ui_sfx_gameshow_countdown_loop_32s_full_01"></audio>')
                .addDirective(buildShowColorDirective(endpointId, color))
                .addDirective(buildStartEventHandler(token, 60000, {}))
                .getResponse();
        } else {
            let colors = Object.keys(COLORS)
            let color_speach = colors.slice(0, -1).join(', ') + ' and ' + colors.slice(-1);
            return handlerInput.responseBuilder
                .speak(`Unknown color, I only know: ${color_speach}. What color do you want me to show you?`)
                .addElicitSlotDirective(ColorKey)
                .getResponse();
        }
    }
}
const TellStoryRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'TellStoryIntent';
    },
    handle: async function (handlerInput) {
        const attributesManager = handlerInput.attributesManager;
        const endpointId = attributesManager.getSessionAttributes().endpointId || [];
        // Set skill duration to 1 minute (2 30-seconds interval)
        putSessionAttribute(handlerInput, 'duration', 2);
        // Set the token to track the event handler
        const token = handlerInput.requestEnvelope.request.requestId;
        putSessionAttribute(handlerInput, 'token', token);
        let color = Alexa.getSlotValue(handlerInput.requestEnvelope, ColorKey);
        color = color.toLowerCase();
        if (color in COLORS) {
            putSessionAttribute(handlerInput, ColorKey, color);
            putSessionAttribute(handlerInput, ModeKey, ModeEnum.STORY);
            return handlerInput.responseBuilder
                .speak('<audio src="soundbank://soundlibrary/ui/gameshow/amzn_ui_sfx_gameshow_countdown_loop_32s_full_01"></audio>')
                .addDirective(buildShowColorDirective(endpointId, color))
                .addDirective(buildStartEventHandler(token, 60000, {}))
                .getResponse();
        } else {
            let colors = Object.keys(COLORS)
            let color_speach = colors.slice(0, -1).join(', ') + ' and ' + colors.slice(-1);
            return handlerInput.responseBuilder
                .speak(`Unknown color, I only know: ${color_speach}. What should it be?`)
                .addElicitSlotDirective(ColorKey)
                .getResponse();
        }
    }
};

const QuizColorRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'QuizColorIntent';
    },
    handle: async function (handlerInput) {
        const attributesManager = handlerInput.attributesManager;
        const endpointId = attributesManager.getSessionAttributes().endpointId || [];
        // Set skill duration to 1 minute (2 30-seconds interval)
        putSessionAttribute(handlerInput, 'duration', 2);
        // Set the token to track the event handler
        const token = handlerInput.requestEnvelope.request.requestId;
        putSessionAttribute(handlerInput, 'token', token);
        let color = Alexa.getSlotValue(handlerInput.requestEnvelope, ColorKey);
        let index = parseInt(attributesManager.getSessionAttributes()[QuizIndexKey]);
        let color_list = JSON.parse(attributesManager.getSessionAttributes()[QuizListKey]);
        let correct_answer = color_list[index];
        if (color === correct_answer) {
            if (index + 1 >= Object.keys(COLORS).length) {
                putSessionAttribute(handlerInput, ModeKey, null);
                return handlerInput.responseBuilder
                    .speak(`Congratulation. You said ${correct_answer}. You finished the quiz! How can I help next?`)
                    .reprompt('How can I help?')
                    .getResponse();
            } else {
                putSessionAttribute(handlerInput, QuizIndexKey, index + 1);
                return handlerInput.responseBuilder
                    .speak(`Congratulation. You said ${correct_answer}. Next one.` +
                        '<audio src="soundbank://soundlibrary/ui/gameshow/amzn_ui_sfx_gameshow_countdown_loop_32s_full_01"></audio>')
                    .addDirective(buildShowColorDirective(endpointId, color_list[index + 1]))
                    .addDirective(buildStartEventHandler(token, 60000, {}))
                    .getResponse();
            }
        } else {
            return handlerInput.responseBuilder
                .speak(`Incorrect. Try again. It's the color of ${HINTS[correct_answer]}.`)
                .reprompt('What color is this?')
                .addElicitSlotDirective(ColorKey)
                .getResponse();
        }

    }
};

const QuizRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'QuizIntent';
    },
    handle: async function (handlerInput) {
        const attributesManager = handlerInput.attributesManager;
        const endpointId = attributesManager.getSessionAttributes().endpointId || [];
        // Set skill duration to 1 minute (2 30-seconds interval)
        putSessionAttribute(handlerInput, 'duration', 2);
        // Set the token to track the event handler
        const token = handlerInput.requestEnvelope.request.requestId;
        putSessionAttribute(handlerInput, 'token', token);
        const color_list = shuffle_random(Object.keys(COLORS));
        putSessionAttribute(handlerInput, QuizListKey, JSON.stringify(color_list));
        putSessionAttribute(handlerInput, QuizIndexKey, 0);
        putSessionAttribute(handlerInput, ModeKey, ModeEnum.QUIZ);
        return handlerInput.responseBuilder
            .speak('<audio src="soundbank://soundlibrary/ui/gameshow/amzn_ui_sfx_gameshow_countdown_loop_32s_full_01"></audio>')
            .addDirective(buildShowColorDirective(endpointId, color_list[0]))
            .addDirective(buildStartEventHandler(token, 60000, {}))
            .getResponse();
    }
};

const HelpIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
    },
    handle(handlerInput) {
        let colors = Object.keys(COLORS)
        let color_speach = colors.slice(0, -1).join(', ') + ' and ' + colors.slice(-1);
        const speakOutput = `I can show you colors and tell you story about them! `
            + `The colors that I know are ${color_speach}. I can quiz you. How can I help?`;
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .getResponse();
    }
};
const CancelAndStopIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'
                || Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');
    },
    handle(handlerInput) {
        const attributesManager = handlerInput.attributesManager;
        const endpointId = attributesManager.getSessionAttributes().endpointId || [];
        return handlerInput.responseBuilder
            .speak('Goodbye')
            .addDirective(buildStopDirective(endpointId))
            .getResponse();
    }
};
const YesIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.YesIntent';
    },
    handle(handlerInput) {
        const attributesManager = handlerInput.attributesManager;
        const endpointId = attributesManager.getSessionAttributes().endpointId || [];

        const mode = attributesManager.getSessionAttributes()[ModeKey] || null;
        const color = attributesManager.getSessionAttributes()[ColorKey];

        if (mode === ModeEnum.STORY) {
            return handlerInput.responseBuilder
                .addDelegateDirective({
                    name: 'TellStoryIntent',
                    confirmationStatus: 'CONFIRMED',
                    slots:
                    {
                        Color:
                        {
                            name: ColorKey,
                            value: color,
                            confirmationStatus: 'NONE'
                        }
                    }
                })
                .getResponse();
        }

        return handlerInput.responseBuilder
            .speak('How can I help?')
            .reprompt('How can I help?')
            .getResponse();
    }
};

const NoIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.NoIntent';
    },
    handle(handlerInput) {
        putSessionAttribute(handlerInput, ModeKey, null);
        return handlerInput.responseBuilder
            .speak('How can I help?')
            .reprompt('How can I help?')
            .getResponse();
    }
};

const SessionEndedRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
    },
    handle(handlerInput) {
        const reason = handlerInput.requestEnvelope.request.reason;
        console.log("==== SESSION ENDED WITH REASON ======");
        console.log(reason);
        return handlerInput.responseBuilder.getResponse();
    }
};

const RequestInterceptor = {
    process(handlerInput) {
        let { attributesManager, requestEnvelope } = handlerInput;
        let sessionAttributes = attributesManager.getSessionAttributes();

        // Log the request for debugging purposes.
        console.log(`==Request==${JSON.stringify(requestEnvelope)}`);
        console.log(`==SessionAttributes==${JSON.stringify(sessionAttributes, null, 2)}`);
    }
};

const ResponseInterceptor = {
    process(handlerInput) {

        let { attributesManager, responseBuilder } = handlerInput;
        let response = responseBuilder.getResponse();
        let sessionAttributes = attributesManager.getSessionAttributes();

        // Log the response for debugging purposes.
        console.log(`==Response==${JSON.stringify(response)}`);
        console.log(`==SessionAttributes==${JSON.stringify(sessionAttributes, null, 2)}`);
    }
};

const EventsReceivedRequestHandler = {
    // Checks for a valid token and endpoint.
    canHandle(handlerInput) {
        let { request } = handlerInput.requestEnvelope;
        console.log('Request type: ' + Alexa.getRequestType(handlerInput.requestEnvelope));
        if (request.type !== 'CustomInterfaceController.EventsReceived') return false;

        const attributesManager = handlerInput.attributesManager;
        let sessionAttributes = attributesManager.getSessionAttributes();
        let customEvent = request.events[0];

        // Validate event token
        if (sessionAttributes.token !== request.token) {
            console.log("Event token doesn't match. Ignoring this event");
            return false;
        }

        // Validate endpoint
        let requestEndpoint = customEvent.endpoint.endpointId;
        if (requestEndpoint !== sessionAttributes.endpointId) {
            console.log("Event endpoint id doesn't match. Ignoring this event");
            return false;
        }
        return true;
    },
    handle(handlerInput) {

        console.log("== Received Custom Event ==");
        let customEvent = handlerInput.requestEnvelope.request.events[0];
        let payload = customEvent.payload;
        let name = customEvent.header.name;

        const attributesManager = handlerInput.attributesManager;
        const mode = attributesManager.getSessionAttributes()[ModeKey] || null;
        const color = attributesManager.getSessionAttributes()[ColorKey];

        if (name === 'notification' && payload.done === true) {
            if (mode === ModeEnum.STORY) {
                putSessionAttribute(handlerInput, ModeKey, null);
                return handlerInput.responseBuilder
                    .speak(`${STORIES[color]} How can I help next?`, 'REPLACE_ALL')
                    .reprompt('How can I help?')
                    .withShouldEndSession(false)
                    .addDirective(buildStopEventHandlerDirective(handlerInput))
                    .getResponse();
            } else if (mode === ModeEnum.SHOW) {
                putSessionAttribute(handlerInput, ModeKey, ModeEnum.STORY);
                return handlerInput.responseBuilder
                    .speak(`Done. Do you want a story about color ${color}?`, 'REPLACE_ALL')
                    .reprompt('How can I help?')
                    .withShouldEndSession(false)
                    .addDirective(buildStopEventHandlerDirective(handlerInput))
                    .getResponse();
            } else if (mode === ModeEnum.QUIZ) {
                return handlerInput.responseBuilder
                    .speak(`What color is this?`, 'REPLACE_ALL')
                    .reprompt('What color is this?')
                    .withShouldEndSession(false)
                    .addDirective(buildStopEventHandlerDirective(handlerInput))
                    .getResponse();
            } else {
                return handlerInput.responseBuilder
                    .speak(`How can I help?`, 'REPLACE_ALL')
                    .reprompt('How can I help?')
                    .withShouldEndSession(false)
                    .addDirective(buildStopEventHandlerDirective(handlerInput))
                    .getResponse();
            }
        }
        return handlerInput.responseBuilder
            .speak('Unknown event. How can I help?')
            .reprompt('How can I help?')
            .withShouldEndSession(false)
            .getResponse();
    }
};
const ExpiredRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'CustomInterfaceController.Expired'
    },
    handle(handlerInput) {
        console.log("== Custom Event Expiration Input ==");

        const attributesManager = handlerInput.attributesManager;
        const endpointId = attributesManager.getSessionAttributes().endpointId || [];

        // Set the token to track the event handler
        const token = handlerInput.requestEnvelope.request.requestId;
        putSessionAttribute(handlerInput, 'token', token);

        let duration = attributesManager.getSessionAttributes().duration || 0;
        if (duration > 0) {
            putSessionAttribute(handlerInput, 'duration', --duration);
            // Extends skill session
            return handlerInput.responseBuilder
                .addDirective(buildStartEventHandler(token, 60000, {}))
                .getResponse();
        }
        else {
            // End skill session
            return handlerInput.responseBuilder
                .speak("Skill duration expired. Goodbye.")
                .addDirective(buildStopDirective(endpointId))
                .withShouldEndSession(true)
                .getResponse();
        }
    }
};

// The intent reflector is used for interaction model testing and debugging.
// It will simply repeat the intent the user said. You can create custom handlers
// for your intents by defining them above, then also adding them to the request
// handler chain below.
const IntentReflectorHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest';
    },
    handle(handlerInput) {
        const intentName = Alexa.getIntentName(handlerInput.requestEnvelope);
        const speakOutput = `You just triggered ${intentName}`;

        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt("I don't understand this command, try again")
            .getResponse();
    }
};

// Generic error handling to capture any syntax or routing errors. If you receive an error
// stating the request handler chain is not found, you have not implemented a handler for
// the intent being invoked or included it in the skill builder below.
const ErrorHandler = {
    canHandle() {
        return true;
    },
    handle(handlerInput, error) {
        console.log(`~~~~ Error handled: ${error.stack}`);
        const speakOutput = `Sorry, I had trouble doing what you asked. Please try again.`;

        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .getResponse();
    }
};

/**
 * A convenience routine to add the a key-value pair to the session attribute.
 * @param handlerInput - the context from Alexa Service
 * @param key - the key to be added
 * @param value - the value be added
 */
function putSessionAttribute(handlerInput, key, value) {
    const attributesManager = handlerInput.attributesManager;
    let sessionAttributes = attributesManager.getSessionAttributes();
    sessionAttributes[key] = value;
    attributesManager.setSessionAttributes(sessionAttributes);
}

/**
 * To get a list of all the gadgets that meet these conditions,
 * Call the Endpoint Enumeration API with the apiEndpoint and apiAccessToken to
 * retrieve the list of all connected gadgets.
 *
 * @param {string} apiEndpoint - the Endpoint API url
 * @param {string} apiAccessToken  - the token from the session object in the Alexa request
 * @see {@link https://developer.amazon.com/docs/alexa-gadgets-toolkit/send-gadget-custom-directive-from-skill.html#call-endpoint-enumeration-api}
 */
function getConnectedEndpoints(apiEndpoint, apiAccessToken) {

    // The preceding https:// need to be stripped off before making the call
    apiEndpoint = (apiEndpoint || '').replace('https://', '');
    return new Promise(((resolve, reject) => {

        const options = {
            host: apiEndpoint,
            path: '/v1/endpoints',
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': 'Bearer ' + apiAccessToken
            }
        };

        const request = Https.request(options, (response) => {
            response.setEncoding('utf8');
            let returnData = '';
            response.on('data', (chunk) => {
                returnData += chunk;
            });

            response.on('end', () => {
                resolve(JSON.parse(returnData));
            });

            response.on('error', (error) => {
                reject(error);
            });
        });
        request.end();
    }));
}

function buildShowColorDirective(endpointId, color) {
    return {
        type: 'CustomInterfaceController.SendDirective',
        header: {
            name: NAME_CONTROL,
            namespace: NAMESPACE
        },
        endpoint: {
            endpointId: endpointId
        },
        payload: {
            type: 'show',
            color: color
        }
    };
}

function buildStopDirective(endpointId) {
    return {
        type: 'CustomInterfaceController.SendDirective',
        header: {
            name: NAME_CONTROL,
            namespace: NAMESPACE
        },
        endpoint: {
            endpointId: endpointId
        },
        payload: {
            type: 'stop'
        }
    };
}

function buildStartEventHandler(token, timeout = 30000, payload) {
    return {
        type: "CustomInterfaceController.StartEventHandler",
        token: token,
        expiration: {
            durationInMilliseconds: timeout,
            expirationPayload: payload
        }
    };
}

function buildStopEventHandlerDirective(handlerInput) {
    let token = handlerInput.attributesManager.getSessionAttributes().token || '';
    return {
        "type": "CustomInterfaceController.StopEventHandler",
        "token": token
    }
}

function shuffle_random(unshuffled) {
    return unshuffled
        .map((a) => ({ sort: Math.random(), value: a }))
        .sort((a, b) => a.sort - b.sort)
        .map((a) => a.value);
}


// The SkillBuilder acts as the entry point for your skill, routing all request and response
// payloads to the handlers above. Make sure any new handlers or interceptors you've
// defined are included below. The order matters - they're processed top to bottom.
exports.handler = Alexa.SkillBuilders.custom()
    .addRequestHandlers(
        LaunchRequestHandler,
        ShowColorRequestHandler,
        TellStoryRequestHandler,
        YesIntentHandler,
        NoIntentHandler,
        QuizRequestHandler,
        QuizColorRequestHandler,
        EventsReceivedRequestHandler,
        ExpiredRequestHandler,
        HelpIntentHandler,
        CancelAndStopIntentHandler,
        SessionEndedRequestHandler,
        IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
    )
    .addRequestInterceptors(RequestInterceptor)
    .addResponseInterceptors(ResponseInterceptor)
    .addErrorHandlers(ErrorHandler)
    .lambda();

Alexa Model

JSON
The model for the VUI
{
  "interactionModel": {
    "languageModel": {
      "invocationName": "color learner",
      "intents": [
        {
          "name": "AMAZON.CancelIntent",
          "samples": []
        },
        {
          "name": "AMAZON.HelpIntent",
          "samples": []
        },
        {
          "name": "AMAZON.StopIntent",
          "samples": []
        },
        {
          "name": "AMAZON.NavigateHomeIntent",
          "samples": []
        },
        {
          "name": "ShowColorIntent",
          "slots": [
            {
              "name": "Color",
              "type": "AMAZON.Color",
              "samples": [
                "{Color}"
              ]
            }
          ],
          "samples": [
            "show me the {Color}",
            "show color {Color}",
            "show {Color}",
            "display color {Color}",
            "display {Color}"
          ]
        },
        {
          "name": "TellStoryIntent",
          "slots": [
            {
              "name": "Color",
              "type": "AMAZON.Color"
            }
          ],
          "samples": [
            "Tell story about color {Color}",
            "More about color {Color}",
            "Tell me a story about color {Color}",
            "Tell story about {Color}",
            "More about {Color}",
            "Tell me a story about {Color}"
          ]
        },
        {
          "name": "AMAZON.YesIntent",
          "samples": []
        },
        {
          "name": "AMAZON.NoIntent",
          "samples": []
        },
        {
          "name": "QuizColorIntent",
          "slots": [
            {
              "name": "Color",
              "type": "AMAZON.Color",
              "samples": [
                "color {Color}",
                "It is {Color}",
                "It's {Color}",
                "{Color}"
              ]
            }
          ],
          "samples": [
            "Color {Color}",
            "It is color {Color}",
            "It's color {Color}",
            "This is color {Color}",
            "This is {Color}",
            "It's {Color}",
            "It is {Color}",
            "{Color}"
          ]
        },
        {
          "name": "QuizIntent",
          "slots": [],
          "samples": [
            "test me",
            "take quiz",
            "take a quiz",
            "Let's take a quiz",
            "Quiz"
          ]
        }
      ],
      "types": []
    },
    "dialog": {
      "intents": [
        {
          "name": "ShowColorIntent",
          "confirmationRequired": false,
          "prompts": {},
          "slots": [
            {
              "name": "Color",
              "type": "AMAZON.Color",
              "confirmationRequired": false,
              "elicitationRequired": true,
              "prompts": {
                "elicitation": "Elicit.Slot.Color"
              }
            }
          ]
        },
        {
          "name": "TellStoryIntent",
          "confirmationRequired": true,
          "prompts": {
            "confirmation": "Confirm.Intent.StoryColor"
          },
          "slots": [
            {
              "name": "Color",
              "type": "AMAZON.Color",
              "confirmationRequired": false,
              "elicitationRequired": true,
              "prompts": {
                "elicitation": "Elicit.Slot.Color"
              }
            }
          ]
        },
        {
          "name": "QuizColorIntent",
          "confirmationRequired": false,
          "prompts": {},
          "slots": [
            {
              "name": "Color",
              "type": "AMAZON.Color",
              "confirmationRequired": false,
              "elicitationRequired": true,
              "prompts": {
                "elicitation": "Elicit.Slot.QuizColor"
              }
            }
          ]
        },
        {
          "name": "QuizIntent",
          "confirmationRequired": true,
          "prompts": {
            "confirmation": "Confirm.Intent.Quiz"
          },
          "slots": []
        }
      ],
      "delegationStrategy": "ALWAYS"
    },
    "prompts": [
      {
        "id": "Elicit.Slot.Color",
        "variations": [
          {
            "type": "PlainText",
            "value": "What color?"
          },
          {
            "type": "PlainText",
            "value": "What color should it be?"
          }
        ]
      },
      {
        "id": "Confirm.Intent.StoryColor",
        "variations": [
          {
            "type": "PlainText",
            "value": "Do you want me to tell you a story about the color {Color}?"
          },
          {
            "type": "PlainText",
            "value": "Should I tell you more about {Color} ?"
          }
        ]
      },
      {
        "id": "Elicit.Slot.QuizColor",
        "variations": [
          {
            "type": "PlainText",
            "value": "What color?"
          },
          {
            "type": "PlainText",
            "value": "What color did you say?"
          }
        ]
      },
      {
        "id": "Confirm.Intent.Quiz",
        "variations": [
          {
            "type": "PlainText",
            "value": "Are you sure you want a test?"
          },
          {
            "type": "PlainText",
            "value": "Do you want to take a quiz?"
          }
        ]
      }
    ]
  }
}

Gadget Source Code

Python
The source code that runs on EV3 brick
#!/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 threading

from ev3dev2.sound import Sound
from ev3dev2.led import Leds
from ev3dev2.motor import LargeMotor, OUTPUT_A, OUTPUT_D
from ev3dev2.sensor.lego import ColorSensor

from agt import AlexaGadget

# set logger to display on both EV3 Brick and console
logging.basicConfig(level=logging.INFO, stream=sys.stdout,
                    format='%(message)s')
logging.getLogger().addHandler(logging.StreamHandler(sys.stderr))
logger = logging.getLogger(__name__)

BLACK = 1
YELLOW = 4

COLORS = {
    'red': 0,
    'green': 60,
    'yellow': 120,
    'blue': 180,
    'white': 240,
    'black': 300
}


class MindstormsGadget(AlexaGadget):
    """
    An Mindstorms gadget that will react to the Alexa wake word.
    """

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

        self.leds = Leds()
        self.sound = Sound()
        self.motor_spin = LargeMotor(OUTPUT_A)
        self.motor_lift = LargeMotor(OUTPUT_D)
        self.color_sensor = ColorSensor()
        self.current_angle = 0
        self.is_lifted = False
        self.lock = threading.Lock()
        self.reset_to_zero()

    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 reset_to_zero(self):
        self.lock.acquire()
        if self.is_lifted:
            self.arm_down(locking=False)
        self.motor_spin.on(speed=10)
        while self.color_sensor.color != BLACK:
            time.sleep(0.1)
        self.motor_spin.off(brake=True)
        self.motor_spin.on(speed=10)
        while self.color_sensor.color != YELLOW:
            time.sleep(0.1)
        self.motor_spin.off(brake=True)
        self.current_angle = 0
        self.lock.release()

    def move_to_color(self, color):
        color_angle = COLORS[color]
        move_angle = color_angle - self.current_angle
        if self.is_lifted and move_angle == 0:
            return
        self.lock.acquire()
        # might have changed so compute again
        move_angle = color_angle - self.current_angle
        if self.is_lifted:
            self.arm_down(locking=False)
        self.motor_spin.on_for_degrees(speed=10, degrees=move_angle)
        self.current_angle = color_angle
        self.arm_up(locking=False)
        self.lock.release()

    def arm_up(self, locking=True):
        if locking:
            self.lock.acquire()
        if not self.is_lifted:
            self.motor_lift.on_for_rotations(speed=90, rotations=-15)
        self.is_lifted = True
        if locking:
            self.lock.release()

    def arm_down(self, locking=True):
        if locking:
            self.lock.acquire()
        if self.is_lifted:
            self.motor_lift.on_for_rotations(speed=90, rotations=15)
        self.is_lifted = False
        if locking:
            self.lock.release()

    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)
            if payload['type'] == 'show':
                self.move_to_color(payload['color'])
                self.send_custom_event(
                    'Custom.Mindstorms.Gadget', 'notification', {'done': True})
            if payload['type'] == 'stop':
                self.reset_to_zero()
        except KeyError:
            print("Missing expected parameters: {}".format(
                directive), file=sys.stderr)


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

    gadget.sound.set_volume(25)

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

    gadget.arm_down()

Credits

Andrei Ciobanu

Andrei Ciobanu

12 projects • 16 followers
Dorin Suba

Dorin Suba

1 project • 1 follower
Romanian software system test engineer currently living in Germany.

Comments