2017-08-22 03:52:27 +00:00
|
|
|
'use strict'
|
|
|
|
|
2017-09-25 02:28:26 +00:00
|
|
|
const log = require('../log')('intval')
|
|
|
|
|
2017-08-28 12:49:47 +00:00
|
|
|
let Gpio
|
|
|
|
try {
|
|
|
|
Gpio = require('onoff').Gpio
|
|
|
|
} catch (e) {
|
2017-09-26 03:26:13 +00:00
|
|
|
log.warn('Failed including Gpio, using sim')
|
2017-08-28 12:49:47 +00:00
|
|
|
Gpio = require('../../lib/onoffsim').Gpio
|
|
|
|
}
|
|
|
|
|
2017-08-26 23:26:30 +00:00
|
|
|
|
2017-08-22 05:31:27 +00:00
|
|
|
const PINS = {
|
|
|
|
fwd : {
|
2017-09-19 19:06:34 +00:00
|
|
|
pin : 13,
|
2017-08-22 05:31:27 +00:00
|
|
|
dir : 'out'
|
|
|
|
},
|
|
|
|
bwd : {
|
2017-09-19 19:06:34 +00:00
|
|
|
pin : 19,
|
2017-08-22 05:31:27 +00:00
|
|
|
dir : 'out'
|
|
|
|
},
|
|
|
|
micro : {
|
2017-10-20 01:21:41 +00:00
|
|
|
pin : 5,
|
2017-08-22 05:31:27 +00:00
|
|
|
dir : 'in',
|
2017-09-19 19:06:34 +00:00
|
|
|
edge : 'both'
|
2017-08-22 05:31:27 +00:00
|
|
|
},
|
|
|
|
release : {
|
2017-10-20 01:21:41 +00:00
|
|
|
pin : 6,
|
2017-08-22 05:31:27 +00:00
|
|
|
dir : 'in',
|
|
|
|
edge : 'both'
|
|
|
|
}
|
|
|
|
}
|
2017-08-22 03:52:27 +00:00
|
|
|
|
2017-09-17 23:51:33 +00:00
|
|
|
/** Class representing the intval3 features */
|
2017-08-22 03:52:27 +00:00
|
|
|
class Intval {
|
|
|
|
constructor () {
|
|
|
|
this._pin = {}
|
2017-08-29 12:23:52 +00:00
|
|
|
this._state = {
|
|
|
|
dir : true, //forward
|
2017-08-30 02:27:56 +00:00
|
|
|
frame : {
|
2017-09-23 12:53:48 +00:00
|
|
|
start : 0, //time frame started, timestamp
|
|
|
|
active : false, //should frame be running
|
|
|
|
time : 0, //length of frame, in ms
|
|
|
|
delay : 0, //delay before start of frame, in ms
|
|
|
|
|
2017-09-25 02:28:26 +00:00
|
|
|
expected : 1000 //expected length of frame, in ms
|
2017-09-17 23:51:33 +00:00
|
|
|
},
|
|
|
|
release : {
|
|
|
|
time: 0,
|
2017-09-23 12:53:48 +00:00
|
|
|
active : false //is pressed
|
|
|
|
},
|
|
|
|
micro : {
|
|
|
|
time : 0,
|
|
|
|
primed : false //is ready to stop frame
|
2017-08-30 02:27:56 +00:00
|
|
|
}
|
2017-08-29 12:23:52 +00:00
|
|
|
}
|
2017-09-26 03:26:13 +00:00
|
|
|
|
2017-09-18 00:25:21 +00:00
|
|
|
this._releaseMin = 50
|
|
|
|
this._releaseSequence = 1000
|
2017-09-25 02:28:26 +00:00
|
|
|
this._microDelay = 10 // delay after stop signal before stopping motors
|
2017-09-26 03:26:13 +00:00
|
|
|
|
2017-08-22 03:52:27 +00:00
|
|
|
this._declarePins()
|
2017-09-17 23:51:33 +00:00
|
|
|
process.on('SIGINT', this._undeclarePins)
|
2017-09-26 03:26:13 +00:00
|
|
|
process.on('uncaughtException', this._undeclarePins)
|
2017-08-22 03:52:27 +00:00
|
|
|
}
|
2017-09-17 23:51:33 +00:00
|
|
|
/**
|
|
|
|
* (internal function) Declares all Gpio pins that will be used
|
|
|
|
*
|
|
|
|
*/
|
2017-08-22 03:52:27 +00:00
|
|
|
_declarePins () {
|
2017-09-19 19:06:34 +00:00
|
|
|
let pin
|
|
|
|
for (let p in PINS) {
|
|
|
|
pin = PINS[p]
|
|
|
|
if (pin.edge) this._pin[p] = Gpio(pin.pin, pin.dir, pin.edge)
|
|
|
|
if (!pin.edge) this._pin[p] = Gpio(pin.pin, pin.dir)
|
2017-09-26 03:26:13 +00:00
|
|
|
log.info('_declarePins', { pin : pin.pin, dir : pin.dir, edge : pin.edge })
|
2017-09-19 19:06:34 +00:00
|
|
|
}
|
2017-08-28 12:49:47 +00:00
|
|
|
this._pin.release.watch(this._watchRelease)
|
2017-08-22 04:49:08 +00:00
|
|
|
}
|
2017-09-19 19:23:48 +00:00
|
|
|
/**
|
2017-09-17 23:51:33 +00:00
|
|
|
* (internal function) Undeclares all Gpio in event of uncaught error
|
|
|
|
* that interupts the node process
|
|
|
|
*
|
|
|
|
*/
|
2017-10-20 01:42:52 +00:00
|
|
|
_undeclarePins (e) {
|
|
|
|
log.error(e)
|
2017-10-20 01:34:14 +00:00
|
|
|
if (!this._pin) {
|
|
|
|
log.warn('_undeclarePins', { reason : 'No pins'})
|
|
|
|
return process.exit()
|
|
|
|
}
|
2017-09-26 03:26:13 +00:00
|
|
|
log.warn('_undeclarePins', { pin : PINS.fwd.pin, val : 0, reason : 'exiting'})
|
|
|
|
this._pin.fwd.writeSync(0)
|
|
|
|
log.warn('_undeclarePins', { pin : PINS.bwd.pin, val : 0, reason : 'exiting'})
|
|
|
|
this._pin.bwd.writeSync(0)
|
2017-09-16 17:35:39 +00:00
|
|
|
this._pin.fwd.unexport()
|
|
|
|
this._pin.bwd.unexport()
|
|
|
|
this._pin.micro.unexport()
|
|
|
|
this._pin.release.unexport()
|
2017-09-26 03:26:13 +00:00
|
|
|
process.exit()
|
2017-09-16 17:35:39 +00:00
|
|
|
}
|
2017-09-19 00:46:18 +00:00
|
|
|
/**
|
|
|
|
* Start motor in forward direction by setting correct pins in h-bridge
|
|
|
|
*
|
|
|
|
*/
|
2017-09-16 17:35:39 +00:00
|
|
|
_startFwd () {
|
2017-09-25 02:28:26 +00:00
|
|
|
this._pin.fwd.writeSync(1)
|
|
|
|
this._pin.bwd.writeSync(0)
|
2017-08-28 12:49:47 +00:00
|
|
|
//start high-cpu watch
|
2017-08-22 04:49:08 +00:00
|
|
|
}
|
2017-09-19 00:46:18 +00:00
|
|
|
/**
|
|
|
|
* Start motor in backward direction by setting correct pins in h-bridge
|
|
|
|
*
|
|
|
|
*/
|
2017-08-22 04:49:08 +00:00
|
|
|
_startBwd () {
|
2017-09-25 02:28:26 +00:00
|
|
|
this._pin.fwd.writeSync(0)
|
|
|
|
this._pin.bwd.writeSync(1)
|
2017-08-22 04:49:08 +00:00
|
|
|
}
|
2017-09-19 19:23:48 +00:00
|
|
|
/**
|
2017-09-19 00:46:18 +00:00
|
|
|
* Stop motor by setting both motor pins to 0 (LOW)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
_stop () {
|
2017-09-25 02:28:26 +00:00
|
|
|
this._pin.fwd.writeSync(0)
|
|
|
|
this._pin.bwd.writeSync(0)
|
2017-09-19 00:46:18 +00:00
|
|
|
|
|
|
|
let len = (+new Date()) - this._state.frame.start
|
|
|
|
|
2017-09-26 03:26:13 +00:00
|
|
|
log.info(`Frame stopped ${len}ms`)
|
2017-09-19 00:46:18 +00:00
|
|
|
|
|
|
|
this._pin.micro.unwatch()
|
|
|
|
this._state.frame.active = false
|
2017-09-23 12:53:48 +00:00
|
|
|
this._state.frame.start = 0
|
2017-09-19 00:46:18 +00:00
|
|
|
}
|
2017-09-19 19:06:34 +00:00
|
|
|
/**
|
2017-09-19 19:20:58 +00:00
|
|
|
* Callback for watching relese switch state changes.
|
|
|
|
* Using GPIO 06 on Raspberry Pi Zero W.
|
2017-09-25 02:28:26 +00:00
|
|
|
*
|
|
|
|
* 1) If closed AND frame active, start timer, set state primed to `true`.
|
|
|
|
* 1) If opened AND frame active, stop frame
|
2017-09-19 19:20:58 +00:00
|
|
|
*
|
|
|
|
* Microswitch + 10K ohm resistor
|
|
|
|
* * 1 === open
|
|
|
|
* * 0 === closed
|
|
|
|
*
|
2017-09-19 19:06:34 +00:00
|
|
|
*
|
|
|
|
* @param {object} err Error object present if problem reading pin
|
|
|
|
* @param {integer} val Current value of the pin
|
|
|
|
*
|
|
|
|
*/
|
2017-08-29 12:23:52 +00:00
|
|
|
_watchMicro (err, val) {
|
2017-09-25 02:28:26 +00:00
|
|
|
const NOW = +new Date()
|
2017-08-29 12:23:52 +00:00
|
|
|
if (err) {
|
2017-09-26 03:26:13 +00:00
|
|
|
log.error('_watchMicro', err)
|
2017-08-29 12:23:52 +00:00
|
|
|
}
|
2017-08-30 02:27:56 +00:00
|
|
|
//determine when to stop
|
2017-09-25 02:28:26 +00:00
|
|
|
if (val === 0 && this._state.frame.active) {
|
|
|
|
if (!this._state.micro.primed) {
|
|
|
|
this._state.micro.primed = true
|
|
|
|
this._state.micro.time = NOW
|
2017-09-26 03:26:13 +00:00
|
|
|
log.info('Mircoswitch primed to stop motor')
|
2017-09-25 02:28:26 +00:00
|
|
|
}
|
|
|
|
} else if (val === 1 && this._state.frame.active) {
|
|
|
|
if (this._state.micro.primed) {
|
|
|
|
this._state.micro.primed = false
|
|
|
|
this._state.micro.time = 0
|
|
|
|
setTimeout( () => {
|
2017-09-26 03:26:13 +00:00
|
|
|
log.info(`Stopped frame after ${NOW - this._state.micro.time}ms`)
|
2017-09-25 02:28:26 +00:00
|
|
|
}, this._microDelay)
|
|
|
|
}
|
|
|
|
}
|
2017-08-29 12:23:52 +00:00
|
|
|
}
|
2017-09-19 19:06:34 +00:00
|
|
|
/**
|
2017-09-19 19:20:58 +00:00
|
|
|
* Callback for watching relese switch state changes.
|
|
|
|
* Using GPIO 05 on Raspberry Pi Zero W.
|
|
|
|
*
|
|
|
|
* 1) If closed, start timer.
|
|
|
|
* 2) If opened, check timer AND
|
2017-10-20 01:30:07 +00:00
|
|
|
* 3) If `press` (`now - this._state.release.time`) greater than minimum and less than `this._releaseSequence`, start frame
|
2017-09-19 19:20:58 +00:00
|
|
|
* 4) If `press` greater than `this._releaseSequence`, start sequence
|
|
|
|
*
|
|
|
|
* Button + 10K ohm resistor
|
|
|
|
* * 1 === open
|
|
|
|
* * 0 === closed
|
2017-09-19 19:06:34 +00:00
|
|
|
*
|
|
|
|
* @param {object} err Error object present if problem reading pin
|
|
|
|
* @param {integer} val Current value of the pin
|
|
|
|
*
|
|
|
|
*/
|
2017-08-28 12:49:47 +00:00
|
|
|
_watchRelease (err, val) {
|
2017-10-20 01:30:07 +00:00
|
|
|
const now = +new Date()
|
2017-09-18 00:25:21 +00:00
|
|
|
let press = 0
|
2017-08-28 12:49:47 +00:00
|
|
|
if (err) {
|
2017-09-26 03:26:13 +00:00
|
|
|
return log.error(err)
|
2017-08-28 12:49:47 +00:00
|
|
|
}
|
2017-09-26 03:26:13 +00:00
|
|
|
log.info(`Release switch val: ${val}`)
|
2017-09-17 23:51:33 +00:00
|
|
|
if (val === 0) {
|
2017-09-23 12:53:48 +00:00
|
|
|
//closed
|
2017-10-20 01:30:07 +00:00
|
|
|
if (this._releaseClosedState(now)) {
|
|
|
|
this._state.release.time = now
|
2017-09-17 23:51:33 +00:00
|
|
|
this._state.release.active = true //maybe unncecessary
|
2017-09-18 00:25:21 +00:00
|
|
|
}
|
|
|
|
} else if (val === 1) {
|
2017-09-23 12:53:48 +00:00
|
|
|
//opened
|
2017-09-18 00:25:21 +00:00
|
|
|
if (this._state.release.active) {
|
2017-10-20 01:30:07 +00:00
|
|
|
press = now - this._state.release.time
|
2017-09-18 00:25:21 +00:00
|
|
|
if (press > this._releaseMin && press < this._releaseSequence) {
|
2017-09-17 23:51:33 +00:00
|
|
|
this.frame()
|
2017-09-18 00:25:21 +00:00
|
|
|
} else if (press >= this._releaseSequence) {
|
2017-09-17 23:51:33 +00:00
|
|
|
this.sequence()
|
|
|
|
}
|
2017-09-26 03:26:13 +00:00
|
|
|
log.info(`Release closed for ${press}ms`)
|
2017-09-17 23:51:33 +00:00
|
|
|
this._state.release.time = 0
|
|
|
|
this._state.release.active = false
|
|
|
|
}
|
|
|
|
}
|
2017-10-20 01:24:20 +00:00
|
|
|
log.info('completed if statement')
|
2017-08-28 12:49:47 +00:00
|
|
|
}
|
2017-10-20 01:30:07 +00:00
|
|
|
|
|
|
|
_releaseClosedState (now) {
|
|
|
|
if (!this._state.release.active && this._state.release.time === 0) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if (this._state.release.active && (now - this._state.release.time) > (this._releaseSequence * 10)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2017-09-19 19:40:42 +00:00
|
|
|
/**
|
|
|
|
* Set the default direction of the camera.
|
|
|
|
* * forward = true
|
|
|
|
* * backward = false
|
|
|
|
*
|
|
|
|
* @param {boolean} [dir=true] Direction of the camera
|
|
|
|
*
|
|
|
|
*/
|
2017-08-29 12:23:52 +00:00
|
|
|
setDir (val = true) {
|
|
|
|
if (typeof val !== 'boolean') {
|
2017-09-26 03:26:13 +00:00
|
|
|
return log.warn('Direction must be represented as either true or false')
|
2017-08-29 12:23:52 +00:00
|
|
|
}
|
|
|
|
this._state.dir = val
|
|
|
|
}
|
2017-09-17 23:51:33 +00:00
|
|
|
/**
|
|
|
|
* Begin a single frame with set variables or defaults
|
|
|
|
*
|
2017-09-19 19:20:58 +00:00
|
|
|
* @param {?boolean} [dir="null"] (optional) Direction of the frame
|
|
|
|
* @param {?integer} [time="null"] (optional) Exposure time, 0 = minimum
|
2017-09-17 23:51:33 +00:00
|
|
|
*
|
|
|
|
*/
|
2017-09-26 03:26:13 +00:00
|
|
|
frame (dir = null, time = null) {
|
2017-09-17 23:51:33 +00:00
|
|
|
if (dir === true || (dir === null && this._state.dir === true) ) {
|
|
|
|
dir = true
|
|
|
|
} else {
|
|
|
|
dir = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (time === null && this._state.time !== 0) {
|
|
|
|
time = this._state.time
|
|
|
|
} else {
|
|
|
|
time = 0
|
|
|
|
}
|
|
|
|
|
2017-08-30 02:27:56 +00:00
|
|
|
this._state.frame.start = +new Date()
|
|
|
|
this._state.frame.active = true
|
2017-08-29 12:23:52 +00:00
|
|
|
this._pin.micro.watch(this._watchMicro)
|
2017-09-17 23:51:33 +00:00
|
|
|
|
2017-09-26 03:26:13 +00:00
|
|
|
log.info('frame', {dir : dir, time : time})
|
|
|
|
|
|
|
|
if (dir) {
|
|
|
|
this._startFwd()
|
2017-08-22 04:49:08 +00:00
|
|
|
} else {
|
2017-09-26 03:26:13 +00:00
|
|
|
this._startBwd()
|
2017-08-22 04:49:08 +00:00
|
|
|
}
|
2017-08-22 03:52:27 +00:00
|
|
|
}
|
2017-09-19 19:40:42 +00:00
|
|
|
/**
|
|
|
|
* Start a sequence of frames, using defaults or explicit instructions
|
|
|
|
*
|
|
|
|
*/
|
2017-09-17 23:51:33 +00:00
|
|
|
sequence () {
|
2017-09-26 03:26:13 +00:00
|
|
|
log.info('sequence', `Started sequence`)
|
2017-09-17 23:51:33 +00:00
|
|
|
}
|
2017-08-22 04:00:24 +00:00
|
|
|
status () {
|
2017-08-29 12:23:52 +00:00
|
|
|
return this._state
|
2017-08-22 04:00:24 +00:00
|
|
|
}
|
2017-08-22 03:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = new Intval()
|