On Github ajfisher / building-droids-with-js
JSConf China, July 11, 2015
Andrew Fisher @ajfisher
Press 'S' for full speaker notes.
Hi! My name is Andrew Fisher and I’m an interaction researcher. Today I want to talk to you about Droids, JavaScript and Web Connected hardware for the next 25 minutes.(CC) Flickr Quasimodo
My initial interest in computing came from an interest in electronics. But the reality was that for a kid growing up in the late 80s, building serious hardware was prohibitively expensive and required facilities that most kids didn’t have access to in those days. Doing anything non-trivial in hardware was a very difficult undertaking. As a result, I moved to software. As you’d expect, in the world of software, the effort to reward ratio was far better than for hardware, especially for the attention span of a teenager. And this situation remained the case until recently.(CC) Phil Farugia
Until this came along - this is an arduino and it has changed the way hardware is thought of. Hardware was no longer expensive - these were designed at the time to be less than 20 euros and the designs were open sourced, meaning anyone could make them. Today you can get them for $5. Sophisticated hardware became accessible to students, artists, kids and web people. With it, this community has brought ideas around design, user experience, art and software and architectural principles. More recently, over the last few years, some great work has been done in the node community getting JS to work with hardware like this - to the point where working with hardware using javascript is now extremely easy. So today, I want to talk to you about that and how all of you can all start working with hardware with JavaScript and we'll see some of the great things the community has done along the way..(CC) Flickr JohnGreenaway
Most people don't naturally think about JavaScript as a language that should be used for programming hardware. JS is the language of the web! It's only place is in the browser? Right? Lets see how some of it's features make JS and hardware really powerful together.(CC) Flickr Oskay
Let's assume the language is as capable as any other - and besides the fact we are all JS developers here. Why use JavaScript compared to some other high level language. For me it really comes down to two things: JavaScript's inherent event handling JavaScript's dynamic object prototypingTwitter: @nodebotanist
JavaScript has incredible event handling. We tend to take this for granted, but due to it's birth as an interface related language, it has one of the most incredible event systems of any modern language. If you work in NodeJS then you know that you have EventEmitter available and it's just amazing at allowing different parts of your application to talk to each other.(CC) Flickr txmx 2
When you're designing something to work in the real world thinking about events makes so much sense that it makes your code very clean, modular and responsive. Say you are designing a door entry system with a card. In JS we simply define an event listener and wait for the card swipe to occur with the detals of the card and then handle it. We can do the same thing in C but we write a lot of code, have to use interrupt handlers and make sure we suspend the system state properly or our door will crash. JS is designed to just work with this sort of behaviour.var motor = new Five.Motor(); motor.start();We're all familiar with the way JS handles objects. And I'll show you more of this in a moment but the hardware framework we use allows us to consider hardware as objects as well such as the case of this motor. Really I don't want to care about the underlying detail of how different types of motors work - I just want to use it to make the wheels of my robot turn.
var left_motor = new Five.motor({controller: 'I2C'}); var right_motor = new Five.motor({controller: 'PCA34567'}); left_motor.start(); right_motor.stop(); left_motor.prototype.double_speed = function() { .. };For end users this is awesome but as a developer writing JS drivers for things like motors being able to define a shape and then allow you to attach the appropriate controller is great too. Likewise as the end user you all know you can extend the prototype and make your own versions of it if you need something really specific such as going twice as fast when you call start.
(CC) Flickr hiperbolica
That's enough of talking about why JS is really well suited to working with robotics. We all work with JavaScript and know it's a real and interesting language - Let's move on to what the hardware stack looks like.(C) Joanne Daudier
Johnny Five was started by this guy - Rick Waldron, and there are now about 30 core project members, nearly 100 contributors and over 2000 commits to the codebase in the last couple of years. It's a very active and expanding project and we're always looking for more contributors to help out. Johnny Five is a hardware abstraction framework so instead of writing code that is specific to a chip you can talk about components that behave in different ways and leave the implementation details up to the people who write the board level interfaces whereupon you can then use it.Glasses (C) Andy Gelme | Image (CC) Matthew Bergman
With all this power, what sort of things can you build? Well I'm going to show you some examples of some fun things people have made and show you some code as well.Simplebot (CC) AJ Fisher
This is a basic teaching bot we use in Australia for nodebots events. Very basic and allows you to explore fundamentals of robotics design and motion. It is very cheap and fast to build so good for beginners.Skirt (C) Kassandra Perch | Image (CC) Matthew Bergman
This skirt, made by Kassandra Perch is fully contained running javascript on a little board embedded into it. It has an accelerometer and as you move around the LEDs inside it light up different colour. So you can even use JavaScript in your clothing!!(C) Adrian Catalan
Here is an example of using nodeJS to make a physical game. This is made by Adrian Catalan and uses nodebots and node-pixel to make a tetris game on LED panels.(OSC) dtex
Donovan Buck has done a lot of work on making sophisticated animation control in nodebots with a library to solve inverse kinematics problems. Here you can see he has attached a 6-leg robot to a leap motion for hand control and it responds very very fast to his movement.(CC) Flickr Daniel Novta
So now you've seen some examples, let's see a demonstration. With hardware you most of the time don't have a screen. So a hello world example is just getting an LED to blink on and off.var five = require("johnny-five"); if (process.argv[2] == null) { console.log("You need to supply a device to connect to"); process.exit() } var board = five.Board({port: process.argv[2]}); board.on("ready", function() { var led = new five.Led(10); led.blink(500); });So here's the code to make this work. You can see here that I have the board connect and then when it is ready the callback fires and I have a simple call to blink every 500ms. This is a convenience method which just sets a timer interval and then calls it.
Image of live demonstration. Code: led.js
So let's see that work. Switch to code. Easy right - that is like the blink tag in hardware!!var five = require("johnny-five"); if (process.argv[2] == null) { console.log("Please supply a device to connect to"); process.exit(); } // web server elements var express = require('express'); var app = express(); var http = require('http'); var server = http.createServer(app); var board; // // // Set up the application server // app.configure(function() { app.set('port', 8001); app.use(app.router); app.use(express.static(__dirname + '/public')); }); server.listen(app.get('port')); // Set up Socket IO var io = require('socket.io').listen(server); io.set('log level', 1); console.log("MESSAGE: Web server now listening"); app.get('/', function(request, response) { response.sendfile(__dirname + '/public/index.html'); }); io.sockets.on("connection", function(socket) { if (board.ready) { socket.emit("connect_ack", {msg: "Welcome Control", state: "ONLINE"}); } else { socket.emit("connect_ack", {msg: "Welcome Control", state: "NOPINS"}); } socket.on("toggle", function(data) { // use the control mech to switch the LED on or off board.digitalWrite(pin, data.state); }); }); // SET up the arduino and firmata var pin = 10; // led pin to turn on. board = new five.Board(process.argv[2], function(err) { if (err){ console.log(err); process.exit(); } console.log("Control via your browser now"); });So that's great but what about if you want something connected to the browser? We'll use the same circuit but this time we can use express to make a web page. You can see here the code is mostly just setting up page routes and then we use web sockets to send a message when the button is clicked. After that it's mostly just the same code as before. Connect to the board and make it respond to a message.
Image of live demonstration. Code: webled.js
So here it is working - as I click the button the LED turns on and off. All with JavaScript and a little bit of hardware.(CC) AJ Fisher
This final demonstration is a little more complex. This is an mBot - made by a company here in Shenzhen called Make Block. These are the bots we'll be using tonight at the nodebots workshop. They have many features, including motors, sensors and can use bluetooth and wifi too.var five = require("johnny-five"); var max_speed_l = 150; var max_speed_r = 140; // set up the input var stdin = process.openStdin(); require('tty').setRawMode(true); var board = new five.Board({port: process.argv[2]}); var l_motor = r_motor = null; board.on("ready", function(err) { if (err){ console.log(err); return; } l_motor = new five.Motor({pins: {pwm: 6, dir: 7}}); r_motor = new five.Motor({pins: {pwm: 5, dir: 4}}); console.info("Board connected. Robot set up. LRUD to control"); }); stdin.on('keypress', function(chunk, key) { // process the keypresses if (key) { switch (key.name) { case "up": l_motor.reverse(max_speed_l); r_motor.forward(max_speed_r); break; case "down": r_motor.reverse(max_speed_r); l_motor.forward(max_speed_l); break; case "left": l_motor.forward(max_speed_l); r_motor.forward(max_speed_r); break; case "right": r_motor.reverse(max_speed_r); l_motor.reverse(max_speed_l); break; case "space": l_motor.stop(); r_motor.stop(); break; } } });This code is a little long but mostly it creates a board and then uses the key presses of a keyboard to then say to drive forwards, backwards, left and right and turns the motors on and off in the right combinations to do this. Even though there is some code it is very simple.
Image of alternative demonstration. Code: mbot.js
Let's see this drive. Hopefully this will work. So this is just running over bluetooth and is controlled from javascript.(CC) Flickr ⣫⣤⣇⣤
So hopefully that gives you a good view of what is happening with JavaScript and hardware. It is lots and lots of fun and is really good for your coding skill because real life events make coding a bit more interesting.July 25, 2015
nodebotsday.com
On July 25 there is a day of NodeBots all around the world. Over 30 cities are taking part with over a thousand JS developers all building robots and web hardware on the day. If you want to take part, then organise a group, go to this site and you can find out more information. Or come talk to me as I am coordinating Australia and Sri Lanka events.JSConf China, July 11, 2015
Andrew Fisher @ajfisher
So hopefully that gives you a good view of why you would use JS, what the stack looks like and also some inspiration for things you can do with it. thank you very much and please come talk to me if you want to find out any more