@@ -89,7 +89,8 @@ def connect_to_board(self, i):
89
89
return
90
90
if not board .status ['framework' ]:
91
91
print_func ('\n Install pyControl framework on board before running experiment.' )
92
- self .setup_failed [i ] = True
92
+ self .setup_failed [i ] = True
93
+ self .subjectboxes [i ].error ()
93
94
board .subject = subject
94
95
board .setup_ID = setup
95
96
return board
@@ -104,6 +105,7 @@ def start_hardware_test(self, i):
104
105
board .process_data ()
105
106
except PyboardError :
106
107
self .setup_failed [i ] = True
108
+ self .subjectboxes [i ].error ()
107
109
108
110
def setup_task (self , i ):
109
111
'''Load the task state machine and set variables on i-th board.'''
@@ -115,6 +117,7 @@ def setup_task(self, i):
115
117
board .setup_state_machine (self .experiment ['task' ])
116
118
except PyboardError :
117
119
self .setup_failed [i ] = True
120
+ self .subjectboxes [i ].error ()
118
121
return
119
122
# Set variables.
120
123
board .subject_variables = [v for v in self .experiment ['variables' ]
@@ -236,18 +239,34 @@ def setup_experiment(self, experiment):
236
239
self .startstopclose_all_button .setEnabled (True )
237
240
self .logs_button .setEnabled (True )
238
241
self .plots_button .setEnabled (True )
242
+ self .setups_started = 0
239
243
self .setups_finished = 0
240
- self .setups_running = 0
241
244
242
245
def startstopclose_all (self ):
243
- if self .setups_running == 0 : # This logic in not very clear and may have some redundent terms.
246
+ '''Called when startstopclose_all_button is clicked. Button is
247
+ only active if all setups are in the same state.'''
248
+ if self .startstopclose_all_button .text () == 'Close Exp.' :
249
+ self .close_experiment ()
250
+ elif self .startstopclose_all_button .text () == 'Start All' :
244
251
for i , board in enumerate (self .boards ):
245
- self .subjectboxes [i ].start_stop_rig ()
246
- elif self .setups_finished < len ( self . boards ) :
252
+ self .subjectboxes [i ].start_task ()
253
+ elif self .startstopclose_all_button . text () == 'Stop All' :
247
254
for i , board in enumerate (self .boards ):
248
- self .subjectboxes [i ].start_stop_rig ()
255
+ self .subjectboxes [i ].stop_task ()
256
+
257
+ def update_startstopclose_button (self ):
258
+ '''Called when a setup is started or stopped to update the
259
+ startstopclose_all button.'''
260
+ if self .setups_finished == len (self .boards ):
261
+ self .startstopclose_all_button .setText ('Close Exp.' )
262
+ self .startstopclose_all_button .setIcon (QtGui .QIcon ("gui/icons/close.svg" ))
249
263
else :
250
- self .close_experiment ()
264
+ self .startstopclose_all_button .setText ('Stop All' )
265
+ self .startstopclose_all_button .setIcon (QtGui .QIcon ("gui/icons/stop.svg" ))
266
+ if self .setups_started == len (self .boards ) and self .setups_finished == 0 :
267
+ self .startstopclose_all_button .setEnabled (True )
268
+ else :
269
+ self .startstopclose_all_button .setEnabled (False )
251
270
252
271
def stop_experiment (self ):
253
272
self .update_timer .stop ()
@@ -282,6 +301,7 @@ def stop_experiment(self):
282
301
pv_file .write (json .dumps (persistent_variables , sort_keys = True , indent = 4 ))
283
302
if summary_variables :
284
303
Summary_variables_dialog (self , sv_dict ).show ()
304
+ self .startstopclose_all_button .setEnabled (True )
285
305
286
306
def abort_experiment (self ):
287
307
'''Called if an error occurs while the experiment is being set up.'''
@@ -293,13 +313,14 @@ def abort_experiment(self):
293
313
board .stop_framework ()
294
314
time .sleep (0.05 )
295
315
board .process_data ()
296
- self .subjectboxes [i ].task_stopped ()
316
+ self .subjectboxes [i ].stop_task ()
297
317
msg = QtGui .QMessageBox ()
298
318
msg .setWindowTitle ('Error' )
299
- msg .setText ('There was an error. Closing Experiment ' )
319
+ msg .setText ('An error occured while setting up experiment ' )
300
320
msg .setIcon (QtGui .QMessageBox .Warning )
301
321
msg .exec ()
302
- self .close_experiment ()
322
+ self .startstopclose_all_button .setText ('Close Exp.' )
323
+ self .startstopclose_all_button .setEnabled (True )
303
324
304
325
def close_experiment (self ):
305
326
self .GUI_main .tab_widget .setTabEnabled (0 , True ) # Enable run task tab.
@@ -335,34 +356,10 @@ def show_hide_logs(self):
335
356
336
357
def update (self ):
337
358
'''Called regularly while experiment is running'''
338
- boards_running = False
339
- for i , board in enumerate (self .boards ):
340
- if board .framework_running :
341
- boards_running = True
342
- try :
343
- board .process_data ()
344
- if not board .framework_running :
345
- self .subjectboxes [i ].task_stopped ()
346
- self .subjectboxes [i ].time_text .setText (str (datetime .now ()- self .subjectboxes [i ].start_time ).split ('.' )[0 ])
347
- except PyboardError :
348
- self .subjectboxes [i ].error ()
359
+ for subjectbox in self .subjectboxes :
360
+ subjectbox .update ()
349
361
self .experiment_plot .update ()
350
- if self .setups_running > 0 : # This logic in not very clear and may have some redundent terms.
351
- if self .setups_running == len (self .boards ) and self .setups_finished == 0 :
352
- self .startstopclose_all_button .setEnabled (True )
353
- self .startstopclose_all_button .setText ('Stop All' )
354
- self .startstopclose_all_button .setIcon (QtGui .QIcon ("gui/icons/stop.svg" ))
355
- else :
356
- self .startstopclose_all_button .setEnabled (False )
357
- if self .setups_finished == 0 :
358
- self .startstopclose_all_button .setText ('Stop All' )
359
- self .startstopclose_all_button .setIcon (QtGui .QIcon ("gui/icons/stop.svg" ))
360
- else :
361
- self .startstopclose_all_button .setText ('Close Experiment' )
362
- self .startstopclose_all_button .setIcon (QtGui .QIcon ("gui/icons/close.svg" ))
363
-
364
- if not boards_running and self .setups_finished == len (self .boards ):
365
- self .startstopclose_all_button .setEnabled (True )
362
+ if self .setups_finished == len (self .boards ):
366
363
self .stop_experiment ()
367
364
368
365
def print_to_logs (self , print_str ):
@@ -419,6 +416,7 @@ def __init__(self, name, setup_number, parent=None):
419
416
self .Vlayout = QtGui .QVBoxLayout (self )
420
417
self .Hlayout = QtGui .QHBoxLayout ()
421
418
self .Hlayout .addWidget (self .start_stop_button )
419
+ self .Hlayout .addWidget (self .variables_button )
422
420
self .Hlayout .addWidget (self .status_label )
423
421
self .Hlayout .addWidget (self .status_text )
424
422
self .Hlayout .addWidget (self .time_label )
@@ -430,7 +428,6 @@ def __init__(self, name, setup_number, parent=None):
430
428
self .Hlayout .addWidget (self .print_label )
431
429
self .Hlayout .addWidget (self .print_text )
432
430
self .Hlayout .setStretchFactor (self .print_text , 10 )
433
- self .Hlayout .addWidget (self .variables_button )
434
431
self .Vlayout .addLayout (self .Hlayout )
435
432
self .Vlayout .addWidget (self .log_textbox )
436
433
@@ -459,15 +456,18 @@ def assign_board(self, board):
459
456
self .variables_dialog = Variables_dialog (self , board )
460
457
self .variables_button .clicked .connect (self .variables_dialog .exec_ )
461
458
self .variables_button .setEnabled (True )
462
- self .start_stop_button .clicked .connect (self .start_stop_rig )
459
+ self .start_stop_button .clicked .connect (self .start_stop_task )
463
460
464
- def start_stop_rig (self ):
461
+ def start_stop_task (self ):
462
+ '''Called when start/stop button on Subjectbox pressed or
463
+ startstopclose_all button is pressed.'''
465
464
if self .state == 'pre_run' :
466
- self .begin_rig ()
465
+ self .start_task ()
467
466
elif self .state == 'running' :
468
- self .task_stopped ()
467
+ self .stop_task ()
469
468
470
- def begin_rig (self ):
469
+ def start_task (self ):
470
+ '''Start the task running on the Subjectbox's board.'''
471
471
self .status_text .setText ('Running' )
472
472
self .state = 'running'
473
473
self .run_exp_tab .experiment_plot .start_experiment (self .setup_number )
@@ -484,28 +484,41 @@ def begin_rig(self):
484
484
485
485
self .start_stop_button .setText ('Stop' )
486
486
self .start_stop_button .setIcon (QtGui .QIcon ("gui/icons/stop.svg" ))
487
- self .run_exp_tab .setups_running += 1
487
+ self .run_exp_tab .setups_started += 1
488
488
489
489
self .run_exp_tab .GUI_main .refresh_timer .stop ()
490
490
self .run_exp_tab .update_timer .start (update_interval )
491
+ self .run_exp_tab .update_startstopclose_button ()
491
492
492
493
def error (self ):
493
494
'''Set state text to error in red.'''
494
- self .state_text .setText ('Error' )
495
- self .state_text .setStyleSheet ('color: red;' )
495
+ self .status_text .setText ('Error' )
496
+ self .status_text .setStyleSheet ('color: red;' )
496
497
497
- def task_stopped (self ):
498
- '''Called when task stops running.'''
498
+ def stop_task (self ):
499
+ '''Called to stop task or if task stops automatically.'''
500
+ if self .board .framework_running :
501
+ self .board .stop_framework ()
499
502
self .state_text .setText ('Stopped' )
500
503
self .state_text .setStyleSheet ('color: grey;' )
501
504
self .status_text .setText ('Stopped' )
502
- self .start_stop_button .setVisible (False )
503
- # Stop running board
504
- if self .board .framework_running :
505
- self .board .stop_framework ()
505
+ self .start_stop_button .setEnabled (False )
506
506
self .run_exp_tab .experiment_plot .active_plots .remove (self .setup_number )
507
507
self .run_exp_tab .setups_finished += 1
508
508
self .variables_button .setEnabled (False )
509
+ self .run_exp_tab .update_startstopclose_button ()
510
+
511
+ def update (self ):
512
+ '''Called regularly while experiment is running.'''
513
+ if self .board .framework_running :
514
+ try :
515
+ self .board .process_data ()
516
+ if not self .board .framework_running :
517
+ self .stop_task ()
518
+ self .time_text .setText (str (datetime .now ()- self .start_time ).split ('.' )[0 ])
519
+ except PyboardError :
520
+ self .stop_task ()
521
+ self .error ()
509
522
510
523
def process_data (self , new_data ):
511
524
'''Update the state, event and print line info.'''
0 commit comments