Learning JavaScript Robotics - Sample Chapter

14
Community Experience Distilled Design, build, and program your own remarkable robots with JavaScript and open source hardware Learning JavaScript Robotics Kassandra Perch Free Sample

description

Chapter No. 1 Getting Started with JS RoboticsDesign, build, and program your own remarkable robots with JavaScript and open source hardwareFor more information: http://bit.ly/1T2WqyM

Transcript of Learning JavaScript Robotics - Sample Chapter

Page 1: Learning JavaScript Robotics - Sample Chapter

C o m m u n i t y E x p e r i e n c e D i s t i l l e d

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

Learning JavaScript RoboticsK

assandra Perch

Learning JavaScript Robotics

There has been a rapid rise in the use of JavaScript in recent times for a variety of applications and JavaScript robotics has also seen a rise in popularity. Johnny-Five is a framework that gives NodeBots a consistent API 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 JavaScript 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 that use I2C and SPI. You will learn to connect your Johnny-Five robots to internet services. By the end of this book, you will have explored the benefi ts of the Johnny-Five framework and the many devices it unlocks.

Who this book is written forIf 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.

$ 29.99 US£ 19.99 UK

Prices do not include local sales tax or VAT where applicable

Kassandra Perch

What you will learn from this book

Familiarize yourself with Johnny-Five Read-Eval-Print-Loop (REPL)

Build robots with basic output devices

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

Use the servo and motor objects to make it much easier to move your robotics projects

Learn about the Animation API which allows you to program complex movements using timing and key frames

Bring other devices into your Johnny-Five projects, such as USB devices

Connect your Johnny-Five projects to external APIs and create your own Internet of Things!

Learning JavaScript Robotics

P U B L I S H I N GP U B L I S H I N G

community experience dist i l led

Visit www.PacktPub.com for books, eBooks, code, downloads, and PacktLib.

Free Sample

Page 2: Learning JavaScript Robotics - Sample Chapter

In this package, you will find: • The author biography • A preview chapter from the book, Chapter 1 'Getting Started with JS Robotics' • A synopsis of the book’s content • More information on Learning JavaScript Robotics

Page 3: Learning JavaScript Robotics - Sample Chapter

About the Author

Kassandra Perch is an open web developer and supporter. She began as a frontend developer and moved to server-side with the advent of Node.js and was especially enthralled by the advance of the NodeBots community. She travels the world speaking at conferences about NodeBots and the fantastic community around them. When she's not building bots, she's knitting, sewing, sculpting, or playing video games with her cats.

Page 4: Learning JavaScript Robotics - Sample Chapter

PrefaceHello! Welcome to Learning JavaScript Robotics. In this book, you'll learn how to write code for Arduino and other robotics platforms in JavaScript using Johnny-Five. We'll cover the basics of Johnny-Five, input and output devices, and even movement devices such as servos and motors. Finally, we'll cover how to connect your bots to the Internet and move your Johnny-Five code between different platforms.

What this book coversChapter 1, Getting Started with JS Robotics, will get you started by setting up an Arduino Uno and exploring the world of NodeBots.

Chapter 2, Working with Johnny-Five, covers the basics of Johnny-Five, including Read-Eval-Print-Loop (REPL), and we will build our fi rst project.

Chapter 3, Using Digital and PWM Output Pins, covers basic output devices, using both digital and PWM pins.

Chapter 4, Using Specialized Output Devices, covers specialized outputs that use one or multiple pins.

Chapter 5, Using Input Devices and Sensors, covers input devices using analog and GPIO pins.

Chapter 6, Moving Your Bot, covers basic servo and motor usage with Johnny-Five.

Chapter 7, Advanced Movement with the Animation Library, covers the Animation Library and how to create advanced movement schemes for your NodeBots.

Chapter 8, Advanced Components – SPI, I2C, and Other Devices, covers the use of I2C, SPI, and other advanced components with Johnny-Five.

Page 5: Learning JavaScript Robotics - Sample Chapter

Preface

