Robotics: Building Autonomous Robots with Raspberry Pi and JavaScript


In recent years, the world of robotics has seen a significant shift towards open-source technologies and platforms. One such platform that has gained immense popularity is Raspberry Pi, a small and affordable single-board computer. Coupled with the power and versatility of JavaScript, developers can now embark on exciting journeys into the world of robotics. In this article, we will explore how to build autonomous robots using Raspberry Pi and JavaScript, diving into code examples, explanations, and their output.

Setting Up Raspberry Pi

Before we delve into the realm of JavaScript robotics, it is essential to set up our Raspberry Pi properly. Firstly, we need to install the necessary operating system, such as Raspbian, which is the official operating system for Raspberry Pi. Once installed, we can connect peripherals like a keyboard, mouse, and monitor, or even use SSH to access the Raspberry Pi remotely.

Once we have our Raspberry Pi up and running, we can start exploring the world of JavaScript robotics.

Controlling Servo Motors

Servo motors are crucial components in many robotic systems, allowing us to control the position or orientation of various parts. JavaScript provides us with libraries like "onoff" that enable us to interface with hardware components like servo motors.

Example 

Let's take a look at a code example that demonstrates how to control a servo motor using JavaScript:

const Gpio = require('onoff').Gpio;

// Create a new servo motor instance
const servo = new Gpio(17, 'out');

// Function to move the servo motor to a specific angle
function moveServo(angle) {
   servo.servoWrite(angle);
}

// Move the servo motor to 0 degrees
moveServo(0);

// Wait for 2 seconds, then move the servo motor to 90 degrees
setTimeout(() => {
   moveServo(90);
}, 2000);

Explanation

In the above code, we import the onoff library and create an instance of the Gpio class for the servo motor connected to GPIO pin 17. The servoWrite method allows us to control the position of the servo motor by specifying the desired angle.

When we run the code, the servo motor initially moves to 0 degrees and then, after a 2-second delay, moves to 90 degrees.

Controlling DC Motors

DC motors are commonly used in robotics to provide locomotion. JavaScript can also control DC motors using libraries like "pigpio". Let's explore an example that demonstrates how to control a DC motor using JavaScript.

Example 

const Gpio = require('pigpio').Gpio;

// Create a new DC motor instance
const motor = new Gpio(17, { mode: Gpio.OUTPUT });

// Function to control the DC motor
function controlMotor(speed, direction) {
   motor.servoWrite(speed * direction);
}

// Move the DC motor forward at full speed
controlMotor(255, 1);

// Wait for 2 seconds, then stop the motor
setTimeout(() => {
   controlMotor(0, 1);
}, 2000);

Explanation

In the above code, we use the "pigpio" library to control the DC motor connected to GPIO pin 17. We create an instance of the Gpio class with the mode set to Gpio.OUTPUT. The servoWrite method is used to control the speed and direction of the DC motor. Positive values of the direction variable move the motor forward, while negative values move it in reverse.

The code example moves the DC motor forward at full speed and stops after a 2-second delay.

Building Autonomous Behavior

Now that we have explored controlling individual components, let's take it a step further and build autonomous behavior for our robot. We can accomplish this by incorporating sensors, such as ultrasonic sensors, and writing code to respond to their input.

Let's consider an example where we build a simple obstacle-avoiding robot using a Raspberry Pi, a servo motor, a DC motor, and an ultrasonic sensor. The servo motor will be used to rotate the ultrasonic sensor, while the DC motor will provide locomotion.

Example 

const Gpio = require('onoff').Gpio;
const UltraSonic = require('ultrasonic-rx');

// Create instances of servo motor, DC motor, and ultrasonic sensor
const servo = new Gpio(17, 'out');
const motor = new Gpio(18, 'out');
const ultrasonic = new UltraSonic({ echoPin: 23, triggerPin: 24 });

// Function to control the servo motor
function controlServo(angle) {
   servo.servoWrite(angle);
}

// Function to control the DC motor
function controlMotor(speed) {
   motor.servoWrite(speed);
}

// Function to move the robot forward
function moveForward() {
   controlMotor(255);
}

// Function to stop the robot
function stop() {
   controlMotor(0);
}

// Function to avoid obstacles
function avoidObstacle() {
  const distance = ultrasonic.distance();

   if (distance < 30) {
      controlServo(90);
      stop();
   } else {
      controlServo(0);
      moveForward();
   }
}

// Continuously monitor the environment for obstacles
setInterval(avoidObstacle, 100);

Explanation

In the above code, we use the "ultrasonic-rx" library to interface with an ultrasonic sensor connected to GPIO pins 23 and 24. We create instances of the Gpio class for the servo motor and DC motor. The controlServo function is responsible for controlling the servo motor's position, while the controlMotor function controls the speed of the DC motor.

The avoidObstacle function reads the distance from the ultrasonic sensor and determines if an obstacle is within 30 centimetres. If an obstacle is detected, the servo motor is rotated to face forward, and the robot stops. Otherwise, the servo motor faces the side, and the robot moves forward.

Conclusion

JavaScript, with the help of platforms like Raspberry Pi, offers an accessible and flexible way to dive into the exciting field of robotics. In this article, we explored how to build autonomous robots using Raspberry Pi and JavaScript. We covered controlling servo and DC motors, as well as building autonomous behaviour with sensors. With the provided code examples, explanations, and outputs, you can start your own journey into JavaScript robotics. The possibilities are endless, and with JavaScript as your ally, you can unlock a world of creativity in building autonomous robots.

Updated on: 25-Jul-2023

150 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements