A ṣe eto iṣakoso ohun ti copter nipa lilo Node.js ati ARDrone

A ṣe eto iṣakoso ohun ti copter nipa lilo Node.js ati ARDrone

Ninu ikẹkọ yii a yoo wo ṣiṣẹda eto kan fun drone pẹlu iṣakoso ohun nipa lilo Node.js ati API ọrọ wẹẹbu. Copter - Parrot ARDrone 2.0.

A leti: fun gbogbo awọn oluka ti "Habr" - ẹdinwo ti 10 rubles nigbati o forukọsilẹ ni eyikeyi iṣẹ-ẹkọ Skillbox nipa lilo koodu ipolowo “Habr”.

Skillbox ṣe iṣeduro: Ilana ti o wulo "Olugbese Alagbeka PRO".

Ifihan

Drones jẹ iyanu. Mo gbadun pupọ lati ṣere pẹlu quad mi, yiya awọn fọto ati awọn fidio, tabi ni igbadun nikan. Ṣugbọn awọn ọkọ ofurufu ti ko ni eniyan (UAVs) ni a lo fun diẹ sii ju ere idaraya lọ. Wọn ṣiṣẹ ni sinima, ṣe iwadi awọn glaciers, ati pe awọn ologun ati awọn aṣoju ti eka ogbin lo.

Ninu ikẹkọ yii a yoo wo ṣiṣẹda eto ti yoo gba ọ laaye lati ṣakoso drone kan. lilo awọn pipaṣẹ ohun. Bẹẹni, copter yoo ṣe ohun ti o sọ fun u lati ṣe. Ni ipari nkan naa o wa eto ti a ti ṣetan ati fidio ti iṣakoso UAV.

Iron

A nilo awọn wọnyi:

  • Parrot ARDrone 2.0;
  • okun àjọlò;
  • ti o dara gbohungbohun.

Idagbasoke ati iṣakoso yoo ṣee ṣe lori awọn ibi iṣẹ pẹlu Windows/Mac/Ubuntu. Tikalararẹ, Mo ṣiṣẹ pẹlu Mac ati Ubuntu 18.04.

Software

Ṣe igbasilẹ ẹya tuntun ti Node.js lati osise ojula.

Tun nilo titun ti ikede Google Chrome.

Oye copter

Jẹ ki a gbiyanju lati ni oye bi Parrot ARDrone ṣe n ṣiṣẹ. Copter yii ni awọn mọto mẹrin.

A ṣe eto iṣakoso ohun ti copter nipa lilo Node.js ati ARDrone

Awọn ọkọ ayọkẹlẹ alatako ṣiṣẹ ni itọsọna kanna. Ọkan bata n yi ni clockwise, awọn miiran counterclockwise. Drone n gbe nipasẹ yiyipada igun ti itara ojulumo si dada ti ilẹ, yiyipada iyara yiyi ti awọn mọto ati ọpọlọpọ awọn agbeka maneuverable miiran.

A ṣe eto iṣakoso ohun ti copter nipa lilo Node.js ati ARDrone

Gẹgẹbi a ti le rii ninu aworan atọka ti o wa loke, iyipada ọpọlọpọ awọn aye-aye yori si iyipada ninu itọsọna gbigbe ti copter. Fun apẹẹrẹ, idinku tabi jijẹ iyara yiyi ti osi ati ọtun rotors ṣẹda yipo. Eyi gba drone laaye lati fo siwaju tabi sẹhin.

Nipa yiyipada iyara ati itọsọna ti awọn mọto, a ṣeto awọn igun titẹ ti o gba laaye copter lati gbe ni awọn itọsọna miiran. Lootọ, fun iṣẹ akanṣe lọwọlọwọ ko si iwulo lati kawe aerodynamics, o kan nilo lati loye awọn ipilẹ ipilẹ.

Bawo ni Parrot ARDrone ṣiṣẹ

Awọn drone ni a Wi-Fi hotspot. Lati le gba ati firanṣẹ awọn aṣẹ si copter, o nilo lati sopọ si aaye yii. Ọpọlọpọ awọn ohun elo oriṣiriṣi wa ti o gba ọ laaye lati ṣakoso awọn quadcopters. Gbogbo rẹ dabi iru eyi:

A ṣe eto iṣakoso ohun ti copter nipa lilo Node.js ati ARDrone

Ni kete ti drone ti sopọ, ṣii ebute naa ati telnet 192.168.1.1 - eyi ni IP copter. Fun Linux o le lo Lainos Busybox.

Ohun elo faaji

Awọn koodu wa yoo pin si awọn modulu wọnyi:

  • ni wiwo olumulo pẹlu API ọrọ fun wiwa ohun;
  • awọn pipaṣẹ sisẹ ati afiwe pẹlu boṣewa;
  • fifiranṣẹ awọn aṣẹ si drone;
  • ifiwe fidio igbohunsafefe.

API ṣiṣẹ niwọn igba ti asopọ Intanẹẹti wa. Lati rii daju eyi, a ṣafikun asopọ Ethernet kan.

O to akoko lati ṣẹda ohun elo kan!

Koodu

Ni akọkọ, jẹ ki a ṣẹda folda tuntun ki o yipada si rẹ nipa lilo ebute naa.

Lẹhinna a ṣẹda iṣẹ akanṣe Node nipa lilo awọn aṣẹ ni isalẹ.

Ni akọkọ, a fi sori ẹrọ awọn igbẹkẹle ti a beere.

npm fi sori ẹrọ 

A yoo ṣe atilẹyin awọn aṣẹ wọnyi:

  • bo kuro;
  • ibalẹ;
  • soke - awọn drone ga soke idaji kan mita ati hovers;
  • isalẹ - ṣubu idaji mita ati didi;
  • si apa osi - lọ idaji mita si apa osi;
  • si ọtun - lọ idaji mita si ọtun;
  • yiyi - yiyi clockwise 90 iwọn;
  • siwaju - lọ siwaju idaji mita;
  • pada - pada sẹhin idaji mita;
  • Duro.

Eyi ni koodu ti o fun ọ laaye lati gba awọn aṣẹ, ṣe àlẹmọ wọn ati ṣakoso drone.

const express = require('express');
const bodyparser = require('body-parser');
var arDrone = require('ar-drone');
const router = express.Router();
const app = express();
const commands = ['takeoff', 'land','up','down','goleft','goright','turn','goforward','gobackward','stop'];
 
var drone  = arDrone.createClient();
// disable emergency
drone.disableEmergency();
// express
app.use(bodyparser.json());
app.use(express.static(__dirname + '/public'));
 
router.get('/',(req,res) => {
    res.sendFile('index.html');
});
 
router.post('/command',(req,res) => {
    console.log('command recieved ', req.body);
    console.log('existing commands', commands);
    let command = req.body.command.replace(/ /g,'');
    if(commands.indexOf(command) !== -1) {
        switch(command.toUpperCase()) {
            case "TAKEOFF":
                console.log('taking off the drone');
                drone.takeoff();
            break;
            case "LAND":
                console.log('landing the drone');
                drone.land();
            break;
            case "UP":
                console.log('taking the drone up half meter');
                drone.up(0.2);
                setTimeout(() => {
                    drone.stop();
                    clearTimeout();
                },2000);
            break;
            case "DOWN":
                console.log('taking the drone down half meter');
                drone.down(0.2);
                setTimeout(() => {
                    drone.stop();
                    clearTimeout();
                },2000);
            break;
            case "GOLEFT":
                console.log('taking the drone left 1 meter');
                drone.left(0.1);
                setTimeout(() => {
                    drone.stop();
                    clearTimeout();
                },1000);
            break;
            case "GORIGHT":
                console.log('taking the drone right 1 meter');
                drone.right(0.1);
                setTimeout(() => {
                    drone.stop();
                    clearTimeout();
                },1000);
            break;
            case "TURN":
                console.log('turning the drone');
                drone.clockwise(0.4);
                setTimeout(() => {
                    drone.stop();
                    clearTimeout();
                },2000);
            break;
            case "GOFORWARD":
                console.log('moving the drone forward by 1 meter');
                drone.front(0.1);
                setTimeout(() => {
                    drone.stop();
                    clearTimeout();
                },2000);
            break;
            case "GOBACKWARD":
                console.log('moving the drone backward 1 meter');
                drone.back(0.1);
                setTimeout(() => {
                    drone.stop();
                    clearTimeout();
                },2000);
            break;
            case "STOP":
                drone.stop();
            break;
            default:
            break;    
        }
    }
    res.send('OK');
});
 
app.use('/',router);
 
app.listen(process.env.port || 3000);

Ati pe eyi ni HTML ati koodu JavaScript ti o tẹtisi olumulo ati fi aṣẹ ranṣẹ si olupin Node.

