Andreas BaumgarteimsaAndreas HurlingTorben E
Published

Coordula is showing you over 11 Million places in the world

Discover over 11 Million places of your world with this awesome wire Lego® Mindstorms® Robot and ALEXA.

IntermediateFull instructions provided3 hours593

Things used in this project

Story

Read more

Custom parts and enclosures

LDD File of Coordula

Building instructions for the Coordula robot

Schematics

Flowsheet

Shows the different operations and interactions necessary to make the robot show you the world!

Code

coordula.py

Python
#!/usr/bin/env python3
import requests
import os
import sys
import time
import logging
import json

from agt import AlexaGadget
from math import asinh,tan,sqrt,radians

from ev3dev2.sensor.lego import InfraredSensor
from ev3dev2.button import Button
from ev3dev2.led import Leds
from ev3dev2.sound import Sound
from ev3dev2.motor import OUTPUT_B, OUTPUT_C, SpeedDPS, LargeMotor

from threading import Thread

# 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 MapBot:
    def __init__(self):
        """
        """
        # most northern latitude (works with the shown world map)
        epgrenz = 75 
        self.ygrenz = asinh(tan(radians(epgrenz)))
        # Speed of the LEGO Motor
        self.speed = 180
        # Half lenght and height of the map
        self.xlen = 520
        self.ylen = 330
        # Distance from point of attachment to the middle of the map
        self.xm = 660
        self.ym = 1047

        # Length of the wire 
        self.ca = 1240
        self.cb = 1240

        # Distance between the two wires 
        self.c = 2 * self.xm
        
        # Offset from the robot centre (where the skateboard is mounted) to the pointer of the minifigure
        self.botOffsetY = 48
        self.botOffsetX = 15

        # Conversion from mm in degree (The value is stored in index.js file on amazon). See line 210
        self.mm2deg = 13.5

        self.leftMotor = LargeMotor(OUTPUT_B)
        self.rightMotor = LargeMotor(OUTPUT_C)

        self.button = Button()
        self.button.on_enter = self.resetPosition

        self.remote = InfraredSensor()

        self.remote.on_channel1_top_left = self.calibrateUp
        self.remote.on_channel1_bottom_left = self.calibrateDown

        self.remote.on_channel1_top_right = self.calibrateLeft
        self.remote.on_channel1_bottom_right = self.calibrateRight

        Thread(target=self.workloop, daemon=True).start()
    
    def workloop(self):
        logger.info("MapBot Worker Started")
        while True:
            self.button.process()
            self.remote.process()
            time.sleep(0.16)
    
    def resetPosition(self, state):
        if state == True:
            self.ca = 1240
            self.cb = 1240
            logger.info("Reset position to {},{}mm".format(self.ca, self.cb))

    def calibrateUp(self, state):
        if state == True:
            self.leftMotor.on(SpeedDPS(-self.speed))
            self.rightMotor.on(SpeedDPS(-self.speed))
        else:
            self.leftMotor.on(SpeedDPS(0))
            self.rightMotor.on(SpeedDPS(0))


    def calibrateDown(self, state):
        if state == True:
            self.leftMotor.on(SpeedDPS(self.speed))
            self.rightMotor.on(SpeedDPS(self.speed))
        else:
            self.leftMotor.on(SpeedDPS(0))
            self.rightMotor.on(SpeedDPS(0))

    def calibrateLeft(self, state):
        if state == True:
            self.leftMotor.on(SpeedDPS(-self.speed))
            self.rightMotor.on(SpeedDPS(self.speed))
        else:
            self.leftMotor.on(SpeedDPS(0))
            self.rightMotor.on(SpeedDPS(0))

    def calibrateRight(self, state):
        if state == True:
            self.leftMotor.on(SpeedDPS(self.speed))
            self.rightMotor.on(SpeedDPS(-self.speed))
        else:
            self.leftMotor.on(SpeedDPS(0))
            self.rightMotor.on(SpeedDPS(0))

    def moveTo(self, lat, lng):
        logger.info("Move to Lat: {}\nLng: {}".format(lat, lng))
        xbild, ybild = self.merkant2coord(lat, lng)
        logger.info("Move to xbild: {}\nybild: {}".format(xbild, ybild))
        a, b = self.coord2length(xbild, ybild )
        logger.info("Length a: {}\nb: {}".format(a, b))

        aDiff = a - self.ca
        bDiff = b - self.cb

        logger.info("Length aDiff: {}\nbDiff: {}".format(aDiff, bDiff))

        aSpeed = self.speed
        bSpeed = self.speed

        if abs(aDiff) > abs(bDiff):
            aSpeed = self.speed
            bSpeed = round(abs(bDiff / aDiff) * self.speed)
        else:
            aSpeed = round(abs(aDiff / bDiff) * self.speed)
            bSpeed = self.speed
	
        logger.info("Speed aSpeed: {}\nbSpeed: {}".format(aSpeed, bSpeed))

        self.leftMotor.on_for_degrees(speed=SpeedDPS(aSpeed), degrees=aDiff * self.mm2deg, block=False)
        self.rightMotor.on_for_degrees(speed=SpeedDPS(bSpeed), degrees=bDiff * self.mm2deg, block=False)
        self.leftMotor.wait_until_not_moving()
        self.rightMotor.wait_until_not_moving()
        # Update der aktuellen Bandlnge
        self.ca = a
        self.cb = b

    def coord2length(self, xbild, ybild):   
        p = xbild
        h = ybild
        q = self.c - p
        a = sqrt(p * p + h * h)
        b = sqrt(q * q + h * h)
        a = round(a)
        b = round(b)
        return a, b

    def merkant2coord(self, lat, lng):
        """
        Umrechnung Karte zu 2D x,y
        """
        y1 = asinh(tan(radians(lat)))
        xbild =  self.botOffsetX + self.xm + lng/180.0 * self.xlen
        ybild = self.botOffsetY + self.ym - y1/self.ygrenz * self.ylen
        return xbild, ybild

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, mapbot):
        """
        Performs Alexa Gadget initialization routines and ev3dev resource allocation.
        """
        super().__init__()
        self.mapbot = mapbot
        self.leds = Leds()

    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"))
            logger.info("Control payload: {}".format(payload))
            lat = float(payload["lat"])
            lng = float(payload["lng"])
            self.mapbot.mm2deg = float(payload["mm2deg"])
            logger.info("Lat: {}\nLng: {}\nmm2deg: {}".format(lat, lng, self.mapbot.mm2deg))
            self.mapbot.moveTo(lat, lng)
            
        except Exception as e:
            logger.info("Wrong Key error {}".format(e))
    

