UNPKG

obniz

Version:

obniz sdk for javascript

90 lines (78 loc) 1.67 kB
class DCMotor { constructor() { this.keys = ['forward', 'back']; this.requiredKeys = ['forward', 'back']; } static info() { return { name: 'DCMotor', }; } wired(obniz) { this.status = { direction: null, power: null, }; this.pwm1_io_num = this.params.forward; this.pwm2_io_num = this.params.back; this.pwm1 = obniz.getFreePwm(); this.pwm1.start({ io: this.pwm1_io_num }); this.pwm1.freq(100000); this.pwm2 = obniz.getFreePwm(); this.pwm2.start({ io: this.pwm2_io_num }); this.pwm2.freq(100000); this.power(30); } // Module functions forward() { this.move(true); } reverse() { this.move(false); } stop() { if (this.status.direction === null) { return; } this.status.direction = null; this.pwm1.duty(0); this.pwm2.duty(0); } move(forward) { if (forward) { if (this.status.direction === true) { return; } this.status.direction = true; } else { if (this.status.direction === false) { return; } this.status.direction = false; } let power = this.power(); this.power(0); this.power(power); } power(power) { if (power === undefined) { return this.status.power; } this.status.power = power; if (this.status.direction === null) { this.pwm1.duty(0); this.pwm2.duty(0); return; } if (this.status.direction) { this.pwm1.duty(power); this.pwm2.duty(0); } else { this.pwm1.duty(0); this.pwm2.duty(power); } } } if (typeof module === 'object') { module.exports = DCMotor; }