Hacking Lego Mindstorms EV3 with JavaScript

Imgur

Last week I presented at Full Frontal about Hardware hacking with JavaScript, the one section that really seemed to strike a cord with the developers in the audience was controlling a Lego Mindstorms robot with JavaScript.

The EV3 kit comes with a good selection of inputs and outputs and can be controlled over bluetooth, usb or have a program copied onto the intelligent brick and then run autonmously.

Combining the ease of building things from Lego with the ability to program the robots with JavaScript is very appealing, so I thought I'd share a couple different approaches to doing this.

Remote control over Bluetooth

When paired with a bluetooth device, you can talk to it just like any other serial device, as long as you know what to tell it.

Unfortunatly there isn't any documentation for the EV3 bluetooth protocol yet but Wenchao Jiang has somehow managed to reverse engineer some of it to allow you to control the speed and direction of up to 4 motors simultatiounly.

The code is up on Github and available on npm as ev3-nodejs-bt, here's an example of how to use it:

// load the node module
var Ev3 = require ("ev3-nodejs-bt");  
var Ev3_base = Ev3.base;

// make a new robot connect to the bluetooth serial port
var robot = new Ev3_base("/dev/tty.EV3-SerialPort");

// connect to the robot and start the program
robot.connect(function(){  
  robot.start_program(function(ev3){

    // function for writing the motor sequence to the ev3
    var setSpeeds = function(a,b,c,d){
      var output = ev3.getOutputSequence(a,b,c,d);
      ev3.sp.write(output);
    }

    // start motors a, b and c moving
    setSpeeds(10, -25, 100, 0);     
  });
});

That code will spin the motors plugged into ports A, B and C at different speeds until the program is stopped.

Speeds can be anything from -100 to 100, where negative values reverse the direction of the motor and zero will stop the particular motor.

Node.js programs, like Lego, are built from lots of small modules. I then took my xbox-controller module and combined it with the mindstorms code to create Node EV3 Robot.

Imgur

It's a tracked robot, based on the mindstorms instructions with a custom built rabbit food hopper attached to the front.

The tracks are controlled using the left analogue stick and the food hopper has a piston which pushes out a nugget when rotated by pressing the A button. Here's a video of it in action:

And a close up of the hopper ejecting food for Bertie to enjoy, he seems to quite like his new friend!

Right now, over bluetooth, you can only set the values of the motors, you can't read back any data from the other sensors in the kit. This is mainly due to the lack of documentation from Lego on the protocol to speak to the EV3 brick.

Hopefully LEGO will release some more information on that soon, alternatively there are is more documentation on using the older NXT system. There are also a couple node modules for NXT, node-nxt and mindstorms_bluetooth are both available on npm and work in a similar way.

Running Node.js on board

Imgur

One of the most interesting aspects of the EV3 system is that the intelligent brick actually has an ARM9 chip and runs linux on board, you can even insert your own microSD with a different operating system if you wish.

There is a dump of the source code of the installed OS on Github to have a poke around in and a guide to messing around with it on BotBench.

And of course, if you can run linux on a device, then it won't be long before someone compiles Node.js onto it. Clemens Akens has been working on exactly that.

There is a collecton of scripts and config files in node-ev3-setup and the beginning of a very nice JavaScript API in the form of an EV3 module which is designed to be ran onboard the device and can read button presses and battery life, as well as setting controlling each motor and reading their current positions.

The project is in the early stages of development but looks very promising for creating completely autonomous lego robots powered by JavaScript in the near future.

Discussion on Hacker News and Twitter.