Hackster is hosting Hackster Holidays, Ep. 4: Livestream & Giveaway Drawing. Start streaming on Wednesday!Stream Hackster Holidays, Ep. 4 on Wednesday!
clzfranco
Published © GPL3+

Boxing robots

A two players game, each user controls one robots with voice commands, the robot with more punches is the winner

IntermediateFull instructions provided4 hours40
Boxing robots

Things used in this project

Story

Read more

Schematics

Boxing Robots Project Walkthrough

This file shows how to build the boxing robot, and explains the key elements of the code

Code

index.js

JavaScript
Main skill 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.
*/

// This sample demonstrates sending directives to an Echo connected gadget from an Alexa skill
// using the Alexa Skills Kit SDK (v2). Please visit https://alexa.design/cookbook for additional
// examples on implementing slots, dialog management, session persistence, api calls, and more.

const Alexa = require('ask-sdk-core');
const Util = require('./util');
const Common = require('./common');
const EV3 = require('./ev3');
const Escape = require('lodash/escape');

// 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 states = {
  START: `_START`,
  SUMO: `_SUMO`,
};

//const welcomeMessage = `Bienvenido al juego de los robots! Puedes pedir que inicie la pelea.  Que quieres hacer?`;
const welcomeMessage = `Welcome to Boxing Robots!  You can ask me about the commands help, or you can ask me to start a fight.  What would you like to do?`;
const endGameMessage = `You can ask me about the commands help, or you can ask me to start a fight.  What would you like to do?`;

const helpMessage = `To move the robot you can say forward, back, go left, go right. To attack the opponent say left or right. What would you like to do?`;


const useCardsFlag = true;

const LaunchRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
    },
    handle: async function(handlerInput) {
        
        let request = handlerInput.requestEnvelope;
        let { apiEndpoint, apiAccessToken } = request.context.System;
        let apiResponse = await EV3.getConnectedEndpoints(apiEndpoint, apiAccessToken);
        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();
        }
        
        if ((apiResponse.endpoints || []).length === 1) {
            return handlerInput.responseBuilder
            .speak(`no se puedo encontrar el segundo EV3`)
            .getResponse();
        }

        // Store the gadget endpointId to be used in this skill session
        let endpointId = apiResponse.endpoints[0].endpointId || [];
        Util.putSessionAttribute(handlerInput, 'endpointId', endpointId);

        let endpointId2 = apiResponse.endpoints[1].endpointId || [];
        Util.putSessionAttribute(handlerInput, 'endpointId2', endpointId2);
        
        // Set skill duration to 5 minutes (ten 30-seconds interval)
        Util.putSessionAttribute(handlerInput, 'duration', 10);

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

        // Construct the directive with the payload containing the move parameters
        const directive1 = EV3.build(endpointId, NAMESPACE, NAME_CONTROL,
            {
                type: 'init',
                player: '1' //player 2
            });
            
        // Construct the directive with the payload containing the move parameters
        const directive2 = EV3.build(endpointId2, NAMESPACE, NAME_CONTROL,
            {
                type: 'init',
                player: '2' //player 2
            });
 
        Util.putAPLmessage(handlerInput, 'Boxing Robots', 'Ready for fight' , 'If you want to play just say start.');
               
        return handlerInput.responseBuilder
            .speak(welcomeMessage)
            .reprompt(helpMessage)
            .addDirective(EV3.buildStartEventHandler(token,60000, {}))
            .addDirective(directive1)
            .addDirective(directive2)
            .getResponse();
    }
};

