Learning JavaScript Robotics Table of Contents Learning JavaScript Robotics Credits About the Author About the Reviewers www.PacktPub.com Support files, eBooks, discount offers, and more Why subscribe? Free access for Packt account holders Preface What this book covers What you need for this book Who this book is for Conventions Reader feedback Customer support Downloading the example code Downloading the color images of this book Errata Piracy Questions Getting Started with JS Robotics Understanding JS Robotics, NodeBots, and Johnny-Five What a NodeBot is, and other basic vocabulary Johnny-Five and the NodeBot revolution What we'll be using in this book Setting up your development environment Installing Node.JS Setting up your project and installing Johnny-Five Connecting your Microcontroller and installing Firmata Hello, World! – Blinking an onboard LED Writing the Johnny-Five script Running the script Summary Working with Johnny-Five What you'll need for this chapter How a Johnny-Five program works Objects, functions, and events Going over our blink-LED script Understanding events in Johnny-Five Why events? Wiring up an external LED Setting up the hardware Using the Read-Eval-Print-Loop (REPL) Making components available to the REPL Using the REPL Summary Using Digital and PWM Output Pins What you'll need for this chapter How GPIO pins work Digital output pins PWM output pins How to tell the difference between Digital and PWM pins Multiple pins with several LEDs Setting up the hardware for the project Writing the script for the project Exploring more about LED objects in Johnny-Five Using PWM pins with the Piezo element Setting up the hardware Writing the script What's going on with the pin? Exploring the Piezo API Summary Using Specialized Output Devices What you'll need for this chapter Outputs that require multiple pins Inter-Integrated Circuits (I2C) Checking compatibility with Johnny-Five Obtaining documentation, wiring diagrams, and so on Project – character LCD display Wiring up – I2C LCDs The code The I2C version The non-I2C version Running the code Summary Using Input Devices and Sensors What you'll need for this chapter How analog input pins work Johnny-Five sensor events Using basic inputs – buttons and potentiometers Wiring up our button and LED Coding button-led.js Wiring the potentiometer and the LED Coding our dimmer switch Using sensors – Light and Temperature Wiring up our photocell Coding our photocell example barcli Coding everything together Wiring up the temperature sensor Coding our temperature example Summary Moving Your Bot What you'll need for this chapter The different kinds of servos and motors Defining motors and servos Things to keep in mind Types of motors Types of servos Do I use a servo or a motor? Servo and motor controllers Motor and servo shields Special concerns when using motors and servos Power concerns Tethering and cables Wiring up servos and motors Wiring up servos Wiring up motors Creating a project with a motor and using the REPL Exploring the motor API Creating a project with a servo and a sensor Exploring the servo API with the REPL Summary Advanced Movement with the Animation Library What you'll need for this chapter What is the Animation API? Why do we need an Animation API? Why animation? Looking at the Animation API Learning the terminology The difference between to() and the Animation API Using the ServoArray object Project – wiring up three servos and creating an array Writing Servo Animations Writing keyframes The keyframe object Keyframe shorthand Examples of writing keyframes Writing segments Segment options Multiple servos in one segment Examples of writing segments The Animation object Project – animating our servo array Animation events Building a servo array with an informative LCD readout Summary Advanced Components – SPI, I2C, and Other Devices What you'll need for this chapter Why do we need the I2C and SPI protocols? Exploring SPI (Serial Peripheral Interface) devices How SPI works How Johnny-Five does SPI Benefits and drawbacks of SPI Building with an SPI device – an LED matrix The build The API Formatting data for the LED matrix The Drawing functions The Code Exploring I2C devices How I2C works Pins used by I2C How I2C devices send and receive data Benefits and drawbacks of I2C Benefits Drawbacks Building with an I2C device – Accelerometer Wiring up our accelerometer Coding up our example External Devices Why External Devices? node-serialport node-hid Building a USB gamepad The hardware The node-gamepad API The code Summary Connecting NodeBots to the World, and Where to Go Next What you'll need for this chapter Connecting NodeBots to the Web It's just a Node Server! Using Twilio Building the WeatherBot Using the TextBot Johnny-Five and the wide world of microcontrollers Moving our WeatherBot to the Particle Photon Tethering and Johnny-Five Other JS libraries and platforms Espruino Tessel Cylon.js JerryScript Tiny Linux computers Vendor libraries Where to go from here Index Learning JavaScript Robotics Learning JavaScript Robotics Copyright © 2015 Packt Publishing All rights reserved No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews Every effort has been made in the preparation of this book to ensure the accuracy of the information presented However, the information contained in this book is sold without warranty, either express or implied Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals However, Packt Publishing cannot guarantee the accuracy of this information First published: November 2015 Production reference: 1231115 Published by Packt Publishing Ltd Livery Place 35 Livery Street Birmingham B3 2PB, UK ISBN 978-1-78588-334-7 www.packtpub.com Credits Author Kassandra Perch Reviewers Chris S Crawford Tomomi Imura Commissioning Editor Neil Alexander Acquisition Editor Aaron Lazar Content Development Editor Mayur Pawanikar Technical Editor Menza Mathew Copy Editor Kausambhi Majumdar Project Coordinator Nidhi Joshi Proofreader Safis Editing Indexer Rekha Nair Production Coordinator Manu Joseph I I2C devices exploring / Exploring I2C devices working / How I2C works pins / Pins used by I2C data, sending / How I2C devices send and receive data data, receiving / How I2C devices send and receive data drawbacks / Benefits and drawbacks of I2C, Benefits, Drawbacks benefits / Benefits used, for building accelerometer / Building with an I2C device – Accelerometer I2C interface URL / What you'll need for this chapter I2C protocol need for / Why do we need the I2C and SPI protocols? inject method / Making components available to the REPL Inter-Integrated Circuits (i2c) about / Inter-Integrated Circuits (I2C) Inverse Kinematic (IK) / The keyframe object J JavaScript robotics about / Understanding JS Robotics, NodeBots, and Johnny-Five development environment / Setting up your development environment JerryScript about / JerryScript URL / JerryScript Johnny-Five about / Understanding JS Robotics, NodeBots, and Johnny-Five, Johnny-Five and the NodeBot revolution URL / Johnny-Five and the NodeBot revolution, Running the script, Checking compatibility with Johnny-Five, Obtaining documentation, wiring diagrams, and so on, What you'll need for this chapter, Servo and motor controllers, Motor and servo shields and NodeBot / Johnny-Five and the NodeBot revolution installing / Setting up your project and installing Johnny-Five requisites / What you'll need for this chapter abstraction / Multiple pins with several LEDs Led objects, exploring / Exploring more about LED objects in Johnny-Five compatibility, checking with / Checking compatibility with Johnny-Five documentation, URL / What you'll need for this chapter sensor events / Johnny-Five sensor events SPI device, using / How Johnny-Five does SPI microcontrollers / Johnny-Five and the wide world of microcontrollers tethering / Tethering and Johnny-Five Johnny-Five program working / How a Johnny-Five program works objects / Objects, functions, and events events / Objects, functions, and events, Understanding events in Johnny-Five functions / Objects, functions, and events blink-LED script / Going over our blink-LED script JS libraries and platforms about / Other JS libraries and platforms Espruino / Espruino Tessel / Tessel Cylon.js / Cylon.js JerryScript / JerryScript tiny Linux computers / Tiny Linux computers vendor libraries / Vendor libraries K key frames, servo animations writing / Writing keyframes, The keyframe object degrees / The keyframe object step / The keyframe object easing / The keyframe object copyDegrees / The keyframe object copyFrame / The keyframe object position / The keyframe object shorthand / Keyframe shorthand non-integer values / Keyframe shorthand writing, examples / Examples of writing keyframes L LED and button, wiring / Wiring up our button and LED and potentiometer, wiring / Wiring the potentiometer and the LED, Coding our dimmer switch Led.RGB class / Obtaining documentation, wiring diagrams, and so on LED matrix building, with SPI device / Building with an SPI device – an LED matrix pin matchup / The build API / The API on([index]) method / The API off([index]) method / The API clear([index]) method / The API brightness([index], brightness) method / The API led([index], row, col, state) method / The API LED matrix API about / The API data, formatting / Formatting data for the LED matrix drawing functions / The Drawing functions code / The Code Led object API / Multiple pins with several LEDs M Master In Slave Out (MISO) / How SPI works Master Out Slave In (MOSI) / How SPI works methods, project on() / Multiple pins with several LEDs off() / Multiple pins with several LEDs blink(time) / Multiple pins with several LEDs pulse(time) / Multiple pins with several LEDs fade(brightness, ms) / Multiple pins with several LEDs stop() / Multiple pins with several LEDs microcontroller about / What a NodeBot is, and other basic vocabulary connecting / Connecting your Microcontroller and installing Firmata motors types / The different kinds of servos and motors, Types of motors defining / Defining motors and servos considerations / Things to keep in mind selecting / Do I use a servo or a motor? controllers / Servo and motor controllers shields / Motor and servo shields using, concerns / Special concerns when using motors and servos power concerns / Power concerns tethering / Tethering and cables cables / Tethering and cables wiring up / Wiring up servos and motors, Wiring up motors used, for creating project / Creating a project with a motor and using the REPL REPL, using / Creating a project with a motor and using the REPL motor API, exploring / Exploring the motor API N node-serialport / Understanding JS Robotics, NodeBots, and Johnny-Five Node.js URL / Installing Node.JS NodeBot about / What a NodeBot is, and other basic vocabulary NodeBots connecting, to Web / Connecting NodeBots to the Web Node Server / It's just a Node Server! Twilio, using / Using Twilio WeatherBot, building / Building the WeatherBot TextBot, using / Using the TextBot used, for fixing issues / Where to go from here Node Package Manager (NPM) / Installing Node.JS non-integer values null / Keyframe shorthand false / Keyframe shorthand O objects, Johnny-Five program / Objects, functions, and events onboard LED blinking / Hello, World! – Blinking an onboard LED Johnny-Five script, writing / Writing the Johnny-Five script Johnny-Five script, running / Running the script outputs devices multiple pins, requiring / Outputs that require multiple pins P Particle Photon URL / What you'll need for this chapter WeatherBot, moving to / Moving our WeatherBot to the Particle Photon photocell about / Using sensors – Light and Temperature wiring / Wiring up our photocell example, coding / Coding our photocell example barcli / barcli summarizing / Coding everything together piezo.off() method / Exploring the Piezo API Piezo element PWM pins, using with / Using PWM pins with the Piezo element potentiometer and LED, wiring / Wiring the potentiometer and the LED, Coding our dimmer switch prerequisites / What you'll need for this chapter, What you'll need for this chapter about / What you'll need for this chapter, What you'll need for this chapter, What you'll need for this chapter button / What you'll need for this chapter rotating potentiometer / What you'll need for this chapter light sensor / What you'll need for this chapter temperature sensor / What you'll need for this chapter LM35 / What you'll need for this chapter microcontroller / What you'll need for this chapter for creating complex movements / What you'll need for this chapter Particle Photon / What you'll need for this chapter project methods / Multiple pins with several LEDs hardware, setting up / Setting up the hardware for the project script, writing / Writing the script for the project Led objects, in Johnny-Five / Exploring more about LED objects in Johnny-Five creating, with servo and sensor / Creating a project with a servo and a sensor pulse() method / Digital output pins PWM pins about / PWM output pins and digital output pins, differentiating between / How to tell the difference between Digital and PWM pins using, with Piezo element / Using PWM pins with the Piezo element PWM pins, using with Piezo element about / Using PWM pins with the Piezo element hardware, setting up / Setting up the hardware script, writing / Writing the script working / What's going on with the pin? Piezo API, exploring / Exploring the Piezo API R REPL about / Using the Read-Eval-Print-Loop (REPL) components, making available / Making components available to the REPL using / Using the REPL using, with motor / Creating a project with a motor and using the REPL using, with sensor / Exploring the servo API with the REPL S SainSmart / What we'll be using in this book segment options, servo animations target / Segment options easing / Segment options loop / Segment options metronomic / Segment options progress / Segment options currentSpeed / Segment options fps / Segment options multiple servos, in one segment / Multiple servos in one segment segments, servo animations writing / Writing segments options / Segment options writing, examples / Examples of writing segments sensor events, Johnny-Five / Johnny-Five sensor events Serial Clock Line (SCL) / Inter-Integrated Circuits (I2C) Serial Data Line (SDA) / Inter-Integrated Circuits (I2C) servo animations writing / Writing Servo Animations key frames, writing / Writing keyframes, The keyframe object segments, writing / Writing segments animation object / The Animation object servo array, animating / Project – animating our servo array servo array building, with informative LCD readout / Building a servo array with an informative LCD readout servos about / The different kinds of servos and motors defining / Defining motors and servos considerations / Things to keep in mind types / Types of servos selecting / Do I use a servo or a motor? controllers / Servo and motor controllers shields / Motor and servo shields using, concerns / Special concerns when using motors and servos power concerns / Power concerns tethering / Tethering and cables cables / Tethering and cables wiring up / Wiring up servos and sensor, used for creating project / Creating a project with a servo and a sensor servo API, exploring with sensor / Exploring the servo API with the REPL Slave Select (SS) / How SPI works SparkFun URL / Power concerns Sparkfun URL / What we'll be using in this book SPI device exploring / Exploring SPI (Serial Peripheral Interface) devices working / How SPI works features / How SPI works used, by Johnny-Five / How Johnny-Five does SPI benefits / Benefits and drawbacks of SPI drawbacks / Benefits and drawbacks of SPI used, for building LED matrix / Building with an SPI device – an LED matrix SPI protocol need for / Why do we need the I2C and SPI protocols? T to() method and Animation API, differentiating between / The difference between to() and the Animation API temperature sensor using / Using sensors – Light and Temperature wiring / Wiring up the temperature sensor example, coding / Coding our temperature example Tessel about / Tessel Tessel 2 / Tessel TextBot using / Using the TextBot this.repl.inject() function / Making components available to the REPL Twilio URL / Using Twilio using / Using Twilio U USB gamepad building / Building a USB gamepad hardware / The hardware node-gamepad API / The node-gamepad API code, adding to controller.js file / The code V VoodooSpark URL / Johnny-Five and the wide world of microcontrollers W WeatherBot building / Building the WeatherBot moving, to Particle Photon / Moving our WeatherBot to the Particle Photon Web NodeBots, connecting to / Connecting NodeBots to the Web wiring diagrams obtaining / Obtaining documentation, wiring diagrams, and so on