Hardware components | ||||||
| × | 1 | ||||
| × | 1 |
This was used as an introduction to the mindstorm programming, and how to interface with the alexa dot device. The team comprised of 4 (1st-5th Grade) elementary school aged kids. They chose to have the mindstorm bot play "Jingle Bells", and move in a circuit that is predefined triggered on alexa voice activation.
#!/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
from ev3dev2.sound import Sound
from ev3dev2.led import Leds
from agt import AlexaGadget
from ev3dev2.motor import Motor, OUTPUT_A, OUTPUT_B, SpeedPercent, SpeedRPM, SpeedRPS, MoveTank
#Import the data stream location where the sensor is physically connected
from ev3dev2.sensor import INPUT_1
#Import the sensor type
from ev3dev2.sensor.lego import TouchSensor
from ev3dev2.sound import Sound
#from ev3dev2.led import Leds
# TODO: Add code here
tank_drive = MoveTank(OUTPUT_A, OUTPUT_B)
# drive in a different turn for 3 seconds
# Speed OUTPUT_A , Speed OUTPUT_B, # of Seconds
tank_drive.on_for_seconds(SpeedRPS(2), SpeedRPS(1.5), 3)
tank_drive.on_for_seconds(SpeedRPS(2), SpeedRPS(2), 2)
tank_drive.on_for_seconds(SpeedRPS(1), SpeedRPS(2), 2)
tank_drive.on_for_seconds(SpeedRPS(2), SpeedRPS(1), 4)
tank_drive.on_for_seconds(SpeedRPS(2), SpeedRPS(1.75), 3)
tank_drive.on_for_seconds(SpeedRPS(2), SpeedRPS(2), 2)
tank_drive.on_for_seconds(SpeedRPS(2), SpeedRPS(1.25), 3)
# set logger to display on both EV3 Brick and console
logging.basicConfig(level=logging.INFO, stream=sys.stdout, format='%(message)s')
logging.getLogger().addHandler(logging.StreamHandler(sys.stderr))
logger = logging.getLogger(__name__)
class MindstormsGadget(AlexaGadget):
"""
An Mindstorms gadget that will react to the Alexa wake word.
"""
def __init__(self):
"""
Performs Alexa Gadget initialization routines and ev3dev resource allocation.
"""
super().__init__()
self.leds = Leds()
self.sound = Sound()
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_alexa_gadget_statelistener_stateupdate(self, directive):
"""
Listens for the wakeword state change and react by turning on the LED.
:param directive: contains a payload with the updated state information from Alexa
"""
color_list = ['BLACK', 'AMBER', 'YELLOW', 'GREEN']
for state in directive.payload.states:
if state.name == 'wakeword': #change this to 'alarms' or 'timers'
if state.value == 'active': #this works for both 'wakeword', 'timers', 'reminders', 'timeinfo', 'reminders'
print("Wake word active", file=sys.stderr)
self.sound.play_song((
('E4', 'e3'),
('E4', 'e3'), # intro anacrouse
('E4', 'h'),
('E4', 'e3'),
('E4', 'e3'), # intro anacrouse
('E4', 'h'),
('E4', 'e3'),
('G4', 'e3'), # intro anacrouse
('C4', 'e3'),
('D4', 'e3'),
('E4', 'h'),
('F4', 'e3'),
('F4', 'e3'),
('F4', 'e3'),
('F4', 'e3'),
('F4', 'e3'),
('E4', 'e3'),
('E4', 'e3'),
('E4', 'e3'),
('E4', 'e3'),
('D4', 'e3'),
('D4', 'e3'),
('E4', 'e3'),
('D4', 'h'),
('G4', 'h'),
('E4', 'e3'),
('E4', 'e3'), # intro anacrouse
('E4', 'h.')))
tank_drive.on_for_seconds(SpeedRPS(2), SpeedRPS(1.5), 1.5)
tank_drive.on_for_seconds(SpeedRPS(2), SpeedRPS(2), 1)
tank_drive.on_for_seconds(SpeedRPS(1), SpeedRPS(2), 1)
tank_drive.on_for_seconds(SpeedRPS(2), SpeedRPS(1), 2)
tank_drive.on_for_seconds(SpeedRPS(2), SpeedRPS(1.75), 1.5)
tank_drive.on_for_seconds(SpeedRPS(2), SpeedRPS(2), 2)
tank_drive.on_for_seconds(SpeedRPS(2), SpeedRPS(1.25), 1.5)
tank_drive.on_for_seconds(SpeedRPS(2), SpeedRPS(2), 2.25)
tank_drive.on_for_seconds(SpeedRPS(2), SpeedRPS(.3),.7)
tank_drive.on_for_seconds(SpeedRPS(2), SpeedRPS(2), .8)
for i in range(0, 4, 1):
self.leds.set_color("LEFT", color_list[i], (i * 0.25))
self.leds.set_color("RIGHT", color_list[i], (i * 0.25))
time.sleep(0.25)
if state.value == 'cleared': #this works for both 'wakeword' and 'timers'
print("Wake word cleared", file=sys.stderr)
self.sound.play_song((('C5', 'e'), ('A3', 'e')))
for i in range(3, -1, -1):
self.leds.set_color("LEFT", color_list[i], (i * 0.25))
self.leds.set_color("RIGHT", color_list[i], (i * 0.25))
time.sleep(0.25)
#def on_notifications_setindicator(directive):
#def on_alerts_setalert(self, directive):
'''
for state in directive.payload.states:
print('payload.state "'+state+'"', file=sys.stderr)
if state.name == 'alarm': #change this to 'alarms' or 'timers'
print('the state value is: '+state.value, file=sys.stderr)
if state.value == 'active': #this works for both 'wakeword', 'timers', 'reminders', 'timeinfo', 'reminders'
print("Alarm active", file=sys.stderr)
self.sound.play_song((('E4', 'e3'),
('E4', 'e3'), # intro anacrouse
('E4', 'h'),
('E4', 'e3'),
('E4', 'e3'), # intro anacrouse
('E4', 'h'),
('E4', 'e3'),
('G4', 'e3'), # intro anacrouse
('C4', 'e3'),
('D4', 'e3'),
('E4', 'h'),
('F4', 'e3'),
('F4', 'e3'),
('F4', 'e3'),
('F4', 'e3'),
('F4', 'e3'),
('E4', 'e3'),
('E4', 'e3'),
('E4', 'e3'),
('E4', 'e3'),
('D4', 'e3'),
('D4', 'e3'),
('E4', 'e3'),
('D4', 'h'),
('G4', 'h'),
('E4', 'e3'),
('E4', 'e3'), # intro anacrouse
('E4', 'h'),
('E4', 'e3'),
('E4', 'e3'), # intro anacrouse
('E4', 'h'),
('E4', 'e3'),
('G4', 'e3'), # intro anacrouse
('C4', 'e3')))
for i in range(0, 4, 1):
self.leds.set_color("LEFT", color_list[i], (i * 0.25))
self.leds.set_color("RIGHT", color_list[i], (i * 0.25))
time.sleep(0.25)
'''
# if state.value == 'cleared': #this works for both 'wakeword' and 'timers'
# print("Timer cleared", file=sys.stderr)
# self.sound.play_song((('C5', 'e'), ('A3', 'e')))
# for i in range(3, -1, -1):
# self.leds.set_color("LEFT", color_list[i], (i * 0.25))
# self.leds.set_color("RIGHT", color_list[i], (i * 0.25))
# time.sleep(0.25)
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')))
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")
Liam and Erik Kvasager, Jackie and Beau Willis
Posted by Tyrel Kvasager
Thanks to Liam Kvasager, Erik Kvasager, Jackie Willis, and Beau Willis.
Comments