js-robotics-lightning-talk



js-robotics-lightning-talk

0 0


js-robotics-lightning-talk

Full-length version of the JS Robotics talk

On Github BrianGenisio / js-robotics-lightning-talk

JavaScript Robotics?

Not a bad idea

@BrianGenisio

Who is @BrianGenisio?

"Using JavaScript for hardware sounds like a really bad idea" -- Coworker

I was like...

He was like...

"Using a dynamic language for hardware just sounds dangerous" -- Coworker

I can fry my hardware in C/C++ just as easily

Programming in C/C++

I really just want to enjoy myself

JavaScript has Good Parts

JavaScript is Accesible

JavaScript is Interpereted

JavaScript is good at async/eventing

JavaScript is good for abstractions

Johnny-Five is in JavaScript

Different hardware, same shape

Open Source Hardware Projects

Arduino

Beagle Bone Black

Raspberry Pi

Galileo

Edison

Spark Core

Electronic Imp

Light Blue Bean

Pinocchio

But really, It's the Community

Lets Look

What can we do with Johnny-Five?

Layers of Abstraction

Control Some Servos

var five = require("johnny-five"),
    board = new five.Board()

board.on("ready", function() {
  var rightWheel = new five.Servo({pin: 11, type: "continuous"});
  var leftWheel = new five.Servo({pin: 7, type: "continuous"});

  rightWheel.cw();
  leftWheel.ccw();

  setTimeout(function() {
    rightWheel.ccw();
    leftWheel.cw();
  }, 1000);

  setTimeout(function() {
    rightWheel.stop();
    leftWheel.stop();
  }, 2000);

});

Servo Wiring

Expand to a Car

module.exports = Car;

function Car(five, rightPin, leftPin) {
  this.rightWheel = new five.Servo({pin: rightPin, type: "continuous"});
  this.leftWheel = new five.Servo({pin: leftPin, type: "continuous"});
}

Car.prototype.forward = function() {
  this.rightWheel.cw();
  this.leftWheel.ccw();
};

Car.prototype.reverse = function() {
  this.rightWheel.ccw();
  this.leftWheel.cw();
};

Car.prototype.right = function() {
  this.rightWheel.ccw();
  this.leftWheel.ccw();
};

Car.prototype.left = function() {
  this.rightWheel.cw();
  this.leftWheel.cw();
};

Car.prototype.stop = function() {
  this.rightWheel.stop();
  this.leftWheel.stop();
};

And Control the Car

var five = require("johnny-five"),
    Car = require("./car"),
    board = new five.Board(),
    stdin = process.openStdin(); 

require('tty').setRawMode(true);    

board.on("ready", function() {

  var car = new Car(five, 11, 7);

  var keyMap = {
    up:    "forward",
    down:  "reverse",
    left:  "left",
    right: "right",
    space: "stop"
  };

  stdin.on('keypress', function (chunk, key) {
    if(keyMap[key.name]) {
      car[keyMap[key.name]]();
    }
  });
});

Reflectance Arrays

Reflectance Array -- Left

Reflectance Array -- Right

Let the Car Drive

var five = require("johnny-five"),
    Car = require("./car"),
    board = new five.Board(); 

board.on("ready", function() {
  var car = new Car(five, 11, 7);

  var eyes = new five.IR.Reflect.Array({
    emitter: 13,
    pins: ["A0", "A1", "A2", "A3", "A4", "A5"],
    autoCalibrate: true
  });

  eyes
    .enable()
    .on("line", function(err, line) {
      if (line < 1000) {
          car.left();
      } else if (line > 4000) {
          car.right();
      } else {
          car.forward();
      }
    });
});

Use Motors Instead

module.exports = OmniBot;

function OmniBot(five) {
  var configs = five.Motor.SHIELD_CONFIGS.ADAFRUIT_V1;

  this.m1 = new five.Motor(configs.M1);
  this.m2 = new five.Motor(configs.M2);
  this.m3 = new five.Motor(configs.M3);
}

OmniBot.prototype.moveAngle = function(angle, magnitude) {
  var components = getComponents(angle, magnitude);
  this.move(components.vx, components.vy);
};

