O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Learning JavaScript Robotics

Book Description

Design, build, and program your own remarkable robots with JavaScript and open source hardware

About This Book

  • Learn how to leverage Johnny-Five's Read, Eval, Print Loop, and Event API to write robot code with JavaScript
  • Unlock a world of exciting possibilities by hooking your JavaScript-programmed robots up to the internet and using external data and APIs
  • Move your project code from the Arduino Uno to a multitude of other robotics platforms

Who This Book Is For

If you've worked with Arduino before or are new to electronics and would like to try writing sketches in JavaScript, then this book is for you! Basic knowledge of JavaScript and Node.js will help you get the most out of this book.

What You Will Learn

  • Familiarise yourself with Johnny-Five Read, Eval, and Print Loop (REPL) to modify and debug robotics code in real time
  • Build robots with basic output devices to create projects that light up, make noise, and more
  • Create projects with complex output devices, and employ the Johnny-Five API to simplify the use of components that require complex interfaces, such as I2C
  • Make use of sensors and input devices to allow your robotics projects to survey the world around them and accept input from users
  • Use the Sensor and Motor objects to make it much easier to move your robotics projects
  • Learn about the Animation API that will allow you to program complex movements using timing and key frames
  • Bring in other devices to your Johnny-Five projects, such as USB devices and remotes
  • Connect your Johnny-Five projects to external APIs and create your own Internet of Things!

In Detail

There has been a rapid rise in the use of JavaScript in recent times in a variety of applications, and JavaScript robotics has seen a rise in popularity too. Johnny-Five is a framework that gives NodeBots a consistent API and platform across several hardware systems.

This book walks you through basic robotics projects including the physical hardware builds and the JavaScript code for them. You'll delve into the concepts of Johnny-Five and JS robotics.

You'll learn about various components such as Digital GPIO pins, PWM output pins, Sensors, servos, and motors to be used with Johnny-Five along with some advanced components such as I2C, and SPI. You will learn to connect your Johnny-Five robots to internet services and other NodeBots to form networks. By the end of this book, you will have explored the benefits of the Johnny-Five framework and the many devices it unlocks.

Style and approach

