def initialize_database(self): self.properties_parser = PropertiesParser() self.wheel_size_in_inches = 2 self.m_per_inches = 0.0254 self.DISC_INERTIA = float(self.properties_parser.get_property("Inertia")) # self.FRICTION_COEFF = float(self.properties_parser.get_property("Friction")) self.ARRAY_SIZE = 20000 self.totalTime = 0 self.array_index = 0 self.time = np.empty(self.ARRAY_SIZE) self.positions_in_rad = np.empty(self.ARRAY_SIZE) self.positions_in_meters = np.empty(self.ARRAY_SIZE) self.velocities_in_rad = np.empty(self.ARRAY_SIZE) self.speed_in_kmh = np.empty(self.ARRAY_SIZE) self.accelerations_in_rad = np.empty(self.ARRAY_SIZE) self.torques = np.empty(self.ARRAY_SIZE) self.derivativeInterval = 0.1 self.numberOfPulsesPerTurn = 1440 self.started = False
def __init__(self, controller): super(PropertiesDialog, self).__init__(parent=None, title=u"Propriétés") self.controller = controller self.properties_parser = PropertiesParser() self.inertia_ctrl, self.friction_ctrl, self.real_time_checkboxes, self.post_processing_checkboxes = self._init_ui() self.Center() self.SetSize((600, 225))
class RealTimePanelController(Observer): def __init__(self, real_time_subplots, app_controller): self.real_time_subplots = real_time_subplots self.app_controller = app_controller self.subplot_factory = RealTimeSubplotFactory() self.property_parser = PropertiesParser() def create_panel(self, panel_parent): self.panel = RealTimePanel( panel_parent, self.real_time_subplots, self.property_parser.get_property("Real-Time Plots") ) self.panel.add_panel_observers(self) def start_plotting(self): self.panel.startTimer() self.app_controller.start_data_acquisition() def stop_plotting(self, save): self.panel.stop_timer() self.panel.Hide() self.app_controller.stop_data_acquisition(save) def update(self, event): if event == "start": self.start_plotting() elif event == "stop": self.stop_plotting(True) elif isinstance(event, PlotTypesChangedObservableEvent): self._create_subplots(event.list_of_plots) def _create_subplots(self, list_of_subplots): self.real_time_subplots = self.subplot_factory.create_subplots(list_of_subplots) self.panel.update_subplots(self.real_time_subplots)
def _initializeApp(self): self.database = RealTimeDatabase() self.database.initialize_database() self.post_processing_database = PostProcessingDatabase() self.dropbox_database = DropboxDatabase() self.dropbox_database.initialize_database() self.properties_parser = PropertiesParser() self.real_time_subplots = self._init_real_time_subplots() self.post_treatment_subplots = self._init_post_processing_subplots() self._init_encoder_controller() self.top_frame_controller = TopFrameController(self.real_time_subplots, self.post_treatment_subplots, self) return True
class PostProcessingPanelController(Observer): def __init__(self, subplots, app_controller): self.subplots = subplots self.dropbox_repository = DropboxRepository() self.app_controller = app_controller self.subplot_factory = PostProcessingSubplotFactory() self.property_parser = PropertiesParser() def create_panel(self, frame): names_of_comparable_files = self.dropbox_repository.fetch_names_of_comparable_files() self.panel = PostProcessingPanel(frame, self.subplots, names_of_comparable_files, self.property_parser.get_property("Post-Processing Plots")) self.panel.add_panel_observers(self) self.panel.draw_plot_canvas() #TODO CRAPPY CODE def update(self, event): if event == "reset": self.app_controller.reset_app() elif isinstance(event, FileCheckedObservableEvent): filename = event.filename self._add_data_line_to_canvas(filename) elif isinstance(event, FileUncheckedObservableEvent): filename = event.filename self._remove_line_from_canvas(filename) elif isinstance(event, PlotTypesChangedObservableEvent): self._create_subplots(event.list_of_plots) def _create_subplots(self, list_of_subplots): self.subplots = self.subplot_factory.create_subplots(list_of_subplots) self.panel.update_subplots(self.subplots) def _add_data_line_to_canvas(self, filename): self.dropbox_repository.add_file_to_compare_to_data(filename) self.panel.refresh_canvas() def _remove_line_from_canvas(self, filename): self.dropbox_repository.remove_file_to_compare_to_data(filename) self.panel.refresh_canvas()
class PropertiesDialog(wx.Dialog): def __init__(self, controller): super(PropertiesDialog, self).__init__(parent=None, title=u"Propriétés") self.controller = controller self.properties_parser = PropertiesParser() self.inertia_ctrl, self.friction_ctrl, self.real_time_checkboxes, self.post_processing_checkboxes = self._init_ui() self.Center() self.SetSize((600, 225)) def _init_ui(self): dialog_panel = wx.Panel(self) vbox = wx.BoxSizer(wx.VERTICAL) properties_sizer = wx.BoxSizer(wx.HORIZONTAL) #Constants constant_box = wx.StaticBox(dialog_panel, label=u'Constantes') constant_box_sizer = wx.StaticBoxSizer(constant_box, orient=wx.VERTICAL) fgs = wx.FlexGridSizer(4, 1, 9, 25) inertia = wx.StaticText(self, label="Inertie") friction = wx.StaticText(self, label="Constante de friction") inertia_text_ctrl = wx.TextCtrl(self) inertia_text_ctrl.AppendText(self.properties_parser.get_property("Inertia")) friction_text_ctrl = wx.TextCtrl(self) friction_text_ctrl.AppendText(self.properties_parser.get_property("Friction")) fgs.AddMany([(inertia), (inertia_text_ctrl, 1, wx.EXPAND), (friction), (friction_text_ctrl, 1, wx.EXPAND)]) constant_box_sizer.Add(fgs) #Real-time plots properties real_time_plots_box = wx.StaticBox(dialog_panel, label=u'Temps-réel') real_time_box_sizer = wx.StaticBoxSizer(real_time_plots_box, orient=wx.VERTICAL) real_time_checkboxes = CustomCheckListBox(self, real_time_plot_types, self.properties_parser.get_property("Real-Time Plots")) real_time_box_sizer.Add(real_time_checkboxes) #Post-processing plots properties post_processing_plots_box = wx.StaticBox(dialog_panel, label=u'Post-traitement') post_processing_box_sizer = wx.StaticBoxSizer(post_processing_plots_box, orient=wx.VERTICAL) post_processing_checkboxes = CustomCheckListBox(self, post_processing_plot_types, self.properties_parser.get_property("Post-Processing Plots")) post_processing_box_sizer.Add(post_processing_checkboxes) properties_sizer.Add(constant_box_sizer) properties_sizer.Add(real_time_box_sizer) properties_sizer.Add(post_processing_box_sizer) dialog_panel.SetSizer(properties_sizer) hbox2 = wx.BoxSizer(wx.HORIZONTAL) okButton = wx.Button(self, label='OK') hbox2.Add(okButton) vbox.Add(dialog_panel, proportion=1, flag=wx.ALL | wx.EXPAND, border=5) vbox.Add(hbox2, flag=wx.ALIGN_CENTER | wx.TOP | wx.BOTTOM, border=10) self.SetSizer(vbox) okButton.Bind(wx.EVT_BUTTON, self.OnClose) return inertia_text_ctrl, friction_text_ctrl, real_time_checkboxes, post_processing_checkboxes def OnClose(self, e): self.controller.save_properties(self.inertia_ctrl.GetValue(), self.friction_ctrl.GetValue(), self.real_time_checkboxes.get_checked(), self.post_processing_checkboxes.get_checked()) self.Destroy()
def __init__(self, subplots, app_controller): self.subplots = subplots self.dropbox_repository = DropboxRepository() self.app_controller = app_controller self.subplot_factory = PostProcessingSubplotFactory() self.property_parser = PropertiesParser()
def save_properties(self, inertia, friction, real_time_plots, post_processing_plots): parser = PropertiesParser() parser.save_property("Inertia", inertia) parser.save_property("Friction", friction) parser.save_property("Real-Time Plots", real_time_plots) parser.save_property("Post-Processing Plots", post_processing_plots) parser.save_to_file()
class RealTimeDatabase(object): __shared_state = {} def __init__(self): self.__dict__ = self.__shared_state def initialize_database(self): self.properties_parser = PropertiesParser() self.wheel_size_in_inches = 2 self.m_per_inches = 0.0254 self.DISC_INERTIA = float(self.properties_parser.get_property("Inertia")) # self.FRICTION_COEFF = float(self.properties_parser.get_property("Friction")) self.ARRAY_SIZE = 20000 self.totalTime = 0 self.array_index = 0 self.time = np.empty(self.ARRAY_SIZE) self.positions_in_rad = np.empty(self.ARRAY_SIZE) self.positions_in_meters = np.empty(self.ARRAY_SIZE) self.velocities_in_rad = np.empty(self.ARRAY_SIZE) self.speed_in_kmh = np.empty(self.ARRAY_SIZE) self.accelerations_in_rad = np.empty(self.ARRAY_SIZE) self.torques = np.empty(self.ARRAY_SIZE) self.derivativeInterval = 0.1 self.numberOfPulsesPerTurn = 1440 self.started = False def get_time(self): return self.time[: self.array_index] def get_positions(self): return self.positions_in_rad[: self.array_index] def get_positions_in_meters(self): return self.positions_in_meters[: self.array_index] def get_velocities(self): return self.velocities_in_rad[: self.array_index] def get_speed_in_kmh(self): return self.speed_in_kmh[: self.array_index] def get_accelerations(self): return self.accelerations_in_rad[: self.array_index] def get_torques(self): return self.torques[: self.array_index] def addPoint(self, position_in_pulses, timeAfterLastPoint): if timeAfterLastPoint < 2147483647: # To eliminate the first point self._add_time_point(timeAfterLastPoint) self._add_position_point(position_in_pulses) self._add_position_in_meters_point(position_in_pulses) self._add_velocity_point() self._add_speed_point_in_kmh() self._add_acceleration_point() self._add_torque_point() self.array_index += 1 def _add_time_point(self, timeAfterLastPoint): self.totalTime += timeAfterLastPoint * 0.001 self.time[self.array_index] = self.totalTime def _add_position_point(self, position_in_pulses): position_in_radians = self._convert_pulses_to_radians(position_in_pulses) self.positions_in_rad[self.array_index] = position_in_radians def _add_position_in_meters_point(self, position_in_pulses): position_in_meters = self._convert_pulses_to_meters(position_in_pulses) self.positions_in_meters[self.array_index] = position_in_meters def _add_velocity_point(self): self.velocities_in_rad[self.array_index] = derivate( self.time[: self.array_index + 1], self.positions_in_rad[: self.array_index + 1], self.derivativeInterval ) self.velocities_in_rad[: self.array_index + 1] = savitzky_golay( self.velocities_in_rad[: self.array_index + 1], 41, 3, deriv=0 ) def _add_speed_point_in_kmh(self): self.speed_in_kmh[self.array_index] = self._convert_rads_per_seconds_to_kmh( self.velocities_in_rad[self.array_index] ) def _add_acceleration_point(self): self.accelerations_in_rad[self.array_index] = derivate( self.time[: self.array_index + 1], self.velocities_in_rad[: self.array_index + 1], self.derivativeInterval ) self.accelerations_in_rad[: self.array_index + 1] = savitzky_golay( self.accelerations_in_rad[: self.array_index + 1], 41, 3, deriv=0 ) def _add_torque_point(self): # self.torques = savitzky_golay(self.accelerations[:self.array_index + 1], 101, 1, deriv=0) self.torques = self.accelerations_in_rad[: self.array_index + 1] * self.DISC_INERTIA # TODO Get the math out def _convert_pulses_to_radians(self, position_in_pulses): return (position_in_pulses / self.numberOfPulsesPerTurn) * (2 * math.pi) def _convert_pulses_to_meters(self, position_in_pulses): position_in_radians = self._convert_pulses_to_radians(position_in_pulses) return position_in_radians * self.m_per_inches * self.wheel_size_in_inches def _convert_rads_per_seconds_to_kmh(self, speed_in_rads): return speed_in_rads * self.wheel_size_in_inches * self.m_per_inches / 1000 * 3600 def serialize_data_as_csv(self): data_string = "" data_string += "Time, Positions, Positions_M, Velocities, Speeds_KMH, Accelerations, Torques \n" for i in range(len(self.time[: self.array_index])): data_string += str(self.time[i]) + "," data_string += str(self.positions_in_rad[i]) + "," data_string += str(self.positions_in_meters[i]) + "," data_string += str(self.velocities_in_rad[i]) + "," data_string += str(self.speed_in_kmh[i]) + "," data_string += str(self.accelerations_in_rad[i]) + "," data_string += str(self.torques[i]) + "\n" return data_string def reset(self): self.initialize_database()
def __init__(self, real_time_subplots, app_controller): self.real_time_subplots = real_time_subplots self.app_controller = app_controller self.subplot_factory = RealTimeSubplotFactory() self.property_parser = PropertiesParser()
class AppController(object): def __init__(self): self._initializeApp() self.REAL_TIME_FILENAME = "RealTime.csv" self.POST_PROCESSING_FILENAME = "PostProcessing.csv" self.INJECTION_TABLE_FILENAME = "InjectionTable.csv" def reset_app(self): self.top_frame_controller.close_frame() self._initializeApp() def _initializeApp(self): self.database = RealTimeDatabase() self.database.initialize_database() self.post_processing_database = PostProcessingDatabase() self.dropbox_database = DropboxDatabase() self.dropbox_database.initialize_database() self.properties_parser = PropertiesParser() self.real_time_subplots = self._init_real_time_subplots() self.post_treatment_subplots = self._init_post_processing_subplots() self._init_encoder_controller() self.top_frame_controller = TopFrameController(self.real_time_subplots, self.post_treatment_subplots, self) return True def _init_encoder_controller(self): self.encoder_controller = EncoderController(self.database) def _init_real_time_subplots(self): plots_to_display = self.properties_parser.get_property("Real-Time Plots") if isinstance(plots_to_display, str): plots_to_display = real_time_plot_types factory = RealTimeSubplotFactory() return factory.create_subplots(plots_to_display) def _init_post_processing_subplots(self): plots_to_display = self.properties_parser.get_property("Post-Processing Plots") if plots_to_display is "": plots_to_display = post_processing_plot_types factory = PostProcessingSubplotFactory() return factory.create_subplots(plots_to_display) def start_data_acquisition(self): self.encoder_controller.start_data_acquisition() def stop_data_acquisition(self, save): self.encoder_controller.stop_data_acquisition() if save: self.save_data_to_dropbox() self.top_frame_controller.create_post_processing_panel() def save_data_to_dropbox(self): directory_name = (str(datetime.now().replace(microsecond=0))) self.post_processing_database.refresh() post_processing_data_string = self.post_processing_database.serialize_data_as_csv() real_time_data_string = self.database.serialize_data_as_csv() injection_table_string = InjectionTable().write_as_csv_string() saver = DropboxSaver() saver.save_data_to_dropbox(directory_name, self.REAL_TIME_FILENAME, real_time_data_string) saver.save_data_to_dropbox(directory_name, self.POST_PROCESSING_FILENAME, post_processing_data_string) saver.save_data_to_dropbox(directory_name, self.INJECTION_TABLE_FILENAME, injection_table_string)