OmniBot.prototype.move = function(vx, vy) {
  var wheels = getVectors(vx, vy);

  moveWheel(this.m1, wheels.w1);
  moveWheel(this.m2, wheels.w2);
  moveWheel(this.m3, wheels.w3);
};

OmniBot.prototype.stop = function() {
  this.m1.stop();
  this.m2.stop();
  this.m3.stop();
};

function getComponents(angle, magnitude) {
  var vx = Math.cos(angle) * magnitude;
  var vy = Math.sin(angle) * magnitude;

  return {
    vx: vx,
    vy: vy
  }
}

function getVectors(vx, vy) {
  var c = Math.sqrt(3/2);

  return {
    w1: -vx,
    w2: 0.5 * vx - c * vy,
    w3: 0.5 * vx + c * vy
  }
}

function moveWheel(motor, magnitude) {
  var minValue = 0;

  if(magnitude < 0 ) {
    motor.rev(Math.abs(magnitude) + minValue);
  } else {
    motor.fwd(magnitude + minValue);
  }
}

Omnibot Wheels

Omnibot Vectors

Omnibot Shield

And Drive the OmniBot

var five = require("johnny-five"),
    board = new five.Board(),
    OmniBot = require("./omni-bot"),
    stdin = process.openStdin(); 

require('tty').setRawMode(true);    

board.on("ready", function() {

  var bot = new OmniBot(five, "M1", "M2", "M3");

  var keyMap = {
    up:    {angle: 0, mag: 255},
    down:  {angle: 180, mag: 255},
    left:  {angle: 270, mag: 255},
    right: {angle: 90, mag: 255},
    space: {angle: 0, mag: 0},
  };

  stdin.on('keypress', function (chunk, key) {
    if(keyMap[key.name]) {
      var command = keyMap[key.name];
      bot.moveAngle(command.angle, command.mag);
    }
  });
});

Some Components

Not Just Movement (LED)

var five = require("johnny-five"),
    board = new five.Board()

board.on("ready", function() {

  var led = five.Led(13);

  // led.on();
  // led.off();
  // led.brightness(100);
  // led.strobe();
  // led.pulse();
  // led.stop();

  this.repl.inject({
    led: led
  });

});

LED Wiring

Understanding PWM

Pulse Width Modulation

RGB

var five = require("johnny-five"),
    board = new five.Board()

board.on("ready", function() {

  var rgb = new five.Led.RGB({
    pins: [9, 10, 11],
    isAnode: true
  });

  // rgb.color("#FF0000"); // red
  // rgb.color("#00FF00"); // green
  // rgb.color("#0000FF"); // blue
  // rgb.color("#FF00FF"); // purple
  // rgb.off();
  // rgb.strobe();
  // rgb.stop();


  this.repl.inject({
    rgb: rgb
  });

});

Play a Song?

var five = require("johnny-five"),
    songs = require("j5-songs"),
    board = new five.Board();

board.on("ready", function() {

  var piezo = new five.Piezo(8);

  //piezo.tone(440, 500);

  //piezo.play({
  //  song: "C D F D A - A A A A G G G G - - C D F D G - G G G G F F F F - -",
  //  beats: 1 / 4,
  //  tempo: 100
  //});

  //piezo.play(songs.load('mario-intro'));
  piezo.play(songs.load('starwars-theme'));

  this.repl.inject({
    piezo: piezo
  });
});

Piezo Wiring

Not Just Arduino (Particle)

var five = require("johnny-five");
var Spark = require("spark-io");

var board = new five.Board({
  io: new Spark({
    token: "API_TOKEN",
    deviceId: "DEVICE_ID" 
  })
});

board.on("ready", function() {
  var led = new five.Led("D7");
  var motion = new five.Motion("D6");

  motion.on("motionstart", function() {
    led.on();
  });

  motion.on("motionend", function() {
    led.off();
  });
});

Particle Wiring

Particle Architecture

Kids Can Play Too

Tip of the Iceberg

JavaScript Robotics is a good idea

Learn More