Chapter 9, Connecting NodeBots to the World, and Where to Go Next, covers how to connect your NodeBots to the Internet and use Johnny-Five code with platforms other than Arduino.

Page 6: Learning JavaScript Robotics - Sample Chapter

Chapter 1

[ 1 ]

Getting Started with JS Robotics

Welcome to the world of JavaScript robotics! Let's explore how easy it is to get started with writing robotics programs in JavaScript using Arduino and Johnny-Five.

In this chapter, we will do the following:

• Explore JS Robotics, NodeBots, and Johnny-Five• Set up our development environment• Blink an on-board LED

Understanding JS Robotics, NodeBots, and Johnny-FiveJavaScript as a robotics language truly began a few years ago with the creation of node-serialport—an NPM module written by Chris Williams. This module allow s Node.JS to communicate with devices over a serial connection; this can include the typical serial connections from older computers, or the USB and Bluetooth connections that we use every day. What exactly is a NodeBot though, and how do we get started with using them with Johnny-Five?

Page 7: Learning JavaScript Robotics - Sample Chapter

Getting Started with JS Robotics

[ 2 ]

What a NodeBot is, and other basic vocabularyA NodeBot is any piece of hardware that is controlled using JavaScript and/or Node.JS. This can encompass a wide variety of projects; there are hundreds of ways to create a NodeBot. In this book, we are going to use the Johnny-Five library, an open source project created by Rick Waldron.

For those readers who are new to robotics, a microcontroller is a small computer that contains a processor, memory, and input/output pins. This serves as the brain of our project—our programs will communicate with or will be loaded onto this microcontroller. Microcontrollers come in many shapes and sizes, and with multiple capabilities.

We're going to use a microcontroller for our projects. What microcontroller should you use? Luckily, our use of Johnny-Five means that we can choose from a large array of different microcontrollers and still write the same code as you'll see in this book!

What exactly is Johnny-Five, and how does it make our lives easier?

Johnny-Five and the NodeBot revolutionJohnny-Five (https://johnny-five.io) is an open source robotics library for Node.JS. It was created by Rick Waldron and has a thriving community of contributors and supporters. This module has been known to work on Windows, Mac, and Linux computers without any issues at the time of writing this book using Node.JS version 4.x.

Johnny-Five was built on top of node-serialport and allows us to write JavaScript applications that communicate with different microcontrollers using different types of connection. For some microcontrollers, such as Arduino-compatible boards, Johnny-Five uses a serial connection. For some newer boards, Johnny-Five emulates this serial connection over an Internet service!

The capability of Johnny-Five to use multiple board types is implemented using its wrapper system. Once the core system is installed, you can install a wrapper for your particular microcontroller, and the APIs will remain the same. This is a powerful concept—you can write code for one platform and quickly move it to another without having to change it.

Page 8: Learning JavaScript Robotics - Sample Chapter

Chapter 1

[ 3 ]

What we'll be using in this bookFor the examples in this book, we'll use an Arduino Uno board. You can get these boards from sites such as Adafruit (www.adafruit.com), SparkFun (www.sparkfun.com), and so on. You can also use a board that is Arduino Uno-compatible. SainSmart, for instance, sells Uno-like boards that will work fi ne for our purposes. For this chapter, you'll need the board itself and a USB cable for it.

In later chapters, we'll be using other components—there will be a table in each chapter with an accessible list of materials for the projects within.

Setting up your development environmentNow that we've covered the basic ideas, we're going to set up the development environment for our fi rst project. All the software used here worked on Windows, Mac, and Linux desktop computers at the time of writing this book.

Installing Node.JSIf you don't have Node.JS already installed, you can download an installer for your platform from nodejs.org. This installer will also install NPM or Node Package Manager, which will be used to manage the rest of the software that we'll be using.

Run the installer on your machine, which may require a restart. After this, open up your terminal application and run the following command:

node –-version

The output from this command should be 4.x.x, where x are integers.

Setting up your project and installing Johnny-FiveIn your terminal, create a folder for your project and change directories to this folder:

mkdir my-robotics-project

cd my-robotics-project

Page 9: Learning JavaScript Robotics - Sample Chapter

Getting Started with JS Robotics

[ 4 ]

Next, we're going to install Johnny-Five:

npm install johnny-five

You should see a spinner, followed by some output. Unless you see an ERR NOT OK message at the end of your output, you're good to go with Johnny-Five.

On a Mac machine, you may need to install XCode developer command-line tools.

Connecting your Microcontroller and installing FirmataFirst, you should get the Arduino IDE. Yes, we are still using JavaScript; however, we must make sure that there's a particular sketch (that's Arduino-speak for program) running on our board in order for Johnny-Five to communicate properly.

