28
/* * __ __ ______ _____ ______ ______ ______ ______ * /\ "-.\ \ /\ __ \ /\ __-. /\ ___\ /\ == \ /\ __ \ /\__ _\ * \ \ \-. \ \ \ \/\ \ \ \ \/\ \ \ \ __\ \ \ __< \ \ \/\ \ \/_/\ \/ * \ \_\\"\_\ \ \_____\ \ \____- \ \_____\ \ \_____\ \ \_____\ \ \_\ * \/_/ \/_/ \/_____/ \/____/ \/_____/ \/_____/ \/_____/ \/_/ * `` ``` `. * .-. .` ..-.` `:-/+` .` * `-``:-` ` -` .+``-/:/. /+` - * :. `+/ /. .- `+: `/- `+- - * .`.--./ -o. :// :. `//. // .` *`:+-::-.-. `o/:-`/ ``` :- .//.`:/` `. *././o-o- .-.:+``.. `-` ./ .::/` `.` *`-.:o+-/. `+o. `. ::` ``.` * :o/o/:`..-++. :--.` `.` :: .-.`` ```` * `/o+/--.`:/. /-/``.-.` `+o. `` `..--..` `.````````` *`/:+/:--:.` `/. .-` `.--` `/-` ./. ::``` *.++.-://` `/` -:::.` `-:-``+: `/- `-- *.+/: /: -: -/ `-::.` `:+/ `/- .:. *-:/-`o :- -/ `-:-` ...--` `/-`:-` *`::`-+ `-.. :. :: .-:-` `--.` .+:--` *-:.`.o:/:+::/. `:- ` `-:-` `.-.` `..` *`+/`.o:``+`// `/- :o/` `---. -. * ./://. +``o `:. :+:` `--.`:. * ./. `` +` :.-` `-. -/: `.-: * .-` :. ``` :/-:-.` `sso ./. * -: ` ...` `--/-:-.` .-` :+. .` ``` * -+- .-. `... `.--::.+.` /+` `:..:- `/-:. * `//` `---` `--.` `.://+: `//` .. :- `.-: * :+- .-..` `---. `-:` `+: .-``:/-`. -- * `//` ``...``.--.` .:. ./::::://:/:` *`/- ```````.```-/: `/+/.--....://` * ` `.` ````.-//o `//--//:::..+/ * `/-...` ````-: `.---:-.-//:.` :/` * ./. .....` --.--.-..-`..:/-`-/` * -/` `..` `...` :. .. .`-``-`..:/./` * ::` ```.. ``.`` `:/ ..-``.`:`.. -`--` * .- ``.-` ``-. ``-::``-.- -..- -`-.:. * .-` `:-` `..-+..`:`/`-: :. -.-` * `` -:` .--.-/- ./ /../ .-`-.` * -. `:://:::` /- :.`/`.::. * `-` ./-:/--/` ./` :.`:--` * `. -:`.--./.``:-.--:-`` * `:- ```......`` * .:. */ Node.js + Robots Nodebots! Mike Anderson ! @mike0x41 VicJS May 2015

Node.js + Robots = Nodebots!

Embed Size (px)

Citation preview

Page 1: Node.js + Robots = Nodebots!

/* * __ __ ______ _____ ______ ______ ______ ______ * /\ "-.\ \ /\ __ \ /\ __-. /\ ___\ /\ == \ /\ __ \ /\__ _\ * \ \ \-. \ \ \ \/\ \ \ \ \/\ \ \ \ __\ \ \ __< \ \ \/\ \ \/_/\ \/ * \ \_\\"\_\ \ \_____\ \ \____- \ \_____\ \ \_____\ \ \_____\ \ \_\ * \/_/ \/_/ \/_____/ \/____/ \/_____/ \/_____/ \/_____/ \/_/ * `` ``` `. * .-. .` ..-.` `:-/+` .` * `-``:-` ` -` .+``-/:/. /+` - * :. `+/ /. .- `+: `/- `+- - * .`.--./ -o. :// :. `//. // .` *`:+-::-.-. `o/:-`/ ``` :- .//.`:/` `. *././o-o- .-.:+``.. `-` ./ .::/` `.` *`-.:o+-/. `+o. `. ::` ``.` * :o/o/:`..-++. :--.` `.` :: .-.`` ```` * `/o+/--.`:/. /-/``.-.` `+o. `` `..--..` `.````````` *`/:+/:--:.` `/. .-` `.--` `/-` ./. ::``` *.++.-://` `/` -:::.` `-:-``+: `/- `-- *.+/: /: -: -/ `-::.` `:+/ `/- .:. *-:/-`o :- -/ `-:-` ...--` `/-`:-` *`::`-+ `-.. :. :: .-:-` `--.` .+:--` *-:.`.o:/:+::/. `:- ` `-:-` `.-.` `..` *`+/`.o:``+`// `/- :o/` `---. -. * ./://. +``o `:. :+:` `--.`:. * ./. `` +` :.-` `-. -/: `.-: * .-` :. ``` :/-:-.` `sso ./. * -: ` ...` `--/-:-.` .-` :+. .` ``` * -+- .-. `... `.--::.+.` /+` `:..:- `/-:. * `//` `---` `--.` `.://+: `//` .. :- `.-: * :+- .-..` `---. `-:` `+: .-``:/-`. -- * `//` ``...``.--.` .:. ./::::://:/:` *`/- ```````.```-/: `/+/.--....://` * ` `.` ````.-//o `//--//:::..+/ * `/-...` ````-: `.---:-.-//:.` :/` * ./. .....` --.--.-..-`..:/-`-/` * -/` `..` `...` :. .. .`-``-`..:/./` * ::` ```.. ``.`` `:/ ..-``.`:`.. -`--` * .- ``.-` ``-. ``-::``-.- -..- -`-.:. * .-` `:-` `..-+..`:`/`-: :. -.-` * `` -:` .--.-/- ./ /../ .-`-.` * -. `:://:::` /- :.`/`.::. * `-` ./-:/--/` ./` :.`:--` * `. -:`.--./.``:-.--:-`` * `:- ```......`` * .:. */

Node.js + Robots

Nodebots!

Mike Anderson! @mike0x41

VicJS May 2015

Page 2: Node.js + Robots = Nodebots!

Hello

• My name is Mike Anderson

• I work at Echosec Systems

• I used to do embedded development

• Sometimes I miss it

Page 3: Node.js + Robots = Nodebots!

Solution

Build robots and connect them to the internet

Page 6: Node.js + Robots = Nodebots!

DoodleBot

Page 7: Node.js + Robots = Nodebots!

The Mobile Web Client

• Try and get doodlebot to draw something by connecting points on an HTML canvas element

• Yes, this is horribly insecure

• DoodleBot is very bad at drawing

its a cloud!

Page 8: Node.js + Robots = Nodebots!

What is DoodleBot?

What do I need to make my own robot with poor drawing abilities?

Page 9: Node.js + Robots = Nodebots!

You Need

Robot Chassis Johnny-Five

Page 10: Node.js + Robots = Nodebots!

The Robot Chassis• Chassis

• Motors

• Battery / Battery Holder

• Wiring

• Tools / Fasteners

• Motor Driver

• Microcontroller

• I recommend SparkFun or Seeed Studio or HobbyKing

2CH Motor Driver

Multi-Chassis - Tank Version

Page 11: Node.js + Robots = Nodebots!

The Micro Controller

Page 12: Node.js + Robots = Nodebots!

What’s Johnny-Five• Robot control framework built

on node.js released in 2012

• Very solid and active community

• Communicates with Arduino micro controller or similar device via the Firmata protocol

• Has support for

• >30 microcontrollers

• >40 components

Page 13: Node.js + Robots = Nodebots!

How To Johnny-Five

• Load the StandardFirmata firmware onto an arduino compatible micro controller

• Connect stuff to the Arduino pins

• Connect the Arduino to the host machine over USB serial

• Initialize a the board and connected components in node on the host machine

One Example

Page 14: Node.js + Robots = Nodebots!

ControllersArduino Raspberry Pi Beaglebone

Black Intel Edison

