-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.h
More file actions
299 lines (263 loc) · 9.24 KB
/
helpers.h
File metadata and controls
299 lines (263 loc) · 9.24 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
#include <boost/asio.hpp>
#include <boost/signals2.hpp>
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
#include <queue>
#include <shared_mutex>
#include <string>
#include <thread>
#include <utility>
#include <vector>
namespace ba = boost::asio;
namespace bs = boost::signals2;
using json = nlohmann::json;
using namespace std::chrono_literals;
// extern ba::thread_pool pool;
extern std::shared_ptr<ba::thread_pool> poolptr;
extern std::atomic<bool> enabled;
extern std::condition_variable suspend_write;
extern int FastJack;
extern std::condition_variable abort_cv;
extern std::atomic<bool> abort_flag;
extern std::mutex abort_mutex;
namespace mach {
using Timestamp = uint64_t;
Timestamp now();
union udouble {
double d;
unsigned long long u;
};
inline const std::map<std::string, std::string> vlj = {
{"V11_NO", "DIO0"}, {"V30", "DIO1"}, {"V12_NO", "DIO2"},
{"V31", "DIO3"}, {"V20", "DIO4"}, {"V32", "DIO5"},
{"V21", "DIO6"}, {"V33", "DIO7"}, {"V35_NO", "DIO8"},
{"V36", "DIO9"}, {"V34", "DIO10"}, {"V38_NO", "DIO11"},
{"V37", "DIO12"}, {"V23_NO", "DIO13"}, {"V10", "DIO14"},
{"V22", "DIO15"},
};
inline const std::map<std::string, std::string> vljf = {
{"DIO0", "V11_NO"}, {"DIO1", "V30"}, {"DIO2", "V12_NO"},
{"DIO3", "V31"}, {"DIO4", "V20"}, {"DIO5", "V32"},
{"DIO6", "V21"}, {"DIO7", "V33"}, {"DIO8", "V35_NO"},
{"DIO9", "V36"}, {"DIO10", "V34"}, {"DIO11", "V38_NO"},
{"DIO12", "V37"}, {"DIO13", "V23_NO"}, {"DIO14", "V10"},
{"DIO15", "V22"},
};
inline std::mutex coutm;
struct Sensor;
struct LJSensors;
struct SocketConn;
struct Server;
inline unsigned short PORT = 6959;
uint64_t sToMs(uint32_t seconds);
void sleep(uint64_t milliseconds);
const char **vectorToChar(const std::vector<std::string> &stringVector);
const double *vectorToDouble(const std::vector<double> &doubleVector);
template<typename Rep, typename Period>
bool sleepOrAbort(std::chrono::duration<Rep, Period> duration);
struct Sensor {
Sensor(std::string name, std::vector<std::string> params,
const std::vector<double> &settings) {
this->name = std::move(name);
this->params = std::move(params);
this->settings = settings;
}
std::string name;
std::vector<std::string> params;
std::vector<double> settings;
};
/*
* labjack 1
* p31 -> AIN 0
* p21 -> AIN 2
* p32 -> AIN 3
* p10 -> AIN 4
* TC 1 (actuslly t2)-> AIN 7
* TC 2 (actuslly t3)-> AIN 9
* p22 -> AIN 11
*
* labjack 2
* p20 -> AIN 1
* p30 -> AIN 7
* pINJ -> AIN 0
* lc -> AIN 2
* inj1 -> AIN 5
* inj2 -> AIN 6
* ign -> AIN 4
*
*/
struct LJSensors {
double p10val = 0, p21val = 0, p31val = 0, p22val = 0, p32val = 0, p20val = 0,
p30val = 0, pinjval = 0, lcellval = 0, t1val = 0, t2val = 0,
inj1val = 0, inj2val = 0, ignval = 0;
// TODO: swap to proper AINs to change
mach::Sensor *p31 = // p31
new Sensor(std::string("AIN0"),
std::vector<std::string>{"AIN0_RANGE", /*"AIN0_NEGATIVE_CH"*/},
std::vector<double>{10 /*, 1.0*/});
mach::Sensor *p21 = // p21
new Sensor(std::string("AIN2"),
std::vector<std::string>{"AIN2_RANGE", /*"AIN4_NEGATIVE_CH"*/},
std::vector<double>{10 /*, 3.0*/});
mach::Sensor *p10 = // p10
new Sensor(
std::string("AIN4"),
std::vector<std::string>{"AIN4_RANGE" /*, "AIN4_NEGATIVE_CH"*/},
std::vector<double>{10 /*, 5.0*/});
mach::Sensor *p22 = // p22
new Sensor(std::string("AIN11"), std::vector<std::string>{"AIN11_range"},
std::vector<double>{10});
mach::Sensor *p32 = // p32
new Sensor(std::string("AIN3"), std::vector<std::string>{"AIN3_range"},
std::vector<double>{10});
mach::Sensor *t1 = new Sensor(
std::string("AIN7"),
std::vector<std::string>{"AIN7_EF_INDEX", "AIN7_EF_CONFIG_B",
"AIN7_EF_CONFIG_D", "AIN7_EF_CONFIG_E",
"AIN7_EF_CONFIG_A"},
std::vector<double>{22, 60052, 1.0, 0.0, 1});
mach::Sensor *t2 =
new Sensor(std::string("AIN9"),
std::vector<std::string>{
"AIN9_EF_INDEX", "AIN9_EF_CONFIG_B", "AIN9_EF_CONFIG_D",
"AIN9_EF_CONFIG_E", "AIN9_EF_CONFIG_A"},
std::vector<double>{22, 60052, 1.0, 0.0, 1});
mach::Sensor *pinj =
new Sensor(std::string("AIN4"), std::vector<std::string>{"AIN4_RANGE"},
std::vector<double>{10});
mach::Sensor *lc = new Sensor(std::string("AIN0"),
std::vector<std::string>{"AIN0_NEGATIVE_CH"},
std::vector<double>{1.0});
mach::Sensor *inj1 =
new Sensor(std::string("AIN10"), std::vector<std::string>{"AIN10_RANGE", "AIN10_NEGATIVE_CH"},
std::vector<double>{0.1, 11.0});
mach::Sensor *inj2 =
new Sensor(std::string("AIN8"), std::vector<std::string>{"AIN8_RANGE", "AIN8_NEGATIVE_CH"},
std::vector<double>{0.1, 9.0});
mach::Sensor *ign =
new Sensor(std::string("AIN6"), std::vector<std::string>{"AIN6_RANGE", "AIN6_NEGATIVE_CH"},
std::vector<double>{0.1, 7.0});
mach::Sensor *p20 = // p20
new Sensor(std::string("AIN3"), std::vector<std::string>{"AIN3_RANGE"},
std::vector<double>{10});
mach::Sensor *p30 = // p30
new Sensor(std::string("AIN2"), std::vector<std::string>{"AIN2_RANGE"},
std::vector<double>{10});
};
struct State {
private:
bool launched = false;
std::map<std::string, bool> valves;
mutable std::shared_mutex slock;
public:
LJSensors lj;
State() {
valves["V10"] = false;
valves["V11_NO"] = true; // NO valve
valves["V12_NO"] = true; // NO valve
valves["V20"] = false;
valves["V21"] = false;
valves["V22"] = false;
valves["V23_NO"] = true; // NO valve
valves["V30"] = false;
valves["V31"] = false;
valves["V32"] = false;
valves["V33"] = false;
valves["V34"] = false;
valves["V35_NO"] = true; // NO valve
valves["V36"] = false;
valves["V37"] = false;
valves["V38_NO"] = true; // NO valve
}
[[nodiscard]] json toJSON() const {
std::shared_lock guard(slock);
std::vector<std::string> db = {
std::to_string(int(lj.p10val)), std::to_string(int(lj.p21val)),
std::to_string(int(lj.p31val)), std::to_string(int(lj.t1val)),
std::to_string(int(lj.t2val)), std::to_string(int(lj.p20val)),
std::to_string(int(lj.p30val)), std::to_string(int(lj.p22val)),
std::to_string(int(lj.p32val)), std::to_string(int(lj.pinjval)),
std::to_string(int(lj.lcellval)), std::to_string(int(lj.inj1val)),
std::to_string(int(lj.inj2val)), std::to_string(int(lj.ignval)),
};
for (auto &d : db) {
while (d.length() < 4) {
d = "0" + d;
}
}
json jsonState;
jsonState["launched"] = launched;
jsonState["writing"] = bool(enabled);
jsonState["lj"]["p10val"] = db[0];
jsonState["lj"]["p21val"] = db[1];
jsonState["lj"]["p31val"] = db[2];
jsonState["lj"]["t1val"] = db[3];
jsonState["lj"]["t2val"] = db[4];
jsonState["lj"]["p20val"] = db[5];
jsonState["lj"]["p30val"] = db[6];
jsonState["lj"]["p22val"] = db[7];
jsonState["lj"]["p32val"] = db[8];
jsonState["lj"]["pinjval"] = db[9];
jsonState["lj"]["lcval"] = db[10];
jsonState["lj"]["inj1val"] = db[11];
jsonState["lj"]["inj2val"] = db[12];
jsonState["lj"]["ignval"] = db[13];
jsonState["valves"] = valves;
return jsonState;
}
[[nodiscard]] bool isLaunched() const {
std::shared_lock guard(slock);
return launched;
}
void setLaunched(bool status) {
std::unique_lock guard(slock);
State::launched = status;
}
[[nodiscard]] const std::map<std::string, bool> &getValves() const {
std::shared_lock guard(slock);
return valves;
}
void setValves(const std::map<std::string, bool> &v) {
std::unique_lock guard(slock);
State::valves = v;
}
};
void dispatchValve(const std::string &name, int handle, std::shared_ptr<State> &state);
struct SocketConn : std::enable_shared_from_this<SocketConn> {
explicit SocketConn(ba::any_io_executor const &ioContext,
std::stop_source &stopSource, const int &lj, std::shared_ptr<State> &st);
void start();
void send(std::string msg, bool immediate = false);
private:
void input();
bool nq(std::string msg, bool immediate);
// return true if messages are pending post-dequeue
bool dq();
void _read();
void _write();
friend struct Server;
boost::asio::streambuf _buf;
std::list<std::string> _mlist;
ba::ip::tcp::socket _s;
bs::scoped_connection _sig;
std::stop_source _ss;
std::shared_ptr<State> _st;
int labjack;
};
struct Server {
explicit Server(ba::io_context &ioContext, std::stop_source &stopSource,
const std::shared_ptr<State> &st, const int &labjack);
void stop();
size_t emit(const std::shared_ptr<State> &s);
private:
size_t reg(const std::shared_ptr<SocketConn> &c);
void accept();
ba::io_context &_ioc;
ba::ip::tcp::acceptor _acc{_ioc, ba::ip::tcp::v4()};
bs::signal<void(std::shared_ptr<State> const &s)> _emit_event;
std::stop_source _ss;
std::shared_ptr<State> _st;
int labjack;
};
} // namespace mach