-
Notifications
You must be signed in to change notification settings - Fork 29
Getting started
This tutorial assumes you have the Myo dongle pictured below.
The Myo should be placed at the thickest part of your forearm, with the bar status LED is facing down towards your hand. See Myo Placement for more details.
PyoMyo uses the dongle included to communicate with the Myo.
PyoMyo communicates to the dongle over serial which then sends BLE packets to the Myo.
Plug the blue dongle into your PC and slide the Myo up to your forearm, if the Myo is charged the motion will cause it to turn on and light the onboard LEDs.
The git repo includes the library and some examples to plot some EMG values from the Myo, open a terminal and run the commands below:
git clone https://github.com/PerlinWarp/pyomyo.git
# Install pyomyo and its only dependency pyserial.
pip install pyomyo
cd pyomyo/examples
# Make sure to run this command in Command prompt / bash NOT git bash
python plot_emgs.py
The Myo should vibrate and a plot of the live EMG values should be displayed on your screen.
Try:
- Tensing muscles in your hand and watch the values change.
- Putting the Myo in different places on your arm.
- Sliding a finger under one of the electrodes and watching the respective channel change.
Once you're ready to move on click inside the terminal and press CTRL + C
to exit.
If this did not work, see Common Problems.
Help is also available on the discord in the pyomyo channel, which can be joined using this link.
pyomyo includes a very simple classifier (Classifier.py
) that can be used in real time. dino_jump.py
is built on top of Classifier.py
and shows how we can use it to generate keypresses to play games.
When the classifier is running, clicking inside the pygame window and pressing a numerical key from 0 to 9 will label the data from the Myo as a class from 0 to 9 and save them in the data/
directory as numpy arrays.
When a class is detected, the dino_handler
function is run with the class number as a parameter, if class 1 is detected then the space key is pressed using pynput which can be installed with pip install pynput
.
To get started open dino_jump.py
in your favorite text editor and read the tutorial at the top.
Try the tutorial out and experiment, you could:
- Try different gestures out to trigger the jump.
- Try training data on one hand, then moving the Myo or switching hands to see if it still works.
- Try adding more keypresses to the
dino_handler
and expand the code to work on other games, like space invaders.
There are two big problems for a classifier like this:
-
Cross Session Generalization
Skin temperature, electrode positioning and sweat are some of the many factors which change EMG readings, making a classifier that works despite these changes is difficult.
-
Cross Subject Generalization
Above is an image from the Myo blog, showing data from two different people making the exact same gesture. EMG data is thought to be uniquely identifiable for many reasons. One obvious example is the palmaris longus forearm muscle which is missing in roughly 14 percent of the population. Muscles strength, thickness of the wrist etc all mean that a classifier trained on one person is unlikely to work on another. As you can be identified by your sEMG data, you should think before sharing any data gathered using the Myo.
When gathering training data for a classifier, experimental setup needs to be considered carefully. Common practices are to fully charge the Myo and then wear it for 10 minutes to allow the temperature of the electrodes to warm to the temperature of your skin.
What next?
Depending on who you are you may want to:
- Run all the other examples and figure out what they do.
- Learn how pyomyo works by reading the Multithreaded code example.
- Use the Myo to play Breakout (A Myo regression example).
- Use
data_collector.py
to generate a csv of your EMG data to analyse. - Star the repo to help it show up in search results ;)
- Join the Discord and tell me about all the mistakes in this tutorial.
- Read about the bigger picture of this project.
- Use the Myo to play SteamVR games.
Here is a basic example of using the PyoMyo in a multithreaded way to print out EMG readings:
import multiprocessing
from pyomyo import Myo, emg_mode
# ------------ Myo Setup ---------------
q = multiprocessing.Queue()
def worker(q):
m = Myo(mode=emg_mode.RAW)
m.connect()
def add_to_queue(emg, movement):
q.put(emg)
m.add_emg_handler(add_to_queue)
def print_battery(bat):
print("Battery level:", bat)
m.add_battery_handler(print_battery)
# Orange logo and bar LEDs
m.set_leds([128, 0, 0], [128, 0, 0])
# Vibrate to know we connected okay
m.vibrate(1)
"""worker function"""
while True:
m.run()
print("Worker Stopped")
# -------- Main Program Loop -----------
if __name__ == "__main__":
p = multiprocessing.Process(target=worker, args=(q,))
p.start()
try:
while True:
while not(q.empty()):
emg = list(q.get())
print(emg)
except KeyboardInterrupt:
print("Quitting")
quit()
Try plugging in the dongle and moving the Myo, if the LEDs are on, they are listening for your dongle. Note: Unless sent a power off command, the Myo never turns off. If it does turn off, charging it will cause it to turn on. The Myo does sleep, but putting it on or moving it should turn it back on.
If the LEDs turn orange and the Myo vibrates, you should see readings coming though.
Try making a fist and squeezing, you should see some change in values.
To make the EMG signals easier to interpret, try changing the EMG mode. From:
m = Myo(mode=emg_mode.RAW)
To:
m = Myo(mode=emg_mode.PREPROCESSED)
Squeezing your fist should now increase the values printed.