if __name__ == '__main__':
    mapbot = MapBot()
    gadget = MindstormsGadget(mapbot)
    # 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.leds.set_color("LEFT", "GREEN")
    gadget.leds.set_color("RIGHT", "GREEN")
    # Gadget main entry point
    gadget.main()
    # Shutdown sequence
    gadget.leds.set_color("LEFT", "BLACK")
    gadget.leds.set_color("RIGHT", "BLACK")

coordula.ini

Python
# Copyright 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
# 
# You may not use this file except in compliance with the terms and conditions 
# set forth in the accompanying LICENSE.TXT file.
#
# THESE MATERIALS ARE PROVIDED ON AN "AS IS" BASIS. AMAZON SPECIFICALLY DISCLAIMS, WITH 
# RESPECT TO THESE MATERIALS, ALL WARRANTIES, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING 
# THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.

[GadgetSettings]
amazonId = 
alexaGadgetSecret = 

[GadgetCapabilities]
Custom.Mindstorms.Gadget = 1.0

model.json

JSON
{
    "interactionModel": {
        "languageModel": {
            "invocationName": "my world",
            "intents": [
                {
                    "name": "AMAZON.CancelIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.HelpIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.StopIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.NavigateHomeIntent",
                    "samples": []
                },
                {
                    "name": "ShowCityIntent",
                    "slots": [
                        {
                            "name": "city",
                            "type": "AMAZON.DE_CITY"
                        }
                    ],
                    "samples": [
                        "show {city}",
                        "show me {city}",
                        "fahre zu {city}",
                        "wo liegt {city}",
                        "zeig mir {city}"
                    ]
                }
            ],
            "types": []
        }
    }
}

