我需要你帮助我的网络服务器控制的机器人项目。 我尝试通过网站控制两个电机。
很多东西已经开始工作,所以我可以打开一个网站并按“w”键,两个电机都开始向前运行。 (我的控件:w = 向前,s = 向后,a = 向左,d = 向右)
但是另一个命令不再被处理。
在命令行上我可以看到此错误消息:
Missing error handler on `socket`.
TypeError: task is not a function
at /home/pi/tank/node_modules/async/dist/async.js:5285:13
at replenish (/home/pi/tank/node_modules/async/dist/async.js:871:21)
at /home/pi/tank/node_modules/async/dist/async.js:881:15
at _parallel (/home/pi/tank/node_modules/async/dist/async.js:5284:9)
at parallelLimit (/home/pi/tank/node_modules/async/dist/async.js:5317:14)
at Object.parallel (/home/pi/tank/node_modules/async/dist/async.js:930:20)
at Object.tank.moveForward (/home/pi/tank/app.js:46:9)
at Socket.<anonymous> (/home/pi/tank/app.js:87:9)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
问题似乎出在 moveForward : function() 和 async.parallel 上 (其他移动功能也有同样的问题。)
var tank = {
//we create an object with the used pins
motors : {
leftFront: 11,
leftBack: 12,
rightFront: 15,
rightBack: 16
},
//we open the gpio pins and set the as outputs
init : function(){
gpio.open(this.motors.leftFront, "output");
gpio.open(this.motors.leftBack, "output");
gpio.open(this.motors.rightFront, "output");
gpio.open(this.motors.rightBack, "output");
},
//in order to move the tank forward, we supply both motors
moveForward : function(){
async.parallel([
gpio.write(this.motors.leftFront, 1),
gpio.write(this.motors.rightFront, 1)
])
},
//in order to move the tank backwards, we supply both motors, but with inverse polarity
moveBackward : function(){
async.parallel([
gpio.write(this.motors.leftBack, 1),
gpio.write(this.motors.rightBack, 1)
]);
},
//in order to turn right, we supply on the left
moveLeft : function(){
gpio.write(this.motors.leftFront, 1);
},
//in order to turn left, we supply on the right
moveRight : function(){
gpio.write(this.motors.rightFront, 1);
},
//in order to stop both motors, we set all pins to 0 value
stop : function(){
async.parallel([
gpio.write(this.motors.leftFront, 0),
gpio.write(this.motors.leftBack, 0),
gpio.write(this.motors.rightFront, 0),
gpio.write(this.motors.rightBack, 0)
]);
}
};
请帮忙!
最佳答案
您没有正确使用async.parallel()
。它需要一个函数数组,并且 gpio.write()
不返回函数。
你需要做这样的事情:
var self = this;
async.parallel([
function (callback) {
gpio.write(self.motors.leftBack, 1, callback);
},
function (callback) {
gpio.write(self.motors.rightBack, 1, callback);
}
]);
为了避免额外的样板文件,您还可以提取一个根据需要创建必要函数的函数:
write: function (motor, value) {
return function (callback) {
gpio.write(motor, value, callback);
};
},
moveForward : function(){
async.parallel([
this.write(this.motors.leftFront, 1),
this.write(this.motors.rightFront, 1)
]);
},
关于javascript - `socket` TypeError : task is not a function - node. js async.js 上缺少错误处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38294553/