const BoxingIntentHandler = {
  canHandle(handlerInput) {
    const attributes = handlerInput.attributesManager.getSessionAttributes();  
    const request = handlerInput.requestEnvelope.request;
    
    return (request.type === "IntentRequest") && request.intent.name === "BoxingIntent";
           
  },
  handle(handlerInput) {
    console.log("Inside BoxingIntent - handle");
    const attributes = handlerInput.attributesManager.getSessionAttributes();
    var state = states.SUMO;
    Util.putSessionAttribute(handlerInput, 'state', state);

    let turn = "1";
    Util.putSessionAttribute(handlerInput, 'turn', turn);

    Util.putSessionAttribute(handlerInput, 'counter', 0);
    Util.putSessionAttribute(handlerInput, 'round', 1);
    
    Util.putSessionAttribute(handlerInput, 'punches_1', 0);
    Util.putSessionAttribute(handlerInput, 'punches_2', 0);
    
    Util.putSessionAttribute(handlerInput, 'power_1', 0);
    Util.putSessionAttribute(handlerInput, 'power_Enabled_1', false);
    
    Util.putSessionAttribute(handlerInput, 'power_2', 0);
    Util.putSessionAttribute(handlerInput, 'power_Enabled_2', false);
    
    Util.putSessionAttribute(handlerInput, 'duration', 10);

 
 // Construct the directive with the payload containing the move parameters
    const directive1 = EV3.build(attributes.endpointId, NAMESPACE, NAME_CONTROL,
        {
            type: 'start'
        });

    const directive2 = EV3.build(attributes.endpointId2, NAMESPACE, NAME_CONTROL,
        {
            type: 'start'
        });

    var player= Util.player_color(turn);

            const bellUrl0= Util.getS3PreSignedUrl('Media/bell2.mp3'); 
            const audioBellUrl = `<audio src="${Escape(bellUrl0)}"></audio>`;
            
            var strRound= audioBellUrl + 'Round 1. ';


    const playerMessage= 'Waiting for player '  + player + ' commands '
 
    Util.putAPLmessagep(handlerInput, 'Boxing Robots',  player + ` player's turn`, 'commands forward, left, right, backward, raise', turn);


 return handlerInput.responseBuilder
            .speak( strRound + playerMessage )
            .reprompt(helpMessage)
            .addDirective(directive1)
            .addDirective(directive2)
            .getResponse();

  },
};

// Add the speed value to the session attribute.
// This allows other intent handler to use the specified speed value
// without asking the user for input.
const SetSpeedIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'SetSpeedIntent';
    },
    handle: function (handlerInput) {

        // Bound speed to (1-100)
        let speed = Alexa.getSlotValue(handlerInput.requestEnvelope, 'Speed');
        speed = Math.max(1, Math.min(100, parseInt(speed)));
        Util.putSessionAttribute(handlerInput, 'speed', speed);

        return handlerInput.responseBuilder
            .speak(`speed set to ${speed} percent.`)
            .reprompt("awaiting command")
            .getResponse();
    }
};

// Construct and send a custom directive to the connected gadget with
// data from the MoveIntent request.
const MoveIntentHandler = {
    canHandle(handlerInput) {
        const attributes = handlerInput.attributesManager.getSessionAttributes();
        
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'MoveIntent';
    },
    handle: function (handlerInput) {
        const request = handlerInput.requestEnvelope;
        const direction = Alexa.getSlotValue(request, 'Direction');

        // Duration is optional, use default if not available
        const duration = Alexa.getSlotValue(request, 'Duration') || "0.5";

        const attributes = handlerInput.attributesManager.getSessionAttributes();

        // Get data from session attribute
        const speed = attributes.speed || "30";
        var speechOutput, repromptOutput;

        let turn= attributes.turn || [];
        
        let counter= attributes.counter;
        let round= attributes.round;
        var endpointId;
        var power_enabled='';
        var punches= '';

        if (turn === "1"){
            endpointId = attributes.endpointId || [];
            turn= "2";
            if (attributes.power_Enabled_2)
                power_enabled= '<br>Power enabled';
            punches= attributes.punches_2;
        }
        else{
            endpointId = attributes.endpointId2 || [];
            turn= "1";
            if (attributes.power_Enabled_1)
                power_enabled= '<br>Power enabled';
            punches= attributes.punches_1;
        }
        
        var player= Util.player_color(turn);
        Util.putSessionAttribute(handlerInput, 'turn', turn);
        
        counter += 1;
        
        var strRound='';

        if (counter===7){
            counter=1;
            round++;
            
            const bellUrl0= Util.getS3PreSignedUrl('Media/bell2.mp3'); 
            const audioBellUrl = `<audio src="${Escape(bellUrl0)}"></audio>`;
            
            strRound= audioBellUrl + 'Round ' + round;

        }
        
        if (round===4){
            var fight_result='';
            
            if (attributes.punches_1 > attributes.punches_2)
                fight_result= Util.player_color('1') + ' player wins. '
            else
            if (attributes.punches_2 > attributes.punches_1)
                fight_result= Util.player_color('2') + ' player wins. '
            else
                fight_result= 'Draw fight';

                
            speechOutput= fight_result;
            speechOutput += endGameMessage;
            
            Util.putAPLmessage(handlerInput, 'Boxing Robots', fight_result , 'If you want to play just say start.');

            return handlerInput.responseBuilder
            .speak(speechOutput)
            .reprompt(repromptOutput)
            .getResponse();
        }
        
        
        Util.putSessionAttribute(handlerInput, 'counter', counter);
        Util.putSessionAttribute(handlerInput, 'round', round);

        // Construct the directive with the payload containing the move parameters
        const directive = EV3.build(endpointId, NAMESPACE, NAME_CONTROL,
            {
                type: 'move',
                direction: direction,
                duration: duration,
                speed: speed
            });
            
        repromptOutput= 'Waiting for player '  + player + ' commands ';
        speechOutput= repromptOutput;
        
        Util.putAPLmessagep(handlerInput, 'Boxing Robots',  player + ` player's turn` + power_enabled + '<br>Punches: '+ punches, 'commands forward, left, right, backward, raise', turn);

        return handlerInput.responseBuilder
            .speak(strRound + speechOutput )
            .reprompt(repromptOutput)
            .addDirective(directive)
            .getResponse();
    }
};

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;
        let endpointId = attributesManager.getSessionAttributes().endpointId || [];
        let endpointId2 = attributesManager.getSessionAttributes().endpointId2 || [];        
 
        if (requestEndpoint !== sessionAttributes.endpointId && requestEndpoint !== sessionAttributes.endpointId2) {
            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 attributes = handlerInput.attributesManager.getSessionAttributes();

        var speechOutput, repromptOutput;
                
        if (name === 'Touch') {
            console.log("******* Received Custom Event ******");
            let player = payload.player;
            var player_color= Util.player_color(player);

            var punches, power, power_Enabled;
            
            speechOutput= '';
            repromptOutput= endGameMessage;
            
            if (player === '1'){
                punches= attributes.punches_1 + 1;
                Util.putSessionAttribute(handlerInput, 'punches_1', punches);
                power= attributes.power_1;
                if (power < 5){
                    power++;
                    Util.putSessionAttribute(handlerInput, 'power_1', punches);
                }
                else{
                    power_Enabled= attributes.power_Enabled_1;
                    if (!power_Enabled){
                        Util.putSessionAttribute(handlerInput, 'power_Enabled_1', true);
                        speechOutput= player_color + ' player has power enabled. '
                    }
                }
            }
            else{
                punches= attributes.punches_2 + 1;
                Util.putSessionAttribute(handlerInput, 'punches_2', punches);
                
                power= attributes.power_2;
                if (power < 5){
                    power++;
                    Util.putSessionAttribute(handlerInput, 'power_2', punches);
                }
                else{
                    power_Enabled= attributes.power_Enabled_2;
                    if (!power_Enabled){
                        Util.putSessionAttribute(handlerInput, 'power_Enabled_2', true);
                        speechOutput= player_color + ' player has power enabled. '
                    }
                }
                
            }
            
            let turn= attributes.turn || [];
            player_color= Util.player_color(turn);
            repromptOutput= 'Waiting for player '  + player_color + ' commands ';
            speechOutput=repromptOutput
            
            return handlerInput.responseBuilder
            .speak('')
            .reprompt(repromptOutput)
            .getResponse();
            

        }
        
        else if (name === 'Sentry') {
            if ('fire' in payload) {
                speechOutput = "Threat eliminated";
            }

        } else if (name === 'Speech') {
            speechOutput = payload.speechOut;

        } else {
            speechOutput = "Event not recognized. Awaiting new command.";
        }
        return handlerInput.responseBuilder
            .speak(speechOutput, "REPLACE_ALL")
            .getResponse();
    }
};

const ExitHandler = {
  canHandle(handlerInput) {
    console.log("Inside ExitHandler");
    const attributes = handlerInput.attributesManager.getSessionAttributes();
    const request = handlerInput.requestEnvelope.request;

    return request.type === `IntentRequest` && (
              request.intent.name === 'AMAZON.StopIntent' ||
              request.intent.name === 'AMAZON.PauseIntent' ||
              request.intent.name === 'AMAZON.CancelIntent'
           );
  },
  handle(handlerInput) {
      const exitSkillMessage= "Thank you for playing, see you soon";
  
    return handlerInput.responseBuilder
      .speak(exitSkillMessage)
      .addDirective(EV3.buildStopEventHandlerDirective(handlerInput))
      .withShouldEndSession(true)
      .getResponse();
  },
};

const ExpiredRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'CustomInterfaceController.Expired'
    },
    handle(handlerInput) {
        console.log("======== Custom Event Expiration Input ==========");

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

        const attributesManager = handlerInput.attributesManager;
        let duration = attributesManager.getSessionAttributes().duration || 0;
        if (duration > 0) {
            Util.putSessionAttribute(handlerInput, 'duration', --duration);

            // Extends skill session
            const speechOutput = `${duration} minutes remaining.`;
            return handlerInput.responseBuilder
                .addDirective(EV3.buildStartEventHandler(token, 60000, {}))
                .speak(speechOutput)
                .getResponse();
        }
        else {
            // End skill session
            return handlerInput.responseBuilder
                .speak("Skill duration expired. Goodbye.")
                .withShouldEndSession(true)
                .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();
    }
};

/*const SKILL_NAME = 'Gloucester Guide';
const FALLBACK_MESSAGE = `The ${SKILL_NAME} skill can\'t help you with that.  It can help you learn about Gloucester if you say tell me about this place. What can I help you with?`;
const FALLBACK_REPROMPT = 'What can I help you with?';
*/

const FallbackHandler = {

  // 2018-May-01: AMAZON.FallackIntent is only currently available in en-US locale.

  //              This handler will not be triggered except in that locale, so it can be

  //              safely deployed for any locale.

  canHandle(handlerInput) {

    const request = handlerInput.requestEnvelope.request;

    return request.type === 'IntentRequest'

      && request.intent.name === 'AMAZON.FallbackIntent';

  },

  handle(handlerInput) {
    
    const attributes = handlerInput.attributesManager.getSessionAttributes();  
    const FALLBACK_REPROMPT = 'What can I help you with?';
    var FALLBACK_MESSAGE;
    
    if (attributes.state === states.START)
        FALLBACK_MESSAGE= "Sorry I can't help you with that. You can ask me about the commands help, or you can ask me to start a fight.  What would you like to do?"
    else{
        var player= Util.player_color(attributes.turn);

        var playerMessage= 'Waiting for player'  + player + ' commands '
        FALLBACK_MESSAGE= "Sorry I can't help you with that. " + playerMessage
    }

    return handlerInput.responseBuilder

      .speak(FALLBACK_MESSAGE)

      .reprompt(FALLBACK_REPROMPT)

      .getResponse();

  },

};


// 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,
        BoxingIntentHandler,
        SetSpeedIntentHandler,
        MoveIntentHandler,
        ExpiredRequestHandler,
        EventsReceivedRequestHandler,
        ExitHandler,
        FallbackHandler,
        Common.HelpIntentHandler,
        Common.SessionEndedRequestHandler,
        Common.IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
    )
    .addErrorHandlers(
        //Common.ErrorHandler,
        ErrorHandler,
    )
    .lambda();

ev3.js

JavaScript
EV3 connection functions
'use strict';

const Https = require('https');
const AWS = require('aws-sdk');
const Escape = require('lodash/escape');

/**
 * Builds a directive to start the EventHandler.
 * @param token - a unique identifier to track the event handler
 * @param {number} timeout - the duration to wait before sending back the expiration
 * payload to the skill.
 * @param payload - the expiration json payload
 * @see {@link https://developer.amazon.com/docs/alexa-gadgets-toolkit/receive-custom-event-from-gadget.html#start}
 */
exports.buildStartEventHandler = function (token, timeout = 30000, payload)  {
    return {
        type: "CustomInterfaceController.StartEventHandler",
        token: token,
        expiration : {
            durationInMilliseconds: timeout,
            expirationPayload: payload
        }
    };
};

/**
 *
 * Builds a directive to stops the active event handler.
 * The event handler is identified by the cached token in the session attribute.
 * @param {string} handlerInput - the context from Alexa Service
 * @see {@link https://developer.amazon.com/docs/alexa-gadgets-toolkit/receive-custom-event-from-gadget.html#stop}
 */
