North (formerly Thalmic Labs), the creator of the Myo armband, was acquired by Google in June 2020. Myo sales ended in October 2018 and Myo software, hardware and SDKs are no longer available or supported. Learn more.
Part Three: Strike a Pose
With Myo armband you can currently do five hand poses: Wave out, wave in, fist, fingers spread and double tap. When and how to use each pose is covered in detail in our UX guidelines. I encourage you to read them before you start seriously building anything for the Myo, but let’s play around a bit now.
To actually detect a pose, we’ll use the second of our predefined callbacks:
onPoseEdge. This is the signature for
function onPoseEdge(pose, edge)
You may now be thinking “Paul, what’s all this ‘edge’ business? And what happened to my meatball sandwich?” I can’t help you with the sandwich, but the idea behind edge is that it will be set to
on when a pose starts, and
off when the user releases it. This will let you do things like start listening for changes in yaw to do something when the user makes a fist and stopping once they let go. You’ll see this “edge” concept again when we discuss keyboard (and mouse button) presses.
pose can have one of the following values:
rest is the state where the user is not actively posing, and
unknown means that the Myo can’t determine any poses (probably because the user has not performed the setup gesture, or has removed the armband).
Let’s print poses and the edge state to the console. Try it yourself, and compare what happens in the debug log and the pose window. Note that you have to unlock the Myo armband with double tap before any poses come through to your script.
You should have added something like this:
function onPoseEdge(pose, edge) myo.debug("onPoseEdge: " .. pose .. ": " .. edge) end`
Make sure you try all the poses and see what the flow looked like. The armband will lock itself automatically after a few seconds. You get that for free, but we’ll come back to more advanced locking behaviour later in the tutorial.
Detecting Specific Poses
You can do some neat stuff in Lua, but if you’re just starting out most of the time your
onPoseEdge implementation is going to be a bunch of
if statements doing something specific on each gesture you care about.
if statements look like this:
then -- do something elseif then -- do something if something else is true else -- do something else end
if statement evaluates the conditional statement, and if it’s true, executes the code after
then (and nothing else in rest of the
else blocks). If not, it goes through any
elseifs in order and does the same thing. If it gets all the way down to an
else, it will execute that code. For example, you might put something like this in
onForegroundWindowChange to detect your cool game being in the foreground:
if (title == "My Cool Game") then return true else return false end
You can put brackets around your conditional like other languages if you are used to it or think it makes it more readable. I do, so I will be, but it doesn’t matter.
As a best practice, we recommend detecting the
pose you want in
onPoseEdge and then handling what happens for a given
pose in a separate function to keep things tidy.
See if you can add logic to your
onPoseEdge implementation that will detect the user starting the wave in, wave out, fist, or finger spread pose, and call a separate function for each. I recommend something generic like
onWaveOut() etc. You don’t have to actually implement those functions yet, just
onPoseEdge. I’ll give you my solution tomorrow when we talk about simulating keyboard input.
Oh, before I go it’s probably worth mentioning we’ve re-structured our blog slightly to avoid swamping non-developers with developer focused content (there will be a LOT of it coming). If you want to see developer stuff, here’s the link to the Developer blog. You can subscribe to the Developer RSS feed here.
We also have a Presentations blog, as well as one for Thalmic Labs itself. RSS feed here. So adjust your feeds accordingly for the content you want! I hope you’ll stick around for the developer stuff, even if you’re not really a developer. At least to start anyway. These Myo Script tutorials are a good way in to get just a little bit more out of your Myo armband!
Good luck, and happy coding!