From 50a335af95c0b6816126ddfddbba8f6eaa1dcb99 Mon Sep 17 00:00:00 2001 From: mmcwilliams Date: Sun, 24 Sep 2017 22:28:26 -0400 Subject: [PATCH] Add log library and microswitch logic --- lib/intval/index.js | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/lib/intval/index.js b/lib/intval/index.js index b3659f3..463c1bf 100644 --- a/lib/intval/index.js +++ b/lib/intval/index.js @@ -1,5 +1,7 @@ 'use strict' +const log = require('../log')('intval') + let Gpio try { Gpio = require('onoff').Gpio @@ -42,7 +44,7 @@ class Intval { time : 0, //length of frame, in ms delay : 0, //delay before start of frame, in ms - expected : 0 //expected length of frame, in ms + expected : 1000 //expected length of frame, in ms }, release : { time: 0, @@ -55,6 +57,7 @@ class Intval { } this._releaseMin = 50 this._releaseSequence = 1000 + this._microDelay = 10 // delay after stop signal before stopping motors this._declarePins() process.on('SIGINT', this._undeclarePins) } @@ -88,8 +91,8 @@ class Intval { * */ _startFwd () { - this._pin.fwd.set(1) - this._pin.bwd.set(0) + this._pin.fwd.writeSync(1) + this._pin.bwd.writeSync(0) //start high-cpu watch } /** @@ -97,16 +100,16 @@ class Intval { * */ _startBwd () { - this._pin.fwd.set(0) - this._pin.bwd.set(1) + this._pin.fwd.writeSync(0) + this._pin.bwd.writeSync(1) } /** * Stop motor by setting both motor pins to 0 (LOW) * */ _stop () { - this._pin.fwd.set(0) - this._pin.bwd.set(0) + this._pin.fwd.writeSync(0) + this._pin.bwd.writeSync(0) let len = (+new Date()) - this._state.frame.start @@ -119,8 +122,9 @@ class Intval { /** * Callback for watching relese switch state changes. * Using GPIO 06 on Raspberry Pi Zero W. - * * If closed, start timer. - * * If opened, check timer AND + * + * 1) If closed AND frame active, start timer, set state primed to `true`. + * 1) If opened AND frame active, stop frame * * Microswitch + 10K ohm resistor * * 1 === open @@ -132,11 +136,26 @@ class Intval { * */ _watchMicro (err, val) { + const NOW = +new Date() if (err) { console.error(err) } - this._state.frame.val = val //determine when to stop + if (val === 0 && this._state.frame.active) { + if (!this._state.micro.primed) { + this._state.micro.primed = true + this._state.micro.time = NOW + console.log('Mircoswitch primed to stop motor') + } + } else if (val === 1 && this._state.frame.active) { + if (this._state.micro.primed) { + this._state.micro.primed = false + this._state.micro.time = 0 + setTimeout( () => { + console.log(`Stopped frame after ${NOW - this._state.micro.time}ms`) + }, this._microDelay) + } + } } /** * Callback for watching relese switch state changes. @@ -178,7 +197,7 @@ class Intval { } else if (press >= this._releaseSequence) { this.sequence() } - console.log(`Release closed for ${press}`) + console.log(`Release closed for ${press}ms`) this._state.release.time = 0 this._state.release.active = false }