-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
430 lines (336 loc) · 10.3 KB
/
main.cpp
File metadata and controls
430 lines (336 loc) · 10.3 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
// Library for managing interrupt signals
#include <csignal>
// Data manager for exclusive read/write
#include "include/SystemDataManager.h"
// Configuration files
#include "include/config.h"
// System data manager object handle
SystemDataManager dataHandle;
// Handle to shared data
auto shared = dataHandle.getData();
// Custom class objects
#include "include/ArucoClass.h"
#include "include/CaptureClass.h"
#include "include/ControllerClass.h"
#include "include/DisplayClass.h"
#include "include/InputClass.h"
#include "include/KalmanClass.h"
#include "include/LoggingClass.h"
#include "include/SerialClass.h"
#include "include/TasksClass.h"
#include "include/TimingClass.h"
#include "include/TouchscreenClass.h"
// New class objects
// New class objects
CaptureClass Capture( dataHandle ); // Camera capture
ArucoClass Aruco( dataHandle ); // Aruco detector
DisplayClass Canvas( dataHandle ); // Display output
InputClass Input( dataHandle ); // Keyboard input
TimingClass Timing( dataHandle ); // Loop timing measurement
TouchscreenClass Touch( dataHandle ); // Touchscreen position reading
SerialClass Serial( dataHandle, 2 ); // Serial interface
LoggingClass Logging( dataHandle ); // Logging interface
ControllerClass Controller( dataHandle ); // Controller
KalmanClass Kalman( dataHandle ); // Kalman filter
TasksClass Tasks( dataHandle, Timing, Logging ); // Tasks interface
// Function prototypes
void SignalHandler( int signum );
void UpdateSystem();
void SelectTask();
void UpdateState();
void PrintState();
// SYSTEM FLAGS
bool FLAG_PrintOpenCVBuildInfo = false;
bool FLAG_PrintState = false;
/**
* @brief Main program loop
*
* @return int
*/
int main() {
// Reverse type
shared->Amplifier.isReverseConstant = true; // ( constant = 1 )
// Testing
shared->Amplifier.commandedLimits = cv::Point3f( 0.2f, 0.2f, 0.2f );
shared->Controller.isLimitSet = true;
shared->Calibration.isCalibrated = true;
// Settings
shared->Logging.isEnabled = false;
shared->Task.userID = 123;
shared->Target.isTargetFound = 1;
shared->System.useRing = false;
// Optimization for C/C++ data types
std::ios_base::sync_with_stdio( false );
std::cout.setf( std::ios::unitbuf );
// For debugging
if ( FLAG_PrintOpenCVBuildInfo ) {
std::cout << "OpenCV Build: " << cv::getBuildInformation() << std::endl;
cv::utils::logging::setLogLevel( cv::utils::logging::LOG_LEVEL_VERBOSE );
}
// Status update
std::cout << "\nMain: Program running...\n\n";
// Start timer for measuring loop frequency
Timing.StartTimer();
// Add shortcut panel
// Canvas.BuildKeyboardShortcuts();
Canvas.AddStaticDisplayPanels();
// Initialize kalman filter
shared->KalmanFilter.pMatrix = cv::Mat::eye( 6, 6, CV_32F ) * 1.0f;
// Set default system state
shared->System.state = stateEnum::IDLE;
// Main loop
while ( shared->System.isMainRunning ) {
// For debugging
if ( FLAG_PrintState ) {
PrintState();
}
// Update timer (for measuring loop frequency)
Timing.Update();
// Parse any input and use OpenCV WaitKey()
Input.ParseInput( cv::pollKey() & 0xFF );
// Capture frame
Capture.GetFrame();
// Run the appropriate task
SelectTask();
// Detect ArUco tags
Aruco.FindTags();
// Update system
UpdateSystem();
// Update controller
Controller.Update();
Controller.UpdateAmplifier();
Controller.UpdateVibrotactile();
// Check touchscreen input
Touch.GetCursorPosition();
// Update serial messages
Serial.Update();
// Update display
Canvas.Update();
// Update shutdown flags for clean shutdown
if ( shared->System.isShuttingDown ) {
shared->System.isMainRunning = false;
}
}
cv::destroyAllWindows();
return 0;
}
/*
*
*
*
*
*
*
*
*
* =========================================================================================
* =========================================================================================
*
* TTTTTTT AAAA SSSSS KK KK SSSSS
* T AA AA SS KK KK SS
* T AA AA SS KK KK SS
* T AAAAAAAA SSSSS KKKK SSSSS
* T AA AA SS KK KK SS
* T AA AA SS KK KK SS
* T AA AA SSSSS KK KK SSSSSS
*
* =========================================================================================
* =========================================================================================
*/
/**
* @brief State machine for running tasks
*
*/
void SelectTask() {
switch ( shared->Task.state ) {
case taskEnum::IDLE: {
// Defaults
shared->Task.command = 'z';
shared->Task.name = "";
// Do nothing
break;
}
case taskEnum::FITTS: {
/** Select how to generate marker
* x = random x position
* y = random y position
* z = random xy position
* v = constant velocity
*/
shared->Task.command = 'y';
// Update name and start
shared->Task.name = "FITTS";
Tasks.Fitts();
break;
}
case taskEnum::CALIBRATE: {
shared->Task.name = "CALIBRATE";
Tasks.Calibration();
break;
}
case taskEnum::LIMIT: {
shared->Task.name = "LIMITS";
Tasks.Limits();
break;
}
default: {
// Defaults
shared->Task.command = 'z';
shared->Task.name = "";
}
}
}
/**
* @brief Update system state using kalman filter
*
*/
void UpdateSystem() {
if ( shared->Target.isTargetFound && shared->Capture.isFrameReady ) {
if ( cv::norm( cv::Point2f( shared->Target.positionUnfilteredMM.x, shared->Target.positionUnfilteredMM.y ) - cv::Point2f( shared->Target.positionFilteredOldMM.x, shared->Target.positionFilteredOldMM.y ) ) > 100.0f ) {
shared->Target.isTargetReset = true;
}
// Save old data
shared->Target.positionFilteredOldMM = shared->Target.positionFilteredNewMM;
shared->Target.velocityFilteredOldMM = shared->Target.velocityFilteredNewMM;
// Update kalman filter
Kalman.Update( shared->Target.positionUnfilteredMM, shared->Timing.elapsedRunningTime );
// Get updated state values
shared->Target.positionFilteredNewMM = Kalman.GetPosition();
shared->Target.velocityFilteredNewMM = Kalman.GetVelocity();
shared->Target.positionIntegratedMM = Kalman.GetIntegralError();
// Update marker if finger calibrated
if ( shared->Calibration.isCalibrated ) {
// Offset based on calibrated touch
}
}
}
/**
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
void PrintState() {
// enum class stateEnum { IDLE, DRIVING_PWM, MEASURING_LIMITS, MEASURING_CURRENT, ZERO_ENCODER };
std::cout << "State tracker: ";
// System
if ( shared->System.state == stateEnum::IDLE ) {
std::cout << "System::IDLE";
} else if ( shared->System.state == stateEnum::DRIVING_PWM ) {
std::cout << "System::DRIVING_PWM";
} else if ( shared->System.state == stateEnum::MEASURING_LIMITS ) {
std::cout << "System::MEASURING_LIMITS";
} else if ( shared->System.state == stateEnum::ZERO_ENCODER ) {
std::cout << "System::ZERO_ENCODER";
} else {
std::cout << "System::ERROR!";
}
std::cout << "\t ";
// Task
if ( shared->Task.state == taskEnum::IDLE ) {
std::cout << "Task::IDLE";
} else if ( shared->Task.state == taskEnum::CALIBRATE ) {
std::cout << "Task::CALIBRATE";
} else if ( shared->Task.state == taskEnum::FITTS ) {
std::cout << "Task::FITTS";
} else if ( shared->Task.state == taskEnum::LIMIT ) {
std::cout << "Task::LIMIT";
} else {
std::cout << "Task::ERROR!";
}
std::cout << "\t ";
// Adjustment system selection
auto& system = shared->Input.selectedAdjustmentSystem;
if ( system == selectSystemEnum::NONE ) {
std::cout << "SelectSystem::NONE";
} else if ( system == selectSystemEnum::GAIN_PROPORTIONAL ) {
std::cout << "SelectSystem::GAIN_PROPORTIONAL";
} else if ( system == selectSystemEnum::GAIN_INTEGRAL ) {
std::cout << "SelectSystem::GAIN_INTEGRAL";
} else if ( system == selectSystemEnum::GAIN_DERIVATIVE ) {
std::cout << "SelectSystem::GAIN_DERIVATIVE";
} else if ( system == selectSystemEnum::AMP_LIMIT ) {
std::cout << "SelectSystem::AMP_LIMIT";
} else if ( system == selectSystemEnum::AMP_TENSION ) {
std::cout << "SelectSystem::AMP_TENSION";
} else {
std::cout << "SelectSystem::ERROR!";
}
std::cout << "\t ";
// Adjustment subsystem selection
auto& subsystem = shared->Input.selectedAdjustmentSubsystem;
if ( subsystem == selectSubsystemEnum::NONE ) {
std::cout << "SelectSubsystem::NONE";
} else if ( subsystem == selectSubsystemEnum::ABD ) {
std::cout << "SelectSubsystem::ABD";
} else if ( subsystem == selectSubsystemEnum::ADD ) {
std::cout << "SelectSubsystem::ADD";
} else if ( subsystem == selectSubsystemEnum::FLEX ) {
std::cout << "SelectSubsystem::FLEX";
} else if ( subsystem == selectSubsystemEnum::EXT ) {
std::cout << "SelectSubsystem::EXT";
} else if ( subsystem == selectSubsystemEnum::ALL ) {
std::cout << "SelectSubsystem::ALL";
} else if ( subsystem == selectSubsystemEnum::AMP_A ) {
std::cout << "SelectSubsystem::AMP_A";
} else if ( subsystem == selectSubsystemEnum::AMP_B ) {
std::cout << "SelectSubsystem::AMP_B";
} else if ( subsystem == selectSubsystemEnum::AMP_C ) {
std::cout << "SelectSubsystem::AMP_C";
} else
std::cout << "\t ";
std::cout << "\t ";
// Torque target
if ( shared->Input.selectLimit == selectLimitEnum::NONE ) {
std::cout << "SelectLimit::NONE";
} else if ( shared->Input.selectLimit == selectLimitEnum::AMP_A ) {
std::cout << "SelectLimit::AMP_A";
} else if ( shared->Input.selectLimit == selectLimitEnum::AMP_B ) {
std::cout << "SelectLimit::AMP_B";
} else if ( shared->Input.selectLimit == selectLimitEnum::AMP_C ) {
std::cout << "SelectLimit::AMP_C";
}
std::cout << "\n";
}
// enum class stateEnum { IDLE, DRIVING_PWM, MEASURING_LIMITS, ZERO_ENCODER };
// enum class taskEnum { IDLE, CALIBRATE, FITTS, LIMIT };
// enum class selectGainTargetEnum { NONE, ABD, ADD, FLEX, EXT, AMPA, AMPB, AMPC, TORQUE, LIMITS };
// enum class selectGainEnum { NONE, KP, KI, KD };
// enum class selectTorqueTargetEnum { NONE, ABD, ADD, FLEX, EXT };
// enum class selectLimitEnum { NONE, AMP_A, AMP_B, AMP_C };
/**
* @brief Shutdown safely if error occurs
*/
void SignalHandler( int signum ) {
std::cout << "\nMain: Interrupt signal (" << signum << ") received.\n";
shared->System.isMainRunning = false;
Touch.Close();
exit( signum );
}