I haven’t been able to stop play­ing around with my Myo arm­band since I got my hands on it this week. In truth it’s ruined a few of my plans for the week­end, but boy it’s been fun try­ing to get the Myo work­ing with var­i­ous apps and games.

Elm has also piqued my inter­est lately, so what bet­ter way to play with the Myo than to build a game in Elm that is best played using ges­tures? And along comes Thun­der Cow, a light-hearted game where you use the fist and fin­gersSpread pos­tures to start and stop a mov­ing arrow. The goal is to time your release so that the arrow stops in the red zone.

Embedded image permalink

To play using the Myo arm­band you also need to add this Lua script into your Myo Script Man­ager. After you’ve added the script, head over here to give it a go.

The Elm source code for the game is avail­able here. There is no native sup­port for sound in Elm, so I had to resort to using Elm’s JS interop capa­bil­i­ties to invoke Javascript func­tions to play the mp3 files.

Share

It’s been a long wait but I have finally got my hands on a Myo arm­band this week!

2014-10-24 20.35.09

 

To start, you need to down­load and install Myo Con­nect from here. You need to go through the set up guide, which includes teach­ing the Myo arm­band how to clas­sify the EMG read­ings from your arm to one of five rec­og­nized pos­ture patterns:

image

It works well out of the box. Despite being a SDK unit, it’s able to inter­face with some appli­ca­tions such as Pow­er­Point and iTunes. Its script­ing capa­bil­ity makes it easy to add sup­port for other appli­ca­tions (more on this later).

The pat­tern detec­tion can feel laggy – only by a frac­tion of a sec­ond but notice­able when you’re try­ing to play a game for instance. The accu­racy of the pat­tern detec­tion is not quite there yet either.

But both issues will improve over time as bet­ter firmware becomes avail­able. The guys at Thalmic Labs will also fine tunes their math model with more data from real world usages.

So don’t let these tem­po­rary short­com­ing hold you back from buy­ing a Myo arm­band. (You might want to keep it out of the reach of your pets though, as mine tried to attack and take a bite out of it as soon as I took it off)

 

Pro­gram­ming your Myo

Once everything’s setup and run­ning you will no doubt be itch­ing to pro­gram against this won­der­ful piece of device.

The SDK allows you to write appli­ca­tion to take full advan­tage of the Myo’s capa­bil­i­ties using C or C++.

sdk-stack.png

Or, you can write Lua scripts to con­trol appli­ca­tions using Myo. This allows you to map pos­tures to key­board or mouse sig­nals (key up/down/press, mouse click, mouse move­ment, etc.). This approach has lim­i­ta­tions – for instance, you can’t pass rota­tion infor­ma­tion into a flight sim­u­la­tion pro­gram via key­board and mouse sig­nals alone. That said, there’s still plenty you can do with Lua scripts.

 

Lua Scripts

To run Lua scripts, you need to make sure Myo Con­nect is run­ning (you’ll see it on your taskbar in Windows):

Image

Right-clicking on the Myo Con­nect icon gives you the option to open the Myo Script Man­ager where you are able to load your Lua scripts.

image

This is how the Can­vas blank.lua script you see above look in its full glory.

There are cou­ple of things to note about this script:

  1. the scrip­tId vari­able spec­i­fies a unique ID for your script
  2. the onFore­ground­Win­dow­Change func­tion defines a call­back func­tion. Myo Con­nect will call this func­tion when the active appli­ca­tion changes. Our func­tion spec­i­fies that the script should become active when you load the Can­vas Blank mini game. It also allows the Myo to con­trol mouse move­ment when that hap­pens so that we can aim with our arm.
  3. the onPoseEdge func­tion also defines a call­back func­tion. Myo Con­nect will call this func­tion when a pos­ture pat­tern turns on or off. Here we spec­ify that a fist pos­ture maps to a mouse left-click, and a fin­gersSpread pos­ture maps to mov­ing the mouse to the cen­tre of the pri­mary display.

 

It was straight for­ward to get the Myo arm­band work­ing with Can­vas Blank. But the play­ing expe­ri­ence is far from sat­is­fac­tory due to the fol­low­ing reasons:

  • the lag and impre­cise pos­ture recognition
  • Myo’s mouse move­ment con­trol is not smooth enough. Cou­pled with an occa­sional lag it breaks the sense of immer­sion (and makes me miss an awful lot!)
  • Pos­tur­ing causes small move­ments in your arm. It is often enough to cause the mouse to move, another rea­son for my many misses…

 

Based on what I’ve seen so far, I’d say that Myo is not ready for appli­ca­tions that need good pre­ci­sion con­trol yet. FPS games are prob­a­bly out of the ques­tion for now.

Again, this is an area where I expect things to improve soon. Per­haps even by the time you see the final com­mer­cial unit (they’re ship­ping the first batches of SDK units right now).

What shouldn’t be under­es­ti­mated, is how easy it was for me to inte­grate with the Myo via Lua scripts. In fact, it’s so easy that you don’t even have to be an expe­ri­enced pro­gram­mer to make it work.

To unlock the full poten­tial of the Myo you need to fall back to the SDK. For now, it means devel­op­ing your appli­ca­tion in C/C++, but other lan­guage bind­ings should become avail­able soon.

 

Use­ful Links

Share

Prob­lem

Con­sider the fol­low­ing “magic” 3-gon ring, filled with the num­bers 1 to 6, and each line adding to nine.

Work­ing clock­wise, and start­ing from the group of three with the numer­i­cally low­est exter­nal node (4,3,2 in this exam­ple), each solu­tion can be described uniquely. For exam­ple, the above solu­tion can be described by the set: 4,3,2; 6,2,1; 5,1,3.

It is pos­si­ble to com­plete the ring with four dif­fer­ent totals: 9, 10, 11, and 12. There are eight solu­tions in total.

image

By con­cate­nat­ing each group it is pos­si­ble to form 9-digit strings; the max­i­mum string for a 3-gon ring is 432621513.

Using the num­bers 1 to 10, and depend­ing on arrange­ments, it is pos­si­ble to form 16– and 17-digit strings. What is the max­i­mum 16-digit string for a “magic” 5-gon ring?

Solu­tion

(see full solu­tion here)

 

Before we go into details on the solu­tion, let’s first struc­ture the ques­tion in a way that’s easy for us to compute.

To con­struct the magic cir­cle, be it for a 3-gon or a 5-gon ring, we can slice up the num­bers into pairs – e.g. A => [1; 2], B => [3; 4], C => [5; 6], D => [7; 8], E => [9; 10] – and the prob­lem becomes find­ing ways in which the num­bers can be per­muted such that:

  1. a0 is the small­est amongst a0, b0, c0, d0, and e0
  2. the sums of a0 + a1 + b1, b0 + b1 + c1, … are the same

For exam­ple:

image

 

First, we’ll find the dif­fer­ent ways the num­bers 1 to 10 can be per­muted, and for each per­mu­ta­tion slice the num­bers into pairs:

image

(this makes use of a per­mute func­tion defined in the Common.fs source file in the solution).

 

Then we need a func­tion to sum a pair of num­ber with the last ele­ment in the adja­cent pair – e.g. a0 + a1 + b1:

image

 

For each per­mu­ta­tion, we need to check:

  1. if a0 is the small­est amongst the head elements
  2. if the sums of the groups of 3 – i.e. a0 + a1 + b1, b0 + b1 + c1, etc. – are the same

image

This pred­i­cate func­tion allows us to find arrange­ments that meet our cri­te­ria. All that’s left is to turn the result groups of 15 num­bers into 16/17-digit strings and find the max­i­mum (see full solu­tion below).

 

Here’s the full solution:

image

 

This solu­tion took 26s to exe­cute on my machine.

Share

Prob­lem

All square roots are peri­odic when writ­ten as con­tin­ued frac­tions and can be writ­ten in the form:

image

For exam­ple, let us con­sider ?23:

image

If we con­tinue we would get the fol­low­ing expansion:

image

The process can be sum­marised as follows:

image

It can be seen that the sequence is repeat­ing. For con­cise­ness, we use the nota­tion ?23 = [4;(1,3,1,8)], to indi­cate that the block (1,3,1,8) repeats indefinitely.

The first ten con­tin­ued frac­tion rep­re­sen­ta­tions of (irra­tional) square roots are:

?2=[1;(2)], period=1

?3=[1;(1,2)], period=2

?5=[2;(4)], period=1

?6=[2;(2,4)], period=2

?7=[2;(1,1,1,4)], period=4

?8=[2;(1,4)], period=2

?10=[3;(6)], period=1

?11=[3;(3,6)], period=2

?12= [3;(2,6)], period=2

?13=[3;(1,1,1,1,6)], period=5

Exactly four con­tin­ued frac­tions, for N <= 13, have an odd period.

How many con­tin­ued frac­tions for N <= 10000 have an odd period?

Solu­tion

(see full solu­tion here).

 

Based on the algo­rithm on con­tin­ued frac­tions from Wikipedia, we can imple­ment the expan­sion algo­rithm as:

image

From the Wikipedia page:

The algo­rithm can also ter­mi­nate on ai when ai = 2 a0, which is eas­ier to implement.

which cor­re­sponds to the ter­mi­na­tion con­di­tion we have in the Repeat active pat­tern (which also checks if the accu­mu­la­tor is empty):

image

Also, this algo­rithm doesn’t work on num­bers that are per­fect squares, i.e. 4, 9, 16, … hence we need to exclude them when search­ing for our answer.

Here’s the solu­tion in full:

image

 

This solu­tion took 92ms to exe­cute on my machine.

Share

Prob­lem

It is well known that if the square root of a nat­ural num­ber is not an inte­ger, then it is irra­tional. The dec­i­mal expan­sion of such square roots is infi­nite with­out any repeat­ing pat­tern at all.

The square root of two is 1.41421356237309504880…, and the dig­i­tal sum of the first one hun­dred dec­i­mal dig­its is 475.

For the first one hun­dred nat­ural num­bers, find the total of the dig­i­tal sums of the first one hun­dred dec­i­mal dig­its for all the irra­tional square roots.

Solu­tion

(see full solu­tion here).

The premise of the prob­lem itself is fairly straight for­ward, the chal­lenge here is down to the way float­ing points are imple­mented on com­put­ers which lacks the pre­ci­sion nec­es­sary to solve this prob­lem. So the bulk of the research went into find­ing a way to gen­er­ate an arbi­trary num­ber of dig­its of a square root.

As usual, Wikipedia has plenty to offer and the eas­i­est solu­tion imple­men­ta­tion wise is this solu­tion by Frazer Jarvis.

image

which trans­lates to:

image

The rest is really straight for­ward, with the only tricky thing being the con­ver­sion from char to int since this returns the inter­nal inte­ger value instead – e.g. int ‘0’ => 48 and int ‘1’ => 49 – hence the need for some hack­ery in the sum func­tion.

Here is the full solution:

image

 

The solu­tion took 95ms to com­plete on my machine.

Share