Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning JavaScript Robotics

You're reading from  Learning JavaScript Robotics

Product type Book
Published in Nov 2015
Publisher
ISBN-13 9781785883347
Pages 160 pages
Edition 1st Edition
Languages
Concepts
Author (1):
Kassandra Perch Kassandra Perch
Profile icon Kassandra Perch

Table of Contents (16) Chapters

Learning JavaScript Robotics
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Getting Started with JS Robotics Working with Johnny-Five Using Digital and PWM Output Pins Using Specialized Output Devices Using Input Devices and Sensors Moving Your Bot Advanced Movement with the Animation Library Advanced Components – SPI, I2C, and Other Devices Connecting NodeBots to the World, and Where to Go Next Index

Chapter 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-Five


JavaScript 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 allows 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?

What a NodeBot is, and other basic vocabulary

A 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.

Note

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 revolution

Johnny-Five (http://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.

What we'll be using in this book

For 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 fine 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 environment


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

Installing Node.JS

If 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-Five

In your terminal, create a folder for your project and change directories to this folder:

mkdir my-robotics-project
cd my-robotics-project

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.

Note

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

Connecting your Microcontroller and installing Firmata

First, 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.

Note

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:

You should see at least one entry in this list that fits 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:

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.

Tip

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!

Hello, World! – Blinking an onboard LED


Now 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 script

In your favorite IDE, create a new hello-world.js file 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 script

In 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:

You should see an LED blink on your Arduino Uno. The following figure 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!

Tip

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

Summary


In 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.

You have been reading a chapter from
Learning JavaScript Robotics
Published in: Nov 2015 Publisher: ISBN-13: 9781785883347
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}