Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Nodebotsday

 Nodebotsday

Overview on Nodebots

mulderp

July 27, 2015
Tweet

More Decks by mulderp

Other Decks in Technology

Transcript

  1. Nodebots!
    Day!
    2015
    Munich Maker Lab

    View Slide

  2. Robots + JavaScript
    http://slides.millermedeiros.com/braziljs/ast/#/

    View Slide

  3. JavaScript …
    … is object-oriented
    … has a large ecosystem
    … runs on hardware

    View Slide

  4. Basic JavaScript
    var robot = {!
    color: ‘yellow’,!
    sound: ‘hello’,!
    !
    blink: function() {!
    // action!
    }!
    }!
    !
    robot.blink();
    basic data
    functions
    as data

    View Slide

  5. event-loop
    var repl = require('repl');!
    !
    var robot = {!
    blink: function(color, delay) {!
    setTimeout(function() {!
    console.log('** ' + color + ' **');!
    }, delay)!
    }!
    }!
    !
    var local = repl.start('command> ');!
    local.context.robot = robot;!

    View Slide

  6. events everywhere
    can make it difficult to work with
    “time critical” systems

    View Slide

  7. talking to
    robots

    View Slide

  8. node-serialport

    View Slide

  9. HOST
    Arduino
    Firmata!
    Protocol
    Firmata
    JS

    View Slide

  10. $ npm install --save firmata!
    !
    // The Firmata protocol provides a simple protocol to an embedded system!
    var firmata = require('firmata');!
    !
    // Change this line to the serial port of a device!
    var modem = '/dev/cu.usbserial-DJ008CEA';!
    !
    // Main part!
    var board = new firmata.Board(modem, function(err){!
    console.log('connected: ' + modem);!
    var ledOn = true;!
    !
    // Configure pin 13 as output!
    board.pinMode(13, board.MODES.OUTPUT);!
    !
    !
    // Blink the LED!
    setInterval(function() {!
    !
    if (ledOn) {!
    console.log('ON');!
    board.digitalWrite(13, board.HIGH);!
    } else {!
    console.log('OFF');!
    board.digitalWrite(13, board.LOW);!
    }!
    !
    ledOn = !ledOn;!
    !
    }, 500);!
    });!

    View Slide

  11. https://github.com/rwaldron/johnny-five
    Johnny-5 API
    Supports RPi, Edison, Galileo with “plugins”, e.g.
    https://github.com/nebrius/raspi-io

    View Slide

  12. Basic LED
    var five = require("johnny-five");!
    var board = new five.Board();!
    !
    board.on("ready", function() {!
    !
    var led = new five.Led(13);!
    !
    // "blink" the led in 500ms on-off phase periods!
    led.blink(500);!
    });
    https://github.com/rwaldron/johnny-five/blob/master/eg/led-blink.js

    View Slide

  13. Nodebot
    var five, temporal, Nodebot;!
    !
    five = require("johnny-five");!
    !
    temporal = require("temporal");!
    !
    five.Board().on("ready", function() {!
    !
    Nodebot = new five.Nodebot({!
    right: 10,!
    left: 11!
    });!
    !
    this.repl.inject({!
    n: Nodebot!
    });!
    !
    })!
    !
    https://github.com/rwaldron/johnny-five/blob/master/eg/nodebot.js

    View Slide

  14. Proximity
    var five = require("johnny-five");!
    var board = new five.Board();!
    !
    board.on("ready", function() {!
    var proximity = new five.Proximity({!
    controller: "SRF10"!
    });!
    !
    proximity.on("data", function() {!
    console.log(this.cm + "cm", this.in + "in");!
    });!
    !
    proximity.on("change", function() {!
    console.log("The obstruction has moved.");!
    });!
    });

    View Slide

  15. Direct I2C
    https://www.npmjs.com/package/i2c
    var i2c = require('i2c');!
    var address = 0x18;!
    var wire = new i2c(address, {device: '/dev/i2c-1'}); // point to your i2c address,
    debug provides REPL interface !
    !
    wire.scan(function(err, data) {!
    // result contains an array of addresses !
    });!
    !
    wire.writeByte(byte, function(err) {});!
    !
    wire.writeBytes(command, [byte0, byte1], function(err) {});!
    !
    wire.readByte(function(err, res) { // result is single byte }) !
    !
    wire.readBytes(command, length, function(err, res) {!
    // result contains a buffer of bytes !
    });!
    !
    wire.on('data', function(data) {!
    // result for continuous stream contains data buffer, address, length, timestamp !
    });!

    View Slide

  16. “full stack” JavaScript
    !
    - ui
    - server
    - hardware

    View Slide

  17. Server
    and adding sockets:
    https://github.com/lmccart/itp-creative-js/blob/master/Spring-2014/week6/04_socket_server/server.js
    var http = require('http');!
    var fs = require('fs');!
    var port = 3474;!
    !
    // we create the server here!
    http.createServer(function (req, res) {!
    // simple logging of an incoming request!
    console.log(req.url);!
    !
    // simple routing!
    if (req.url == '/') {!
    res.writeHead(200, {'Content-Type': ‘text/html'});!
    !
    // serving a file!
    res.end(fs.readFileSync('static/index.html'));!
    }!
    }).listen(port);!

    View Slide

  18. User Interfaces
    - processing library: http://p5js.org/reference
    - jQuery/Backbone/Angular/Ember/React

    View Slide

  19. https://github.com/rockbot/vektor

    View Slide

  20. nodecopter
    http://www.nodecopter.com/
    https://github.com/MobileRobotics-Ulaval/CollaborativeDroneLocalization

    View Slide

  21. projects + libs
    http://www.openrov.com/

    View Slide

  22. npm
    • world’s largest package management system
    • npm init
    • npm install —save

    View Slide

  23. JavaScript!
    inside

    View Slide