-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathz-axis.js
92 lines (76 loc) · 1.83 KB
/
z-axis.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
let five = require('johnny-five')
class zAxis {
constructor() {
this.position = {step: null}
this.stepPin = new five.Pin({pin: 4, type: 'digital', mode: 1})
this.dir = new five.Pin({pin: 7, type: 'digital', mode: 1})
this.limit = -190
// Set inital direction
this.dir.low() // Go up by default
// Set initial step value
this.stepPin.low()
this.limitSwitch = new five.Button({
pin: 12,
invert: true
})
this.limitSwitch.on('press', () => {
console.log( 'Z closed' )
})
this.limitSwitch.on('release', () => {
console.log( 'Z open' )
})
}
step(numSteps = 1) {
if (numSteps >= 0) {
this.dir.low()
} else {
this.dir.high()
}
for (let i = 0; i < Math.abs(numSteps); i++) {
this.stepPin.low()
this.stepPin.high()
}
}
up(numSteps = 1) {
// Check if valid
if (this.position.step == null) { return }
if (typeof(numSteps) !== 'number') { return }
if (numSteps <= 0) { return }
if (this.position.step + numSteps > 0) {
return
} else {
this.step(numSteps)
this.position.step += numSteps
}
}
down(numSteps = 1) {
// Check if valid
if (this.position.step == null) { return }
if (typeof(numSteps) !== 'number') { return }
if (numSteps <= 0) { return }
if (this.position.step - numSteps < this.limit) {
return
} else {
this.step(-numSteps)
this.position.step -= numSteps
}
}
home(interval = 4) {
if (this.limitSwitch.value == 1) {
this.position.step = 0
return
} else {
this.step(1)
setTimeout(this.home.bind(this), interval, interval)
}
}
}
module.exports = zAxis
// Test cases
// >> z.down(50)
// >> z.down()
// >> z.down(0)
// >> z.down(1000)
// >> z.down(-1000)
// >> z.down('yo')
// >> z.home()