Sometimes timer isn't enough to get your attention as your are so use to it. But with physical routing of znap, you might be able to pay more attention! In this guide, we are going to build out a simple Znap alarm. By end of article, your alexa alarm will be able to get your attention too.
Step 1: Build ZnapZnap comes with instruction set with Ev3 Core and Ev3 expansion, it's one of the iconic robot that's on the cover page of the ev3 core expansion. The build instructions can be seen here https://le-www-live-s.legocdn.com/sc/media/lessons/mindstorms-ev3/building-instructions/model-expansion-set/ev3-model-expansion-set-znap-71b712c154ffb81ace8bf73384f2197c.pdf
The entire znap takes about 2-3 hours to build, even for a beginner like Sarah :)
Step 2: Go through AlexaGadget's guideAlexagadget have already written environment setup and 4 missions, we only need to complete first 2 missions to get Znap running, since those instructions are extremely useful, we can use them to setup both local and alexa enviroment. Easiest way is to complete mission 2 and we will take it from there, the full guides can be seen at.
https://www.hackster.io/alexagadgets
The very specific thing is that we do need AmazonId and Secrets from mission-01, which will determine whether we will be able to connect device to the echo device.
Once that's done we can move to Step 3
Step 3: Write python code on ev3devIn this article we will be controlling the timer, so in our znap.ini file we will be focusing on.
[GadgetCapabilities]
Alexa.Gadget.StateListener = 1.0 - timers
This is to ensure we are using timer ability on StateListener instead of what mission 1 and 2 are using, which was wakeword and tempo.
import os
import sys
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_D, LargeMotor, MediumMotor, MoveTank, SpeedPercent
We will be importing motor supports, MoveTank and SpeedPercent, since we will be moving the robot.
class MindstormsGadget(AlexaGadget):
"""
A Mindstorms gadget that performs movement in sync with music tempo.
"""
def __init__(self):
"""
Performs Alexa Gadget initialization routines and ev3dev resource allocation.
"""
super().__init__()
self.leds = Leds()
self.sound = Sound()
self.snap = MediumMotor(OUTPUT_B)
self.drive = MoveTank(OUTPUT_A, OUTPUT_D)
Over here, we will be setting OUTPUT_B as Medium Motor, which was following the guide, then A and D and MoveTank so we can use the functions from MoveTank.
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:
#print(state.name, file=sys.stderr)
if state.name == 'timers':
if state.value == 'active':
print("alarm active", file=sys.stderr)
self.sound.play_song((('A3', 'e'), ('C5', 'e')))
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)
self.snap.on_for_rotations(SpeedPercent(100), 5)
self.snap.on_for_rotations(SpeedPercent(100), -5)
self.drive.on_for_seconds(SpeedPercent(-50), SpeedPercent(-5), 2)
elif state.value == 'cleared':
print("alarm 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)
So now, we will be listening to payload state as 'timer' then 'active', when that is on, we will be set the LED as well moving snapper's head. When the alarm hits the snapper will be snap it's head then try to move in a circle, as it's worried for us :). And when it's cleared it will stop everything to go back to normal.
We can now see the full demo. You can use utter words such as
"Alexa, set alarm for 1 hour" or "Alexa, set timer for 10 minutes" to test out the application.
Comments