Reader small image

You're reading from  Learning JavaScript Robotics

Product typeBook
Published inNov 2015
Reading LevelIntermediate
Publisher
ISBN-139781785883347
Edition1st Edition
Languages
Tools
Concepts
Right arrow
Author (1)
Kassandra Perch
Kassandra Perch
author image
Kassandra Perch

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.
Read more about Kassandra Perch

Right arrow

Chapter 2. Working with Johnny-Five

In this chapter, we'll begin working with Johnny-Five to build our own robotics projects. We'll cover what makes Johnny-Five a great library for starting with robotics, and we will build our first robot. We'll learn how we can manipulate this robot in real time from the command line—a feat not easily replicated on other platforms! By the end of this chapter, you'll have an open understanding of the software involved, which will serve as a great foundation for more complicated hardware builds.

This chapter will cover the following topics:

  • How a Johnny-Five program works

  • Understanding events in Johnny-Five

  • Wiring an LED and making it blink

  • Using Read-Eval-Print-Loop (REPL)

What you'll need for this chapter


All you'll need for this chapter is your microcontroller (the examples here still use an Arduino Uno) and a small handful of LEDs—we'll be wiring only one LED, but you may want a couple of spares, in case one burns out.

How a Johnny-Five program works


In this section, we'll take a look at the internals of a Johnny-Five program in more detail, so we can start building more complex applications.

Objects, functions, and events

Johnny-Five programs work using an event-based structure, and there are three concepts to keep in mind: objects, functions, and events.

Objects tend to programmatically represent our physical electronic components and are usually constructed using the new keyword. A few examples of objects in Johnny-Five include the five object, which represents the Johnny-Five library, the Board object, which represents our microcontroller; and the LED object, which will programmatically represent our LED:

var led = new five.Led(11);

Functions are available for the objects we create and usually represent the actions that our robots can do. For instance, the LED object has an on() and off() function that turns the LED on and off:

led.on();
led.off();
led.blink();
led.stop();

Events are fired on objects and...

Understanding events in Johnny-Five


The events in Johnny-Five are a very important concept—this is also a new concept, especially if you are used to low-level language programming. It's very similar to the idea of interrupts, but definitely strays away from the traditional robotics programming paradigm of an event loop. While you can create timers and loops in Johnny-Five, it highly encourages an event-based programming approach, which can need some practice.

Why events?

A question that gets asked a lot is, "Why event-based? Why not loop-based and interrupt-based as in previous methods?".

A lot of this has to do with the way robots work and the way we think about how we program robots. When you think about what you want a robot to do, you're less likely to think "Every X seconds, I want to check for A and start task B..." and you are more likely to think "When Y happens, I want to start event C...".

The event-based system in Johnny-Five works really well with this train of thought by allowing...

Wiring up an external LED


For our first hardware project, we're going to wire an LED to the Arduino. An LED, or a Light Emitting Diode, is a component that lights up when electric current is passed through it. They come in many colors and sizes and are one of the most easy-to-use and versatile components in hobbyist robotics.

Setting up the hardware

First, take an LED. We'll determine the positive and negative leads of the LED—for this component, it's straightforward: the positive lead is longer than the negative lead.

Determining the positive and negative ends of an LED

To wire the LED to an Arduino, place the positive lead on pin 11 and the negative lead on the pin marked GND, just like in the following diagram:

Wiring up our LED

You can also use a breadboard if you wish to—it will look like the following:

Wiring an LED with a breadboard

Now that we've wired up the LED, we're going to make it blink as we did in the last chapter. The script looks very familiar:

var five = require("johnny-five")...

Using the Read-Eval-Print-Loop (REPL)


The Read, Eval, Print Loop, or REPL, is a concept relative to many scripting languages, but it is new to libraries, and definitely new to robotics. Think about how you alter the state in a typical Arduino program: you modify the source code, re-load it onto the board, and wait for it to run.

However, due to the way Johnny-Five works, we can modify the state of our robot code while the code is running. This is because we use Firmata—the board is just a thin client that reacts to instructions from our node program, so if we let our Node programs send different instructions, we can change how our robot works in real time.

The way to do this in a Johnny-Five program is by injecting components into the REPL, which allows us to use them.

Making components available to the REPL

We're going to modify our script from the previous section in order to manipulate our LED. To do this, we're going to use the this.repl.inject() function. The this keyword, when used within...

Summary


In this chapter, we learned how to wire up an LED and how to use the REPL to modify our robot's state in real time. We understood what software is involved when working with complex hardware. We also looked at what makes Johnny-Five stand out in the robotics world by exploring the REPL and the event structure of Johnny-Five programs.

In the next chapter, we'll explore pins—including analog and PWM pins—and talk more about how an LED can be set to different values of brightness.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning JavaScript Robotics
Published in: Nov 2015Publisher: 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.
undefined
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

Author (1)

author image
Kassandra Perch

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.
Read more about Kassandra Perch