Naman Garg
Created January 1, 2020

Polýplevros

The true ambassador of versatility and efficiency. An improvised TANK3R This is what truly defines my project.

AdvancedFull instructions provided20 days97
Polýplevros

Things used in this project

Story

Read more

Schematics

CIRCUIT DIAGRAM

Code

INDEX

JavaScript
index.js
/*

*/

// This skill sample demonstrates how to send directives and receive events from 

const Alexa = require('ask-sdk-core');
const Util = require('./util');
const Common = require('./common');

// The audio tag to include background music
const BG_MUSIC = '<audio src="soundbank://soundlibrary/ui/gameshow/amzn_ui_sfx_gameshow_waiting_loop_30s_01"></audio>';

// 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 LaunchRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
    },
    handle: async function(handlerInput) {

        const request = handlerInput.requestEnvelope;
        const { apiEndpoint, apiAccessToken } = request.context.System;
        const apiResponse = await Util.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();
        }

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

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

        let speechOutput = "Welcome, voice interface activated";
        return handlerInput.responseBuilder
            .speak(speechOutput + BG_MUSIC)
            .reprompt("Awaiting your commands.")
            .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);

        let speechOutput = `speed set to ${speed} percent.`;
        return handlerInput.responseBuilder
            .speak(speechOutput + BG_MUSIC)
            .getResponse();
    }
};

// Construct and send a custom directive to the connected gadget with
// data from the MoveIntent.
const MoveIntentHandler = {
    canHandle(handlerInput) {
        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 rotations = Alexa.getSlotValue(request, 'rotations') || "2";

        // Get data from session attribute
        const attributesManager = handlerInput.attributesManager;
        const speed = attributesManager.getSessionAttributes().speed || "50";
        const endpointId = attributesManager.getSessionAttributes().endpointId || [];

        // Construct the directive with the payload containing the move parameters
        let directive = Util.build(endpointId, NAMESPACE, NAME_CONTROL,
            {
                type: 'move',
                direction: direction,
                rotations: rotations,
                speed: speed
            });

        const speechOutput = (direction === "brake")
            ?  "Applying brake"
            : `${direction} ${rotations} rotations at ${speed} percent speed`;

        return handlerInput.responseBuilder
            .speak(speechOutput + BG_MUSIC)
            .reprompt("Awaiting your commands.")
            .addDirective(directive)
            .getResponse();
    }
};

