291080889 Learning JavaScript Robotics Sample Chapter
291080889 Learning JavaScript Robotics Sample Chapter
ee
Learning JavaScript Sa
This book walks you through basic robotics projects, Build robots with basic output devices
including the physical hardware builds and the JavaScript
code for them. You'll delve into the concepts of Create projects with complex output devices
Johnny-Five and JavaScript robotics. You'll learn about and employ the Johnny-Five API to simplify
various components, such as Digital GPIO pins, PWM the use of components that require complex
output pins, sensors, servos, and motors to be used with interfaces, such as I2C
Johnny-Five, along with some advanced components
that use I2C and SPI. You will learn to connect your Use the servo and motor objects to make it
Johnny-Five robots to internet services. By the end of this much easier to move your robotics projects
book, you will have explored the benefits of the Johnny-Five 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
framework and the many devices it unlocks. Learn about the Animation API which allows
you to program complex movements using
timing and key frames
Kassandra Perch
this book.
$ 29.99 US
£ 19.99 UK
Design, build, and program your own remarkable robots
with JavaScript and open source hardware
Prices do not include
community experience distilled local sales tax or VAT
P U B L I S H I N G where applicable
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.
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.
Chapter 1
[1]
Getting Started with JS Robotics
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?
[2]
Chapter 1
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.
[3]
Getting Started with JS Robotics
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.
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:
[4]
Chapter 1
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.
[5]
Getting Started with JS Robotics
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.
[6]
Chapter 1
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.
[7]
Getting Started with JS Robotics
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!
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.
[8]
Get more information Learning JavaScript Robotics
www.PacktPub.com
Stay Connected: