forked from hugs/tapsterbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
159 lines (142 loc) · 4.76 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
#! /usr/local/bin/node
// Load all Libraries and Config
var
calibrationFile,
mode,
config_mobi,
config_robo,
config,
device,
calibrationLib = require("./lib/calibration"),
Robot = require("./lib/robot").Robot,
kinematics = require("./lib/kinematics"),
motion = require("./lib/motion");
for(i=2; i<process.argv.length; i++){
val=process.argv[i];
if(val.startsWith('--hardware'))
{
mode = val.split('=')[1];
}
else if(val.startsWith('--device')){
device = val.split('=')[1];
}
else
{
console.error("Unsupported option: " + val);
}
}
if(mode == undefined){
console.error("Hardware not specified. Please specify hardware mode using --hardware option.");
process.exit(1);
}
if(device != undefined)
config_mobi = require("./config.mobi/" + device);
else if(mode!="headless"){
console.error("Device not specified. Please specify device using --device option.");
process.exit(1);
}
if(mode=="headless")
{
config_robo = require("./config.robo/v1");
}
else if(mode=="brainybot1")
{
config_robo = require("./config.robo/v1");
calibrationFile = "./calibration-device/v1/cal-" + device + ".json";
}
else if(mode=="brainybot2")
{
config_robo = require("./config.robo/v2");
calibrationFile = "./calibration-device/v2/cal-" + device + ".json";
}
else {
console.error("Unsupported mode: " + mode);
process.exit(1);
}
config = {
...config_mobi,
...config_robo,
};
console.log(config);
const Hapi = require('hapi');
const server = Hapi.server({
port: config.port,
});
const init = async () => {
await server.register(require('inert'));
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: 'web',
index: true,
listing: true,
defaultExtension: 'html'
}
},
config: {
cache: {
expiresIn: 1 // prevent HTML caching
}
}
});
await server.start();
console.log(`Server running at: ${server.info.uri}`);
};
init();
// Initialize Socket.IO and make globally available
global.io = require('socket.io')(server.listener);
var robot;
if(mode=="headless")
{
global.headless=true;
robot = new Robot(); // Initialize Robot instance
var rest = require('./lib/rest')(server, robot); // load REST API
var listeners = require('./lib/listeners')(io, robot, config); // Lets Start socket Listeners
}
else
{
if(mode=="brainybot1")
{
var five = require("johnny-five");
var board = new five.Board({ debug: true, port: config.serialport || null });
board.on("ready", function() {
// Initialize servos
var s1 = new five.Servo({ pin: config.s1.pin });
var s2 = new five.Servo({ pin: config.s2.pin });
var s3 = new five.Servo({ pin: config.s3.pin });
// Initialize kinematics
var k = new kinematics.Kinematics({
e: config.e,
f: config.f,
re: config.re,
rf: config.rf
});
robot = new Robot(s1, s2, s3, calibrationFile, k, config, "v1"); // Initialize Robot instance
var rest = require('./lib/rest')(server, robot); // load REST API
var listeners = require('./lib/listeners')(io, robot, config); // Lets Start socket Listeners
});
}else if(mode=="brainybot2"){
var Board = require("./lib/bb2linker/board");
var Servo = require("./lib/bb2linker/servo");
var board = new Board({ port: config.serialport || null , baudrate: config.baudrate, debug: false});
board.on("ready", function() {
// Initialize servos
var s1 = new Servo(board, { id: config.s1.pin, range: [config.s1.min, config.s1.max]});
var s2 = new Servo(board, { id: config.s2.pin, range: [config.s2.min, config.s2.max]});
var s3 = new Servo(board, { id: config.s3.pin, range: [config.s3.min, config.s3.max]});
// Initialize kinematics
var k = new kinematics.Kinematics({
e: config.e,
f: config.f,
re: config.re,
rf: config.rf
});
robot = new Robot(s1, s2, s3, calibrationFile, k, config, "v2", board); // Initialize Robot instance
var rest = require('./lib/rest')(server, robot); // load REST API
var listeners = require('./lib/listeners')(io, robot, config); // Lets Start socket Listeners
});
}
}
global.ip = "127.0.0.1";