// Construct and send a custom directive to the connected gadget with data from
// the SetCommandIntent.
const SetCommandIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'SetCommandIntent';
    },
    handle: function (handlerInput) {

        let command = Alexa.getSlotValue(handlerInput.requestEnvelope, 'Command');
        if (!command) {
            return handlerInput.responseBuilder
                .speak("Can you repeat that?")
                .reprompt("What was that again?")
                .getResponse();
        }

        const attributesManager = handlerInput.attributesManager;
        let endpointId = attributesManager.getSessionAttributes().endpointId || [];
        let speed = attributesManager.getSessionAttributes().speed || "50";

        // Construct the directive with the payload containing the move parameters
        let directive = Util.build(endpointId, NAMESPACE, NAME_CONTROL,
            {
                type: 'command',
                command: command,
                speed: speed
            });

        let speechOutput = `command ${command} activated`;
        if (command === 'close gripper' || command === 'close arm') {
            speechOutput = '';
        }
        else if (command === 'open gripper' || command === 'open arm') {
            speechOutput = '';
        }
        else if (command === 'threat removal') {
            speechOutput = '';
        }
        else if (command === 'open gate') {
            speechOutput = '';
        }
        else if (command === 'close gate') {
            speechOutput = '';
        }
        else if (command === 'identify colour') {
            speechOutput = '';
        }
        else if (command === 'ultrasonic sensor') {
            speechOutput = '';
        }
        else if (command === 'smart object detection') {
            speechOutput = '';
        }
        else if (command === 'line following') {
            speechOutput = '';
        }
        else {
            speechOutput = 'Error, bad command!';
        }

        return handlerInput.responseBuilder
            .speak(speechOutput + BG_MUSIC)
            .reprompt("awaiting new command!")
            .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;
        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;

        let speechOutput;
        if (name === 'Proximity') {
            let distance = parseInt(payload.distance);
            if (distance < 10) {
                let speechOutput = "Intruder detected! What would you like to do?";
                return handlerInput.responseBuilder
                    .speak(speechOutput, "REPLACE_ALL")
                    .withShouldEndSession(false)
                    .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 + BG_MUSIC, "REPLACE_ALL")
            .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,
        SetSpeedIntentHandler,
        SetCommandIntentHandler,
        MoveIntentHandler,
        EventsReceivedRequestHandler,
        ExpiredRequestHandler,
        Common.HelpIntentHandler,
        Common.CancelAndStopIntentHandler,
        Common.SessionEndedRequestHandler,
        Common.IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
    )
    .addRequestInterceptors(Common.RequestInterceptor)
    .addErrorHandlers(
        Common.ErrorHandler,
    )
    .lambda();

python code

Python
MAIN CODE (.PY)
#!/usr/bin/env python3


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, MediumMotor, LargeMotor, follow_for_ms
from ev3dev2.sensor.lego import ColorSensor, UltrasonicSensor, TouchSensor
from ev3dev2.sensor import INPUT_1, INPUT_2, INPUT_3, INPUT_4

# Set the logging level to INFO to see messages from AlexaGadget
logging.basicConfig(level=logging.INFO, stream=sys.stdout, format='%(message)s')
logging.getLogger().addHandler(logging.StreamHandler(sys.stderr))
logger = logging.getLogger(__name__)




class Direction(Enum):
    """
    The list of directional commands and their variations.
    These variations correspond to the skill slot values.
    """
    FORWARD = ['forward', 'forwards', 'go forward']
    BACKWARD = ['back', 'backward', 'backwards', 'go backward']
    LEFT = ['left', 'go left']
    RIGHT = ['right', 'go right']
    STOP = ['stop', 'brake']



class Command(Enum):
    """
    The list of preset commands and their invocation variation.
    These variations correspond to the skill slot values.
    """
    
    PICK_UP = ['close gripper']
    DROP = ['open gripper']
    OPEN_GATE = ['open gate']
    CLOSE_GATE = ['close gate']
    CHECK_COLOUR = ['identify color']
    CHECK_DISTANCE = ['check distance']
    GRAB_OR_REMOVE = ['intelligent sense']
    LINE_FOLLOW = ['line following']
    TOUCH_SENSOR = ['touch sensor']

class EventName(Enum):
    SPEECH = "Speech"
    DISTANCE = "Distance"
    COLOR = "Color"
    SMART = "Smart"
    ANTI_CRASHING = "Anti Crashing"
    TOUCH = "Touch"
    LINE_FOLLOWING = "Line Follow"


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.distance_mode = False
        self.color_mode = False
        self.smart_mode = False
        self.anti_crashing_mode = False
        self.touch_mode = False
        self.line_follow_mode = False

        # Ev3dev initialization
        self.leds = Leds()
        self.sound = Sound()
        self.drive = MoveTank(OUTPUT_B, OUTPUT_C)
        self.gripper = MediumMotor(OUTPUT_A)
        self.gate = LargeMotor(OUTPUT_D)
        self.us = UltrasonicSensor(INPUT_3)
        self.cs1 = ColorSensor(INPUT_1)
        self.ts = TouchSensor(INPUT_4)
        self.drive.cs2 = ColorSensor(INPUT_2)

        threading.Thread(target=self._distance_thread, daemon=True).start()
        threading.Thread(target=self._color_thread, daemon=True).start()
        threading.Thread(target=self._smart_thread, daemon=True).start()
        threading.Thread(target=self._anti_crashing_thread, daemon=True).start()
        threading.Thread(target=self._touch_thread, daemon=True).start()
        threading.Thread(target=self._line_follow_thread, daemon=True).start()

        

    def on_connected(self, device_addr):
        """
        Gadget connected to the paired Echo device.
        :param device_addr: the address of the device we connected to
        """
        self.leds.set_color("LEFT", "GREEN")
        self.leds.set_color("RIGHT", "GREEN")
        logger.info("{} connected to Echo device".format(self.friendly_name))

    def on_disconnected(self, device_addr):
        """
        Gadget disconnected from the paired Echo device.
        :param device_addr: the address of the device we disconnected from
        """
        self.leds.set_color("LEFT", "BLACK")
        self.leds.set_color("RIGHT", "BLACK")
        logger.info("{} disconnected from Echo device".format(self.friendly_name))

    def on_custom_mindstorms_gadget_control(self, directive):
        """
        Handles the Custom.Mindstorms.Gadget control directive.
        :param directive: the custom directive with the matching namespace and name
        """
        try:
            payload = json.loads(directive.payload.decode("utf-8"))
            print("Control payload: {}".format(payload), file=sys.stderr)
            control_type = payload["type"]
            if control_type == "move":

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

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

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

    def _move(self, direction, rotations: 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 rotations: the rotations in numbers
        :param speed: the speed percentage as an integer
        :param is_blocking: if set, motor run until duration expired before accepting another command
        """
        print("Move command: ({}, {}, {}, {})".format(direction, speed, rotations, is_blocking), file=sys.stderr)
        if direction in Direction.FORWARD.value:
            self.drive.on_for_rotations(SpeedPercent(speed), SpeedPercent(speed),rotations, block=is_blocking)

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

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

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

    def _activate( self, command, speed=50):
        """
        Handles preset commands.
        :param command: the preset command
        :param speed: the speed if applicable
        """
        print("Activate command: ({}, {})".format(command, speed), file=sys.stderr)
        

        if command in Command.PICK_UP.value:
            #picking up an object with the gripper
            self.gripper.on_for_degrees(SpeedPercent(20),355)

        if command in Command.DROP.value:
            #placing an object with the gripper
            self.gripper.on_for_degrees(SpeedPercent(20), -355)


        if command in Command.OPEN_GATE.value:
            #opening the gate
            self.gate.on_for_degrees(SpeedPercent(-50), 700)

        if command in Command.CLOSE_GATE.value:
            #closing the gate
            self.gate.on_for_degrees(SpeedPercent(50),700)

        if command in Command.CHECK_COLOUR.value:
            #colour identification with front facing coloursensor(cs1) 
            self.color_mode = True



        if command in Command.CHECK_DISTANCE.value:
            #distance in front of the bot and any obstacle
            self.distance_mode=True


        if command in Command.GRAB_OR_REMOVE.value:
            #smart colour detection, will leave it if its a wall and remove it if an object.
            self.smart_mode=True

        if command in Command.LINE_FOLLOW.value:
            #line following     
            self.line_follow_mode=True

        if command in Command.TOUCH_SENSOR.value:
            #TANK3R will dance until touch sesnor pressed
            self.touch_mode=True

                        



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

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


    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)


    def _distance_thread(self):
        while True:
            while self.distance_mode:
                distance=self.us.distance_inches_ping
                print("Distance: {}".format(distance), file=sys.stderr)
                if distance < 3:
                        self._send_event(EventName.DISTANCE, {'distance': distance})
                        self.gate.on_for_degrees(SpeedPercent(100), -700)
                        self.gate.on_for_degrees(SpeedPercent(100), 700)
                        self.gate.on_for_degrees(SpeedPercent(100), -700)
                        self.gate.on_for_degrees(SpeedPercent(100), 700)
                        self.distance_mode= False
                else:                           
                        self.distance_mode= False

    def _color_thread(self):
        while True:
            while self.color_mode:
                #different colours and speech responses by alexa
                colourdetected=self.cs1.color_name
                if colourdetected == 'NoColor':
                    print("Color: {}".format(colourdetected), file=sys.stderr)
                    self._send_event(EventName.COLOR, {'0': colourdetected})
                    self.color_mode=False
                elif colourdetected == 'Black':
                    print("Color: {}".format(colourdetected), file=sys.stderr)
                    self._send_event(EventName.COLOR, {'1': colourdetected})
                    self.color_mode=False
                elif colourdetected == 'Blue':
                    print("Color: {}".format(colourdetected), file=sys.stderr)
                    self._send_event(EventName.COLOR, {'2': colourdetected})
                    self.color_mode=False
                elif colourdetected == 'Green':
                    print("Color: {}".format(colourdetected), file=sys.stderr)
                    self._send_event(EventName.COLOR, {'3': colourdetected})
                    self.color_mode=False
                elif colourdetected == 'Yellow':
                    print("Color: {}".format(colourdetected), file=sys.stderr)
                    self._send_event(EventName.COLOR, {'4': colourdetected})
                    self.color_mode=False
                elif colourdetected == 'Red':
                    print("Color: {}".format(colourdetected), file=sys.stderr)
                    self._send_event(EventName.COLOR, {'5': colourdetected})
                    self.color_mode=False
                elif colourdetected == 'White':
                    print("Color: {}".format(colourdetected), file=sys.stderr)
                    self._send_event(EventName.COLOR, {'6': colourdetected})
                    self.color_mode=False
                elif colourdetected == 'Brown':
                    print("Color: {}".format(colourdetected), file=sys.stderr)
                    self._send_event(EventName.COLOR, {'7': colourdetected})
                    self.color_mode=False
                else:
                    print("Color: {}".format(distance), file=sys.stderr)
                    self._send_event(EventName.COLOR, {'error': colourdetected})
                    self.color_mode=False

    def _smart_thread(self):
        while True:
            while self.smart_mode:
                #intelligent sense, red is a breach so it will remove it. White is a wall so  it will be
                #avoided. Blue is a human so it will also be avoided.(in a different way)
                colourdetected=self.cs1.color_name
                if colourdetected == 'Red':
                    self._send_event(EventName.SMART, {'red': colourdetected})
                    self.gate.on_for_degrees(SpeedPercent(100), -700)
                    self.gate.on_for_degrees(SpeedPercent(100), 700)
                    self.color_mode = False

                elif colourdetected == 'White':
                    self._send_event(EventName.SMART, {'white': colourdetected})
                    self.drive.on_for_rotations(SpeedPercent(100), SpeedPercent(100), -3)
                    self.color_mode = False

                elif colourdetected == 'Blue':
                    self._send_event(EventName.SMART, {'blue': colourdetected})
                    self.drive.on_for_rotations(SpeedPercent(100), SpeedPercent(100), -3)
                    self.drive.on_for_rotations(SpeedPercent(100), SpeedPercent(-100), 2)
                    self.drive.on_for_rotations(SpeedPercent(100), SpeedPercent(100), 3)
                    self.drive.on_for_rotations(SpeedPercent(-100), SpeedPercent(100), 2)
                    self.drive.on_for_rotations(SpeedPercent(100), SpeedPercent(100), 5)             
                    self.color_mode = False

                else :
                    self._send_event(EventName.SMART, {'error': colourdetected})
                    self.color_mode = False


    def _anti_crashing_thread(self):
        while True:
            while self.anti_crashing_mode:
                #a normal anti crashing mode, when wall s detected, its gonna go back
                distance=self.us.distance_inches_ping
                print("Distance: {}".format(distance), file=sys.stderr)
                if distance < 5:
                    self._send_event(EventName.ANTI_CRASHING, {'less': distance})
                    self.drive.on_for_rotations(SpeedPercent(100), SpeedPercent(100), 3)
                    self.anti_crashing_mode=False
                else :
                    self._send_event(EventName.ANTI_CRASHING, {'more': distance})
                    self.anti_crashing_mode=False



    def _touch_thread(self):
        while True:
            while self.touch_mode:
                #TANK3R will dance until ev3 touch sensor is pressed.
                while not self.ts.value():
                    self.drive.on_for_rotations(SpeedPercent(100), SpeedPercent(-100), 1)
                    self.drive.on_for_rotations(SpeedPercent(-100), SpeedPercent(100), 1)
                    self.gate.on_for_degrees(SpeedPercent(100), -700)
                    self.gate.on_for_degrees(SpeedPercent(100), 700)
                    self.drive.on_for_rotations(SpeedPercent(-100), SpeedPercent(100), 10)
                    self.drive.on_for_rotations(SpeedPercent(100), SpeedPercent(-100), 10)
                self._send_event(EventName.TOUCH, {'touch': pressed})
                self.touch_mode=False


    def _line_follow_thread(self):
        while True:

        #line following
            while self.line_follow_mode:
                self.drive.cs2.reflected_light_intensity
                self.drive.follow_line( kp=11.3, ki= 0.05, kd=3.2, speed=SpeedPercent(20), follow_left_edge=True, follow_for=follow_for_ms, ms=5000)
                self.line_follow_mode=False
                self._send_event(EventName.LINE_FOLLOWING, {'linefollow': lineFollow})



                  



if __name__ == '__main__':

    gadget = MindstormsGadget()

    # Setting the LCD font and turning off the blinking LEDs
    os.system('setfont Lat7-Terminus12x6')
    gadget.leds.set_color("LEFT", "BLACK")
    gadget.leds.set_color("RIGHT", "BLACK")

    # Starting it up
    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()

    # Shutting it down
    gadget.sound.play_song((('E5', 'e'), ('C4', 'e')))
    gadget.leds.set_color("LEFT", "BLACK")
    gadget.leds.set_color("RIGHT", "BLACK")

INI CODE

INI
[GadgetSettings]
amazonId = 
alexaGadgetSecret = 

[GadgetCapabilities]
Custom.Mindstorms.Gadget = 1.0

COMMON

JavaScript
No preview (download only).

MODEL

JSON
{
  "interactionModel": {
    "languageModel": {
      "invocationName": "mindstorms",
      "intents": [
        {
          "name": "AMAZON.CancelIntent",
          "samples": []
        },
        {
          "name": "AMAZON.HelpIntent",
          "samples": []
        },
        {
          "name": "AMAZON.StopIntent",
          "samples": []
        },
        {
          "name": "AMAZON.NavigateHomeIntent",
          "samples": []
        },
        {
          "name": "MoveIntent",
          "slots": [
            {
              "name": "Direction",
              "type": "DirectionType"
            },
            {
              "name": "Duration",
              "type": "AMAZON.NUMBER"
            }
          ],
          "samples": [
            "{Direction} now",
            "{Direction} {Duration} seconds",
            "move {Direction} for {Duration} seconds"
          ]
        },
        {
          "name": "SetSpeedIntent",
          "slots": [
            {
              "name": "Speed",
              "type": "AMAZON.NUMBER"
            }
          ],
          "samples": [
            "set speed {Speed} percent",
            "set {Speed} percent speed",
            "set speed to {Speed} percent"
          ]
        },
        {
          "name": "SetCommandIntent",
          "slots": [
            {
              "name": "Command",
              "type": "CommandType"
            }
          ],
          "samples": [
            "activate {Command} mode",
            "move in a {Command}",
            "fire {Command}",
            "activate {Command}"
          ]
        }
      ],
      "types": [
        {
          "name": "DirectionType",
          "values": [
            {
              "name": {
                "value": "brake"
              }
            },
            {
              "name": {
                "value": "go backward"
              }
            },
            {
              "name": {
                "value": "go forward"
              }
            },
            {
              "name": {
                "value": "go right"
              }
            },
            {
              "name": {
                "value": "go left"
              }
            },
            {
              "name": {
                "value": "right"
              }
            },
            {
              "name": {
                "value": "left"
              }
            },
            {
              "name": {
                "value": "backwards"
              }
            },
            {
              "name": {
                "value": "backward"
              }
            },
            {
              "name": {
                "value": "forwards"
              }
            },
            {
              "name": {
                "value": "forward"
              }
            }
          ]
        },
        {
          "name": "CommandType",
          "values": [
            {
              "name": {
                "value": "sentry"
              }
            },
            {
              "name": {
                "value": "circle"
              }
            },
            {
              "name": {
                "value": "square"
              }
            },
            {
              "name": {
                "value": "patrol"
              }
            },
            {
              "name": {
                "value": "cannon"
              }
            },
            {
              "name": {
                "value": "all shot"
              }
            },
            {
              "name": {
                "value": "one shot"
              }
            }
          ]
        }
      ]
    }
  }
}

PACKAGE

JSON
{
  "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"
  }
}

UTIL

JavaScript
'use strict';

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

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

/**
 * Get the authenticated URL to access the S3 Object. This URL expires after 60 seconds.
 * @param s3ObjectKey - the S3 object key
 * @returns {string} the pre-signed S3 URL
 */
exports.getS3PreSignedUrl = function getS3PreSignedUrl(s3ObjectKey) {

    const bucketName = process.env.S3_PERSISTENCE_BUCKET;
    return Escape(s3SigV4Client.getSignedUrl('getObject', {
        Bucket: bucketName,
        Key: s3ObjectKey,
        Expires: 60 // the Expires is capped for 1 minute
    }));
};

/**
 * 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
    };
};

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

Credits

Naman Garg

Naman Garg

5 projects • 4 followers

Comments