Ka whakamahere matou i te mana reo o te copter ma te whakamahi i te Node.js me te ARDrone

Ka whakamahere matou i te mana reo o te copter ma te whakamahi i te Node.js me te ARDrone

I roto i tenei akoranga ka titiro tatou ki te hanga kaupapa mo te drone me te mana reo ma te whakamahi i te Node.js me te API korero Tukutuku. Copter - Parrot ARDrone 2.0.

Ka whakamahara matou: mo nga kaipānui katoa o "Habr" - he utu mo te 10 rubles i te wa e whakauru ana ki tetahi akoranga Skillbox ma te whakamahi i te waehere whakatairanga "Habr".

Ka tūtohu a Skillbox: Akoranga mahi "Whakawhanake Waea PRO".

Whakataki

He mea whakamiharo nga drones. He pai ki ahau te takaro me taku whaa, te tango whakaahua me nga ataata, me te ngahau noa ranei. Engari ko nga waka rererangi kore (UAV) e whakamahia ana mo nga mahi whakangahau. Ka mahi ratou i roto i te whare pikitia, e ako ana i nga hukapapa, ka whakamahia e nga hoia me nga kanohi o te wahanga ahuwhenua.

I roto i tenei akoranga ka titiro tatou ki te hanga i tetahi kaupapa ka taea e koe te whakahaere i te drone. te whakamahi i nga whakahau reo. Ae, ka mahia e te copter nga mea e kiia ana e koe kia mahia. I te mutunga o te tuhinga he kaupapa kua oti te hanga me te ataata o te mana UAV.

Iron

E hiahia ana matou ki enei e whai ake nei:

  • Parrot ARDrone 2.0;
  • taura Itarangi;
  • hopuoro pai.

Ka whakahaerehia te whakawhanaketanga me te whakahaere i runga i nga teihana mahi me Windows/Mac/Ubuntu. Ko ahau, i mahi tahi ahau me Mac me Ubuntu 18.04.

Pūmanawa

Tikiake i te putanga hou o Node.js mai pae mōhiohio.

E hiahiatia ana hoki putanga hou o Google Chrome.

Te mohio ki te copter

Me ngana ki te mohio me pehea te mahi a Parrot ARDrone. E wha nga motuka o tenei copter.

Ka whakamahere matou i te mana reo o te copter ma te whakamahi i te Node.js me te ARDrone

Ko nga motuka whakahē e mahi ana i te huarahi kotahi. Ko tetahi takirua ka huri i te karaka, ko tetahi atu karaka. Ka neke te drone ma te huri i te koki o te hianga e pa ana ki te mata o te whenua, te huri i te tere o te hurihanga o nga motuka me etahi atu nekehanga ka taea.

Ka whakamahere matou i te mana reo o te copter ma te whakamahi i te Node.js me te ARDrone

Ka taea e tatou te kite i te hoahoa i runga ake nei, ko te whakarereke i nga momo tawhā ka puta he huringa i te ahunga o te neke o te copter. Hei tauira, ko te whakaheke, te whakanui ake ranei i te tere hurihuri o nga rotors maui me te taha matau ka hangaia he roera. Ma tenei ka taea e te drone te rere whakamua, whakamuri ranei.

Ma te whakarereke i te tere me te ahunga o nga motuka, ka whakatauhia e matou nga koki honga e taea ai e te copter te neke ki etahi atu huarahi. Inaa, mo te kaupapa o naianei kaore he take ki te ako i te aerodynamics, me maarama koe ki nga kaupapa matua.

Me pehea te mahi a Parrot ARDrone

Ko te drone he hotspot Wi-Fi. Hei whiwhi me te tuku whakahau ki te copter, me hono koe ki tenei waahi. He maha nga tono rereke ka taea e koe te whakahaere i nga quadcopters. He penei te ahua o te katoa:

Ka whakamahere matou i te mana reo o te copter ma te whakamahi i te Node.js me te ARDrone

