Ríomhaimid rialú gutha an chopair ag baint úsáide as Node.js agus ARDrone

Ríomhaimid rialú gutha an chopair ag baint úsáide as Node.js agus ARDrone

Sa rang teagaisc seo féachfaimid ar chlár a chruthú le haghaidh drone le rialú gutha ag baint úsáide as Node.js agus Web Speech API. Copter - Parrot ARDrone 2.0.

Meabhraímid: do léitheoirí uile "Habr" - lascaine de 10 rúbal nuair a chláraíonn siad in aon chúrsa Scilbox ag baint úsáide as an gcód bolscaireachta "Habr".

Molann Skillbox: Cúrsa praiticiúil "Forbróir Soghluaiste OCP".

Réamhrá

Tá drones iontach. Is breá liom a bheith ag súgradh le mo quad, ag tógáil grianghraif agus físeáin, nó ag spraoi. Ach úsáidtear aerfheithiclí gan fhoireann (UAVanna) le haghaidh níos mó ná siamsaíocht amháin. Oibríonn siad sa phictiúrlann, déanann siad staidéar ar oighearshruthanna, agus úsáideann an míleata agus ionadaithe na hearnála talmhaíochta iad.

Sa rang teagaisc seo féachfaimid ar chlár a chruthú a ligfidh duit drón a rialú. ag baint úsáide as orduithe gutha. Sea, déanfaidh an copter cad a insíonn tú dó a dhéanamh. Ag deireadh an ailt tá clár réidh de dhéantús an duine agus físeáin de rialú UAV.

Iarann

Teastaíonn na rudaí seo a leanas uainn:

  • Parrot ARDrone 2.0;
  • Cábla Ethernet;
  • micreafón maith.

Déanfar forbairt agus bainistíocht ar stáisiúin oibre le Windows/Mac/Ubuntu. Go pearsanta, d'oibrigh mé le Mac agus Ubuntu 18.04.

Bogearraí

Íoslódáil an leagan is déanaí de nod. suíomh oifigiúil.

Chomh maith leis sin ag teastáil Google chrome leagan is déanaí.

An copter a thuiscint

Déanaimis iarracht a thuiscint conas a oibríonn Parrot ARDrone. Tá ceithre mhótar ag an copter seo.

Ríomhaimid rialú gutha an chopair ag baint úsáide as Node.js agus ARDrone

Oibríonn mótair codarsnacha sa treo céanna. Rothlaíonn péire amháin deiseal, an ceann eile tuathalach. Bogann an drone tríd an uillinn claonta a athrú i gcoibhneas le dromchla an domhain, ag athrú luas rothlaithe an mhótair agus roinnt gluaiseachtaí inaistrithe eile.

Ríomhaimid rialú gutha an chopair ag baint úsáide as Node.js agus ARDrone

Mar is féidir linn a fheiceáil sa léaráid thuas, déantar athrú ar threo gluaiseachta an chopair mar thoradh ar athrú paraiméadair éagsúla. Mar shampla, cruthaítear rolla trí luas rothlaithe na rotors clé agus ar dheis a laghdú nó a mhéadú. Ligeann sé seo don drone eitilt ar aghaidh nó siar.

Trí luas agus treo an mhótair a athrú, socraímid uillinneacha tilt a ligeann don copter bogadh i dtreonna eile. I ndáiríre, don tionscadal reatha níl aon ghá le staidéar a dhéanamh ar aeraidinimic, ní mór duit ach na bunphrionsabail a thuiscint.

Conas a oibríonn Parrot ARDrone

Is hotspot Wi-Fi é an drone. D'fhonn orduithe a fháil agus a sheoladh chuig an copter, ní mór duit ceangal leis an bpointe seo. Tá go leor feidhmchláir éagsúla ann a ligeann duit quadcopters a rialú. Breathnaíonn sé ar fad rud éigin mar seo:

Ríomhaimid rialú gutha an chopair ag baint úsáide as Node.js agus ARDrone

Chomh luath agus atá an drone ceangailte, oscail an teirminéal agus telnet 192.168.1.1 - is é seo IP an copter. Le haghaidh Linux is féidir leat é a úsáid Busybox le haghaidh Linux.

