/
IScale_DynClamp.cpp
1465 lines (1255 loc) · 44.6 KB
/
IScale_DynClamp.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
/*
* Copyright (C) 2011 Weill Medical College of Cornell University
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 675 Mass
* Ave, Cambridge, MA 02139, USA.
*/
#include "IScale_DynClamp.h"
#include <iostream>
#include <math.h>
#include <main_window.h>
#include <data_recorder.h>
#include <QtGlobal>
#if QT_VERSION >= 0x050000
#include <QtWidgets>
#else
#include <QtGui>
#endif
using namespace std;
namespace {
class IScale_DynClamp_SyncEvent : public RT::Event {
public:
int callback(void) {
return 0;
}
};
}
// Create Module Instance
extern "C" Plugin::Object *createRTXIPlugin(void) {
return new IScale_DynClamp::Module();
}
// INPUTs, OUTPUTs, PARAMETERs, and STATEs
static Workspace::variable_t vars[] = {
{ "Input Voltage (V)", "Input voltage (V) from target cell",
Workspace::INPUT, },
{ "Output Current (A)", "Output current (A) to target cell or internal input",
Workspace::OUTPUT, },
{ "Time (ms)", "Time Elapsed (ms)", Workspace::STATE, },
{ "Voltage (mV)",
"Membrane voltage (mV) of target cell computed from amplifier input",
Workspace::STATE, },
{ "Beat Number", "Number of beats", Workspace::STATE, },
{ "APD (ms)", "Action potential duration of cell (ms)", Workspace::STATE, },
{ "Target Curent (A/F)",
"Value of model current targeted for scaling (A/F)", Workspace::STATE, },
{ "Scaled Target Current (A/F)",
"Value of model current after scaling (A/F)", Workspace::STATE, },
{ "APD Repolarization %", "APD Repolarization %", Workspace::PARAMETER, },
{ "Minimum APD (ms)",
"Minimum depolarization duration considered to be an action potential (ms)",
Workspace::PARAMETER, },
{ "Stim Window (ms)",
"Window of time after stimulus that is ignored by APD calculation",
Workspace::PARAMETER, },
{ "Number of Trials", "Number of times the protocol will be repeated",
Workspace::PARAMETER, },
{ "Interval Time (ms)", "Amont of time between each trial",
Workspace::PARAMETER, },
{ "BCL (ms)", "Basic Cycle Length", Workspace::PARAMETER, },
{ "Stim Mag (nA)", "Amplitude of stimulation pulse (nA)",
Workspace::PARAMETER, },
{ "Stim Length (ms)", "Duration of stimulation pulse (nA)",
Workspace::PARAMETER, },
{ "Cm (pF)", "Membrane capacitance of target cell (pF)",
Workspace::PARAMETER, },
{ "LJP (mv)", "Liquid Junction Potential (mV)", Workspace::PARAMETER, },
};
// Number of variables in vars
static size_t num_vars = sizeof(vars) / sizeof(Workspace::variable_t);
IScale_DynClamp::Module::Module(void) :
QWidget(MainWindow::getInstance()->centralWidget()), RT::Thread(0),
Workspace::Instance("Current-scaling Dynamic Clamp", vars, num_vars) {
// Build Module GUI
//QWidget::setAttribute(Qt::WA_DeleteOnClose);
setWindowTitle(QString::number( getID() ) + " Current-scaling Dynamic Clamp");
createGUI();
initialize(); // Initialize parameters, initialize states, reset model, and update rate
refreshDisplay();
show();
} // End constructor
IScale_DynClamp::Module::~Module(void) {
delete protocol;
modelCell = NULL;
delete livshitzRudy2009;
delete faberRudy2000;
/*
setActive(false);
IScale_DynClamp_SyncEvent event;
RT::System::getInstance()->postEvent(&event);
Plugin::Manager::getInstance()->unload(this);
mainWindow->close();
*/
} // End destructor
void IScale_DynClamp::Module::execute(void) { // Real-Time Execution
voltage = input(0) * 1e3 - LJP;
switch( executeMode ) {
case IDLE:
break;
case THRESHOLD:
// Apply stimulus for given number of ms (StimLength)
if( time - cycleStartTime <= stimLength ) {
backToBaseline = false;
peakVoltageT = Vrest;
// stimulsLevel is in nA, convert to A for amplifier
output( 0 ) = stimulusLevel * 1e-9;
}
else {
output( 0 ) = 0;
// Find peak voltage after stimulus
if( voltage > peakVoltageT ) peakVoltageT = voltage;
// If Vm is back to resting membrane potential (within 2 mV;
// determined when threshold detection button is first pressed)
// Vrest: voltage at the time threshold test starts
if( voltage-Vrest < 2 ) {
if ( !backToBaseline ) {
responseDuration = time-cycleStartTime;
responseTime = time;
backToBaseline = true;
}
// Calculate time length of voltage response
// If the response was more than 50ms long and peakVoltage is
// more than 10mV, consider it an action potential
if( responseDuration > 50 && peakVoltageT > 10 ) {
// Set the current stimulus value as the calculated threshold * 2
stimMag = stimulusLevel*1.5;
thresholdOn = false;
executeMode = IDLE;
}
// If no action potential occurred, and Vm is back to rest
else {
// If the cell has rested for 200ms since returning to baseline
if( time-responseTime > 200 ) {
// Increase the magnitude of the stimulus and try again
stimulusLevel += 0.1;
// Record the time of stimulus application
cycleStartTime = time;
}
}
}
}
time += period;
break;
case PACE:
time += period;
stepTime += 1;
// If time is greater than BCL, advance the beat
if ( stepTime - cycleStartTime >= BCLInt ) {
beatNum++;
cycleStartTime = stepTime;
Vrest = voltage;
// First step of APD calculation called at each stimulus
calculateAPD( 1 );
}
// Stimulate cell for stimLength(ms)
if ( (stepTime - cycleStartTime) < stimLengthInt ) {
// stimMag in nA, convert to A for amplifier
outputCurrent = stimMag * 1e-9;
}
else outputCurrent = 0;
// Inject Current
output( 0 ) = outputCurrent;
//Calulate APD
calculateAPD( 2 ); // Second step of APD calculation
break;
case PROTOCOL:
time += period;
stepTime += 1;
if( protocolMode == STEPINIT ) {
// Record data if dataRecord is toggled
if( recordData && !recording && currentStep == 0 ) {
Event::Object event(Event::START_RECORDING_EVENT);
Event::Manager::getInstance()->postEventRT(&event);
recording = true;
}
modelInit = true;
// Model changes do not use up a thread loop by themselves
// Using a while loop makes sure if multiple model changes are called
// consecutively, they will all be called within one execute loop
while( modelInit ) {
// If end of protocol has been reached
if( currentStep >= protocolContainer->size() ) {
protocolMode = END;
modelInit = false;
}
else {
stepPtr = protocolContainer->at( currentStep );
stepType = stepPtr->stepType;
if( stepType == ProtocolStep::CHANGEMODEL ) {
if( stepPtr->modelType == ProtocolStep::LIVRUDY2009 ) {
modelCell = livshitzRudy2009;
}
else modelCell = faberRudy2000;
currentStep++;
}
else if( stepType == ProtocolStep::STARTMODEL ) {
voltageClamp = true;
currentStep++;
}
else if( stepType == ProtocolStep::STOPMODEL ) {
voltageClamp = false;
currentStep++;
}
else if( stepType == ProtocolStep::RESETMODEL ) {
modelCell->resetModel();
currentStep++;
}
else {
stepTime = 0;
cycleStartTime = 0;
if( stepType == ProtocolStep::PACE || stepType == ProtocolStep::SCALE ) {
// set to -1 since time starts at 0, not 1
stepEndTime = (( stepPtr->BCL * stepPtr->numBeats ) / period ) - 1;
}
else if( stepType == ProtocolStep::DIPACE || stepType == ProtocolStep::DISCALE ) {
stepEndBeat = beatNum + stepPtr->numBeats;
// pad stepEndTime to hell to avoid setting off:
// if( stepTime => stepEndTime ) {...}
stepEndTime = (( 1 * stepPtr->numBeats ) / period ) - 1;
ProtocolStep::stepType_t nextStepType = (protocolContainer->at(currentStep+1))->stepType;
if( nextStepType == ProtocolStep::DIPACE ||
nextStepType == ProtocolStep::DISCALE ) {
if(!isDINext) {
isDINext = true;
startDIVoltage = voltage;
}
} else {
isDINext = false;
}
}
else {
// set to -1 since time starts at 0, not 1
stepEndTime = ( stepPtr->waitTime / period ) - 1;
}
pBCLInt = stepPtr->BCL / period; // BCL for protocol
pDIInt = stepPtr->DI / period; // DI for protocol
protocolMode = EXEC;
beatNum++;
Vrest = voltage;
calculateAPD( 1 );
modelInit = false;
}
} // end else
} // end while( modelInit )
} // end STEPINIT
if( protocolMode == EXEC ) { // Execute protocol
// Pace cell at BCL
if( stepType == ProtocolStep::PACE || stepType == ProtocolStep::SCALE) {
if ( stepTime - cycleStartTime >= pBCLInt ) {
beatNum++;
cycleStartTime = stepTime;
Vrest = voltage;
calculateAPD( 1 );
}
// Stimulate cell for stimLength(ms)
if ( (stepTime - cycleStartTime) < stimLengthInt ) {
outputCurrent = stimMag * 1e-9;
}
else outputCurrent = 0;
if( voltageClamp || stepType == ProtocolStep::SCALE ) {
totalModelCurrent = modelCell->voltageClamp(voltage);
}
// If Scaling step, scale current
if( stepType == ProtocolStep::SCALE) {
targetCurrent = modelCell->getParameter( stepPtr->currentToScale );
scaledCurrent = targetCurrent +
( targetCurrent * ( stepPtr->scalingPercentage / 100.0 ) );
// Scale current to cell size; Cm in pF, convert to F
outputCurrent += ( targetCurrent - scaledCurrent ) * Cm * 1e-12;
}
output(0) = outputCurrent;
calculateAPD(2);
} // end if(PACE || SCALE)
// Stimulate based on set diastolic intervals
else if( stepType == ProtocolStep::DIPACE || stepType == ProtocolStep::DISCALE) {
if ( APDMode == DONE ) {
if ( time - APEnd >= (pDIInt * period) ) {
if ( beatNum < stepEndBeat ) {
cycleStartTime = stepTime;
beatNum++;
if (isDINext) {
Vrest = startDIVoltage;
} else {
Vrest = voltage;
}
calculateAPD(1);
}
else {
currentStep++;
protocolMode = STEPINIT;
}
}
}
// Stimulate cell for stimLength(ms)
if ( (stepTime - cycleStartTime) < stimLengthInt ) {
outputCurrent = stimMag * 1e-9;
}
else outputCurrent = 0;
if( voltageClamp || stepType == ProtocolStep::DISCALE ) {
totalModelCurrent = modelCell->voltageClamp(voltage);
}
// If Scaling step, scale current
if( stepType == ProtocolStep::DISCALE) {
targetCurrent = modelCell->getParameter( stepPtr->currentToScale );
scaledCurrent = targetCurrent +
( targetCurrent * ( stepPtr->scalingPercentage / 100.0 ) );
// Scale current to cell size; Cm in pF, convert to F
outputCurrent += ( targetCurrent - scaledCurrent ) * Cm * 1e-12;
}
output(0) = outputCurrent;
calculateAPD(2);
stepEndTime++;
} // end if(DIPACE || DISCALE)
else { // If stepType = WAIT
output(0) = 0;
}
if( stepTime >= stepEndTime ) {
currentStep++;
protocolMode = STEPINIT;
}
} // end EXEC
// End of Protocol: Stop Data recorder and untoggle button
if( protocolMode == END ) {
if(recording == true) {
Event::Object event(Event::STOP_RECORDING_EVENT);
Event::Manager::getInstance()->postEventRT(&event);
recording = false;
}
protocolOn = false;
executeMode = IDLE;
} // end END
} // end switch( executeMode )
} // end execute()
/*
void IScale_DynClamp::Module::execute(void) { // Real-Time Execution
voltage = input(0) * 1e3 - LJP;
switch( executeMode ) {
case IDLE:
break;
case THRESHOLD:
// Apply stimulus for given number of ms (StimLength)
if( time - cycleStartTime <= stimLength ) {
backToBaseline = false;
peakVoltageT = Vrest;
// stimulsLevel is in nA, convert to A for amplifier
output( 0 ) = stimulusLevel * 1e-9;
}
else {
output( 0 ) = 0;
// Find peak voltage after stimulus
if( voltage > peakVoltageT ) peakVoltageT = voltage;
// If Vm is back to resting membrane potential (within 2 mV;
// determined when threshold detection button is first pressed)
// Vrest: voltage at the time threshold test starts
if( voltage-Vrest < 2 ) {
if ( !backToBaseline ) {
responseDuration = time-cycleStartTime;
responseTime = time;
backToBaseline = true;
}
// Calculate time length of voltage response
// If the response was more than 50ms long and peakVoltage is
// more than 10mV, consider it an action potential
if( responseDuration > 50 && peakVoltageT > 10 ) {
// Set the current stimulus value as the calculated threshold * 2
stimMag = stimulusLevel*1.5;
thresholdOn = false;
executeMode = IDLE;
}
// If no action potential occurred, and Vm is back to rest
else {
// If the cell has rested for 200ms since returning to baseline
if( time-responseTime > 200 ) {
// Increase the magnitude of the stimulus and try again
stimulusLevel += 0.1;
// Record the time of stimulus application
cycleStartTime = time;
}
}
}
}
time += period;
break;
case PACE:
time += period;
stepTime += 1;
// If time is greater than BCL, advance the beat
if ( stepTime - cycleStartTime >= BCLInt ) {
beatNum++;
cycleStartTime = stepTime;
Vrest = voltage;
// First step of APD calculation called at each stimulus
calculateAPD( 1 );
}
// Stimulate cell for stimLength(ms)
if ( (stepTime - cycleStartTime) < stimLengthInt ) {
// stimMag in nA, convert to A for amplifier
outputCurrent = stimMag * 1e-9;
}
else outputCurrent = 0;
// Inject Current
output( 0 ) = outputCurrent;
// Calulate APD
calculateAPD( 2 ); // Second step of APD calculation
break;
case PROTOCOL:
time += period;
stepTime += 1;
switch( protocolMode ) {
case STEPINIT:
// Record data if dataRecord is toggled
if( recordData && !recording && currentStep == 0 ) {
Event::Object event(Event::START_RECORDING_EVENT);
Event::Manager::getInstance()->postEventRT(&event);
recording = true;
}
modelInit = true;
// Model changes do not use up a thread loop by themselves
// Using a while loop makes sure if multiple model changes are called
// consecutively, they will all be called within one execute loop
while( modelInit ) {
// If end of protocol has been reached
if( currentStep >= protocolContainer->size() ) {
protocolMode = END;
modelInit = false;
}
else {
stepPtr = protocolContainer->at( currentStep );
stepType = stepPtr->stepType;
switch( stepType ) {
case ProtocolStep::CHANGEMODEL:
if( stepPtr->modelType == ProtocolStep::LIVRUDY2009 ) {
modelCell = livshitzRudy2009;
}
else modelCell = faberRudy2000;
currentStep++;
break;
case ProtocolStep::STARTMODEL:
voltageClamp = true;
currentStep++;
break;
case ProtocolStep::STOPMODEL:
voltageClamp = false;
currentStep++;
break;
case ProtocolStep::RESETMODEL:
modelCell->resetModel();
currentStep++;
break;
case ProtocolStep::PACE:
stepTime = 0;
cycleStartTime = 0;
// set to -1 since time starts at 0, not 1
stepEndTime = (( stepPtr->BCL * stepPtr->numBeats ) / period ) - 1;
pBCLInt = stepPtr->BCL / period; // BCL for protocol
protocolMode = EXEC;
beatNum++;
Vrest = voltage;
calculateAPD( 1 );
modelInit = false;
break;
case ProtocolStep::SCALE:
stepTime = 0;
cycleStartTime = 0;
// set to -1 since time starts at 0, not 1
stepEndTime = (( stepPtr->BCL * stepPtr->numBeats ) / period ) - 1;
pBCLInt = stepPtr->BCL / period; // BCL for protocol
protocolMode = EXEC;
beatNum++;
Vrest = voltage;
calculateAPD( 1 );
modelInit = false;
break;
case ProtocolStep::DIPACE:
stepTime = 0;
cycleStartTime = 0;
stepEndBeat = beatNum + stepPtr->numBeats;
// pad stepEndTime to hell to avoid setting off:
// if( stepTime => stepEndTime ) {...}
stepEndTime = (( 1 * stepPtr->numBeats ) / period ) - 1;
pDIInt = stepPtr->DI / period; // DI for protocol
protocolMode = EXEC;
beatNum++;
Vrest = voltage;
calculateAPD( 1 );
modelInit = false;
break;
case ProtocolStep::DISCALE:
stepTime = 0;
cycleStartTime = 0;
stepEndBeat = beatNum + stepPtr->numBeats;
// pad stepEndTime to hell to avoid setting off:
// if( stepTime => stepEndTime ) {...}
stepEndTime = (( 1 * stepPtr->numBeats ) / period ) - 1;
pDIInt = stepPtr->DI / period; // DI for protocol
protocolMode = EXEC;
beatNum++;
Vrest = voltage;
calculateAPD( 1 );
modelInit = false;
break;
case ProtocolStep::WAIT:
stepTime = 0;
cycleStartTime = 0;
stepEndTime = ( stepPtr->waitTime / period ) - 1;
protocolMode = EXEC;
beatNum++;
Vrest = voltage;
modelInit = false;
break;
default:
break;
} // end switch( stepType )
} // end else
} // end while( modelInit )
break; // end STEPINIT
case EXEC: // execute protocol
// Pace cell at BCL
switch( stepType ) {
case ProtocolStep::PACE:
if ( stepTime - cycleStartTime >= pBCLInt ) {
beatNum++;
cycleStartTime = stepTime;
Vrest = voltage;
calculateAPD( 1 );
}
// Stimulate cell for stimLength(ms)
if ( (stepTime - cycleStartTime) < stimLengthInt ) {
outputCurrent = stimMag * 1e-9;
}
else outputCurrent = 0;
// voltage clamp the model; returns totalModelCurrent for
// debugging purposes only
if( voltageClamp ) {
totalModelCurrent = modelCell->voltageClamp(voltage);
}
output(0) = outputCurrent;
calculateAPD(2);
break;
case ProtocolStep::SCALE:
if ( stepTime - cycleStartTime >= pBCLInt ) {
beatNum++;
cycleStartTime = stepTime;
Vrest = voltage;
calculateAPD( 1 );
}
// Stimulate cell for stimLength(ms)
if ( (stepTime - cycleStartTime) < stimLengthInt ) {
outputCurrent = stimMag * 1e-9;
}
else outputCurrent = 0;
// Calling voltageClamp() is required for SCALE, but
// the totalModelCurrent return value may be discarded.
totalModelCurrent = modelCell->voltageClamp(voltage);
// If Scaling step, scale current
targetCurrent = modelCell->getParameter( stepPtr->currentToScale );
scaledCurrent = targetCurrent + ( targetCurrent * ( stepPtr->scalingPercentage / 100.0 ) );
// Scale current to cell size; Cm in pF, convert to F
outputCurrent += ( targetCurrent - scaledCurrent ) * Cm * 1e-12;
output(0) = outputCurrent;
calculateAPD(2);
break;
case ProtocolStep::DIPACE:
if ( APDMode == DONE ) {
if ( stepTime - APEndStepTime >= pDIInt ) {
if ( beatNum < stepEndBeat ) {
cycleStartTime = stepTime;
beatNum++;
Vrest = voltage;
calculateAPD(1);
}
else {
currentStep++;
protocolMode = STEPINIT;
}
}
}
// Stimulate cell for stimLength(ms)
if ( (stepTime - cycleStartTime) < stimLengthInt ) {
outputCurrent = stimMag * 1e-9;
}
else outputCurrent = 0;
// voltage clamp the model; returns totalModelCurrent for
// debugging purposes only
if( voltageClamp ) {
totalModelCurrent = modelCell->voltageClamp(voltage);
}
output(0) = outputCurrent;
calculateAPD(2);
stepEndTime++;
break;
case ProtocolStep::DISCALE:
if ( APDMode == DONE ) {
if ( time - APEnd >= (pDIInt * period) ) {
if ( beatNum < stepEndBeat ) {
cycleStartTime = stepTime;
beatNum++;
Vrest = voltage;
calculateAPD(1);
}
else {
currentStep++;
protocolMode = STEPINIT;
}
}
}
// Stimulate cell for stimLength(ms)
if ( (stepTime - cycleStartTime) < stimLengthInt ) {
outputCurrent = stimMag * 1e-9;
}
else outputCurrent = 0;
// Calling voltageClamp() is required for DISCALE, but
// the totalModelCurrent return value may be discarded.
totalModelCurrent = modelCell->voltageClamp(voltage);
// If Scaling step, scale current
targetCurrent = modelCell->getParameter( stepPtr->currentToScale );
scaledCurrent = targetCurrent + ( targetCurrent * ( stepPtr->scalingPercentage / 100.0 ) );
// Scale current to cell size; Cm in pF, convert to F
outputCurrent += ( targetCurrent - scaledCurrent ) * Cm * 1e-12;
output(0) = outputCurrent;
calculateAPD(2);
stepEndTime++;
break;
case ProtocolStep::WAIT:
output(0) = 0;
break;
default:
break;
} // end switch( stepType)
if( stepTime >= stepEndTime ) {
currentStep++;
protocolMode = STEPINIT;
}
break; // end EXEC
case END:
// End of Protocol: Stop Data recorder and untoggle button
if(recording == true) {
Event::Object event(Event::STOP_RECORDING_EVENT);
Event::Manager::getInstance()->postEventRT(&event);
recording = false;
}
protocolOn = false;
executeMode = IDLE;
break;// end END
default:
break;
} // end switch( protocolMode )
default:
break;
} // end switch( executeMode )
} // end execute()
*/
// Initialize all variables, protocol, and model cell
void IScale_DynClamp::Module::initialize(void){
protocol = new Protocol();
livshitzRudy2009 = new ModelCell();
livshitzRudy2009->changeModel( ModelCell::LIVRUDY2009 );
faberRudy2000 = new ModelCell();
faberRudy2000->changeModel( ModelCell::FABERRUDY2000 );
modelCell = livshitzRudy2009; // Livshitz Rudy model is the default
protocolContainer = &protocol->protocolContainer; // Pointer to protocol container
// States
time = 0;
voltage = 0;
beatNum = 0;
APD = 0;
targetCurrent = 0;
scaledCurrent = 0;
executeMode = IDLE;
// Parameters
APDRepol = 90;
minAPD = 50;
stimWindow = 4;
numTrials = 1;
intervalTime = 1000;
BCL = 1000;
stimMag = 4;
stimLength = 1;
Cm = 100;
LJP = 0;
startDIVoltage = 0;
mainWindow->APDRepolEdit->setText( QString::number(APDRepol) );
mainWindow->minAPDEdit->setText( QString::number(minAPD) );
mainWindow->stimWindowEdit->setText( QString::number(stimWindow) );
mainWindow->numTrialEdit->setText( QString::number(numTrials) );
mainWindow->intervalTimeEdit->setText( QString::number(intervalTime) );
mainWindow->BCLEdit->setText( QString::number(BCL) );
mainWindow->stimMagEdit->setText( QString::number(stimMag) );
mainWindow->stimLengthEdit->setText( QString::number(stimLength) );
mainWindow->CmEdit->setText( QString::number(Cm) );
mainWindow->LJPEdit->setText( QString::number(LJP) );
// Flags
recordData = mainWindow->recordDataCheckBox->isChecked();
recording = false;
voltageClamp = false;
modelInit = true;
loadedFile = "";
protocolOn = false;
isDINext = false;
// APD parameters
upstrokeThreshold = -40;
}
void IScale_DynClamp::Module::reset( void ) {
// Grabs RTXI thread period and converts to ms (from ns)
period = RT::System::getInstance()->getPeriod()*1e-6;
BCLInt = BCL / period;
stimLengthInt = stimLength / period;
modelCell->setModelRate(100000, period);
stepTime = -1;
time = -period;
cycleStartTime = 0;
beatNum = 1;
Vrest = voltage;
calculateAPD( 1 );
// Protocol variables
currentStep = 0;
targetCurrent = 0;
scaledCurrent = 0;
}
void IScale_DynClamp::Module::addStep( void ) {
int idx = mainWindow->protocolEditorListBox->currentRow();
// Protocol is empty or nothing is selected, add step to end
if( idx == -1 ) {
// Update protocolEditorListBox if a step was added
if( protocol->addStep( this ) ) {
rebuildListBox();
}
}
// If a step is selected, add step after
else {
// Update protocolEditorListBox if a step was added
if( protocol->addStep( this, idx ) ) {
rebuildListBox();
}
}
}
void IScale_DynClamp::Module::deleteStep( void ) {
int idx = mainWindow->protocolEditorListBox->currentRow();
// Protocol is empty or nothing is selected, return
if( idx == -1 ) return ;
// Delete the currently selected step in the list box
protocol->deleteStep( this, idx );
rebuildListBox();
}
void IScale_DynClamp::Module::saveProtocol( void ) {
protocol->saveProtocol( this );
}
void IScale_DynClamp::Module::loadProtocol( void ) {
loadedFile = protocol->loadProtocol( this );
rebuildListBox();
}
void IScale_DynClamp::Module::clearProtocol( void ) {
protocol->clearProtocol();
rebuildListBox();
}
void IScale_DynClamp::Module::toggleThreshold( void ) {
thresholdOn = mainWindow->thresholdButton->isChecked();
setActive(false); //breakage maybe...
IScale_DynClamp_SyncEvent event;
RT::System::getInstance()->postEvent(&event);
// Start protocol, reinitialize parameters to start values
if( thresholdOn ) {
executeMode = THRESHOLD;
reset();
Vrest = input(0) * 1e3;
peakVoltageT = Vrest;
stimulusLevel = 2.0; // na
responseDuration = 0;
responseTime = 0;
setActive( true );
}
// Stop protocol, only called when pace button is unclicked in the
// middle of a run
else {
executeMode = IDLE;
setActive( false );
}
//ToggleThresholdEvent event( this, thresholdOn );
//RT::System::getInstance()->postEvent( &event );
}
void IScale_DynClamp::Module::toggleProtocol( void ) {
bool protocolOn = mainWindow->startProtocolButton->isChecked();
setActive(false);
IScale_DynClamp_SyncEvent event;
RT::System::getInstance()->postEvent(&event);
if( protocolOn ){
if( protocolContainer->size() <= 0 ) {
QMessageBox * msgBox = new QMessageBox;
msgBox->setWindowTitle("Error");
msgBox->setText("I need a protocol first, buddy");
msgBox->setStandardButtons(QMessageBox::Ok);
msgBox->setDefaultButton(QMessageBox::NoButton);
msgBox->setWindowModality(Qt::WindowModal);
msgBox->open();
protocolOn = false;
executeMode = IDLE;
} else {
// Keep on IDLE until update is finished
executeMode = IDLE;
voltageClamp = false;
modelInit = true;
reset();
modelCell->resetModel();
// beatNum is changed at beginning of protocol, so it must start
// at 0 instead of 1
beatNum = 0;
// Used to highlight the current step in list box, -1 to force first
// step to be highlighted
stepTracker = -1;
protocolMode = STEPINIT;
executeMode = PROTOCOL;
setActive( true );
}
}
// Stop protocol, only called when protocol button is unclicked in the
// middle of a run
else {
// Stop data recorder if recording
if( recording ) {
::Event::Object event(::Event::STOP_RECORDING_EVENT);
::Event::Manager::getInstance()->postEventRT(&event);
recording = false;
}
executeMode = IDLE;
setActive( false );
}
//ToggleProtocolEvent event( this, protocolOn );
//RT::System::getInstance()->postEvent( &event );
}
void IScale_DynClamp::Module::togglePace( void ) {
paceOn = mainWindow->staticPacingButton->isChecked();
setActive(false);
IScale_DynClamp_SyncEvent event;
RT::System::getInstance()->postEvent(&event);
// Start protocol, reinitialize parameters to start values
if( paceOn ) {
reset();
executeMode = PACE;
setActive( true );
}
// Stop protocol, only called when pace button is unclicked in the
// middle of a run
else {