Áron Horváth
Published

Mimicry

In this project I made a robot with moving mouth, eyebrows and arms so it looks like a living creature, using Mindstorms and Amazon Alexa.

IntermediateShowcase (no instructions)10 hours67
Mimicry

Things used in this project

Hardware components

Echo Dot
Amazon Alexa Echo Dot
×1
Mindstorms EV3 Programming Brick / Kit
LEGO Mindstorms EV3 Programming Brick / Kit
×1

Software apps and online services

VS Code
Microsoft VS Code

Story

Read more

Code

Mimicry

Python
This the code main code which is running on my brick.
I use Amazon's skill codes.
import time
import logging
import threading
import random

from agt import AlexaGadget

from ev3dev2.led import Leds
from ev3dev2.sound import Sound
from ev3dev2.motor import OUTPUT_A, OUTPUT_B, OUTPUT_C, MediumMotor

# Set the logging level to INFO to see messages from AlexaGadget
logging.basicConfig(level=logging.INFO)

 
class MindstormsGadget(AlexaGadget):
    """
    A Mindstorms gadget that communicates with you using SpeechData
    """

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

        # Ev3dev initialization
        self.leds = Leds()
        self.sound = Sound()
        self.mouth_motor = MediumMotor(OUTPUT_A)
        self.eye_motor = MediumMotor(OUTPUT_B)
        self.hand_motor = MediumMotor(OUTPUT_C)
        self.next_char_is_on = 1


    def on_connected(self, device_addr):
        """
        Gadget connected to the paired Echo device.
        :param friendly_name: the friendly name of the gadget that has connected to the echo device
        """
        self.leds.set_color("LEFT", "GREEN")
        self.leds.set_color("RIGHT", "GREEN")
        print("{} connected to Echo device".format(self.friendly_name))

    def on_disconnected(self, device_addr):
        """
        Gadget disconnected from the paired Echo device.
        :param friendly_name: the friendly name of the gadget that has disconnected from the echo device
        """
        self.leds.set_color("LEFT", "BLACK")
        self.leds.set_color("RIGHT", "BLACK")
        print("{} disconnected from Echo device".format(self.friendly_name))

    def on_alexa_gadget_speechdata_speechmarks(self, directive):
        """
        The Gadget received Speechmarks from Alexa
        """
        speech_data = directive.payload.speechmarksData
        for speechmark in speech_data:
            print("speechmark value: {}".format(speechmark.value))
            if speechmark.value in ['E', 'e', 'a', 'u', 'O', 'o', 'i', '@']:
                    threading.Thread(target=self.say).start()
                    if self.next_char_is_on:
                        threading.Thread(target=self.handmove).start()
                        self.next_char_is_on=0
                    else:
                        self.next_char_is_on=1

    def on_alexa_gadget_statelistener_stateupdate(self, directive):
        """
        Wakeword detected
        Moving the eyebrows
        """
        for state in directive.payload.states:
            if state.name == 'wakeword':

                if state.value == 'active':
                    self.eye_motor.run_timed(speed_sp=1000, time_sp=100)

                elif state.value == 'cleared':
                    self.eye_motor.run_timed(speed_sp=-1000, time_sp=100)

    def say(self):
        """
        Moving the mouth
        """
        self.mouth_motor.run_timed(speed_sp=300, time_sp=100)
        time.sleep(0.1)
        self.mouth_motor.run_timed(speed_sp=-300, time_sp=100)

    def handmove(self):
        """
        Moving the eyebrows
        Moving the arms
        """
        self.hand_motor.run_timed(speed_sp=300, time_sp=100)
        self.eye_motor.run_timed(speed_sp=300, time_sp=100)
        time.sleep(0.1)
        self.hand_motor.run_timed(speed_sp=-300, time_sp=150)
        self.eye_motor.run_timed(speed_sp=-300, time_sp=100)
        time.sleep(0.15)


if __name__ == '__main__':
    # Startup sequence
    gadget = MindstormsGadget()
    gadget.sound.play_song((('C2', 'e'), ('D2', 'e'), ('E3', 'q')))
    gadget.leds.set_color("LEFT", "GREEN")
    gadget.leds.set_color("RIGHT", "GREEN")

    # Gadget main entry point
    gadget.main()

    # Shutdown sequence
    gadget.sound.play_song((('E3', 'e'), ('C2', 'e')))
    gadget.leds.set_color("LEFT", "BLACK")
    gadget.leds.set_color("RIGHT", "BLACK")

Credits

Áron Horváth

Áron Horváth

1 project • 2 followers

Comments