I te wa e hono ana te drone, whakatuwherahia te tauranga me te telnet 192.168.1.1 - koinei te IP o te copter. Mo Linux ka taea e koe te whakamahi Linux Busybox.

Te hoahoanga tono

Ka wehewehea to maatau waehere ki nga waahanga e whai ake nei:

  • atanga kaiwhakamahi me te API korero mo te rapu reo;
  • te tātari i nga whakahau me te whakataurite ki tetahi paerewa;
  • te tuku whakahau ki te haruru;
  • haapurororaa ataata ora.

Ka mahi te API i te mea he hononga Ipurangi. Hei whakarite i tenei, ka taapirihia he hononga Ethernet.

Kua tae ki te wa ki te hanga tono!

Waehere

Tuatahi, me hanga he kōpaki hou ka huri ki te whakamahi i te tauranga.

Na ka hangaia he kaupapa Node ma te whakamahi i nga whakahau i raro nei.

Tuatahi, ka whakauruhia e matou nga whakawhirinaki e hiahiatia ana.

npm tāuta 

Ka tautokohia e matou nga whakahau e whai ake nei:

  • rere atu;
  • taunga;
  • ki runga - ka piki te haruru mo te haurua mita ka rewa;
  • ki raro - ka taka ki te haurua mita ka maroke;
  • ki te maui - ka haere ki te haurua mita ki te taha maui;
  • ki te taha matau - he hawhe mita ki te taha matau;
  • hurihanga - huri karaka 90 nga nekehanga;
  • whakamua - ka anga whakamua he hawhe mita;
  • hoki - ka hoki ki muri i te hawhe mita;
  • mutu.

Anei te waehere ka taea e koe te whakaae ki nga whakahau, te tātari me te whakahaere i te 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);

Na konei ko te HTML me te waehere JavaScript e whakarongo ana ki te kaiwhakamahi me te tuku whakahau ki te tūmau 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>

Me te waehere JavaScript hoki hei mahi me nga whakahau reo, ka tukuna atu ki te tūmau 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'
 });
};

Te whakarewa i te tono

Ka taea te whakarewa i te kaupapa penei (he mea nui kia mohio kei te hono te copter ki Wi-Fi me te taura Ethernet ki te rorohiko).

Whakatuwherahia te localhost:3000 i roto i te tirotiro ka paato i te Whakaaetanga Timata.

Ka whakamahere matou i te mana reo o te copter ma te whakamahi i te Node.js me te ARDrone

Ka ngana matou ki te whakahaere i te drone me te harikoa.

Paoho ataata mai i te haruru

I roto i te kaupapa, hanga he konae hou ka kape i tenei waehere ki reira:

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

Anei te waehere HTML, ka tuu ki roto i te kōpaki tūmatanui.

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

Whakarewa me te hono atu ki te localhost:8080 ki te tiro ataata mai i te kamera o mua.

Ka whakamahere matou i te mana reo o te copter ma te whakamahi i te Node.js me te ARDrone

Tautoko Awhina

  • Rerehia tenei haruru ki roto.
  • Me mau tonu te uhi whakamarumaru ki runga i to haruru i mua i te rewa.
  • Tirohia mehemea kua utaina te pākahiko.
  • Mena he rereke te mahi a te haruru, purihia ki raro ka huri ki runga. Ma tenei mahi ka tuu te copter ki te aratau ohorere ka mutu tonu nga rotors.

Kua rite te waehere me te demo

DEMO ORA

Tikiake

Kua tupu!

Ko te tuhi waehere me te matakitaki i te miihini ka timata ki te whakarongo ka pai koe! Inaianei kua mohio matou me pehea te whakaako i te drone ki te whakarongo ki nga whakahau reo. Inaa, he maha atu nga mea ka taea: te tohu kanohi a te kaiwhakamahi, nga rererangi motuhake, te tohu tohu me te maha atu.

He aha taau e whakaaro ai hei whakapai ake i te kaupapa?

Ka tūtohu a Skillbox:

Source: will.com

Tāpiri i te kōrero