Most people enjoys live music is always better, with advancement of Robotics it's possible that someday we will have a live robotic band playing our music. In this article we will focus on making Lego Gorilla playing drum while listening to music on Amazon Echo. This guide heavily follows mission 2 under alexagadget's guide, so we will go over it in the upcoming steps.
Hardware RequirementWe will be using Lego Mindstorm EV3 Education Kit and a Drum that you can purchase from amazon for like $10, the list is placed on things needed.
- Mindstorm EV3 Education Set 45544
- 8 or 10 inch drum
Gorilla comes with EV3 Education Core Set, which is listed on Lego Education's website instructions. The full pdf can be found under https://le-www-live-s.legocdn.com/sc/media/files/support/mindstorms-ev3/building-instructions/design%20engineering%20projects/gorilla-0dfe2153bbfb0f8d71fce1d017c5e584.pdf The Gorilla is fairly easy to build, and can be completed in about 2-3 hours.
After it's build we should have something that looks like this
Since we are customizing our project, we'd need to anchor the drum so that the Gorilla can hit it. We will first swap out the piece of Lego Technic Pin (2780) with Lego Long Pin with Friction (6558). This allows us to attach 2 more Lego Beam 15 onto Gorilla's arm as shown below. We will use those Lego beam 15 as drum sticks.
To Anchor the Drum, we will build a device like below, where the tip can be used to lock the drum in. We will use robot's weight itself to hold the anchor, the piece below should be perfect to handle both.
Once the pointy end are inside the drum holder, we can add 2 more pieces to lock the drum's position so it doesn't float around.
The front view of the drum should look something like this.
After the anchor is built we can lock this into the gorilla bot itself. The back of the bot looks like the photo below and this should stop the drum from tilting down.
When everything is built, it should look like the Gorilla with a drum like the photo below.
To do the Mindstorms challenge, AlexaGadget have written an extensive guide on how to setup Alexa enviroment as well as setting up the libraries needed for getting ev3 to interact with Amazon Echo. The current project is modified from mission 2 from https://www.hackster.io/alexagadgets. To complete this step, it is easiest to go through setup, mission 1 and mission 2 of the the guide.
The Setup helps us to setup the environment, Mission 1 would help us to get AmazonId and alexaGadgetSecret, Mission 2 would help us to program the tempo and motors. In order to continue we would need
Step 4: ev3dev python codedrumbot.py and drumbot.ini closely follows mission-02, as we will be using exact same amount of classes. We first need to make sure that we are using Alexa capabilities for Alexa.Gadget.MusicData, since we are playing drums towards that music data
[GadgetSettings]
amazonId = [amazonId =]
alexaGadgetSecret = [alexaGadgetSecret]
[GadgetCapabilities]
Alexa.Gadget.MusicData = 1.0 - tempo
Now we can go on and start to modify python codes. The left and right motor in this case would be A and D as shown below, our import will reflect that
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_D, LargeMotor
During init, this will also be reflected and changed as shown below
def __init__(self):
"""
Performs Alexa Gadget initialization routines and ev3dev resource allocation.
"""
super().__init__()
# Ev3dev initialization
self.leds = Leds()
self.sound = Sound()
self.left_motor = LargeMotor(OUTPUT_A)
self.right_motor = LargeMotor(OUTPUT_D)
# Gadget states
self.bpm = 0
self.trigger_bpm = "off"
When musict_tempo data is being called via on_alexa_gadget_musicdata_tempo, if there is tempo data we will call the _drum_loop to move the gorilla's arm as well as turning the LED green, when the tempo data is 0 we stop playing drums. Unlike mission-02, our modification here is that we do not need dance_pose. Please make sure to take that out otherwise the bot acts weird.
def on_alexa_gadget_musicdata_tempo(self, directive):
"""
Provides the music tempo of the song currently playing on the Echo device.
:param directive: the music data directive containing the beat per minute value
"""
tempo_data = directive.payload.tempoData
for tempo in tempo_data:
print("tempo value: {}".format(tempo.value), file=sys.stderr)
if tempo.value > 0:
self.leds.set_color("LEFT", "GREEN")
self.leds.set_color("RIGHT", "GREEN")
time.sleep(3)
# starts the drum loop
self.trigger_bpm = "on"
threading.Thread(target=self._drum_loop, args=(tempo.value,)).start()
elif tempo.value == 0:
# stops the drum loop
self.trigger_bpm = "off"
self.leds.set_color("LEFT", "BLACK")
self.leds.set_color("RIGHT", "BLACK")
Inside drum loop, we are performing motor movement in sync with the beat per minute value from tempo data, this is using exact same method as mission-02. The LED will change colors based on same beat.
def _drum_loop(self, bpm):
"""
Perform motor movement in sync with the beat per minute value from tempo data.
:param bpm: beat per minute from AGT
"""
color_list = ["GREEN", "RED", "AMBER", "YELLOW"]
led_color = random.choice(color_list)
motor_speed = 400
milli_per_beat = min(1000, (round(60000 / bpm)) * 0.65)
print("Adjusted milli_per_beat: {}".format(milli_per_beat), file=sys.stderr)
while self.trigger_bpm == "on":
# Alternate led color and motor direction
led_color = "BLACK" if led_color != "BLACK" else random.choice(color_list)
motor_speed = -motor_speed
self.leds.set_color("LEFT", led_color)
self.leds.set_color("RIGHT", led_color)
self.right_motor.run_timed(speed_sp=motor_speed, time_sp=150)
self.left_motor.run_timed(speed_sp=-motor_speed, time_sp=150)
time.sleep(milli_per_beat / 1000)
print("Exiting BPM process.", file=sys.stderr)
When all said and done, we should
Demo:Now after all the hard work, we now have a Gorilla that can play drums when musics are playing!
Comments