-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.js.old
More file actions
executable file
·254 lines (219 loc) · 5.32 KB
/
node.js.old
File metadata and controls
executable file
·254 lines (219 loc) · 5.32 KB
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
var os = require('os'); os.tmpDir = os.tmpdir;
require('async-functions');
var fs = require('fs');
var SerialPort = require('serialport');
var InfiniteLoop = require('infinite-loop');
var ip = require('ip');
var il = new InfiniteLoop();
var port = new SerialPort('/dev/ttyACM0', {
baudRate: 115200
});
var redGoals = 0;
var blueGoals = 0;
var started = false;
var playing = false;
const WebSocket = require('ws');
var connected = false;
var options = {rejectUnauthorized: false};
const ws = new WebSocket('wss://iafoosball.me:9003/tables/table-1',options);
ws.on('open', function open() {
//ws.send('something');
console.log("Open ws");
connected = true;
});
//Laser2 = redside
ws.on('message', function incoming(data) {
console.log("Msg received");
//read();
console.log(data);
try{
var json = JSON.parse(data);
if(json.started==undefined){
json.started = false;
}
console.log("Started "+started+" json.started "+json.started);
if(started==false&&json.started==true){
il.add(game);
il.run();
arduino.async('21');
playing = true;
}
if(playing&&json.started==false){
if(redGoals>blueGoals){
arduino.async('23');
}else if(blueGoals>redGoals){
arduino.async('22');
}else{
arduino.async('26');
}
redGoals = 0;
blueGoals = 0;
match_id = 0;
playing = false;
}
started = json.started;
if(json.scoreRed!=redGoals){
redGoals = json.scoreRed;
arduino.async('"'+redGoals+'"');
}
if(json.scoreBlue!=blueGoals){
blueGoals = json.scoreBlue;
arduino.async('"'+(blueGoals+10)+'"');
}
}catch(e){
console.log("Not valid json");
}
});
//var speed = 0;
//Connection to Arduino
/*
if(fs.existsSync('/dev/ttyACM1')){
var port = new SerialPort('/dev/ttyACM1', {
baudRate: 115200
});
}else{
var port = new SerialPort('/dev/ttyACM0', {
baudRate: 115200
});
}
var port = new SerialPort('/dev/ttyACM1', function (err) {
if (err) {
var port = new SerialPort('/dev/ttyACM0', function (err) {
baudRate: 115200
if (err) {
}
});
return console.log('Error tring other port: ', err.message);
}
});
*/
console.log("start");
var arduino = function(str){
console.log("arduino");
port.write(str, function(err) {
if (err) {
return console.log('Error on write: ', err.message);
}
console.log('message written '+str);
sleep.msleep(1200);
});
}
function sendMsg(s) {
console.log(ws.connected);
if(connected){
console.log("Sending ws");
console.log(s);
ws.send(s);
}else{
console.log("Not connected");
}
}
var http = require('http');
var start = false;
var local_ip = ip.address();
var ElapsedTime = require('elapsed-time');
var sleep = require('sleep');
// Goal GPIO Inputs
var Gpio = require('pigpio').Gpio,
//laser4 = new Gpio(23, {mode: Gpio.INPUT}),
//laser3 = new Gpio(3, {mode: Gpio.INPUT}),
laser2 = new Gpio(2, {mode: Gpio.INPUT});
laser1 = new Gpio(18, {mode: Gpio.INPUT}),
function read(){
console.log('Laser1 '+laser1.digitalRead());
console.log('Laser2 '+laser2.digitalRead());
console.log("asd");
}
var hit1 = false;
var hit2 = false;
var et;
function game(){
var read1 = laser1.digitalRead();
var read2 = laser2.digitalRead();
if(read1==0){
var command = ('{ "command": "addGoal", "values": { "speed": 0, "side": "red", "position": "attack" }}');
sendMsg(command);
sleep.msleep(1200);
}
if(read2==0){
var command = ('{ "command": "addGoal", "values": { "speed": 0, "side": "blue", "position": "attack" }}');
sendMsg(command);
sleep.msleep(1200);
}
/*
if(read1==0&&!hit1){
et = ElapsedTime.new().start();
hit1 = true;
console.log("Laser1 hit "+et);
}
if(read2==0&&hit1){
var time = et.getRawValue();
console.log("Laser2 hit");
console.log(time);
speed = 2.8/(time*0.0000001)*3.6;
console.log("speed "+speed);
hit1 = false;
redGoals++;
console.log('"'+redGoals+'"');
if(redGoals==10){
arduino.async('23');
redGoals = 0;
blueGoals = 0;
match_id = 0;
playing = false;
}else{
arduino.async('"'+redGoals+'"');
}
//var post = ('{"command": "addGoal", "values"{ "side":"red", "speed":' + speed + ',"position": "attack"}}');
//postData(post);
var command = ('{ "command": "addGoal", "values": { "speed": ' + speed + ', "side": "red", "position": "attack" }}');
sendMsg(command);
sleep.msleep(1200);
}
if(read3==0&&!hit2){
et = ElapsedTime.new().start();
hit2 = true;
console.log("Laser3 hit "+et);
}
if(read4==0&&hit2){
var time = et.getRawValue();
console.log("Laser4 hit");
console.log(time);
speed = 2.8/(time*0.0000001)*3.6;
console.log("speed "+speed);
hit2 = false;
blueGoals++;
var postblue = blueGoals+10;
if(blueGoals==10){
arduino.async('22');
blueGoals = 0;
redGoals = 0;
match_id = 0;
playing = false;
}else{
arduino.async('"'+postblue+'"');
}
//var post = ('{"command": "addGoal", "values"{ "side":"blue", "speed":' + speed + ',"position": "attack"}}');
//postData(post);
var command = ('{ "command": "addGoal", "values": { "speed": ' + speed + ', "side": "blue", "position": "attack" }}');
sendMsg(command);
sleep.msleep(1200);
}
if(playing){
if(et.getRawValue()>300000000000){
playing = false;
var post = ('{"removeGame":'+match_id+'}');
postData(post);
blueGoals = 0;
redGoals = 0;
arduino.async('26');
}
}
*/
}
function sendData(s){
if (connection.connected) {
connection.sendUTF(number.toString());
setTimeout(sendNumber, 1000);
}
}