-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.js
51 lines (41 loc) · 1.38 KB
/
robot.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
// Modules
const Motors = require("./js/Motors.js");
const VideoBuffer = require("./js/VideoBuffer.js");
const Libcamera = require("./js/Libcamera.js");
const fs = require("fs");
const io = require("socket.io-client");
const ChildProcess = require("child_process");
// Load config
const configFile = __dirname + "/config.json";
if (!fs.existsSync(configFile)) process.exit();
const config = JSON.parse(fs.readFileSync("config.json").toString());
if (!config.server || !config.server.hostname || !config.server.authtoken)
process.exit();
// Connect to server
const socket = io("wss://" + config.server.hostname + "/", {
auth: { token: config.server.authtoken },
});
// Create video buffer for output stream
const videoBuffer = new VideoBuffer();
// Start motors
const pythonProcess = ChildProcess.spawn("python3", ["-u", "./js/Motors.py"]);
const motors = new Motors(pythonProcess);
// Start camera
const libcamera = new Libcamera(config.libcamera);
// Send all chunks to the videobuffer
libcamera.on("data", videoBuffer.pushVideoChunk);
// Start camera
libcamera.start();
// Connect to websocket
socket.on("connect", () => {
videoBuffer.addReceiverSocket(socket);
});
// Motor controlls
socket.on("motors", (data) => {
motors.setMotors(data);
});
// Camera controls
socket.on("camera", function (value) {
if (value === "start") libcamera.start();
if (value === "stop") libcamera.stop();
});