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
€19.99 €13.98
Book Nov 2015 160 pages 1st Edition
eBook
€19.99 €13.98
Print
€24.99
Subscription
€14.99 Monthly
eBook
€19.99 €13.98
Print
€24.99
Subscription
€14.99 Monthly

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

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 eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

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

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.