This step-by-step guide to the Johnny-Five ecosystem is explained in a conversational style, packed with examples and tips. Each chapter also explores the Johnny-Five documentation to enable you to start exploring the API on your own.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Learning JavaScript Robotics
    1. Table of Contents
    2. Learning JavaScript Robotics
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    8. 1. Getting Started with JS Robotics
      1. Understanding JS Robotics, NodeBots, and Johnny-Five
        1. What a NodeBot is, and other basic vocabulary
        2. Johnny-Five and the NodeBot revolution
        3. What we'll be using in this book
      2. Setting up your development environment
        1. Installing Node.JS
        2. Setting up your project and installing Johnny-Five
        3. Connecting your Microcontroller and installing Firmata
      3. Hello, World! – Blinking an onboard LED
        1. Writing the Johnny-Five script
        2. Running the script
      4. Summary
    9. 2. Working with Johnny-Five
      1. What you'll need for this chapter
      2. How a Johnny-Five program works
        1. Objects, functions, and events
        2. Going over our blink-LED script
      3. Understanding events in Johnny-Five
        1. Why events?
      4. Wiring up an external LED
        1. Setting up the hardware
      5. Using the Read-Eval-Print-Loop (REPL)
        1. Making components available to the REPL
        2. Using the REPL
      6. Summary
    10. 3. Using Digital and PWM Output Pins
      1. What you'll need for this chapter
      2. How GPIO pins work
        1. Digital output pins
        2. PWM output pins
        3. How to tell the difference between Digital and PWM pins
      3. Multiple pins with several LEDs
        1. Setting up the hardware for the project
        2. Writing the script for the project
        3. Exploring more about LED objects in Johnny-Five
      4. Using PWM pins with the Piezo element
        1. Setting up the hardware
        2. Writing the script
        3. What's going on with the pin?
        4. Exploring the Piezo API
      5. Summary
    11. 4. Using Specialized Output Devices
      1. What you'll need for this chapter
      2. Outputs that require multiple pins
        1. Inter-Integrated Circuits (I2C)
      3. Checking compatibility with Johnny-Five
      4. Obtaining documentation, wiring diagrams, and so on
      5. Project – character LCD display
        1. Wiring up – I2C LCDs
        2. The code
          1. The I2C version
          2. The non-I2C version
        3. Running the code
      6. Summary
    12. 5. Using Input Devices and Sensors
      1. What you'll need for this chapter
      2. How analog input pins work
      3. Johnny-Five sensor events
      4. Using basic inputs – buttons and potentiometers
        1. Wiring up our button and LED
        2. Coding button-led.js
        3. Wiring the potentiometer and the LED
        4. Coding our dimmer switch
      5. Using sensors – Light and Temperature
        1. Wiring up our photocell
        2. Coding our photocell example
          1. barcli
        3. Coding everything together
        4. Wiring up the temperature sensor
        5. Coding our temperature example
      6. Summary
    13. 6. Moving Your Bot
      1. What you'll need for this chapter
      2. The different kinds of servos and motors
        1. Defining motors and servos
        2. Things to keep in mind
        3. Types of motors
        4. Types of servos
        5. Do I use a servo or a motor?
        6. Servo and motor controllers
        7. Motor and servo shields
      3. Special concerns when using motors and servos
        1. Power concerns
        2. Tethering and cables
      4. Wiring up servos and motors
        1. Wiring up servos
        2. Wiring up motors
      5. Creating a project with a motor and using the REPL
        1. Exploring the motor API
      6. Creating a project with a servo and a sensor
        1. Exploring the servo API with the REPL
      7. Summary
    14. 7. Advanced Movement with the Animation Library
      1. What you'll need for this chapter
      2. What is the Animation API?
        1. Why do we need an Animation API?
        2. Why animation?
      3. Looking at the Animation API
        1. Learning the terminology
        2. The difference between .to() and the Animation API
        3. Using the ServoArray object
        4. Project – wiring up three servos and creating an array
      4. Writing Servo Animations
        1. Writing keyframes
          1. The keyframe object
          2. Keyframe shorthand
          3. Examples of writing keyframes
        2. Writing segments
          1. Segment options
            1. Multiple servos in one segment
          2. Examples of writing segments
        3. The Animation object
        4. Project – animating our servo array
      5. Animation events
        1. Building a servo array with an informative LCD readout
      6. Summary
    15. 8. Advanced Components – SPI, I2C, and Other Devices
      1. What you'll need for this chapter
      2. Why do we need the I2C and SPI protocols?
      3. Exploring SPI (Serial Peripheral Interface) devices
        1. How SPI works
        2. How Johnny-Five does SPI
        3. Benefits and drawbacks of SPI
        4. Building with an SPI device – an LED matrix
          1. The build
          2. The API
            1. Formatting data for the LED matrix
            2. The Drawing functions
            3. The Code
      4. Exploring I2C devices
        1. How I2C works
          1. Pins used by I2C
          2. How I2C devices send and receive data
        2. Benefits and drawbacks of I2C
          1. Benefits
          2. Drawbacks
        3. Building with an I2C device – Accelerometer
          1. Wiring up our accelerometer
          2. Coding up our example
      5. External Devices
        1. Why External Devices?
          1. node-serialport
          2. node-hid
        2. Building a USB gamepad
          1. The hardware
          2. The node-gamepad API
          3. The code
      6. Summary
    16. 9. Connecting NodeBots to the World, and Where to Go Next
      1. What you'll need for this chapter
      2. Connecting NodeBots to the Web
        1. It's just a Node Server!
        2. Using Twilio
        3. Building the WeatherBot
        4. Using the TextBot
      3. Johnny-Five and the wide world of microcontrollers
        1. Moving our WeatherBot to the Particle Photon
        2. Tethering and Johnny-Five
      4. Other JS libraries and platforms
        1. Espruino
        2. Tessel
        3. Cylon.js
        4. JerryScript
        5. Tiny Linux computers
        6. Vendor libraries
      5. Where to go from here
    17. Index