Friday, 21 April 2017

Node.js functions not working when calling upon them

Im using an Rapsberry pi 2 to connect to an Parrot Ar drone using the node-ar-drone library. I have tried the test scripts from the library and they work fine. But when I then add the rpi-gpio library and add buttons to my raspberry to be able to control different movements like forward, back, left, right, etc. It does not work.

I have made functions for the different directions, I then call the functions in another function that checks if a button on a specific GPIO pin is pressed, and then the function executes if a button press is detected. And in the end I have all the functions that check the GPIO pins for button presses in a while loop.

Everytime I execute the script I get an "TypeError: Cannot read property '38' of undefined" error

I have tried to run a single direction function with rpi-gpio and a button connected and that worked just fine.

So it seems like something happens when I call the function that checks the GPIO pins.

This is the error that I get:

pi@raspberrypi:~/node_modules/ar-drone/examples $ sudo node test5.js
/home/pi/node_modules/rpi-gpio/rpi-gpio.js:329
        return currentPins[channel] + '';
                          ^

TypeError: Cannot read property '38' of undefined
    at getPinRpi (/home/pi/node_modules/rpi-gpio/rpi-gpio.js:329:27)
    at Gpio.read.input (/home/pi/node_modules/rpi-gpio/rpi-gpio.js:248:19)
    at readInput_FlightTakeoff (/home/pi/node_modules/ar-
drone/examples/test5.js:125:10)
    at Object.<anonymous> (/home/pi/node_modules/ar-
drone/examples/test5.js:214:3)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)

When I run this script I get the error message:

var arDrone = require('ar-drone');
var gpio = require('rpi-gpio');

// Pin setup - start
gpio.setup(29, gpio.DIR_IN, readInput_FlightFront);
gpio.setup(31, gpio.DIR_IN, readInput_FlightBack); // Typical setup
gpio.setup(33, gpio.DIR_IN, readInput_FlightLeft);
gpio.setup(35, gpio.DIR_IN, readInput_FlightRight);
gpio.setup(36, gpio.DIR_IN, readInput_FlightDown);
gpio.setup(37, gpio.DIR_IN, readInput_FlightUp);
gpio.setup(38, gpio.DIR_IN, readInput_FlightTakeoff);
gpio.setup(40, gpio.DIR_IN, readInput_FlightLand);
// Pin setup - stop


// Function setup - start
function flightFuncTakeoff(){
// function that makes the drone take off
  var client = arDrone.createClient();
  client.disableEmergency();

    client.takeoff();
    client.stop();
}

function flightFuncLand(){
// function that makes the drone stop it's movement and land
  var client = arDrone.createClient();
  client.disableEmergency();

    client.stop();
    client.land();
}

function flightFuncFront(){
// function that makes the drone fly forwards
  var client = arDrone.createClient();
  client.disableEmergency();

    client
      .after(500, function() {
        this.front(0.5);
      })
      .after(1000, function() {
        this.stop();
      })
}

function flightFuncBack(){
// function that makes the drone fly backwards
  var client = arDrone.createClient();
  client.disableEmergency();

    client
      .after(500, function() {
        this.back(0.5);
      })
      .after(1000, function() {
        this.stop();
      })
}

function flightFuncLeft(){
// function that makes the drone fly to the left
  var client = arDrone.createClient();
  client.disableEmergency();

    client
      .after(500, function() {
        this.left(0.5);
      })
      .after(1000, function() {
        this.stop();
      })
}

function flightFuncRight(){
// function that makes the drone fly to the right
  var client = arDrone.createClient();
  client.disableEmergency();

    client
      .after(500, function() {
        this.right(0.5);
      })
      .after(1000, function() {
        this.stop();
      })
}

function flightFuncUp(){
// function that makes the drone fly upwards
  var client = arDrone.createClient();
  client.disableEmergency();

    client
      .after(500, function() {
        this.up(0.5);
      })
      .after(1000, function() {
        this.stop();
      })
}

function flightFuncDown(){
// function that makes the drone fly downwards
  var client = arDrone.createClient();
  client.disableEmergency();

    client
      .after(500, function() {
        this.down(0.5);
      })
      .after(1000, function() {
        this.stop();
      })
}
// Function setup - stop



// Special function - start
function readInput_FlightTakeoff() {
// readInput from pin 38
    gpio.read(38, function(err, value) {
        if(value == false){
          flightFuncTakeoff(); // run the function
        } else {
          process.stdout.write(""); // will print out an error message.
        }
    });
}

function readInput_FlightLand() {
// readinput from pin 40
    gpio.read(40, function(err, value) {
        if(value == false){
          flightFuncLand(); // run the function
        } else {
          process.stdout.write(""); // will print out an error message.
        }
    });
}

function readInput_FlightFront() {
// readinput from pin 29
    gpio.read(29, function(err, value) {
        if(value == false){
          flightFuncFront(); // run the function
        } else {
          process.stdout.write(""); // will print out an error message.
        }
    });
}

function readInput_FlightBack() {
// readinput from pin 31
    gpio.read(31, function(err, value) {
        if(value == false){
          flightFuncBack(); // run the function
        } else {
          process.stdout.write(""); // will print out an error message. 
        }
    });
}

function readInput_FlightLeft() {
// readinput from pin 33
    gpio.read(33, function(err, value) {
        if(value == false){
          flightFuncLeft(); // run the function
        } else {
          process.stdout.write(""); // will print out an error message.
        }
    });
}
function readInput_FlightRight() {
// readinput from pin 35
    gpio.read(35, function(err, value) {
        if(value == false){
          flightFuncRight(); // run the function
        } else {
          process.stdout.write(""); // will print out an error message.
        }
    });    
}
function readInput_FlightUp() {
// readinput from pin 37
    gpio.read(37, function(err, value) {
        if(value == false){
          flightFuncUp(); // run the function
        } else {
          process.stdout.write(""); // will print out an error message.
        }
    });
}
function readInput_FlightDown() {
// readinput from pin 36
    gpio.read(36, function(err, value) {
        if(value == false){
          flightFuncDown(); // run the function
        } else {
          process.stdout.write(""); // will print out an error message.
        }
    });
}
// Special function - stop


while(true == true) {
  readInput_FlightTakeoff();
  readInput_FlightLand();
  readInput_FlightFront();
  readInput_FlightBack();
  readInput_FlightLeft();
  readInput_FlightRight();
  readInput_FlightUp();
  readInput_FlightDown();
}



via AskGoogle

No comments:

Post a Comment