-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.cpp
More file actions
480 lines (452 loc) · 14.5 KB
/
helpers.cpp
File metadata and controls
480 lines (452 loc) · 14.5 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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#include "helpers.h"
#include "LabJackM.h"
#include <boost/asio/thread_pool.hpp>
#include <exception>
#include <memory>
#include <memory_resource>
#include <string>
#include <thread>
#include <vector>
uint64_t mach::sToMs(uint32_t sec) {
using namespace std::chrono;
seconds duration(sec);
milliseconds msDuration = duration_cast<milliseconds>(duration);
return msDuration.count();
}
void mach::sleep(uint64_t milliseconds) {
auto start = std::chrono::high_resolution_clock::now();
while (true) {
auto now = std::chrono::high_resolution_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::milliseconds>(now - start);
if (duration.count() >= milliseconds) {
break;
}
}
}
mach::Timestamp mach::now() {
return mach::Timestamp(
std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::high_resolution_clock::now().time_since_epoch())
.count());
}
const double *mach::vectorToDouble(const std::vector<double> &doubleVector) {
return doubleVector.data();
}
const char **mach::vectorToChar(const std::vector<std::string> &stringVector) {
const char **charPtrArray = new const char *[stringVector.size()];
for (size_t i = 0; i < stringVector.size(); i++) {
charPtrArray[i] = stringVector[i].c_str();
}
return charPtrArray;
}
void mach::SocketConn::send(std::string msg, bool immediate) {
post(_s.get_executor(),
[self = shared_from_this(), msg = std::move(msg), immediate] {
if (self->nq(msg, immediate))
self->_write();
});
}
template<typename Rep, typename Period>
bool mach::sleepOrAbort(std::chrono::duration<Rep, Period> duration) {
std::unique_lock<std::mutex> lock(abort_mutex);
abort_cv.wait_for(lock, duration, []() { return abort_flag.load(); });
return abort_flag.load();
}
void mach::dispatchValve(const std::string &name, int handle, std::shared_ptr<State> &state) {
if (name == "ign") {
LJM_eWriteName(FastJack, "DIO6", 0);
if (sleepOrAbort(5s)) return;
LJM_eWriteName(FastJack, "DIO6", 1);
return;
}
if (name == "wstart") {
enabled = true;
suspend_write.notify_all();
return;
}
if (name == "wstop") {
enabled = false;
suspend_write.notify_all();
return;
}
if (name == "stop") {
LJM_eWriteName(handle, mach::vlj.at("V10").c_str(), 1);
LJM_eWriteName(handle, mach::vlj.at("V21").c_str(), 1);
LJM_eWriteName(handle, mach::vlj.at("V20").c_str(), 1);
LJM_eWriteName(handle, mach::vlj.at("V30").c_str(), 1);
LJM_eWriteName(handle, mach::vlj.at("V31").c_str(), 1);
LJM_eWriteName(handle, mach::vlj.at("V32").c_str(), 1);
LJM_eWriteName(handle, mach::vlj.at("V34").c_str(), 1);
LJM_eWriteName(handle, mach::vlj.at("V36").c_str(), 1);
LJM_eWriteName(handle, mach::vlj.at("V37").c_str(), 1);
LJM_eWriteName(handle, mach::vlj.at("V11_NO").c_str(), 1);
LJM_eWriteName(handle, mach::vlj.at("V12_NO").c_str(), 1);
LJM_eWriteName(handle, mach::vlj.at("V22").c_str(), 1);
LJM_eWriteName(handle, mach::vlj.at("V23_NO").c_str(), 1);
LJM_eWriteName(handle, mach::vlj.at("V33").c_str(), 1);
LJM_eWriteName(handle, mach::vlj.at("V35_NO").c_str(), 1);
LJM_eWriteName(handle, mach::vlj.at("V38_NO").c_str(), 1);
return;
}
/*
* 3.6.1 which is tank pressurization
* open v21
* open v32 at the same time preferably
*
*/
if (name == "361") {
return;
}
/*
* 3.6.2 tank pressurization continued
* open v31
* 1 second later
* close v32
*/
if (name == "362") {
return;
}
/*
* 3.5.1 nitrous fill probably
* open v21
* open v32 at the same time preferably
*/
if (name == "351") {
return;
}
/*
* 4.1.1f which i guess is eth
* open v21
* 10 seconds later
* open v20 (t-0)
* 7 seconds later
* close v21
* 3 seconds later
* close v20
*/
// if (name == "411f") {
// // open v21
// // LJM_eWriteName(SlowJack, vlj.at("V21").c_str(), 0);
// // std::this_thread::sleep_for(10s);
// sleep(sToMs(10));
// // open v20
// LJM_eWriteName(handle, vlj.at("V20").c_str(), 0);
// // std::this_thread::sleep_for(7s);
// sleep(sToMs(7));
// // close v21
// LJM_eWriteName(handle, vlj.at("V21").c_str(), 1);
// // std::this_thread::sleep_for(3s);
// sleep(sToMs(3));
// // close v20
// LJM_eWriteName(handle, vlj.at("V20").c_str(), 1);
// // close
// return;
// }
/*
* 4.0.3 which i guess is oxidizer
* open v34
* 10 seconds later
* close v34
* open v30 preferably at same time (t-0)
* 3 seconds later
* close v30
* close v31 preferably at the same time
* open v33 preferably also at the same time
* 7 seconds later
* close v33
*/
if (name == "403") {
// open v34
LJM_eWriteName(handle, vlj.at("V34").c_str(), 0);
if (sleepOrAbort(10s)) return;
// close v34
LJM_eWriteName(handle, vlj.at("V34").c_str(), 1);
// open v30
LJM_eWriteName(handle, vlj.at("V30").c_str(), 0);
if (sleepOrAbort(3s)) return;
// close v30 and v31 and open v33
LJM_eWriteName(handle, vlj.at("V30").c_str(), 1);
LJM_eWriteName(handle, vlj.at("V31").c_str(), 1);
LJM_eWriteName(handle, vlj.at("V33").c_str(), 0);
if (sleepOrAbort(7s)) return;
LJM_eWriteName(handle, vlj.at("V33").c_str(), 1);
return;
};
/*
* 4.0.3A which i guess is oxidizer
* open v34
* 10 seconds later
* close v34
* open v30 preferably at same time (t-0)
* 3 seconds later
* close v30
* close v31 preferably at the same time
* open v33 preferably also at the same time
* 7 seconds later
* close v33
*/
if (name == "403a") {
// open v34
LJM_eWriteName(handle, vlj.at("V34").c_str(), 0);
if (sleepOrAbort(10s)) return;
// close v34
LJM_eWriteName(handle, vlj.at("V34").c_str(), 1);
// open v30
LJM_eWriteName(handle, vlj.at("V30").c_str(), 0);
if (sleepOrAbort(7s)) return;
// close v30 and v31 and open v33
LJM_eWriteName(handle, vlj.at("V30").c_str(), 1);
LJM_eWriteName(handle, vlj.at("V31").c_str(), 1);
LJM_eWriteName(handle, vlj.at("V33").c_str(), 0);
if (sleepOrAbort(3s)) return;
LJM_eWriteName(handle, vlj.at("V33").c_str(), 1);
return;
};
/*
* Ignition sequence
* Turn on ingnitor
*/
// if (name == "ign") {
// // Turn on igitor
// LJM_eWriteName(FastJack, "DIO6", 0);
// return;
// };
/*
* 5.0.2 Hot Fire sequence
* open v34
* 10 seconds later
* Turn on ingnitor
* Once ignitor reaches 900C
* close v34
* open v20
* open v30 preferably at same time (t-0)
* 4.5 seconds later
* close v30
* open v33
* 2.5 seconds later
* close v20 preferably at the same time
* open v22 preferably also at the same time
* 3 seconds later
* close v33
* close v22
* close v31
* close v10
*/
if (name == "502") {
// open v34
LJM_eWriteName(handle, vlj.at("V34").c_str(), 0);
if (sleepOrAbort(5s)) return;
// Turn on igitor and wait until thermocouple reaches 900C
LJM_eWriteName(FastJack, "DIO6", 0);
while(state->lj.ignval < 365){
if (sleepOrAbort(1us)) return;
}
// close v34, open v20 and open v30
LJM_eWriteName(handle, vlj.at("V34").c_str(), 1);
LJM_eWriteName(handle, vlj.at("V20").c_str(), 0);
LJM_eWriteName(handle, vlj.at("V30").c_str(), 0);
if (sleepOrAbort(4.5s)) return;
// close v30 and open v33
LJM_eWriteName(handle, vlj.at("V30").c_str(), 1);
LJM_eWriteName(handle, vlj.at("V33").c_str(), 0);
if (sleepOrAbort(2.5s)) return;
// close v20 and open v22
LJM_eWriteName(handle, vlj.at("V20").c_str(), 1);
LJM_eWriteName(handle, vlj.at("V22").c_str(), 0);
if (sleepOrAbort(3s)) return;
LJM_eWriteName(handle, vlj.at("V33").c_str(), 1);
LJM_eWriteName(handle, vlj.at("V22").c_str(), 1);
LJM_eWriteName(handle, vlj.at("V31").c_str(), 1);
LJM_eWriteName(handle, vlj.at("V10").c_str(), 1);
return;
};
/*
* 4.1.1 which i guess is oxidizer final
* open v34
* 10 seconds later
* close v34
* (IGNITER LATER) [t-0]
* supposed thermocouple detection
* open v20
* .25 seconds later
* open v30
* 5 seconds later
* close v30
* close v31 preferably at the same time
* open v33 preferably also at the same time
* 7 seconds later
* close v33
* close v21
*/
if (name == "411") {
return;
}
/*
* close v11, v12, v35, v36, v23
*/
if (name == "cavv") {
return;
}
/*
* open v11,v12,v35,v36,v23
*/
if (name == "oavv") {
return;
}
size_t nindex = name.rfind('_');
std::string valve = name.substr(0, nindex);
std::string status = name.substr(nindex + 1);
std::cout << valve << status << std::endl;
size_t count =
std::count_if(name.begin(), name.end(), [](char c) { return c == '_'; });
if (count == 2) {
if (status == "open")
LJM_eWriteName(handle, mach::vlj.at(valve).c_str(), 1);
else
LJM_eWriteName(handle, mach::vlj.at(valve).c_str(), 0);
} else {
if (status == "open")
LJM_eWriteName(handle, mach::vlj.at(valve).c_str(), 0);
else
LJM_eWriteName(handle, mach::vlj.at(valve).c_str(), 1);
}
return;
}
void mach::SocketConn::start() { _read(); }
mach::SocketConn::SocketConn(ba::any_io_executor const &ioContext,
std::stop_source &stopSource, const int &lj, std::shared_ptr<State> &st)
: _s(ioContext), _ss(stopSource), labjack(lj), _st(st){};
void mach::SocketConn::input() {
std::string val;
if (std::getline(std::istream(&_buf), val)) {
{
std::lock_guard<std::mutex> l(coutm);
std::cout << val << std::endl;
}
if (val == "abort") {
poolptr->stop();
std::cout<<"past stop" <<std::endl;
{
std::lock_guard<std::mutex> lock(abort_mutex);
abort_flag.store(true);
}
abort_cv.notify_all();
poolptr->join();
std::cout<<"past join" <<std::endl;
poolptr.reset();
abort_flag.store(false);
std::cout<<"past reset" <<std::endl;
poolptr = std::make_shared<ba::thread_pool>(5);
std::cout<<"past shared" <<std::endl;
LJM_eWriteName(labjack, mach::vlj.at("V10").c_str(), 1);
LJM_eWriteName(labjack, mach::vlj.at("V21").c_str(), 1);
LJM_eWriteName(labjack, mach::vlj.at("V20").c_str(), 1);
LJM_eWriteName(labjack, mach::vlj.at("V30").c_str(), 1);
LJM_eWriteName(labjack, mach::vlj.at("V31").c_str(), 1);
LJM_eWriteName(labjack, mach::vlj.at("V32").c_str(), 1);
LJM_eWriteName(labjack, mach::vlj.at("V34").c_str(), 1);
LJM_eWriteName(labjack, mach::vlj.at("V36").c_str(), 1);
LJM_eWriteName(labjack, mach::vlj.at("V37").c_str(), 1);
LJM_eWriteName(labjack, mach::vlj.at("V11_NO").c_str(), 0);
LJM_eWriteName(labjack, mach::vlj.at("V12_NO").c_str(), 1);
LJM_eWriteName(labjack, mach::vlj.at("V22").c_str(), 1);
LJM_eWriteName(labjack, mach::vlj.at("V23_NO").c_str(), 1);
LJM_eWriteName(labjack, mach::vlj.at("V33").c_str(), 1);
LJM_eWriteName(labjack, mach::vlj.at("V35_NO").c_str(), 1);
LJM_eWriteName(labjack, mach::vlj.at("V38_NO").c_str(), 0);
// LJM_eWriteName(labjack, mach::vlj.at("V10").c_str(), 0);
// LJM_eWriteName(labjack, mach::vlj.at("V12_NO").c_str(), 1);
// LJM_eWriteName(labjack, mach::vlj.at("V38_NO").c_str(), 1);
// LJM_eWriteName(labjack, mach::vlj.at("V11_NO").c_str(), 1);
// LJM_eWriteName(labjack, mach::vlj.at("V33").c_str(), 0);
// LJM_eWriteName(labjack, mach::vlj.at("V22").c_str(), 0);
// if (sleepOrAbort(3s)) return;
// LJM_eWriteName(labjack, mach::vlj.at("V33").c_str(), 1);
// LJM_eWriteName(labjack, mach::vlj.at("V22").c_str(), 1);
// LJM_eWriteName(labjack, mach::vlj.at("V16").c_str(), 1);
// LJM_eWriteName(labjack, mach::vlj.at("V12_NO").c_str(), 0);
std::cout<<" ABORT DONE LOL" <<std::endl;
return;
}
ba::post(poolptr->get_executor(),
[this, val]() { dispatchValve(val, labjack, _st); });
// std::lock_guard<std::mutex> lock(sigm);
// vData = val;
// isString = true;
// sigcondition.notify_all();
}
}
bool mach::SocketConn::nq(std::string msg, bool immediate) {
immediate &= !_mlist.empty(); // assert same
if (immediate)
_mlist.insert(std::next(begin(_mlist)),
std::move(msg)); // insert at beginning of list
else
_mlist.push_back(std::move(msg)); // queue to end
return _mlist.size() == 1;
}
bool mach::SocketConn::dq() {
assert(!_mlist.empty());
_mlist.pop_front();
return !_mlist.empty();
}
void mach::SocketConn::_read() {
ba::async_read_until(_s, _buf, "\n",
[this, self = shared_from_this()](
boost::system::error_code ec, size_t n) {
input();
if (!ec)
_read();
});
};
void mach::SocketConn::_write() {
ba::async_write(_s, ba::buffer(_mlist.front()),
[this, self = shared_from_this()](
boost::system::error_code ec, size_t n) {
if (!ec && dq())
_write();
});
};
mach::Server::Server(ba::io_context &ioContext, std::stop_source &stopSource,
const std::shared_ptr<State> &st, const int &lj)
: _ioc(ioContext), _ss(stopSource), _st(st), labjack(lj) {
_acc.bind({{}, PORT});
_acc.set_option(ba::ip::tcp::acceptor::reuse_address());
_acc.listen();
accept();
}
void mach::Server::stop() {
_ioc.post([this] {
this->_acc.cancel();
this->_acc.close();
});
}
size_t mach::Server::emit(const std::shared_ptr<State> &s) {
_emit_event(s);
return _emit_event.num_slots();
}
size_t mach::Server::reg(const std::shared_ptr<SocketConn> &c) {
c->_sig = _emit_event.connect(
[safe_c = std::weak_ptr<SocketConn>(c)](const std::shared_ptr<State> &s) {
if (auto c = safe_c.lock()) {
std::string msg = s->toJSON().dump() + "\n";
c->send(msg, true);
}
});
return _emit_event.num_slots();
}
void mach::Server::accept() {
auto sess = std::make_shared<SocketConn>(_acc.get_executor(), _ss, labjack, _st);
_acc.async_accept(sess->_s, [this, sess](boost::system::error_code ec) {
auto endpoint = ec ? ba::ip::tcp::endpoint{} : sess->_s.remote_endpoint();
std::lock_guard<std::mutex> l(coutm);
std::cout << "Connected from " << endpoint << " (" << ec.message() << ")"
<< std::endl;
if (!ec) {
reg(sess);
sess->start();
accept();
}
});
}