I need your help with my webserver controlled robot project. I try to control two motors via website.
A lot of stuff is already working so I can open a website and hit the "w" key and both Motors start to run forward. (MyControlls: w = Forward, s = backward, a = left, d = right)
But another command is not processed anymore.
On command line I can see this error message:
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)
It seem that the problem is around the moveForward : function() and async.parallel (The other Move-functions have the same issue.)
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)
]);
}
};
Please help!
You're not using async.parallel()
correctly. It takes an array of functions, and gpio.write()
does not return a function.
You need to do something like this:
var self = this;
async.parallel([
function (callback) {
gpio.write(self.motors.leftBack, 1, callback);
},
function (callback) {
gpio.write(self.motors.rightBack, 1, callback);
}
]);
In order to avoid the extra boilerplate, you could also factor out a function that creates the necessary functions as needed:
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)
]);
},
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.