-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNoahKhanLabjack.cpp
280 lines (202 loc) · 7.04 KB
/
NoahKhanLabjack.cpp
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
#include "NoahKhanLabjack.h"
/**
* program used for Watson Lab data collection via Labjack T7.
**/
// For printf
#include <stdio.h>
// For cin/cout
#include <iostream>
// For csv files
#include <fstream>
// For the LabJackM Library
// For time
#include <chrono>
#include<string>
#include "global.h"
#include "LJM_Utilities.h"
NoahKhanLabjack::NoahKhanLabjack(int frameNum, const char* names[MAX_FRAMES], const char* trigChannel, const char* serialNo, std::string boxName)
{
err = LJM_Open(LJM_dtANY, LJM_ctANY, serialNo, &handle);
ErrorCheck(err, "LJM_Open");
std::string serialnumber(serialNo);
this->boxName = boxName;
//std::cout << this->boxName << std::endl;
//printf("%d",handle);
deviceType = GetDeviceType(handle);
msDelay = READ_DELAY_MS;
tempNumFrames = frameNum;
triggerChannel = trigChannel;
//PrintDeviceInfoFromHandle(handle);
for (int i = 0; i < tempNumFrames; i++) {
aNames[i] = " ";
tempNames[i] = names[i];
aValues[i] = 2.36;//assigned random values.
}
numFrames = 0;
}
NoahKhanLabjack::~NoahKhanLabjack() {
printf("Destructor called on LabJack \n");
if (handle != -1) { // Check if handle is valid
LJM_Close(handle);
}
}
inline std::uint64_t getTime() {
using namespace std::chrono;
milliseconds ms = duration_cast<milliseconds>(
system_clock::now().time_since_epoch()
);
return ms.count();
}
bool NoahKhanLabjack::printCheck() {
bool print_check = false;
for (int j = 0; j < numFrames; j++)
{
if (j == light_index and std::abs(aValues[j] - prev_aValues[j]) > LIGHT_CHANGE_THRESHOLD) {
print_check = true;
}
else if (j == wheel_index and std::abs(aValues[j] - prev_aValues[j]) > WHEEL_CHANGE_THRESHOLD) {
print_check = true;
}
else if (j != light_index and j != wheel_index and aValues[j] != prev_aValues[j]) {
print_check = true;
}
prev_aValues[j] = aValues[j];
}
return print_check;
}
void NoahKhanLabjack::start() {
printf("\nStarting the loop for LabJack : %s\n", this->boxName.c_str());
initializeDevice();
monitoringLoop();
finalizeMonitoring();
}
void NoahKhanLabjack::initializeDevice() {
// Initial light off attempt
handleLJMError(LJM_eWriteAddress(handle, LIGHT_CONTROL_ADDRESS, 0, 0), "Error writing to address for turning off the light");
// Start data collection interval
err = LJM_StartInterval(INTERVAL_HANDLE, msDelay * 1000);
ErrorCheck(err, "Start interval failed");
// Initialize monitoring setup
for (int i = 0; i < tempNumFrames; i++) {
aNames[i] = tempNames[i];
prev_aValues[i] = 0; // Reset previous values
}
numFrames = tempNumFrames;
}
void NoahKhanLabjack::monitoringLoop() {
int skippedIntervals;
while (1) {
uint64_t currentTime = getTime();
updateLightStatus(currentTime);
err = LJM_eReadNames(handle, numFrames, (const char**)aNames, aValues, &errorAddress);
ErrorCheckWithAddress(err, errorAddress, "Read data failed");
processSensorData(currentTime);
// Handle timing for the next data interval
err = LJM_WaitForNextInterval(INTERVAL_HANDLE, &skippedIntervals);
ErrorCheck(err, "Waiting for next interval failed");
if (skippedIntervals > 3) {
printf("Skipped %d intervals.\n", skippedIntervals);
}
}
}
void NoahKhanLabjack::processSensorData(uint64_t currentTime) {
if (this->boxName == global_control_LJ) {
std::lock_guard<std::mutex> lock(flagMutexStop);
if (aValues[0] == 0 && check == 1)
{
printf("\n\n\nWaiting for the starting pulse on Labjack %s.\n", this->boxName.c_str());
globalStopFlag = 1; check = 0;
}
else if (aValues[0] == 1 && check == 0) {
check = 1; globalStopFlag = 0;
name_timestamp = std::to_string(currentTime);
printf("\nStarting Labjacks at time stamp %s : %s \n", this->boxName.c_str(),name_timestamp.c_str());
}
}
if (globalStopFlag != 1 ) {
DataRecord record{};
try {
record.digitalStatus = 0;
uint8_t digital_status = 0;
for (int j = 0; j < 8; ++j) {
digital_status |= (static_cast<uint8_t>(aValues[j + digital_startindex]) << j);
}
record.digitalStatus = digital_status;
constexpr double LIGHT_SCALE_FACTOR = 0.5;
constexpr int MAX_LIGHT_CATEGORY = 9;
int category = static_cast<int>(aValues[light_index] / LIGHT_SCALE_FACTOR);
record.lightStatus = static_cast<unsigned char>((category <= MAX_LIGHT_CATEGORY) ? category : MAX_LIGHT_CATEGORY);
record.pulse = static_cast<bool>(aValues[pulse_index]); // Explicit cast for clarity
record.timestamp = currentTime; // Direct assignment
record.wheel = static_cast<float>(aValues[wheel_index]); // Type casting to float
record.camera = static_cast<bool>(aValues[camera_index]); // Explicit cast for clarity
}
catch (const std::runtime_error& e) {
std::cerr << "Failed to convert data: " << e.what() << std::endl;
}
// Save data to binary file
encodeAndSaveBinary(output_directory + "/" + this->boxName + "_LJ_" + name_timestamp + ".bin", record);
if (print_feature) {
if (printCheck()) {
printSensorValues();
}
}
}
}
void NoahKhanLabjack::encodeAndSaveBinary(const std::string& filename, const DataRecord& record) {
std::ofstream outputFile(filename, std::ios::out | std::ios::binary | std::ios::app);
if (!outputFile.is_open()) {
std::cerr << "\nError opening binary file for writing." << std::endl;
return;
}
outputFile.write(reinterpret_cast<const char*>(&record), sizeof(DataRecord));
outputFile.close();
}
void NoahKhanLabjack::finalizeMonitoring() {
err = LJM_CleanInterval(INTERVAL_HANDLE);
ErrorCheck(err, "LJM_CleanInterval");
err = LJM_Close(handle);
ErrorCheck(err, "LJM_Close");
WaitForUserIfWindows();
}
void NoahKhanLabjack::updateLightStatus(uint64_t currentTime) {
std::time_t currentTimeSec = currentTime / 1000;
std::tm* now_tm = std::localtime(¤tTimeSec);
int currentMinuteOfDay = now_tm->tm_hour * 60 + now_tm->tm_min;
bool shouldLightBeOn = shouldTurnLightOn(currentMinuteOfDay);
if (shouldLightBeOn && !light_flag) {
turnLightOn();
light_flag = true;
}
else if (!shouldLightBeOn && light_flag) {
turnLightOff();
light_flag = false;
}
}
bool NoahKhanLabjack::shouldTurnLightOn(int currentMinute) {
if (startTimeMins <= stopTimeMins) {
return currentMinute >= startTimeMins && currentMinute <= stopTimeMins && startTimeMins != stopTimeMins;
}
else { // Handles overnight scenario
return currentMinute >= startTimeMins || currentMinute <= stopTimeMins;
}
}
void NoahKhanLabjack::turnLightOn() {
handleLJMError(LJM_eWriteAddress(handle, LIGHT_CONTROL_ADDRESS, 0, 1.0), "Turning on the light");
printf("Light turned on for: %s\n", boxName.c_str());
}
void NoahKhanLabjack::turnLightOff() {
handleLJMError(LJM_eWriteAddress(handle, LIGHT_CONTROL_ADDRESS, 0, 0.0), "Turning off the light");
printf("Light turned off for: %s\n", boxName.c_str());
}
void NoahKhanLabjack::printSensorValues() {
printf("\nSensor data for %s:\n", boxName.c_str());
for (int i = 0; i < numFrames; i++) {
printf("%s = %.4f\n", aNames[i], aValues[i]);
}
}
inline void NoahKhanLabjack::handleLJMError(int errorCode, const std::string& message) {
if (errorCode != LJME_NOERROR) {
std::cerr << "Error: " << message << std::endl;
}
}