-
Notifications
You must be signed in to change notification settings - Fork 2
/
ExperimentManager.cpp
1300 lines (1253 loc) · 39.5 KB
/
ExperimentManager.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
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "stdafx.h"
#include "ExperimentManager.h"
#include "nidaqmx2.h"
#include <fstream>
#include "TtlSystem.h"
#include "DacSystem.h"
#include "constants.h"
#include "MasterWindow.h"
ExperimentManager::ExperimentManager()
{
functionsFolderLocation = FUNCTIONS_FOLDER_LOCATION;
}
/*
* The workhorse of actually running experiments. This thread procedure analyzes all of the GUI settings and current
* configuration settings to determine how to program and run the experiment.
* @param rawInput: This is the only input to the procedure. It MUST be a pointer to a ExperimentThreadInput structure.
* @return UINT: The return value is not used, i just return TRUE.
*/
UINT __cdecl ExperimentManager::experimentThreadProcedure(LPVOID rawInput)
{
// convert the input to the correct structure.
ExperimentThreadInput* input = (ExperimentThreadInput*)rawInput;
// change the status of the parent object to reflect that the thread is running.
input->thisObj->experimentIsRunning = true;
// warnings will be passed by reference to a series of function calls which can append warnings to the string.
// at a certain point the string will get outputted to the error console. Remember, errors themselves are handled
// by thrower() calls.
std::string warnings;
ULONGLONG startTime = GetTickCount();
try
{
expUpdate("Handling Key... ", input->status, input->quiet);
// load the variables.
input->key->loadVariables( input->vars );
// create the key. Happens only once.
input->key->generateKey();
input->key->exportKey();
expUpdate("Done.\r\n", input->status, input->quiet);
expUpdate("Loading Agilent Info...", input->status, input->quiet);
for (auto agilent : input->agilents)
{
agilent->handleInput();
}
expUpdate("Analyzing Master Script...", input->status, input->quiet);
// analyze the master script.
std::vector<std::pair<unsigned int, unsigned int>> ttlShadeLocations;
std::vector<unsigned int> dacShadeLocations;
//tektronicsInfo tektronicsSettings;
input->dacs->resetDacEvents();
input->ttls->resetTtlEvents();
input->rsg->clearFrequencies();
input->thisObj->loadVariables( input->vars );
input->thisObj->analyzeMasterScript( input->ttls, input->dacs, ttlShadeLocations, dacShadeLocations, input->rsg,
input->vars);
expUpdate("Done.\r\n", input->status, input->quiet);
// update ttl and dac looks & interaction based on which ones are used in the experiment.
input->ttls->shadeTTLs( ttlShadeLocations );
input->dacs->shadeDacs( dacShadeLocations );
//
key workingKey;
workingKey = input->key->getKey();
int variations;
if (input->vars.size() == 0 )
{
variations = 1;
}
else
{
variations = workingKey[input->vars[0].name].first.size();
if ( variations == 0 )
{
variations = 1;
}
}
// go ahead and check if abort was pressed real fast...
if ( input->thisObj->isAborting )
{
expUpdate("\r\nABORTED!\r\n", input->status, input->quiet);
return 0;
}
for (auto& agilent : input->agilents)
{
for (int varInc = 0; varInc < variations; varInc++)
{
// not needed yet because no agilent scripts in this program.
//agilent->programScript( varInc, input->key->getKey(), ? ? ? , input-> ? ? ? );
}
}
// must interpret key before setting the trigger events.
input->ttls->interpretKey(input->key->getKey(), input->vars);
input->dacs->interpretKey(input->key->getKey(), input->vars, warnings);
input->rsg->interpretKey(input->key->getKey(), input->vars);
input->tektronics1->interpretKey(input->key->getKey(), input->vars);
input->tektronics2->interpretKey(input->key->getKey(), input->vars);
ULONGLONG varProgramStartTime = GetTickCount();
expUpdate("Programming All Variation Data...\r\n", input->status, input->quiet);
for (int varInc = 0; varInc < variations; varInc++)
{
// reading these variables should be safe.
if (input->thisObj->isAborting)
{
thrower("\r\nABORTED!\r\n", 0);
}
expUpdate("Programming Variation #" + str(varInc+1) + " Data...\r\n", input->status, input->quiet);
input->dacs->analyzeDacCommands(varInc);
input->dacs->setDacTriggerEvents(input->ttls, varInc);
// prepare dac and ttls. data to final forms.
input->dacs->makeFinalDataFormat(varInc);
input->ttls->analyzeCommandList(varInc);
input->ttls->convertToFinalFormat(varInc);
if (input->ttls->countDacTriggers(varInc) != input->dacs->getNumberSnapshots(varInc))
{
thrower("ERROR: number of dac triggers from the ttl system does not match the number of dac snapshots!"
" Number of dac triggers was " + str(input->ttls->countDacTriggers(varInc)) + " while number of dac "
"snapshots was " + str(input->dacs->getNumberSnapshots(varInc)));
}
input->rsg->orderEvents(varInc);
input->dacs->checkTimingsWork(varInc);
}
if (input->niawgSocket->connectSelected())
{
expUpdate("Connecting to new master...", input->status, input->quiet);
input->niawgSocket->initializeWinsock();
input->niawgSocket->connect();
input->niawgSocket->send("Repetitions: " + str(input->repetitionNumber));
input->niawgSocket->sendVars(input->vars, input->key->getKey());
expUpdate("success.\r\n", input->status, input->quiet);
}
ULONGLONG varProgramEndTime = GetTickCount();
expUpdate("Programming took " + str((varProgramEndTime - varProgramStartTime) / 1000.0) + " seconds.\r\n",
input->status, input->quiet);
expUpdate("Programmed time per repetition: " + str(input->ttls->getTotalTime(0)) + "\r\n",
input->status, input->quiet);
ULONGLONG totalTime = 0;
for (USHORT var = 0; var < variations; var++)
{
totalTime += input->ttls->getTotalTime(var) * input->repetitionNumber;
}
expUpdate("Programmed Total Experiment time: " + str(totalTime) + "\r\n", input->status, input->quiet);
expUpdate("Number of TTL Events in experiment: " + str(input->ttls->getNumberEvents(0)) + "\r\n", input->status,
input->quiet);
expUpdate("Number of DAC Events in experiment: " + str(input->dacs->getNumberEvents(0)) + "\r\n", input->status,
input->quiet);
if (input->debugOptions.showTtls)
{
// output to status
input->status->addStatusText(input->ttls->getTtlSequenceMessage(0));
// output to debug file
std::ofstream debugFile((DEBUG_OUTPUT_LOCATION + str("TTL-Sequence.txt")).c_str(), std::ios_base::app);
if (debugFile.is_open())
{
debugFile << input->ttls->getTtlSequenceMessage(0);
debugFile.close();
}
else
{
expUpdate("ERROR: Debug text file failed to open! Continuing...\r\n", input->status,
input->quiet);
}
}
if (input->debugOptions.showDacs)
{
// output to status
input->status->addStatusText(input->dacs->getDacSequenceMessage(0));
// output to debug file.
std::ofstream debugFile((DEBUG_OUTPUT_LOCATION + str("DAC-Sequence.txt")).c_str(),
std::ios_base::app);
if (debugFile.is_open())
{
debugFile << input->dacs->getDacSequenceMessage(0);
debugFile.close();
}
else
{
input->error->addStatusText("ERROR: Debug text file failed to open! Continuing...\r\n");
}
}
input->globalControl->setUsages(input->vars);
// no quiet on warnings.
expUpdate(warnings, input->status);
/// /////////////////////////////
/// Begin experiment loop
/// //////////
// loop for variations
// TODO: If ! randomizing repetitions
for (int varInc = 0; varInc < variations; varInc++)
{
expUpdate("Variation #" + str(varInc + 1) + "\r\n", input->status, input->quiet);
Sleep(input->debugOptions.sleepTime);
for (auto var : input->key->getKey())
{
// if varies...
if (var.second.second)
{
if (var.second.first.size() == 0)
{
thrower("ERROR: Variable " + var.first + " varies, but has no values assigned to it!");
}
expUpdate(var.first + ": " + str(var.second.first[varInc]) + "\r\n", input->status,
input->quiet);
}
}
expUpdate("Programming Hardware...\r\n", input->status, input->quiet);
input->rsg->programRSG(input->gpib, varInc);
input->rsg->setInfoDisp(varInc);
// program devices
for (auto& agilent : input->agilents)
{
if (!agilent->connected())
{
continue;
}
agilent->convertInputToFinalSettings( input->key->getKey(), varInc );
deviceOutputInfo info = agilent->getOutputInfo();
for (auto chan : range( 2 ))
{
switch (info.channel[chan].option)
{
case -2:
// don't do anything.
break;
case -1:
agilent->outputOff( chan );
break;
case 0:
agilent->setDC( chan, info.channel[chan].dc );
break;
case 1:
agilent->setSingleFreq( chan, info.channel[chan].sine );
break;
case 2:
agilent->setSquare( chan, info.channel[chan].square );
break;
case 3:
agilent->setExistingWaveform( chan, info.channel[chan].preloadedArb );
break;
case 4:
// TODO
default:
thrower( "ERROR: unrecognized channel 1 setting: " + str( info.channel[chan].option ) );
}
}
}
input->tektronics1->programMachine(input->gpib, varInc);
input->tektronics2->programMachine(input->gpib, varInc);
// loop for repetitionNumber
input->repControl->updateNumber(0);
if (input->niawgSocket->connectSelected())
{
std::string msg = input->niawgSocket->recieve();
if (msg != "go")
{
thrower("ERROR: Recieved message from new master that wasn't go! Message was: \"" + msg + "\"");
}
expUpdate("Recieved \"go\" flag from new master.\r\n", input->status, input->quiet);
}
expUpdate("Running Experiment.\r\n", input->status, input->quiet);
for ( int repInc = 0; repInc < input->repetitionNumber; repInc++ )
{
// reading these variables should be safe.
if ( input->thisObj->isAborting )
{
thrower( "\r\nABORTED!\r\n", 0 );
}
else if ( input->thisObj->isPaused )
{
expUpdate("\r\nPaused!\r\n...", input->status, input->quiet);
// wait...
while ( input->thisObj->isPaused )
{
// this could be changed to be a bit smarter I think.
Sleep( 100 );
}
expUpdate("\r\nUn-Paused!\r\n", input->status, input->quiet);
}
input->repControl->updateNumber(repInc + 1);
// this apparently needs to be re-written each time from looking at the VB6 code.
input->dacs->stopDacs();
input->dacs->configureClocks(varInc);
input->dacs->writeDacs(varInc);
input->dacs->startDacs();
input->ttls->writeData(varInc);
input->ttls->startBoard();
// wait until finished.
input->ttls->waitTillFinished(varInc);
}
expUpdate("\r\n", input->status, input->quiet);
}
// do experiment stuff...
expUpdate("\r\nExperiment Finished Normally.\r\n", input->status, input->quiet);
// this is necessary. If not, the dac system will still be "running" and won't allow updates through normal
// means.
input->dacs->stopDacs();
input->dacs->unshadeDacs();
// make sure the display accurately displays the state that the experiment finished at.
try
{
// it's possible to run experiments without dacs.
input->dacs->setDacStatusNoForceOut(input->dacs->getFinalSnapshot());
}
catch (Error& err)
{
// this gets thrown if no dac events. just continue.
}
input->ttls->unshadeTtls();
input->ttls->setTtlStatusNoForceOut(input->ttls->getFinalSnapshot());
}
catch ( Error& exception)
{
if ( input->thisObj->isAborting )
{
expUpdate("\r\nABORTED!\r\n", input->status, input->quiet);
}
else
{
// No quiet option for a bad exit.
input->status->addStatusText( "Bad Exit!\r\n", 0 );
std::string exceptionTxt = exception.what();
input->error->addStatusText( exception.what(), 0 );
}
input->thisObj->experimentIsRunning = false;
std::lock_guard<std::mutex> locker( input->thisObj->abortLock );
input->thisObj->isAborting = false;
input->ttls->unshadeTtls();
input->dacs->unshadeDacs();
delete input;
return false;
}
ULONGLONG endTime = GetTickCount();
expUpdate("Experiment took " + str((endTime - startTime) / 1000.0) + " seconds.\r\n", input->status, input->quiet);
input->thisObj->experimentIsRunning = false;
return true;
}
bool ExperimentManager::runningStatus()
{
return experimentIsRunning;
}
/***
* this function is very similar to startExperimentThread but instead of getting anything from the current profile, it
* knows exactly where to look for the MOT profile. This is currently hard-coded.
*/
void ExperimentManager::loadMotSettings(MasterWindow* master)
{
if ( experimentIsRunning )
{
thrower( "Experiment is Running! Please abort the current run before setting the MOT settings." );
}
ExperimentThreadInput* input = new ExperimentThreadInput;
input->quiet = true;
input->ttls = &master->ttlBoard;
input->dacs = &master->dacBoards;
input->globalControl = &master->globalVariables;
// don't get configuration variables. The MOT shouldn't depend on config variables.
input->vars = master->globalVariables.getEverything();
// Only set it once, clearly.
input->repetitionNumber = 1;
input->repControl = &master->repetitionControl;
input->thisObj = this;
input->status = &master->generalStatus;
input->error = &master->errorStatus;
input->key = &master->masterKey;
input->masterScriptAddress = MOT_ROUTINE_ADDRESS;
input->rsg = &master->RhodeSchwarzGenerator;
input->gpib = &master->gpib;
input->debugOptions = master->debugControl.getOptions();
input->agilents.push_back( &master->topBottomAgilent );
input->agilents.push_back( &master->uWaveAxialAgilent );
input->tektronics1 = &master->tektronics1;
input->tektronics2 = &master->tektronics2;
// don't load, leave whatever the user is working with on the screen.
// no logging needed for simple mot load.
// TODO: Make a control for this.
input->connectToNIAWG = false;
input->niawgSocket = &master->niawgSocket;
// gather parameters.
loadMasterScript(input->masterScriptAddress);
// start thread.
runningThread = AfxBeginThread(experimentThreadProcedure, input);
}
void ExperimentManager::startExperimentThread(MasterWindow* master)
{
if ( experimentIsRunning )
{
thrower( "Experiment is already Running! You can only run one experiment at a time! Please abort before "
"running again." );
}
ExperimentThreadInput* input = new ExperimentThreadInput;
input->quiet = false;
input->ttls = &master->ttlBoard;
input->dacs = &master->dacBoards;
input->globalControl = &master->globalVariables;
// load the variables. This little loop is for letting configuration variables overwrite the globals.
std::vector<variable> configVars = master->configVariables.getEverything();
std::vector<variable> globals = master->globalVariables.getEverything();
std::vector<variable> experimentVars = configVars;
for (auto& globalVar : globals)
{
globalVar.overwritten = false;
bool nameExists = false;
for (auto& configVar : experimentVars)
{
if (configVar.name == globalVar.name)
{
globalVar.overwritten = true;
configVar.overwritten = true;
}
}
if (!globalVar.overwritten)
{
experimentVars.push_back(globalVar);
}
}
input->vars = experimentVars;
master->globalVariables.setUsages(globals);
// it's only necessary to add the two sets of variables together at this
// point. They are not treated differently by the analysis.
//input->vars.insert( input->vars.end(), globals.begin(), globals.end());
input->repetitionNumber = master->repetitionControl.getRepetitionNumber();
input->repControl = &master->repetitionControl;
input->thisObj = this;
input->status = &master->generalStatus;
input->error = &master->errorStatus;
input->key = &master->masterKey;
input->masterScriptAddress = master->profile.getMasterAddressFromConfig();
input->rsg = &master->RhodeSchwarzGenerator;
input->gpib = &master->gpib;
input->debugOptions = master->debugControl.getOptions();
input->agilents.push_back( &master->topBottomAgilent );
input->agilents.push_back( &master->uWaveAxialAgilent );
master->tektronics1.getSettings();
master->tektronics2.getSettings();
input->tektronics1 = &master->tektronics1;
input->tektronics2 = &master->tektronics2;
loadMasterScript(master->profile.getMasterAddressFromConfig());
master->logger.generateLog(master);
master->logger.exportLog();
// TODO: Make a control for this?
input->connectToNIAWG = false;
input->niawgSocket = &master->niawgSocket;
// start thread.
runningThread = AfxBeginThread(experimentThreadProcedure, input, THREAD_PRIORITY_HIGHEST);
}
bool ExperimentManager::getIsPaused()
{
return isPaused;
}
void ExperimentManager::pause()
{
// the locker object locks the lock (the pauseLock obj), and unlocks it when it is destroyed at the end of this function.
std::lock_guard<std::mutex> locker( pauseLock );
isPaused = true;
}
void ExperimentManager::unPause()
{
// the locker object locks the lock (the pauseLock obj), and unlocks it when it is destroyed at the end of this function.
std::lock_guard<std::mutex> locker( pauseLock );
isPaused = false;
}
void ExperimentManager::abort()
{
std::lock_guard<std::mutex> locker( abortLock );
isAborting = true;
}
void ExperimentManager::loadMasterScript(std::string scriptAddress)
{
std::ifstream scriptFile;
// check if file address is good.
FILE *file;
fopen_s( &file, scriptAddress.c_str(), "r" );
if ( !file )
{
thrower("ERROR: Master Script File " + scriptAddress + " does not exist!");
}
else
{
fclose( file );
}
scriptFile.open(scriptAddress.c_str());
// check opened correctly
if (!scriptFile.is_open())
{
thrower("ERROR: File passed test making sure the file exists, but it still failed to open!");
}
// dump the file into the stringstream.
std::stringstream buf( std::ios_base::app | std::ios_base::out | std::ios_base::in );
buf << scriptFile.rdbuf();
// IMPORTANT!
// always pulses the oscilloscope trigger at the end!
buf << "\r\n t += 0.1 \r\n pulseon: " + str(OSCILLOSCOPE_TRIGGER) + " 0.5";
// this is used to more easily deal some of the analysis of the script.
buf << "\r\n\r\n__END__";
// for whatever reason, after loading rdbuf into a stringstream, the stream seems to not
// want to >> into a string. tried resetting too using seekg, but whatever, this works.
currentMasterScript.str("");
//currentMasterScript = std::stringstream(buf.str());
currentMasterScript.str( buf.str());
currentMasterScript.clear();
currentMasterScript.seekg(0);
//std::string str(currentMasterScript.str());
scriptFile.close();
}
void ExperimentManager::loadVariables(std::vector<variable> newVariables)
{
//variables = newVariables;
}
// makes sure formatting is correct, returns the arguments and the function name from reading the firs real line of a function file.
void ExperimentManager::analyzeFunctionDefinition(std::string defLine, std::string& functionName, std::vector<std::string>& args)
{
args.clear();
ScriptStream defStream(defLine);
std::string word;
defStream >> word;
if (word == "")
{
defStream >> word;
}
if (word != "def")
{
thrower("ERROR: Function file in functions folder was not a function because it did not start with \"def\"! Functions must start with this. Instead it"
" started with " + word);
}
std::string functionDeclaration, functionArgumentList;
functionDeclaration = defStream.getline( ':' );
int initNamePos = defLine.find_first_not_of(" \t");
functionName = functionDeclaration.substr(initNamePos, functionDeclaration.find_first_of("(") - initNamePos);
if (functionName.find_first_of(" ") != std::string::npos)
{
thrower("ERROR: Function name included a space!");
}
int initPos = functionDeclaration.find_first_of("(");
if (initPos == std::string::npos)
{
thrower("ERROR: No starting parenthesis \"(\" in function definition. Use \"()\" if no arguments.");
}
initPos++;
int endPos = functionDeclaration.find_last_of(")");
if (endPos == std::string::npos)
{
thrower("ERROR: No ending parenthesis \")\" in function definition. Use \"()\" if no arguments.");
}
functionArgumentList = functionDeclaration.substr(initPos, endPos - initPos);
endPos = functionArgumentList.find_first_of(",");
initPos = functionArgumentList.find_first_not_of(" \t");
bool good = true;
while (initPos != std::string::npos)
{
// get initial argument
std::string tempArg = functionArgumentList.substr(initPos, endPos - initPos);
if (endPos == std::string::npos)
{
functionArgumentList = "";
}
else
{
functionArgumentList.erase(0, endPos + 1);
}
// clean up any spaces on beginning and end.
int lastChar = tempArg.find_last_not_of(" \t");
int lastSpace = tempArg.find_last_of(" \t");
if (lastSpace > lastChar)
{
tempArg = tempArg.substr(0, lastChar + 1);
}
// now it should be clean. Check if there are spaces in the middle.
if (tempArg.find_first_of(" \t") != std::string::npos)
{
thrower("ERROR: bad argument list in function. It looks like there might have been a space or tab inside the function argument?");
}
if (tempArg == "")
{
thrower("ERROR: bad argument list in function. It looks like there might have been a stray \",\"?");
}
args.push_back(tempArg);
endPos = functionArgumentList.find_first_of(",");
initPos = functionArgumentList.find_first_not_of(" \t");
}
}
void ExperimentManager::analyzeFunction( std::string function, std::vector<std::string> args, TtlSystem* ttls,
DacSystem* dacs, std::vector<std::pair<unsigned int, unsigned int>>& ttlShades,
std::vector<unsigned int>& dacShades, RhodeSchwarz* rsg, std::vector<variable>& vars)
{
/// load the file
std::fstream functionFile;
// check if file address is good.
FILE *file;
fopen_s( &file, (FUNCTIONS_FOLDER_LOCATION + function + FUNCTION_EXTENSION).c_str(), "r" );
if ( !file )
{
thrower("ERROR: Function " + function + " does not exist!");
}
else
{
fclose( file );
}
functionFile.open((FUNCTIONS_FOLDER_LOCATION + function + FUNCTION_EXTENSION).c_str(), std::ios::in);
// check opened correctly
if (!functionFile.is_open())
{
thrower("ERROR: Function file " + function + "File passed test making sure the file exists, but it still failed to open!");
}
// append __END__ to the end of the file for analysis purposes.
std::stringstream buf;
ScriptStream functionStream;
buf << functionFile.rdbuf();
functionStream << buf.str();
functionStream << "\r\n\r\n__END__";
functionFile.close();
// functionStream.loadReplacements()
if (functionStream.str() == "")
{
thrower("ERROR: Function File for " + function + "was empty!");
}
std::string word;
// the following are used for repeat: functionality
std::vector<unsigned int> totalRepeatNum;
std::vector<unsigned int> currentRepeatNum;
std::vector<unsigned int> repeatPos;
/// get the function arguments.
std::string defLine;
defLine = functionStream.getline( ':' );
std::string name;
std::vector<std::string> functionArgs;
analyzeFunctionDefinition( defLine, name, functionArgs );
if (functionArgs.size() != args.size())
{
std::string functionArgsString;
for (auto elem : args)
{
functionArgsString += elem + ",";
}
thrower("ERROR: incorrect number of arguments in the call for function " + function + ". Number in call was: "
+ str(args.size()) + ", number expected was " + str(functionArgs.size()) + ". "
"Function arguments were:" + functionArgsString + ".");
}
std::vector<std::pair<std::string, std::string>> replacements;
for (int replacementInc =0; replacementInc < args.size(); replacementInc++)
{
replacements.push_back( { functionArgs[replacementInc], args[replacementInc] } );
}
functionStream.loadReplacements( replacements );
currentFunctionText = functionStream.str();
//
functionStream >> word;
while (!(functionStream.peek() == EOF) || word != "__end__")
{
if (word == "t")
{
std::string command, time;
functionStream >> command;
if (command == "++")
{
operationTime.second++;
}
if (command == "+=")
{
functionStream >> time;
try
{
operationTime.second += reduce(time);
}
catch (Error& exception)
{
// Assume it's an expression with variables, to be evaluated later.
operationTime.first.push_back(time);
}
}
else if (command == "=")
{
functionStream >> time;
try
{
operationTime.second = reduce(time);
}
catch (Error& exception)
{
operationTime.first.push_back(time);
// check if it's a variable.
bool isVar = false;
for (int varInc = 0; varInc < vars.size(); varInc++)
{
// assume it's an expression with a variable to be evaluated later
}
if (!isVar)
{
thrower("ERROR: tried and failed to convert " + time + " to an integer for a time += command.\r\n");
}
}
}
else
{
thrower("ERROR: unrecognized time operator: " + command + ". Expected operators are \"++\", \"+=\", "
"and \"=\"\r\n");
}
}
else if (word == "t++")
{
operationTime.second++;
}
else if (word == "t+=")
{
std::string time;
functionStream >> time;
try
{
operationTime.second += std::stoi(time);
}
catch (std::invalid_argument &exception)
{
// check if it's a variable.
bool isVar = false;
for (int varInc = 0; varInc < vars.size(); varInc++)
{
if (vars[varInc].name == time)
{
isVar = true;
vars[varInc].active = true;
operationTime.first.push_back(time);
break;
}
}
if (!isVar)
{
thrower("ERROR: tried and failed to convert " + time + " to an integer for a time += command.");
}
}
}
else if (word == "t=")
{
std::string time;
functionStream >> time;
try
{
operationTime.second = std::stoi(time);
}
catch (std::invalid_argument &exception)
{
// check if it's a variable.
bool isVar = false;
for (int varInc = 0; varInc < vars.size(); varInc++)
{
if (vars[varInc].name == time)
{
isVar = true;
vars[varInc].active = true;
operationTime.first.push_back(time);
// because it's equals. There shouldn't be any extra terms added to this now.
operationTime.second = 0;
break;
}
}
if (!isVar)
{
thrower("ERROR: tried and failed to convert " + time + " to an integer for a time += command.");
}
}
}
/// callcppcode command
else if (word == "callcppcode")
{
// and that's it...
callCppCodeFunction();
}
/// deal with ttl commands
else if (word == "on:" || word == "off:")
{
std::string name;
functionStream >> name;
ttls->handleTtlScriptCommand( word, operationTime, name, ttlShades );
}
else if (word == "pulseon:" || word == "pulseoff:")
{
// this requires handling time as it is handled above.
std::string name;
std::string pulseLength;
functionStream >> name;
functionStream >> pulseLength;
// should be good to go.
ttls->handleTtlScriptCommand( word, operationTime, name, pulseLength, ttlShades );
}
/// deal with dac commands
else if (word == "dac:")
{
std::string name;
functionStream >> name;
std::string value;
functionStream >> value;
try
{
dacs->handleDacScriptCommand(operationTime, name, "__NONE__", value, "0", "__NONE__", dacShades, vars, ttls);
}
catch (Error& err)
{
thrower(err.whatStr() + "... in \"dac:\" command inside function " + function);
}
}
else if (word == "dacramp:")
{
std::string name, initVal, finalVal, rampTime, rampInc;
// get dac name
functionStream >> name;
// get ramp initial value
functionStream >> initVal;
// get ramp final value
functionStream >> finalVal;
// get total ramp time;
functionStream >> rampTime;
// get ramp point increment.
functionStream >> rampInc;
//
try
{
dacs->handleDacScriptCommand(operationTime, name, initVal, finalVal, rampTime, rampInc, dacShades, vars, ttls);
}
catch (Error& err)
{
thrower(err.whatStr() + "... in \"dacramp:\" command inside function " + function);
}
}
/// Handle RSG calls.
else if (word == "RSG:")
{
rsgEventStructuralInfo info;
functionStream >> info.frequency;
functionStream >> info.power;
// test frequency
info.time = operationTime;
rsg->addFrequency( info );
// set up a trigger for this event.
ttls->handleTtlScriptCommand( "pulseon:", operationTime, rsg->getRsgTtl(), str(rsg->getTriggerTime()), ttlShades );
}
/// deal with function calls.
else if (word == "call")
{
// calling a user-defined function. Get the name and the arguments to pass to the function handler.
std::string functionCall, functionName, functionInputArgs;
functionCall = functionStream.getline( '\r' );
int pos = functionCall.find_first_of("(") + 1;
int finalpos2 = functionCall.find_last_of(")");
int finalpos = functionCall.find_last_of(")");
functionName = functionCall.substr(0, pos - 1);
functionInputArgs = functionCall.substr(pos, finalpos - pos);
std::string arg;
std::vector<std::string> newArgs;
while (true)
{
pos = functionInputArgs.find_first_of(',');
if (pos == std::string::npos)
{
arg = functionInputArgs.substr(0, functionInputArgs.size());
if (arg != "")
{
newArgs.push_back(arg);
}
break;
}
arg = functionInputArgs.substr(0, pos);
if (arg != "")
{
newArgs.push_back(arg);
}
// cut out that argument off the string.
functionInputArgs = functionInputArgs.substr(pos, functionInputArgs.size());
}
if (functionName == function)
{
thrower( "ERROR: Recursive function call detected! " + function + " called itself! This is not allowed." );
}
try
{
analyzeFunction(functionName, newArgs, ttls, dacs, ttlShades, dacShades, rsg, vars);
}
catch (Error& err)
{
// the fact that each function call will re-throw with this will end up putting the whole function call
// stack onto the error message.
thrower(err.whatStr() + "... In function call to function " + functionName);
}
}
else if ( word == "repeat:" )
{
std::string repeatStr;
functionStream >> repeatStr;
try
{
totalRepeatNum.push_back( std::stoi( repeatStr ) );
}
catch ( std::invalid_argument& )
{
thrower( "ERROR: the repeat number failed to convert to an integer! Note that the repeat number can not"
" currently be a variable." );
}
repeatPos.push_back( functionStream.tellg() );
currentRepeatNum.push_back(1);
}
else if ( word == "end" )
{
if (currentRepeatNum.size() == 0)
{
thrower( "ERROR: mismatched \"end\" command for repeat loop! there were more \"end\" commands than \"repeat\" commands." );
}
if ( currentRepeatNum.back() < totalRepeatNum.back() )
{
functionStream.seekg( repeatPos.back() );
currentRepeatNum.back()++;
}
else
{
// remove the entries corresponding to this repeat loop.
currentRepeatNum.pop_back();
repeatPos.pop_back();
totalRepeatNum.pop_back();
// and continue (no seekg)
}
}
else
{
thrower("ERROR: unrecognized master script command: " + word);
}
functionStream >> word;
}
}
void ExperimentManager::analyzeMasterScript(TtlSystem* ttls, DacSystem* dacs,
std::vector<std::pair<unsigned int, unsigned int>>& ttlShades,
std::vector<unsigned int>& dacShades, RhodeSchwarz* rsg, std::vector<variable>& vars)
{
// reset this.
currentMasterScriptText = currentMasterScript.str();
// starts at 0.1 if not initialized by the user.
operationTime.second = 0.1;
operationTime.first.clear();
if (currentMasterScript.str() == "")
{
thrower("ERROR: Master script is empty!\r\n");
}
std::string word;
currentMasterScript >> word;
std::vector<unsigned int > totalRepeatNum;
std::vector<unsigned int > currentRepeatNum;
std::vector<unsigned int > repeatPos;
// the analysis loop.
while (!(currentMasterScript.peek() == EOF) || word != "__end__")
{
std::stringstream individualCommandStream;
// catch if the user uses a space between t and it's operator.
if (word == "t")
{
std::string command;
// assume that there's an '=' or '+=' etc. to still get...
currentMasterScript >> command;
word += command;
}
/// Handle Time Commands.
if (word == "t++")
{
operationTime.second++;
}
else if (word == "t+=")
{
std::string time;
currentMasterScript >> time;
try
{
operationTime.second += reduce(time);
}
catch (Error& exception)