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
Learning JavaScript Robotics

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

By Kassandra Perch
$15.99 per month
Book Nov 2015 160 pages 1st Edition
eBook
$25.99 $17.99
Print
$32.99
Subscription
$15.99 Monthly
eBook
$25.99 $17.99
Print
$32.99
Subscription
$15.99 Monthly

What do you get with a Packt Subscription?

Free for first 7 days. $15.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details


Publication date : Nov 25, 2015
Length 160 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781785883347
Vendor :
Netscape
Category :
Concepts :
Table of content icon View table of contents Preview book icon Preview Book

Learning JavaScript Robotics

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.

Left arrow icon Right arrow icon

Key benefits

What you will learn

Familiarise yourself with JohnnyFive 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 JohnnyFive 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 JohnnyFive projects, such as USB devices and remotes Connect your JohnnyFive projects to external APIs and create your own Internet of Things!

What do you get with a Packt Subscription?

Free for first 7 days. $15.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details


Publication date : Nov 25, 2015
Length 160 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781785883347
Vendor :
Netscape
Category :
Concepts :

Table of Contents

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

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.