hybridgroup / cylon-gpio

Cylon drivers for GPIO devices
http://cylonjs.com
Other
19 stars 14 forks source link

direct pin : uncaught , unspecified error event #49

Closed tsuncp closed 8 years ago

tsuncp commented 8 years ago

"use strict";
var camera = require('./Camera');
var deviceList = {
    lidar: { driver: "lidar-lite" }
}

var lidarList = [{ driver: 'direct-pin', pin: 40 }];

var Cylon = require("cylon");

var capturedTime = 0;
function init() {
    for (var i = 0; i < lidarList.length; i++) {
        deviceList["liderEn" + i] = {
            driver: 'direct-pin', pin: lidarList[i].pin
        }
    }
    Cylon.robot({
        connections: {
            raspi: { adaptor: "raspi" }
        },

        devices: deviceList,
        work: function (my) {
            every(100, function () {
                switchLider(my)
                my.lidar.distance(function (err, data) {
                    if (err) { console.error(err); }
                    console.log("distance: " + data);
                    if (data < 10) {
                        var time = Date.now()
                        if (time - capturedTime >= 10000) {
                            capturedTime = time;
                            camera.capture(time + ".jpg", function (err, timestamp, filename) {
                                if (err) {
                                    console.log(err)
                                } else {
                                    console.log("captured : " + filename)
                                }
                            })
                        }
                    }
                });
            });
        }
    }).start();
}

var currentPinIndex = 0;
function switchLider(my) {
    for (var i = 0; i < lidarList.length; i++) {
        var s = currentPinIndex++ == i?1:0;
        my["liderEn" + i].digitalWrite(s, function (err, val) {
            if (err) {
                console.log(err);
            }
        });
        if (currentPinIndex >= lidarList.length) {
            currentPinIndex = 0;
        }
    }
}

init();

Result :

2015-10-27T09:15:18.757Z : [Robot 1] - Starting connections. 2015-10-27T09:15:18.802Z : [Robot 1] - Starting connection 'raspi'. 2015-10-27T09:15:18.814Z : [Robot 1] - Starting devices. 2015-10-27T09:15:18.815Z : [Robot 1] - Starting device 'lidar'. 2015-10-27T09:15:18.817Z : [Robot 1] - Starting device 'liderEn0' on pin 40. 2015-10-27T09:15:18.817Z : [Robot 1] - Working. distance: 225

Error: Uncaught, unspecified "error" event. at Error (native) at DigitalPin.emit (events.js:87:13) at DigitalPin._setModeCallback (/home/pi/Documents/Projects/nodeDetect/node_modules/cylon/lib/io/digital-pin.js:152:17) at DigitalPin. (/home/pi/Documents/Projects/nodeDetect/node_modules/cylon/lib/io/digital-pin.js:146:10) at fs.js:1077:21 at FSReqWrap.oncomplete (fs.js:95:15)

Error is generated at the first time and program is down. Then run again , no error caused.
Restart program, same error happened. and run again . no problem.