ev3js
Version:
LEGO Mindstorms EV3 API for Node.js
107 lines (100 loc) • 4.07 kB
JavaScript
var Device = require('./device.js');
var Motor = function(port){
port = port.toUpperCase();
if(port.match(/^[A-D]$/)){
this.path += '/' + this.search('out' + port) + '/';
}else{
throw new Error('Port should be "A"-"D".');
}
};
Motor.prototype = new Device();
(function(){
this.path = '/sys/class/tacho-motor';
this.accessor('duty_cycle', 'str2num', false);
this.accessor('duty_cycle_sp', 'str2num', function(n){
return Math.max(Math.min(n, 100), -100);
});
this.accessor('encoder_mode', true, true);
this.accessor('encoder_modes', true, false);
this.accessor('estop', 'strnum2bool', 'bool2num');
this.accessor('log', true, false);
this.accessor('polarity_mode', true, true);
this.accessor('polarity_modes', true, false);
this.accessor('port_name', true, false);
this.accessor('position', 'str2num', true);
this.accessor('position_mode', true, true);
this.accessor('position_modes', true, false);
this.accessor('position_sp', 'str2num', true);
this.accessor('pulses_per_second', 'str2num', false);
this.accessor('pulses_per_second_sp', 'str2num', function(n){
return Math.max(Math.min(n, 2000), -2000);
});
this.accessor('ramp_up_sp', 'str2num', true);
this.accessor('ramp_down_sp', 'str2num', true);
this.accessor('regulation_mode', 'str2onoff', 'onoff2str');
this.accessor('regulation_modes', 'str2onoff', false);
this.accessor('reset', false, 'bool2num');
this.accessor('run', 'strnum2bool', 'bool2num');
this.accessor('run_mode', true, true);
this.accessor('run_modes', true, false);
this.accessor('speed_regulation_D', 'str2num', true);
this.accessor('speed_regulation_I', 'str2num', true);
this.accessor('speed_regulation_K', 'str2num', true);
this.accessor('speed_regulation_P', 'str2num', true);
this.accessor('state', true, false);
this.accessor('stop_mode', true, true);
this.accessor('stop_modes', true, false);
this.accessor('time_sp', true, true);
this.accessor('type', true, false);
this._run = this.run;
this.run = function(option, callback){
if(typeof option === 'object'){
var sets = {};
if(option.speed != null){
sets.regulationMode = true;
sets.pulsesPerSecondSp = option.speed;
} else if(option.power != null){
sets.regulationMode = false;
}
if(option.power != null){
sets.dutyCycleSp = option.power;
}
if(typeof option.ramp === 'number'){
option.ramp = [option.ramp, option.ramp];
} else if(typeof option.ramp !== 'object' || option.ramp.constructor !== Array){
option.ramp = [0, 0];
}
switch(option.mode){
case 'time':
this._run(false);
sets.runMode = 'time';
sets.timeSp = option.time;
sets.rampUpSp = option.ramp[0];
sets.rampDownSp = option.ramp[1];
break;
case 'position':
sets.runMode = 'position';
sets.positionMode = option.position || 'absolute';
sets.rampUpSp = option.ramp[0];
sets.rampDownSp = option.ramp[1];
sets.positionSp = option.target;
break;
case 'forever':
default:
sets.runMode = 'forever';
}
if(option.initialize != null){
sets.position = option.initialize;
}
var i = Object.keys(sets).length;
for(var name in sets){
this[name](sets[name], function(){
--i || this._run(true, callback);
}.bind(this));
}
} else {
return this._run(option, callback);
}
};
}.bind(Motor.prototype))();
module.exports = Motor;