index.js

JavaScript
// This sample demonstrates handling intents 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 fetch = require('node-fetch');
const Util = require('./util');

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

        let request = handlerInput.requestEnvelope;
        let { apiEndpoint, apiAccessToken } = request.context.System;
        let 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
        let endpointId = apiResponse.endpoints[0].endpointId || [];
        Util.putSessionAttribute(handlerInput, 'endpointId', endpointId);

        return handlerInput.responseBuilder
            .speak("Hey! Cordula here.  Which city would you like to see?")
            .reprompt("Just say a cityname")
            .getResponse();
    }
};

const ShowCityIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'ShowCityIntent';
    },
    handle: async function (handlerInput) {
        const attributesManager = handlerInput.attributesManager;
        const endpointId = attributesManager.getSessionAttributes().endpointId || [];
        
        let city = Alexa.getSlotValue(handlerInput.requestEnvelope, 'city');
        console.log(city)
        try {
            const response = await fetch(`http://api.geonames.org/search?name=${encodeURIComponent(city)}&username=YOUR-USERNAME&maxRows=1&type=json`);
            const geo = await response.json();
            console.log(geo)
            if (geo.totalResultsCount > 0) {
                // Construct the directive with the payload containing the search parameters
                let directive = Util.build(endpointId, NAMESPACE, NAME_CONTROL,  {
                    city,
                    mm2deg: 13.5,
                     ...geo.geonames[0]
                });
                    
                const speakOutput = 'Cordula is showing you ';
                return handlerInput.responseBuilder
                    .speak(speakOutput + city)
                    .reprompt(speakOutput + city)
                    .addDirective(directive)
                    .getResponse(); 
                    
            } else {
                throw new Error("City not found");
            }
        } catch (e) {
            console.log(e)
            return handlerInput.responseBuilder
                .speak("Sorry I could not find a location for this city.")
                .reprompt("Would you like to see a different city?")
                .getResponse();   
        }
    }
};

const HelpIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
    },
    handle(handlerInput) {
        const speakOutput = 'You can say hello to me! 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 speakOutput = 'Goodbye!';
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .getResponse();
    }
};
const SessionEndedRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
    },
    handle(handlerInput) {
        // Any cleanup logic goes here.
        return handlerInput.responseBuilder.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('add a reprompt if you want to keep the session open for the user to respond')
            .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();
    }
};

// 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,
        ShowCityIntentHandler,
        HelpIntentHandler,
        CancelAndStopIntentHandler,
        SessionEndedRequestHandler,
        IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
    )
    .addErrorHandlers(
        ErrorHandler,
    )
    .lambda();

util.js

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

'use strict';

const Https = require('https');

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

package.json

JavaScript
{
  "name": "hello-world",
  "version": "1.1.0",
  "description": "alexa utility for quickly building skills",
  "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",
    "node-fetch": "2.6.0"
  }
}

common.js

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

'use strict'

const Alexa = require('ask-sdk-core');

const HelpIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
    },
    handle(handlerInput) {
        const speakOutput = 'You can say hello to me! 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 speakOutput = 'Goodbye!';
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .getResponse();
    }
};
const SessionEndedRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
    },
    handle(handlerInput) {
        // Any cleanup logic goes here.
        return handlerInput.responseBuilder.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();
    }
};

// The request interceptor is used for request handling testing and debugging.
// It will simply log the request in raw json format before any processing is performed.
const RequestInterceptor = {
    process(handlerInput) {
        let { attributesManager, requestEnvelope } = handlerInput;
        let sessionAttributes = attributesManager.getSessionAttributes();

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

module.exports = {
    HelpIntentHandler,
    CancelAndStopIntentHandler,
    SessionEndedRequestHandler,
    IntentReflectorHandler,
    ErrorHandler,
    RequestInterceptor
    };

Source code of the Coordula project.

Credits

Andreas Baumgart

Andreas Baumgart

1 project • 4 followers
eimsa

eimsa

1 project • 1 follower
Andreas Hurling

Andreas Hurling

1 project • 0 followers
Torben E

Torben E

1 project • 1 follower
Thanks to Daniel R. Strebe.

Comments