-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
134 lines (108 loc) · 4.57 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
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const sassMiddleware = require('node-sass-middleware');
const LiveCam = require('livecam');
const SerialPort = require('serialport');
const Readline = require('@serialport/parser-readline');
const fs = require('fs');
const Gpio = require('pigpio').Gpio;
const yaml = require('js-yaml');
const config = yaml.safeLoad(fs.readFileSync('config.yaml', 'utf8'));
// var usersRouter = require('./routes/users');
const gpio = {};
if(config.gpio.enabled) {
gpio['left_joy_x'] = new Gpio(config.gpio.left_joy_x, {mode: Gpio.OUTPUT});
gpio['left_joy_y'] = new Gpio(config.gpio.left_joy_y, {mode: Gpio.OUTPUT});
gpio['right_joy_x'] = new Gpio(config.gpio.right_joy_x, {mode: Gpio.OUTPUT});
gpio['right_joy_y'] = new Gpio(config.gpio.right_joy_y, {mode: Gpio.OUTPUT});
}
const webcam_server = new LiveCam({
// address and port of the webcam UI
'ui_addr': '0.0.0.0',
'ui_port': 11000,
// address and port of the webcam Socket.IO server
// this server broadcasts GStreamer's video frames
// for consumption in browser side.
'broadcast_addr': '0.0.0.0',
'broadcast_port': 12000,
// address and port of GStreamer's tcp sink
'gst_tcp_addr': '127.0.0.1',
'gst_tcp_port': 10000,
// callback function called when server starts
'start': function () {
console.log('WebCam server started!');
},
// webcam object holds configuration of webcam frames
'webcam': {
// should width of the frame be resized (default : 0)
// provide 0 to match webcam input
'width': config.webcam.width,
// should height of the frame be resized (default : 0)
// provide 0 to match webcam input
'height': config.webcam.height,
// framerate of the feed (default : 0)
// provide 0 to match webcam input
'framerate': config.webcam.framerate
}
});
var app = express();
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());
app.use(sassMiddleware({
src: path.join(__dirname, 'public'),
dest: path.join(__dirname, 'public'),
indentedSyntax: false, // true = .sass and false = .scss
sourceMap: true
}));
app.use(express.static(path.join(__dirname, 'public')));
//
// app.use('/users', usersRouter);
//
// function getSerialDevices() {
// return fs.readdirSync('/dev').filter(f => f.indexOf('/dev/ttyUSB') === 0);
// }
// Init for the websocket server that connects websocket input to serial output
function ws_server(io) {
const serialPort = new SerialPort(config.robot_serial.port, {baudRate: config.robot_serial.baudrate, autoOpen: false});
serialPort.open(serialPortError => {
const parser = new Readline();
serialPort.pipe(parser);
parser.on('data', line => console.log(`> ${line}`));
io.on('connection', function (socket) {
console.log('got connection', socket);
socket.emit('message', 'server connected');
if(serialPortError) {
socket.emit('robot-status', 'disconnected');
socket.emit('message', `Could not connect to robot -- ${serialPortError.message}`);
} else {
socket.emit('robot-status', 'connected');
}
socket.on('gamepad', joyVal => {
if(config.gpio.enabled) {
gpio['left_joy_x'].pwmWrite(Math.max(0, Math.min(255, Math.ceil((joyVal.leftStick.x + 1) * (255.0/2.0)))));
gpio['left_joy_y'].pwmWrite(Math.max(0, Math.min(255, Math.ceil((joyVal.leftStick.y + 1) * (255.0/2.0)))));
gpio['right_joy_x'].pwmWrite(Math.max(0, Math.min(255, Math.ceil((joyVal.rightStick.x + 1) * (255.0/2.0)))));
gpio['right_joy_y'].pwmWrite(Math.max(0, Math.min(255, Math.ceil((joyVal.rightStick.y + 1) * (255.0/2.0)))));
socket.emit('message', `cmd: ${JSON.stringify(joyVal)}`);
} else {
serialPort.write(`${JSON.stringify(joyVal)}\n`, err => {
if(err) {
socket.emit('message', `cmd failed: ${err.message}`);
} else {
socket.emit('message', `cmd: ${JSON.stringify(joyVal)}`);
}
});
}
});
});
});
}
if(config.webcam.enabled) {
webcam_server.broadcast();
}
module.exports.app = app;
module.exports.ws_server = ws_server;