Book Image

Hands-On Robotics with JavaScript

By : Kassandra Perch
Book Image

Hands-On Robotics with JavaScript

By: Kassandra Perch

Overview of this book

JavaScript has an effective set of frameworks and libraries that provide support for embedded device programming and the robotics ecosystem. You’ll be able to put your JavaScript knowledge to work with this practical robotics guide. The book starts by guiding you in setting up an environment to program robots with JavaScript and Rasberry Pi 3. You will build beginner-level projects, such as a line-following robot, and then upgrade your robotics skills with a series of projects that help you get to grips with the Johnny-Five library. As you progress, you’ll learn how you can improve your projects by enabling advanced hardware components and programming concepts. You’ll even build an advanced AI-enabled robot, connect its NodeBots to the internet, create a NodeBots Swarm, and explore Message Queuing Telemetry Transport (MQTT). By the end of this book, you will have enhanced your robot programming skills by building a range of simple to complex projects.
Table of Contents (19 chapters)
Title Page
Dedication
Packt Upsell
Contributors
Preface
Index

The Johnny-Five motor object


The motor object in Johnny-Five allows us to easily control our motors without having to worry about communicating with the hat via the Pi. Let's code a test setup with the REPL before coding our project, to make sure everything is working.

Create a new project folder and, inside it, run the following:

npm init -y

And, create a file in the folder named motor-test.js. Start by requiring in Johnny-Five and Raspi-IO, instantiating your board object, and creating a board.on('ready') handler, as we usually do:

constRaspi=require('raspi-io')
constfive=require('johnny-five')
constboard=newfive.Board({
  io:newRaspi()
})

board.on('ready', () => {

})

 

Now, we're ready to set up our motor object, keeping in mind that we'll need to configure for our hat.

Constructors for our hat

If you are using the Adafruit hat, your constructor is as follows:

let motor = new five.Motor(five.Motor.SHIELD_CONFIGS.ADAFRUIT_V2.M1)

And if you're using the L293D hat, your constructor is as follows...