-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
308 lines (250 loc) · 8.04 KB
/
server.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
require('dotenv').config();
require('app-module-path').addPath(__dirname);
// Parse config file
const yaml = require('js-yaml');
const Conf = require('conf');
function loadConfig() {
process.config = new Conf({
cwd: process.cwd(),
fileExtension: 'yaml',
serialize: yaml.dump,
deserialize: yaml.load,
});
}
loadConfig();
const path = require('path');
const fs = require('fs');
const Roboclaw = require('classes/Roboclaw.js');
const Simulation = require('classes/Simulation.js');
const AnalogToDigital = require('classes/AnalogToDigital.js');
const EventLoop = require('classes/EventLoop.js');
const Logger = require('classes/Logger.js');
const HX711 = require('pi-hx711');
const eventLoop = new EventLoop(50);
eventLoop.start();
const express = require('express');
const app = express();
const http = require('http').Server(app);
const multer = require('multer');
const io = require('socket.io')(http);
// Setup static files
app.use(express.static('dist', {index: false}));
app.use('/public', express.static('public'));
// Setup the view engine
app.set('views', path.resolve(__dirname, 'dist'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
// Bundle if in development mode
if(process.env.NODE_ENV == 'development') {
console.log('Running in development mode!');
const webpack = require('webpack');
const middleware = require('webpack-dev-middleware');
const options = require('webpack.dev');
const instance = middleware(webpack(options), {
serverSideRender: true,
index: false,
writeToDisk: true,
publicPath: '/',
});
app.use(instance);
instance.waitUntilValid(() => {
main();
});
} else {
main();
}
function main() {
/*****************************************************************************
* ROUTES
****************************************************************************/
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, `profiles/${req.params.motor}`);
},
filename: function (req, file, callback) {
callback(null, file.originalname);
}
})
var upload = multer({ storage: storage })
app.get('/api/profiles/:motor/:name', async (req, res) => {
const filePath = path.join(__dirname, 'profiles', req.params.motor, req.params.name);
try {
let file = await fs.promises.readFile(filePath, 'utf8');
if(file) {
file = file.replace(/(\r\n|\n|\r)/gm, '');
const points = file.split(',');
for(let i in points) {
points[i] = parseFloat(points[i]);
}
res.json(points);
} else {
res.json(null);
}
} catch(error) {
res.status(400).json({error});
}
});
app.post('/api/profiles/:motor/upload', upload.any(), async (req, res) => {
res.json({error: false});
});
app.get('/api/logs/:name', (req, res) => {
res.download(logger.getLogPath(req.params.name));
});
app.get('*', (req, res) => {
res.render('index.html');
});
http.listen(process.env.PORT, () => {
console.log(`Listening on port ${process.env.PORT}`);
});
/*****************************************************************************
* CONTROL
****************************************************************************/
const rc = new Roboclaw('/dev/ttyS0', {
baudRate: Number.parseInt(process.config.get('baudrate'), 10),
});
rc.on('open', () => {
console.log('SerialPort:', 'Initalized.');
});
rc.on('error', (err) => {
console.error('SerialPort:', err);
});
const simulation = new Simulation(rc, io, eventLoop);
const logger = new Logger('logs');
const loadCell = new HX711(6, 5, {
scale: () => process.config.get('loadcell.scale'),
offset: () => process.config.get('loadcell.offset'),
continous: 100,
});
/*****************************************************************************
* Websocket Communication to GUI
****************************************************************************/
io.on('connection', (socket) => {
socket.join('frame');
socket.join('ads');
socket.join('loadcell');
socket.join('logs');
socket.on('profiles.list', async (motor, callback) => {
const profiles = await simulation.getProfilesList(motor);
callback(profiles);
});
socket.on('cycle-duration.get', (callback) => {
callback(simulation.duration);
});
socket.on('cycle-duration.set', (duration) => {
simulation.duration = duration;
});
socket.on('axis.profile.get', (motor, callback) => {
callback(simulation.getProfile(motor));
});
socket.on('axis.profile.set', async (motor, file, callback) => {
const points = await simulation.setProfile(motor, file);
if(typeof callback === 'function') {
callback(points);
}
});
socket.on('axis.profile.delete', async (motor, file, callback) => {
const results = await simulation.deleteProfile(motor, file);
if(typeof callback === 'function') {
callback(results);
}
});
socket.on('motion.goToStart', () => {
simulation.goToStart();
});
socket.on('motion.start', () => {
simulation.start();
});
socket.on('motion.stop', () => {
simulation.stop();
});
socket.on('motion.state', (callback) => {
callback(simulation.running, simulation.atStart);
});
socket.on('logs.list', async (callback) => {
try {
callback(await logger.getList());
} catch(error) {
callback({error});
}
});
socket.on('logs.delete', async (name, callback) => {
try {
callback(await logger.delete(name));
} catch(error) {
callback({error});
}
});
});
simulation.on('state', async (running, atStart) => {
io.to('frame').emit('motion.state', running, atStart)
});
/*****************************************************************************
* Analog to Digital
****************************************************************************/
const ads = new AnalogToDigital();
// Update the motor encoder based on ads value at boot
ads.once('averages', async (values) => {
// Calibrate motors based on analog encoder
const adsMotors = [0,1,2];
for(let motor in adsMotors) {
const params = simulation.getMotorParams(motor);
let position = ((values[motor] / 100) * params.scale_factor) / 2;
rc.setEncValue(motor, position);
console.log(`Setting Motor ${motor} Pos: ${position} (${values[motor]}%)`);
}
// Calibrate load axis
const load = await loadCell.read();
const position = load * process.config.get('axis.3.conversion');
rc.setEncValue(3, position);
console.log(`Setting Load Axis Pos: ${position} (${load} ${process.config.get('loadcell.units')})`);
});
ads.active = true;
/*****************************************************************************
* LOGGING
****************************************************************************/
const logLoop = new EventLoop(10);
logLoop.register(ads);
logLoop.start();
simulation.on('stop', async () => {
try {
const name = await logger.save();
io.to("logs").emit('logs.add', name);
} catch(err) {
console.error(err);
}
});
// Setup the logger
logger.setHeaders([
'MS',
'FLEXION',
'ABDUCTION',
'ROTATION',
'VOLT',
'LOAD',
]);
// On new encoder values
ads.on('update', async (values) => {
const load = loadCell.getLast();
io.to("ads").emit('ads.values', values, [
process.config.get('ads.A0.units'),
process.config.get('ads.A1.units'),
process.config.get('ads.A2.units'),
process.config.get('ads.A3.units'),
]);
io.to("loadcell").emit('loadcell.value',
load,
process.config.get('loadcell.units'),
process.config.get('loadcell.decimals')
)
if(simulation.running) {
logger.add([
simulation.elapsed,
(values[0] / 100) * 180,
(values[1] / 100) * 180,
simulation.profiles[2].rawMotorPos,
(values[3] / 100) * 180,
load,
]);
}
});
}