You can get the installer at the Arduino website (http://www.arduino.cc/en/Main/Software). This book assumes that you have version 1.6.4, but the versions in the 1.4 range should work as well.

Once you've downloaded the software, open it. Then, we'll make sure that your serial connection works.

If you are using a board other than an Arduino, this step is not necessary. However, there may be other steps. These will be outlined with the wrapper plugin for your board.

Plug the USB cable into both the board and the computer. A few LEDs should light up on this board—this is normal. Then, go to the Tools menu in the Arduino IDE and hover over the ports submenu. You should see a list of ports that looks somewhat like the following screenshot:

Page 10: Learning JavaScript Robotics - Sample Chapter

Chapter 1

[ 5 ]

You should see at least one entry in this list that fi ts the following format: /dev/cu.usbmodem*****. It may or may not have Arduino Uno next to it. If you see this, go ahead and click on it, because this is the port you will want to use for the Firmata installation. If you have this, it means your board can communicate with your computer, and you're ready to install Firmata.

To install Firmata on your board, go to File | Examples | Firmata | StandardFirmata, as shown in the following screenshot:

Page 11: Learning JavaScript Robotics - Sample Chapter

Getting Started with JS Robotics

[ 6 ]

Once you've opened the sketch, you should get an IDE window that looks like the following screenshot:

Once this sketch is up, click on the Upload button (it looks like an arrow pointing to the right) to upload Firmata to your board. Once the uploading is done, you can close the Arduino IDE, and you will be ready to start working with JavaScript.

A developer named Suz Hinton (@noopkat) is working on a node program called AVRGirl that will remove this step in the near future. Take a look at www.github.com/noopkat/avrgirl to learn more!

Page 12: Learning JavaScript Robotics - Sample Chapter

Chapter 1

[ 7 ]

Hello, World! – Blinking an onboard LEDNow that we have our development environment set up, we can begin writing the JavaScript to use with our Arduino board. We'll start by blinking an LED that is already built into the Arduino microcontroller.

Writing the Johnny-Five scriptIn your favorite IDE, create a new hello-world.js fi le in your project directory. Then, copy and paste, or write, the following code:

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

board.on("ready", function() { var led = new five.Led(13); led.blink(500);});

We'll go over more of what this script does in Chapter 2, Working with Johnny-Five, but the basic overview is this: we require this script in the Johnny-Five module and use it to create a new board object. When this board is ready, we will create an LED object at pin 13 (this pin is wired to the onboard LED on an Arduino Uno board). We then program this LED to blink every half second.

Running the scriptIn order to run the script, go to your terminal, and in your project folder, run the following:

node hello-world.js

You should see an output that looks like the following:

Page 13: Learning JavaScript Robotics - Sample Chapter

Getting Started with JS Robotics

[ 8 ]

You should see an LED blink on your Arduino Uno. The following fi gure shows where the LED is on the board:

If all is well and the LED is blinking, congratulations! You're ready to start building robots and applications with Arduino and Johnny-Five!

If there is a problem, many troubleshooting issues can be solved by checking the Johnny-Five website (www.johnny-five.io).

SummaryIn this chapter, we learned about JS robotics and understood what a NodeBot is. We went through the hardware components that we will be using in the book, and we also learned how to set up the development environment. Finally, we got to know how to get the on-board LED to blink. In the next chapter, we'll dive deep into what makes Johnny-Five so powerful, and we will start writing and building some more complex projects.