<!DOCTYPE html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Voice Controlled Notes App</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/shoelace-css/1.0.0-beta16/shoelace.css">
        <link rel="stylesheet" href="styles.css">
 
    </head>
    <body>
        <div class="container">
 
            <h1>Voice Controlled Drone</h1>
            <p class="page-description">A tiny app that allows you to control AR drone using voice</p>
 
            <h3 class="no-browser-support">Sorry, Your Browser Doesn't Support the Web Speech API. Try Opening This Demo In Google Chrome.</h3>
 
            <div class="app">
                <h3>Give the command</h3>
                <div class="input-single">
                    <textarea id="note-textarea" placeholder="Create a new note by typing or using voice recognition." rows="6"></textarea>
                </div>    
                <button id="start-record-btn" title="Start Recording">Start Recognition</button>
                <button id="pause-record-btn" title="Pause Recording">Pause Recognition</button>
                <p id="recording-instructions">Press the <strong>Start Recognition</strong> button and allow access.</p>
 
            </div>
 
        </div>
 
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script src="script.js"></script>
 
    </body>
</html>

Ati koodu JavaScript tun lati ṣiṣẹ pẹlu awọn pipaṣẹ ohun, fifiranṣẹ wọn si olupin Node.

try {
 var SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
 var recognition = new SpeechRecognition();
 }
 catch(e) {
 console.error(e);
 $('.no-browser-support').show();
 $('.app').hide();
 }
// other code, please refer GitHub source
recognition.onresult = function(event) {
// event is a SpeechRecognitionEvent object.
// It holds all the lines we have captured so far.
 // We only need the current one.
 var current = event.resultIndex;
// Get a transcript of what was said.
var transcript = event.results[current][0].transcript;
// send it to the backend
$.ajax({
 type: 'POST',
 url: '/command/',
 data: JSON.stringify({command: transcript}),
 success: function(data) { console.log(data) },
 contentType: "application/json",
 dataType: 'json'
 });
};

Ifilọlẹ ohun elo

Eto naa le ṣe ifilọlẹ bi atẹle (o ṣe pataki lati rii daju pe copter ti sopọ si Wi-Fi ati okun Ethernet ti sopọ si kọnputa).

Ṣii localhost: 3000 ninu ẹrọ aṣawakiri ki o tẹ Ibẹrẹ idanimọ.

A ṣe eto iṣakoso ohun ti copter nipa lilo Node.js ati ARDrone

A gbiyanju lati šakoso awọn drone ati ki o wa dun.

Fidio igbohunsafefe lati ọdọ drone

Ninu iṣẹ akanṣe, ṣẹda faili tuntun ki o daakọ koodu yii nibẹ:

const http = require("http");
const drone = require("dronestream");
 
const server = http.createServer(function(req, res) {
 
require("fs").createReadStream(__dirname + "/public/video.html").pipe(res);
 });
 
drone.listen(server);
 
server.listen(4000);

Ati pe eyi ni koodu HTML, a gbe si inu folda ti gbogbo eniyan.

<!doctype html>
 <html>
 <head>
 <meta http-equiv="content-type" content="text/html; charset=utf-8">
 <title>Stream as module</title>
 <script src="/dronestream/nodecopter-client.js" type="text/javascript" charset="utf-8"></script>
 </head>
 <body>
 <h1 id="heading">Drone video stream</h1>
 <div id="droneStream" style="width: 640px; height: 360px"> </div>
 
<script type="text/javascript" charset="utf-8">
 
new NodecopterStream(document.getElementById("droneStream"));
 
</script>
 
</body>
</html>

Lọlẹ ati sopọ si localhost:8080 lati wo fidio lati kamẹra iwaju.

A ṣe eto iṣakoso ohun ti copter nipa lilo Node.js ati ARDrone

Awọn italolobo iranlọwọ

  • Fò yi drone ninu ile.
  • Nigbagbogbo fi ideri aabo sori drone rẹ ṣaaju gbigbe kuro.
  • Ṣayẹwo boya batiri ti gba agbara.
  • Ti drone ba huwa ajeji, mu u mọlẹ ki o si yi i pada. Iṣe yii yoo fi copter sinu ipo pajawiri ati awọn rotors yoo da duro lẹsẹkẹsẹ.

Ṣetan koodu ati demo

Ririnkiri LIVE

DOWNLOAD

O ṣẹlẹ!

Awọn koodu kikọ ati lẹhinna wiwo ẹrọ bẹrẹ lati gbọràn yoo fun ọ ni idunnu! Bayi a ti ṣawari bi a ṣe le kọ drone lati tẹtisi awọn pipaṣẹ ohun. Ni otitọ, awọn aye diẹ sii wa: idanimọ oju olumulo, awọn ọkọ ofurufu adase, idanimọ idari ati pupọ diẹ sii.

Kini o le daba lati mu eto naa dara si?

Skillbox ṣe iṣeduro:

orisun: www.habr.com

Fi ọrọìwòye kun