WiFi / BT Extra Shield USB Dongle USB Dongle Low power

built in

Power ~1.5 W (5V) ~3.2W (5V) ~2.25W (5V) <1W (7-15V)

PinsArduino

shield layout 5V

Needs breakout

3.3V

Arduino shield layout

5V

Needs breakout

1.8VRuns Node Nope Yup You bet Comes

installed

Cost $25 $50 + $15 $60 + $15 $85

Page 15: Node.js + Robots = Nodebots!

Controllers I went with

Arduino Raspberry Pi Beaglebone Black Intel Edison

WiFi / BT Extra Shield USB Dongle USB Dongle Low power

built in

Power ~1.5 W (5V) ~3.2W (5V) ~2.25W (5V) <1W (7-15V)

PinsArduino

shield layout 5V

Needs breakout

3.3V

Arduino shield layout

5V

Needs breakout

1.8VRuns Node Nope Yup You bet Comes

installed

Cost $25 $50 + $15 $60 + $15 $85

Page 16: Node.js + Robots = Nodebots!

Johnny-Five

Why Javascript for robots?

!

Isn’t it like the worst possible language for embedded development?

Page 17: Node.js + Robots = Nodebots!

No, It’s AwesomeHere’s Why

Page 18: Node.js + Robots = Nodebots!

