Home  >  Article  >  Web Front-end  >  Robotics: Build autonomous robots with Raspberry Pi and JavaScript

Robotics: Build autonomous robots with Raspberry Pi and JavaScript

WBOY
WBOYforward
2023-09-07 10:01:021323browse

机器人技术:使用 Raspberry Pi 和 JavaScript 构建自主机器人

In recent years, the world of robotics has undergone a major shift toward open source technologies and platforms. A very popular platform is the Raspberry Pi, a small and affordable single-board computer. Combined with the power and versatility of JavaScript, developers can now embark on an exciting journey into the world of robotics. In this article, we'll explore how to build an autonomous robot using a Raspberry Pi and JavaScript, diving into code examples, explanations, and its output.

Setting up the Raspberry Pi

Before we delve into the realm of JavaScript robotics, it’s crucial to set up your Raspberry Pi correctly. First, we need to install the necessary operating system, such as Raspbian, which is the official operating system of Raspberry Pi. Once installed, we can connect peripherals like keyboard, mouse, and monitor, and even remotely access the Raspberry Pi using SSH.

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

Control servo motor

Servo motors are key components in many robotic systems, allowing us to control the position or orientation of individual components. JavaScript provides us with libraries like “onoff” that allow us to interact with hardware components like servo motors.

Example

Let’s 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);

illustrate

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, then after a 2 second delay it moves to 90 degrees.

Control DC motor

DC motors are commonly used in robotics to provide motion. 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);

illustrate

In the above code, we are using the "pigpio" library to control a DC motor connected to GPIO pin 17. We create an instance of the Gpio class and set the mode to Gpio.OUTPUT. The servoWrite method is used to control the speed and direction of a DC motor. Positive values ​​for the direction variable move the motor forward, while negative values ​​move the motor backward.

Code example moves a DC motor forward at full speed and stops after a 2 second delay.

Establish autonomous behavior

Now that we have explored controlling the various components, let's take it one step further and build autonomous behavior for our robot. We can do 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 avoidance robot using a Raspberry Pi, servo motors, DC motors, and ultrasonic sensors. A servo motor will be used to rotate the ultrasonic sensor, while a DC motor will provide the motion.

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);

illustrate

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

avoidObstacle function reads the distance of the ultrasonic sensor and determines if an obstacle is within 30 cm. If an obstacle is detected, the servo motor will rotate to the front and the robot will stop. Otherwise, the servo motor faces sideways and the robot moves forward.

in conclusion

JavaScript, with the help of platforms like the Raspberry Pi, provides an accessible and flexible way to delve into the exciting field of robotics. In this article, we explore how to build an autonomous robot using a Raspberry Pi and JavaScript. We cover controlling servo and DC motors, and using sensors to build autonomous behavior. With provided code examples, explanations, and output, you can start your own JavaScript bot journey. The possibilities are endless, and with JavaScript as your ally, you can unlock a world of creativity in building autonomous robots.

The above is the detailed content of Robotics: Build autonomous robots with Raspberry Pi and JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete