3232from MLC .Application import MLC_CALLBACKS
3333from MLC .GUI .Autogenerated .autogenerated import Ui_ExperimentInProgressWindow
3434from MLC .GUI .Experiment .QtCharts .QtChartWrapper import QtChartWrapper
35+ from MLC .arduino .protocol import ProtocolIOException , ProtocolSetupException
3536from MLC .Log .log import get_gui_logger
3637
3738from PyQt5 .QtCore import pyqtSignal
@@ -59,6 +60,7 @@ def __init__(self):
5960 self ._condition = Condition ()
6061 self ._experiment_stopped = False
6162 self ._experiment_cancelled = False
63+ self ._experiment_failure = False
6264
6365 def stop_experiment (self ):
6466 self ._condition .acquire ()
@@ -78,6 +80,12 @@ def cancel_experiment(self):
7880 self ._condition .notify ()
7981 self ._condition .release ()
8082
83+ def fail_experiment (self ):
84+ self ._condition .acquire ()
85+ self ._experiment_failure = True
86+ self ._condition .notify ()
87+ self ._condition .release ()
88+
8189 def wait_if_experiment_stopped (self ):
8290 had_to_wait = False
8391 self ._condition .acquire ()
@@ -94,11 +102,19 @@ def experiment_cancelled(self):
94102 self ._condition .release ()
95103 return cancelled
96104
105+ def experiment_failure (self ):
106+ self ._condition .acquire ()
107+ failure = self ._experiment_failure
108+ self ._condition .release ()
109+ return failure
110+
97111
98112class ExperimentInProgressWindow (QMainWindow ):
99113 indiv_evaluated = pyqtSignal ([int , int , int , float ])
100114 new_generation = pyqtSignal ()
101115 simulation_finished = pyqtSignal ()
116+ board_setup_failure = pyqtSignal ([str ])
117+ board_io_error = pyqtSignal ([str ])
102118
103119 def __init__ (self , parent , parent_signal , chart_params , from_gen , to_gen ):
104120 QMainWindow .__init__ (self , parent )
@@ -115,6 +131,8 @@ def __init__(self, parent, parent_signal, chart_params, from_gen, to_gen):
115131 self .indiv_evaluated .connect (self ._update_dialog )
116132 self .simulation_finished .connect (self ._simulation_finished )
117133 self .new_generation .connect (self ._create_new_chart )
134+ self .board_setup_failure .connect (self ._board_setup_failure )
135+ self .board_io_error .connect (self ._board_io_error )
118136
119137 # Signal to be emited when the experiment finished
120138 self ._parent_signal = parent_signal
@@ -172,7 +190,8 @@ def _update_dialog(self, indivs_per_gen_counter, total_indivs_counter, gen_count
172190
173191 def _simulation_finished (self ):
174192 logger .debug ('{0} [SIM_FINISHED] - Executing _simulation_finished function' .format (self ._log_prefix ))
175- self ._parent_signal .emit (self ._experiment_condition .experiment_cancelled ())
193+ self ._parent_signal .emit (self ._experiment_condition .experiment_cancelled (),
194+ self ._experiment_condition .experiment_failure ())
176195
177196 def _update_current_gen_experiment (self , indiv_index , cost ):
178197 if cost > self ._chart_params ["max_cost" ]:
@@ -227,6 +246,21 @@ def _create_new_chart(self):
227246 picture_layout .addWidget (indiv_canvas )
228247 self ._indiv_chart = indiv_chart
229248
249+ def _board_setup_failure (self , error ):
250+ logger .debug ('{0} [SIM_FINISHED] - Board setup failure: {1}' .format (self ._log_prefix , error ))
251+ selection = QMessageBox .critical (self , "Board Setup error" ,
252+ error ,
253+ QMessageBox .Ok )
254+ self ._experiment_condition .cancel_experiment ()
255+ self ._simulation_finished ()
256+
257+ def _board_io_error (self , error ):
258+ logger .debug ('{0} [SIM_FINISHED] - Board IO error: {1}' .format (self ._log_prefix , error ))
259+ selection = QMessageBox .critical (self , "Board IO error" ,
260+ error ,
261+ QMessageBox .Ok )
262+ self ._experiment_condition .cancel_experiment ()
263+ self ._simulation_finished ()
230264
231265class ExperimentInProgress (Thread ):
232266
@@ -283,6 +317,16 @@ def run(self):
283317 logger .info ('{0} [RUN] - Thread was cancelled by the user'
284318 .format (self ._log_prefix ))
285319 self ._dialog .simulation_finished .emit ()
320+ except ProtocolSetupException , err :
321+ logger .info ('{0} [RUN] - Error in arduino configuration'
322+ .format (self ._log_prefix ))
323+ self ._experiment_condition .fail_experiment ()
324+ self ._dialog .board_setup_failure .emit (str (err ))
325+ except ProtocolIOException , err :
326+ logger .info ('{0} [RUN] - I/O error in arduino connection'
327+ .format (self ._log_prefix ))
328+ self ._experiment_condition .fail_experiment ()
329+ self ._dialog .board_io_error .emit (srt (err ))
286330 except Exception :
287331 logger .info ('{0} [RUN] - Unknown Exception catch. Aborting experiment'
288332 .format (self ._log_prefix ))
0 commit comments