exports.buildStopEventHandlerDirective = function (handlerInput) {

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


/**
 * Build a custom directive payload to the gadget with the specified endpointId
 * @param {string} endpointId - the gadget endpoint Id
 * @param {string} namespace - the namespace of the skill
 * @param {string} name - the name of the skill within the scope of this namespace
 * @param {object} payload - the payload data
 * @see {@link https://developer.amazon.com/docs/alexa-gadgets-toolkit/send-gadget-custom-directive-from-skill.html#respond}
 */
exports.build = function (endpointId, namespace, name, payload) {
    // Construct the custom directive that needs to be sent
    // Gadget should declare the capabilities in the discovery response to
    // receive the directives under the following namespace.
    return {
        type: 'CustomInterfaceController.SendDirective',
        header: {
            name: name,
            namespace: namespace
        },
        endpoint: {
            endpointId: endpointId
        },
        payload
    };
};

/**
 * 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}
 */
exports.getConnectedEndpoints = function(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();
    }));
};

util.js

JavaScript
Functions for the skill
'use strict';

const Https = require('https');
const AWS = require('aws-sdk');
const Escape = require('lodash/escape');


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

const s3SigV4Client = new AWS.S3({
    signatureVersion: 'v4'
});


module.exports= {

    getS3PreSignedUrl(s3ObjectKey) {
    
        const bucketName = process.env.S3_PERSISTENCE_BUCKET;
        const s3PreSignedUrl = s3SigV4Client.getSignedUrl('getObject', {
            Bucket: bucketName,
            Key: s3ObjectKey,
            Expires: 60*1 // the Expires is capped for 1 minute
        });
        console.log(`Util.s3PreSignedUrl: ${s3ObjectKey} URL ${s3PreSignedUrl}`);
        return s3PreSignedUrl;
    
    },

    getPersistenceAdapter(tableName) {
    // This function is an indirect way to detect if this is part of an Alexa-Hosted skill
    function isAlexaHosted() {
        return process.env.S3_PERSISTENCE_BUCKET;
    }
    if (isAlexaHosted()) {
        const {S3PersistenceAdapter} = require('ask-sdk-s3-persistence-adapter');
        return new S3PersistenceAdapter({
            bucketName: process.env.S3_PERSISTENCE_BUCKET
        });
    } else {
        // IMPORTANT: don't forget to give DynamoDB access to the role you're using to run this lambda (via IAM policy)
        const {DynamoDbPersistenceAdapter} = require('ask-sdk-dynamodb-persistence-adapter');
        return new DynamoDbPersistenceAdapter({
            tableName: tableName || 'simon_says',
            createTable: true
        });
    }
    },

   
   putSessionAttribute(handlerInput, key, value) {
    const attributesManager = handlerInput.attributesManager;
    let sessionAttributes = attributesManager.getSessionAttributes();
    sessionAttributes[key] = value;
    attributesManager.setSessionAttributes(sessionAttributes);
},

   supportsAPL(handlerInput) {
        const {supportedInterfaces} = handlerInput.requestEnvelope.context.System.device;
        return !!supportedInterfaces['Alexa.Presentation.APL'];
    },    
    

  putAPLmessage(handlerInput, headerTxt, mainTxt, hintTxt){
    
     if (this.supportsAPL(handlerInput)){
    
        const {Viewport} = handlerInput.requestEnvelope.context;
        const resolution = Viewport.pixelWidth + 'x' + Viewport.pixelHeight;
          
        handlerInput.responseBuilder
                .addDirective({
                    type: 'Alexa.Presentation.APL.RenderDocument',
                    document:  require('./documents/launch.json'),
                    datasources: {
                     launchData: {
                                    type: 'object',
                                    properties: {
                                        headerTitle: headerTxt,
                                        mainText: mainTxt,
                                        hintString: hintTxt,
                                        //logoImage: isBirthday ? null : Viewport.pixelWidth > 480 ? util.getS3PreSignedUrl('Media/full_icon_512.png') : util.getS3PreSignedUrl('Media/full_icon_108.png'),
                                        backgroundImage: this.getS3PreSignedUrl('Media/simon_'+resolution+'.png') ,
                                        backgroundOpacity: "0.5"
                                    },
                                    transformers: [{
                                        inputPath: 'hintString',
                                        transformer: 'textToHint',
                                    }]
                                }
                      }
                    
                });  
     }
     
  },  
 
   putAPLmessagep(handlerInput, headerTxt, mainTxt, hintTxt, player){
    
     if (this.supportsAPL(handlerInput)){
    
        const {Viewport} = handlerInput.requestEnvelope.context;
        const resolution = Viewport.pixelWidth + 'x' + Viewport.pixelHeight;
          
        handlerInput.responseBuilder
                .addDirective({
                    type: 'Alexa.Presentation.APL.RenderDocument',
                    document:  require('./documents/launch.json'),
                    datasources: {
                     launchData: {
                                    type: 'object',
                                    properties: {
                                        headerTitle: headerTxt,
                                        mainText: mainTxt,
                                        hintString: hintTxt,
                                        //logoImage: isBirthday ? null : Viewport.pixelWidth > 480 ? util.getS3PreSignedUrl('Media/full_icon_512.png') : util.getS3PreSignedUrl('Media/full_icon_108.png'),
                                        backgroundImage: this.getS3PreSignedUrl('Media/player_' + player + '_' + resolution+'.png') ,
                                        backgroundOpacity: "0.5"
                                    },
                                    transformers: [{
                                        inputPath: 'hintString',
                                        transformer: 'textToHint',
                                    }]
                                }
                      }
                    
                });  
     }
     
  },
  
    player_color (player){
        var p;
        if (player==='1') p='green';
        else p='red';
    return p;
  }
    
}

package.json

JSON
Packages required by the skill
{
  "name": "agt-mindstorms",
  "version": "1.1.0",
  "description": "A sample skill demonstrating how to use AGT with Lego Mindstorms",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Amazon Alexa",
  "license": "ISC",
  "dependencies": {
    "ask-sdk-core": "^2.6.0",
    "ask-sdk-model": "^1.18.0",
    "aws-sdk": "^2.326.0",
    "request": "^2.81.0",
    "lodash": "^4.17.11"
  }
}

launch.json

JSON
APL document template
{
    "type": "APL",
    "version": "1.1",
    "theme": "dark",
    "import": [
        {
            "name": "alexa-viewport-profiles",
            "version": "1.0.0"
        },
        {
            "name": "alexa-layouts",
            "version": "1.0.0"
        },
        {
            "name": "alexa-styles",
            "version": "1.0.0"
        }
    ],
    "layouts": {
        "LaunchScreen": {
            "description": "A basic launch screen with a text and logo",
            "parameters": [
                {
                    "name": "mainText",
                    "type": "string"
                },
                {
                    "name": "logo",
                    "type": "string"
                }
            ],
            "items": [
                {
                    "type": "Container",
                    "width": "100%",
                    "height": "100%",
                    "justifyContent": "center",
                    "alignItems": "center",
                    "item": [
                        {
                            "type": "Image",
                            "source": "${logo}",
                            "width": "20vw",
                            "height": "20vw",
                            "scale": "best-fill"
                        },
                        {
                            "type": "Text",
                            "text": "${mainText}",
                            "style": "textStyleDisplay5",
                            "textAlign": "center",
                            "paddingTop": "30dp",
                            "color": "white"
                        }
                    ]
                }
            ]
        }
    },
    "mainTemplate": {
        "parameters": [
            "payload"
        ],
        "items": [
            {
                "type": "Container",
                "direction": "column",
                "items": [
                    {
                        "type": "Image",
                        "source": "${payload.launchData.properties.backgroundImage}",
                        "scale": "best-fill",
                        "width": "100vw",
                        "height": "100vh",
                        "opacity": "${payload.launchData.properties.backgroundOpacity}"
                    },
                    {
                        "type": "Container",
                        "position": "absolute",
                        "width": "100vw",
                        "height": "100vh",
                        "direction": "column",
                        "items": [
                            {
                                "headerTitle": "${payload.launchData.properties.headerTitle}",
                                "type": "AlexaHeader"
                            },
                            {
                                "when": "${@viewportProfile == @hubRoundSmall}",
                                "type": "Container",
                                "width": "100vw",
                                "height": "60vh",
                                "position": "relative",
                                "alignItems": "center",
                                "justifyContent": "center",
                                "direction": "column",
                                "items": [
                                    {
                                        "type": "LaunchScreen",
                                        "mainText": "${payload.launchData.properties.mainText}",
                                        "logo": "${payload.launchData.properties.logoImage}"
                                    }
                                ]
                            },
                            {
                                "when": "${@viewportProfile == @hubLandscapeSmall || @viewportProfile == @hubLandscapeMedium || @viewportProfile == @hubLandscapeLarge || @viewportProfile == @tvLandscapeXLarge}",
                                "type": "Container",
                                "width": "100vw",
                                "height": "70vh",
                                "direction": "column",
                                "alignItems": "center",
                                "justifyContent": "center",
                                "items": [
                                    {
                                        "type": "LaunchScreen",
                                        "mainText": "${payload.launchData.properties.mainText}",
                                        "logo": "${payload.launchData.properties.logoImage}"
                                    }
                                ]
                            },
                            {
                                "footerHint": "${payload.launchData.properties.hintString}",
                                "type": "AlexaFooter",
                                "when": "${@viewportProfile == @hubLandscapeSmall || @viewportProfile == @hubLandscapeMedium || @viewportProfile == @hubLandscapeLarge || @viewportProfile == @tvLandscapeXLarge}"
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

boxer.py

Python
Main code for the EV3 robot
#!/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, OUTPUT_D, MoveTank, SpeedPercent, LargeMotor, MediumMotor
from ev3dev2.sensor.lego import TouchSensor

#from ev3dev2.sensor.lego import 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']
    LEFT = ['turn left', 'go left']
    RIGHT = ['turn right', 'go right']
    #STOP = ['stop', 'brake']
    STOP = ['brake']
    #JAB= ['jab']
    #HOOK= ['hook']
    JAB= ['left']
    HOOK= ['right']
    POWER_PUNCH= ['power punch','power']

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']

class EventName(Enum):
    """
    The list of custom event name sent from this gadget
    """
    SENTRY = "Sentry"
    PROXIMITY = "Proximity"
    SPEECH = "Speech"
    COLOR = "Color"
    TOUCH = 'Touch'
    #MOVING= 'Moving'

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.boxing_mode= False
        self.player= "1"

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

        self.armL = LargeMotor(OUTPUT_A)
        self.armR = LargeMotor(OUTPUT_D)

        self.tsL = TouchSensor('in1')
        self.tsR = TouchSensor('in4')
        self.pounches = 0
      
    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))
        self.color_mode = False

    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"], float(payload["duration"]), int(payload["speed"]))

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

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

            if control_type == "start":
                # Expected params: [command]
                self._start()


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

    def _start(self):
        self.color_mode = True
        logger.info('Player ' + self.player + ' ready!')
        self._init_arm(self.armL, 200)
        self._init_arm(self.armR, 200)
        self.boxing_mode= True
        self.pounches= 0


    def _init_player(self, player):
        self.player= player
        if self.player=="1":
            self.leds.set_color("LEFT", "GREEN")
            self.leds.set_color("RIGHT", "GREEN")
        else:
            self.leds.set_color("LEFT", "RED")
            self.leds.set_color("RIGHT", "RED")
            logger.info("I'm Player 2")

    def _init_arm(self,arm, speed):
        arm.run_timed(time_sp=500, speed_sp=-speed)
        arm.wait_while('running')

        arm.stop()


    def _move_arm(self, arm, ts, speed):
        arm.run_timed(time_sp=500, speed_sp=speed)
  
        while any(arm.state):
            if ts.value():
                self.sound.beep()
                self._send_event(EventName.TOUCH, {'player': self.player})
                logger.info("Send TOUCH event to skill ")
                self.pounches=self.pounches+1
                print('Pounches: '+ str(self.pounches))
            time.sleep(0.05)


        arm.stop(stop_action="brake")

        arm.run_timed(time_sp=500, speed_sp=-speed)
        arm.wait_while('running')

        arm.stop()

    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
        """
  
        self.moving_mode=True

        if direction in Direction.FORWARD.value:
            self.drive.on_for_seconds(SpeedPercent(speed), SpeedPercent(speed), duration, block=is_blocking, brake=False)

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

        if direction in (Direction.RIGHT.value + Direction.LEFT.value):
            self._turn(direction, speed)

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

        if direction in Direction.JAB.value:
            self._move_arm(self.armL,self.tsL,400)

        if direction in Direction.HOOK.value:
            self._move_arm(self.armR,self.tsR,400)

        if direction in Direction.POWER_PUNCH.value:
            self._move_arm(self.armR,self.tsR,400)
            self._move_arm(self.armL,self.tsL,400)
            self._move_arm(self.armR,self.tsR,400)

        self.moving_mode= False

    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
        """
        speed= 10

        if direction in Direction.LEFT.value:
            self.drive.on_for_seconds(SpeedPercent(-speed), SpeedPercent(speed), 1, brake=False)

        if direction in Direction.RIGHT.value:
            self.drive.on_for_seconds(SpeedPercent(speed), SpeedPercent(-speed), 1, brake=False)

    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')))
    #Player 1
    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

clzfranco

clzfranco

2 projects • 2 followers

Comments