var five = require("johnny-five"); var board = new five.Board(); !board.on("ready", function() { var led = new five.Led(13); var markerServo = new five.Servo(12); var motorLeft = new five.Motor([9,7]); var motorRight = new five.Motor([10,8]); var encoderLeft = new five.Button(3); //currently broken var encoderRight = new five.Button(2); ! this.repl.inject({ markerServo: markerServo, motorLeft: motorLeft, motorRight: motorRight, encoderLeft: encoderLeft, encoderRight: encoderRight }); }

Read–eval–print loop

Page 19: Node.js + Robots = Nodebots!
Page 20: Node.js + Robots = Nodebots!

/** * Go straight forward or back * Turns out this kit can't really do speed * * @param {String} dir - either 'fwd', or 'rev' * @param {Number} time - the amount of time in ms * to head in that direction * @return {Promise} when complete */ var go = function (dir, time) { return new Promise( function (resolve) { motorLeft[dir](255); motorRight[dir](255); currentDirection = dir; setTimeout( function () { motorRight.stop(); motorLeft.stop(); currentDirection = null; resolve(); },time); }); };

Create task blocks for common actions

Wrap them in promises to get around asnyc issues

Use promise chaining to create sequences of activities

Page 21: Node.js + Robots = Nodebots!

Integrate with well known libraries like socket-io// robot/socket.js !var socket = require('socket.io-client')(appServerAddress); !socket.on('connect', function() { control.queueUpCommand(0,'marker',['down']); control.queueUpCommand(1,'marker',['up']); socket.emit('IAMAROBOT'); }); !socket.on('command', function(cmd) { control.queueUpCommand(cmd.id, cmd.command, cmd.args); }); !// server.js !socket.on('IAMAROBOT', function () { doodlebot = socket; ! socket.emit('command',{ id: 'server', command: 'marker', args: ['down'] });

Page 22: Node.js + Robots = Nodebots!

//client.js !function onMouseDown(event) { if (points.length >= 2) { var coordinate = getCoordinate([event.clientX, event.clientY]); points.push(coordinate); var line = points.slice(-2); drawLine(line[0],line[1]); socket.emit('point',coordinate); } } !//server.js !socket.on('point', function(point) { console.log('point: ', point); points.push(point); socket.broadcast.emit('point', point); if (doodlebot) { generatePathCommands(socket.id, points.slice(-3)); } });

The drawing app

Page 23: Node.js + Robots = Nodebots!

function generatePathCommands(id, points) { if (points.length === 3) { var angle = findAngle(points[0],points[1],points[2]); angle = angle / 3.1415 * 360; //degrees! angle = angle - 180; //bearing! if (angle > 2) { doodlebot.emit('command',{ id: id, command: 'turn', args: [angle] }); } var distance = findDistance(points[1],points[2]); if (distance > 0.05) { doodlebot.emit('command',{ id: id, command: 'go', args:['fwd',distance*50] }); } } }

Page 24: Node.js + Robots = Nodebots!
Page 25: Node.js + Robots = Nodebots!

You now have an internet enabled smart robot that

draws poorly!

Page 26: Node.js + Robots = Nodebots!

Pitfalls• The Edison has very odd behaviour and issues

• Stick to the supported platforms list on Johnny five

• The Dagu Mini Driver board is cheap for a reason (Firmata firmware doesn’t fit without modification)

!

!

• Even trying to stay super cheap, robots are expensive projects

Page 27: Node.js + Robots = Nodebots!

Total Cost CAD with Tax, Shipping, Duty, Fees, etc

Intel Edison W/ Mini Breakout Board

Amazon Canada (Prime) $96.31

USB Micro B OTG to Micro A

Amazon Canada (Prime) $7.71

DoodleBot Robot Kit HobbyKing $76.50

9V Battery pack + batteries DigiKey $23.51

Total $204.03

Page 28: Node.js + Robots = Nodebots!

/* * __ __ ______ _____ ______ ______ ______ ______ * /\ "-.\ \ /\ __ \ /\ __-. /\ ___\ /\ == \ /\ __ \ /\__ _\ * \ \ \-. \ \ \ \/\ \ \ \ \/\ \ \ \ __\ \ \ __< \ \ \/\ \ \/_/\ \/ * \ \_\\"\_\ \ \_____\ \ \____- \ \_____\ \ \_____\ \ \_____\ \ \_\ * \/_/ \/_/ \/_____/ \/____/ \/_____/ \/_____/ \/_____/ \/_/ * `` ``` `. * .-. .` ..-.` `:-/+` .` * `-``:-` ` -` .+``-/:/. /+` - * :. `+/ /. .- `+: `/- `+- - * .`.--./ -o. :// :. `//. // .` *`:+-::-.-. `o/:-`/ ``` :- .//.`:/` `. *././o-o- .-.:+``.. `-` ./ .::/` `.` *`-.:o+-/. `+o. `. ::` ``.` * :o/o/:`..-++. :--.` `.` :: .-.`` ```` * `/o+/--.`:/. /-/``.-.` `+o. `` `..--..` `.````````` *`/:+/:--:.` `/. .-` `.--` `/-` ./. ::``` *.++.-://` `/` -:::.` `-:-``+: `/- `-- *.+/: /: -: -/ `-::.` `:+/ `/- .:. *-:/-`o :- -/ `-:-` ...--` `/-`:-` *`::`-+ `-.. :. :: .-:-` `--.` .+:--` *-:.`.o:/:+::/. `:- ` `-:-` `.-.` `..` *`+/`.o:``+`// `/- :o/` `---. -. * ./://. +``o `:. :+:` `--.`:. * ./. `` +` :.-` `-. -/: `.-: * .-` :. ``` :/-:-.` `sso ./. * -: ` ...` `--/-:-.` .-` :+. .` ``` * -+- .-. `... `.--::.+.` /+` `:..:- `/-:. * `//` `---` `--.` `.://+: `//` .. :- `.-: * :+- .-..` `---. `-:` `+: .-``:/-`. -- * `//` ``...``.--.` .:. ./::::://:/:` *`/- ```````.```-/: `/+/.--....://` * ` `.` ````.-//o `//--//:::..+/ * `/-...` ````-: `.---:-.-//:.` :/` * ./. .....` --.--.-..-`..:/-`-/` * -/` `..` `...` :. .. .`-``-`..:/./` * ::` ```.. ``.`` `:/ ..-``.`:`.. -`--` * .- ``.-` ``-. ``-::``-.- -..- -`-.:. * .-` `:-` `..-+..`:`/`-: :. -.-` * `` -:` .--.-/- ./ /../ .-`-.` * -. `:://:::` /- :.`/`.::. * `-` ./-:/--/` ./` :.`:--` * `. -:`.--./.``:-.--:-`` * `:- ```......`` * .:. */

Totally Worth

It

! @mike0x41

Thank you for coming out