-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
153 lines (120 loc) · 3.26 KB
/
app.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
var Gpio = require('pigpio').Gpio,
topMotor = new Gpio(23, { mode: Gpio.OUTPUT }),
bottomMotor = new Gpio(18, { mode: Gpio.OUTPUT }),
button = new Gpio(15, {
mode: Gpio.INPUT,
pullUpDown: Gpio.PUD_UP,
edge: Gpio.FALLING_EDGE
}),
counterButton = new Gpio(22, {
mode: Gpio.INPUT,
pullUpDown: Gpio.PUD_UP,
edge: Gpio.FALLING_EDGE
}),
timeout = null,
port = 5000,
counter = 0,
making = false;
// ratio = 80 / 14,
// layerAngle = 60,
// motorAngle = ratio * layerAngle;
var express = require('express'),
app = express();
app.use(express.static(__dirname + '/public'));
app.post('/feed', function (req, res) {
response = handleInterrupt(0) ? 'Yep' : 'Nop';
res.send(response);
});
app.listen(port, function () {
console.log('Pi listening on port ' + port);
});
// Raspberry
button.on('interrupt', handleInterrupt);
function handleInterrupt (level) {
if (level !== 0 || timeout) return false;
timeout = setTimeout(initialize, 200);
return true;
}
function initialize () {
if (making) {
stop(bottomMotor);
stop(topMotor);
making = false;
} else {
turn(bottomMotor, 2000);
turn(topMotor, 1000);
making = true;
}
timeout = null;
// nextStage(true).then(nextStage).then(nextStage).then(function () {
// timeout = null;
// });
}
function nextStage (bindHandler) {
return new Promise(function (resolve, reject) {
turn(topMotor, 1400);
turn(bottomMotor, 1600);
waitForAngle(bindHandler).then(function () {
stop(topMotor);
stop(bottomMotor);
setTimout(function () {
turn(topMotor, 1400);
turn(topMotor, 1600);
waitForAngle().then(resolve);
}, 1000);
});
// turn topMotor 60 deg and turn bottomMotor 60 deg
// topMotor rumble
// turn topMotor 60 deg
// turn bottomMotor 60 deg and resolve
});
}
// waitForAngle(true).then(function () {
// console.log('bra')
// waitForAngle().then(function () {
// console.log('works!')
// })
// })
function waitForAngle (bindHandler) {
var timeut = null;
return new Promise(function (resolve, reject) {
if (bindHandler) {
counterButton.on('interrupt', function (level) {
if (level !== 0 || timeut) return false;
console.log(counter)
timeut = setTimeout(function () {
}, 100);
counter++;
if (counter === 13 || counter === 27 || counter === 40 || counter === 53 || counter === 67 || counter === 80) {
console.log('tick!');
counter = counter % 80;
counterButton.disableInterrupt();
resolve();
}
});
} else {
counterButton.enableInterrupt(Gpio.FALLING_EDGE);
}
});
}
function rumble (motor, initialMikros) {
var microseconds = initialMikros,
interval = null,
rumbleCount = 0;
turn(motor, microseconds);
interval = setInterval(function () {
microseconds = microseconds === 1600 ? 1400 : 1600;
turn(motor, microseconds);
rumbleCount++;
if (rumbleCount >= 4) clearInterval(interval);
}, 20);
}
function stop (motor) {
return turn(motor, 0);
}
function turn (motor, microseconds) {
return motor.servoWrite(microseconds);
}
function getPulse () {
return motor.getServoPulseWidth();
}