Ailtireacht feidhmchlár

Roinnfear ár gcód sna modúil seo a leanas:

  • comhéadan úsáideora le API cainte chun guth a bhrath;
  • orduithe a scagadh agus a chur i gcomparáid le caighdeán;
  • orduithe a sheoladh chuig an drone;
  • craoladh físeáin beo.

Oibríonn an API chomh fada agus a bhfuil nasc Idirlín ann. Chun seo a chinntiú, cuirimid nasc Ethernet leis.

Tá sé in am iarratas a chruthú!

Cód

Gcéad dul síos, déanaimis fillteán nua a chruthú agus aistrigh chuige ag baint úsáide as an teirminéal.

Ansin cruthaímid tionscadal Nód ag baint úsáide as na horduithe thíos.

Ar dtús, déanaimid na spleáchais riachtanacha a shuiteáil.

npm a shuiteáil 

Tacóimid leis na horduithe seo a leanas:

  • éirí de thalamh;
  • tuirlingt ;
  • suas - ardaíonn an drone leath mhéadar agus hovers;
  • síos - titeann leath mhéadar agus reo;
  • ar chlé - téann leath mhéadar ar an taobh clé;
  • to the right - téann leath mhéadar ar dheis;
  • rothlú - rothlaíonn sé deiseal 90 céim;
  • ar aghaidh - téann leath mhéadar ar aghaidh;
  • ar ais - téann siar leath mhéadar;
  • stad.

Seo é an cód a ligeann duit glacadh le horduithe, iad a scagadh agus an drone a rialú.

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

Agus seo é an cód HTML agus JavaScript a éisteann leis an úsáideoir agus a chuireann ordú chuig an bhfreastalaí Nód.

<!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>

Agus freisin cód JavaScript a bheith ag obair le horduithe gutha, iad a sheoladh chuig an bhfreastalaí Nód.

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

Seoladh an t-iarratas

Is féidir an clár a sheoladh mar seo a leanas (tá sé tábhachtach a chinntiú go bhfuil an copter ceangailte le Wi-Fi agus go bhfuil an cábla Ethernet ceangailte leis an ríomhaire).

Oscail localhost: 3000 sa bhrabhsálaí agus cliceáil Tosaigh Aitheantas.

Ríomhaimid rialú gutha an chopair ag baint úsáide as Node.js agus ARDrone

Déanaimid iarracht an drone a rialú agus táimid sásta.

Físeán a chraoladh ó drone

Sa tionscadal, cruthaigh comhad nua agus cóipeáil an cód seo ann:

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

Agus anseo an cód HTML, cuirimid é taobh istigh den fhillteán poiblí.

<!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>

Seoladh agus ceangail le localhost:8080 chun físeáin a fheiceáil ón gceamara tosaigh.

Ríomhaimid rialú gutha an chopair ag baint úsáide as Node.js agus ARDrone

Leideanna Úsáideacha

  • Eitilt an drone seo taobh istigh.
  • Cuir an clúdach cosanta ar do dhron i gcónaí roimh éirí de thalamh.
  • Seiceáil an bhfuil an ceallraí luchtaithe.
  • Má iompraíonn an drone go aisteach, coinnigh síos é agus smeach anonn é. Cuirfidh an gníomh seo an copter i mód éigeandála agus stopfaidh na rotors láithreach.

Réidh cód agus taispeána

DEMO BEO

DOWNLOAD

Tharla!

Beidh scríobh cód agus ansin féachaint ar an meaisín tús a ghéilleadh a thabhairt duit pléisiúir! Anois táimid tar éis a dhéanamh amach conas drone a mhúineadh chun éisteacht le horduithe gutha. Go deimhin, tá i bhfad níos mó féidearthachtaí ann: aithint aghaidhe úsáideora, eitiltí uathrialacha, aithint gothaí agus go leor eile.

Cad is féidir leat a mholadh chun an clár a fheabhsú?

Molann Skillbox:

Foinse: will.com

Add a comment