def __init__(self): Process.__init__(self) self.exit = Event() self.csv = CSVExport() self.i2cMux = PCA9547() self.settings0 = RTIMU.Settings(SETTINGS_FILE_0) self.settings1 = RTIMU.Settings(SETTINGS_FILE_1) self.settings2 = RTIMU.Settings(SETTINGS_FILE_2) self.settings3 = RTIMU.Settings(SETTINGS_FILE_3) self.settings4 = RTIMU.Settings(SETTINGS_FILE_4) self.settings5 = RTIMU.Settings(SETTINGS_FILE_5) self.imu0 = RTIMU.RTIMU(self.settings0) self.imu1 = RTIMU.RTIMU(self.settings1) self.imu2 = RTIMU.RTIMU(self.settings2) self.imu3 = RTIMU.RTIMU(self.settings3) self.imu4 = RTIMU.RTIMU(self.settings4) self.imu5 = RTIMU.RTIMU(self.settings5) self.settings = [ self.settings0, self.settings1, self.settings2, self.settings3, self.settings4, self.settings5 ] self.imus = [ self.imu0, self.imu1, self.imu2, self.imu3, self.imu4, self.imu5 ] self.detectedIMU = [False] * 6 self.detectImu() self.pollInterval = 2
def start(self): ## Reference for the Threaded serial adquisition # # Threaded adquisition for a serial device with interrupts # using Qt4 signals self.data = SerialThread(str(self.ui.cBox_IMU.currentText())) self.data.openPort(str(self.ui.cBox_Port.currentText()), int(self.ui.cBox_Speed.currentText())) ## Register the data object with the cube self.cube = Cube(self.data) # start file if self.ui.chBox_export.isChecked(): ## export data self.csv = CSVExport() self.ui.statusbar.showMessage("Exporting data") else: self.ui.statusbar.showMessage("Adquiring data") # start data thread self.data.start() # dataThread new data signal self.connect(self.data, QtCore.SIGNAL('newData()'), self.updateData) # update UI self.setUILocked(True) # start timer for updating plot self.timer.start(20) # start timer every second for activity self.timerActivity.start(1000) ## Reference for the activity detection algorithm # # Activity detection algorithm propossed by Dr. Pablo Reyes self.activity = ActivityDetection(70, 175, 20, .02) ## Reference for the fall detection algorithm # # Fall detection algorithm propossed by Dr. Pablo Reyes self.fall = FallDetection(.02, 2.8, .65) #~ self.fall = FallDetection(.02, 2.0, .65) ## Reference for self.posture = Posture()
def start(self): self.data = SerialProcess(self.queue) # Select serial port configuration form ui self.data.openPort(str(self.ui.cBox_Port.currentText()), int(self.ui.cBox_Speed.currentText())) if self.data.start() is False: QtGui.QMessageBox.question(self, "Can't open Serial Port", "Serial port is already opened.", QtGui.QMessageBox.Ok) else: # start CSV data export if enabled if self.ui.chBox_export.isChecked(): # export data self.csv = CSVExport() self.ui.statusbar.showMessage("Exporting data") else: self.ui.statusbar.showMessage("Acquiring data") # start data process, lock the ui and set plot update time self.set_ui_locked(True) self.timer_plot_update.start(PLOT_UPDATE_TIME) self.timer_freq_update.start(PLOT_UPDATE_TIME*10)
class MainWindow(QtGui.QMainWindow): ## # @brief Configures initial settings of the window. # Initialize data, plots, imported functions and timers # @param self Object handler def __init__(self): QtGui.QMainWindow.__init__(self) self.ui = Ui_MainWindow() self.ui.setupUi(self) # Initializes plots self.ui.plt.setBackground(background=None) self.ui.plt.setAntialiasing(True) self.plt1 = self.ui.plt.addPlot(row=1, col=1) # Variables self.queue = Queue(N_SAMPLES) self.data = None self.csv = None self.DATA0 = deque([], maxlen=N_SAMPLES) self.TIME = deque([], maxlen=N_SAMPLES) self.reset_buffers() ## # @brief Reference update plot timer # Qt4 timer to trigger the @updatePlot function self.timer_plot_update = QtCore.QTimer(self) self.timer_freq_update = QtCore.QTimer(self) # Qt signals and slots QtCore.QObject.connect(self.ui.pButton_Start, QtCore.SIGNAL('clicked()'), self.start) QtCore.QObject.connect(self.ui.pButton_Stop, QtCore.SIGNAL('clicked()'), self.stop) QtCore.QObject.connect(self.timer_plot_update, QtCore.SIGNAL('timeout()'), self.update_plot) QtCore.QObject.connect(self.timer_freq_update, QtCore.SIGNAL('timeout()'), self.update_freq) # Configure UI ports = getSerialPorts() if len(ports) <= 0: ans = QtGui.QMessageBox.question(self, "No serial ports available", "Connect a serial device.\n" + "Scan again?", QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) if ans == QtGui.QMessageBox.Yes: ports = getSerialPorts() self.ui.cBox_Port.addItems(ports) self.ui.cBox_Speed.addItems(["9600", "57600", "115200"]) self.ui.cBox_Speed.setCurrentIndex(2) self.set_ui_locked(False) # Configure plots self.configure_plot(self.plt1, "CSV1", "") ## # @brief Start SerialProcess for data acquisition # @param self Object handler def start(self): self.data = SerialProcess(self.queue) # Select serial port configuration form ui self.data.openPort(str(self.ui.cBox_Port.currentText()), int(self.ui.cBox_Speed.currentText())) if self.data.start() is False: QtGui.QMessageBox.question(self, "Can't open Serial Port", "Serial port is already opened.", QtGui.QMessageBox.Ok) else: # start CSV data export if enabled if self.ui.chBox_export.isChecked(): # export data self.csv = CSVExport() self.ui.statusbar.showMessage("Exporting data") else: self.ui.statusbar.showMessage("Acquiring data") # start data process, lock the ui and set plot update time self.set_ui_locked(True) self.timer_plot_update.start(PLOT_UPDATE_TIME) self.timer_freq_update.start(PLOT_UPDATE_TIME*10) ## # @brief Updates graphs and writes to CSV files if enabled # @param self Object handler def update_plot(self): # Get new data from buffer while self.queue.qsize() != 0: data = self.queue.get(True, 1) self.TIME.append(data[0]) self.DATA0.append(data[1]) # If enabled, write to log file if self.ui.chBox_export.isChecked(): self.csv.csvWrite(data) # Draw new data self.plt1.clear() self.plt1.plot(x=list(self.TIME)[-PLOT_UPDATE_POINTS:], y=list(self.DATA0)[-PLOT_UPDATE_POINTS:], pen='#2196F3') def update_freq(self): # Show adquisition frequency self.ui.statusbar.showMessage("Sampling at " + str(1/(self.TIME[-1] - self.TIME[-2])) + " Hz") ## # @brief Stops SerialProcess for data acquisition # @param self Object handler def stop(self): self.data.closePort() self.data.join() self.timer_plot_update.stop() self.timer_freq_update.stop() self.set_ui_locked(False) self.reset_buffers() self.ui.statusbar.showMessage("Stopped data acquisition") ## # @brief Basic configurations for a plot # @param self Object handler # @param plot Plot to be customized # @param title Title for the plot # @param unit Unit for the plot # @param plot_range List with min and max values to show in the plot @staticmethod def configure_plot(plot, title, unit, y_min=0, y_max=0, label_color='#2196F3', label_size='11pt'): label_style = {'color': label_color, 'font-size': label_size} plot.setLabel('left', title, unit, **label_style) plot.setLabel('bottom', 'Time', 's', **label_style) plot.showGrid(x=False, y=True) if y_min != y_max: plot.setYRange(y_min, y_max) else: plot.enableAutoRange(axis=None, enable=True) plot.setMouseEnabled(x=False, y=False) ## # @brief Basic configurations for a plot # @param self Object handler # @param enabled Sets the ui locked def set_ui_locked(self, enabled): self.ui.pButton_Stop.setEnabled(enabled) self.ui.pButton_Start.setEnabled(not enabled) self.ui.cBox_Port.setEnabled(not enabled) self.ui.cBox_Speed.setEnabled(not enabled) self.ui.chBox_export.setEnabled(not enabled) ## # @brief Clears the buffers # @param self Object handler def reset_buffers(self): self.DATA0.clear() self.TIME.clear() ## # @brief Function to be executed while closing the main window # @param self Object handler # @param event Event who calls the exit def closeEvent(self, event): log.i('Starting close') try: if self.data.is_alive(): log.w('SerialProcess running, stopping it') self.stop() except: pass app.exit() sys.exit()
class MainProcess(Process): ## Constructor # # Inits the process, the CSV file, the PCA9547 Multiplexor, the settings # file for each sensor (with calibration) # @param self The object pointer def __init__(self): Process.__init__(self) self.exit = Event() self.csv = CSVExport() self.i2cMux = PCA9547() self.settings0 = RTIMU.Settings(SETTINGS_FILE_0) self.settings1 = RTIMU.Settings(SETTINGS_FILE_1) self.settings2 = RTIMU.Settings(SETTINGS_FILE_2) self.settings3 = RTIMU.Settings(SETTINGS_FILE_3) self.settings4 = RTIMU.Settings(SETTINGS_FILE_4) self.settings5 = RTIMU.Settings(SETTINGS_FILE_5) self.imu0 = RTIMU.RTIMU(self.settings0) self.imu1 = RTIMU.RTIMU(self.settings1) self.imu2 = RTIMU.RTIMU(self.settings2) self.imu3 = RTIMU.RTIMU(self.settings3) self.imu4 = RTIMU.RTIMU(self.settings4) self.imu5 = RTIMU.RTIMU(self.settings5) self.settings = [ self.settings0, self.settings1, self.settings2, self.settings3, self.settings4, self.settings5 ] self.imus = [ self.imu0, self.imu1, self.imu2, self.imu3, self.imu4, self.imu5 ] self.detectedIMU = [False] * 6 self.detectImu() self.pollInterval = 2 ## Detectes the number of IMUs avaliable in each channel # # @param self The object pointer # @return number of detected IMUs def detectImu(self): try: for n, imu in enumerate(self.imus): self.i2cMux.setChannel(n) if imu.IMUInit(): self.detectedIMU[n] = True else: self.detectedIMU[n] = False except: print("Error in detection") return self.detectedIMU def setPollInterval(self, pollInterval): self.pollInterval = pollInterval ## Sets file name for the CSV file # # @param self The object pointer # @param txt file name def setFileName(self, txt): self.csv.setTitle(txt) ## gets the current data from the current sensor # # @param self The object pointer # @return all sensor data as a vector def getData(self): return self.allData ## stops the thread and signals to exit # # @param self The object pointer def stop(self): self.exit.set() def saveSettings(self): try: for s in self.setting: s.save() except: print("Error in detection") return self.detectedIMU ## Thread loop # # Get data in the polling interval definid time, and stores # data form all the avaliable sensor with his identificator and timestamp # @param self The object pointer # @return finalized job def run(self): self.csv.createFile() init_time = time.time() while not self.exit.is_set(): try: for n, imu in enumerate(self.imus): if self.detectedIMU[n]: self.i2cMux.setChannel(n) if imu.IMURead(): data0 = imu.getIMUData() quaternion = data0["fusionQPose"] acceleration = data0["accel"] gyro = data0["gyro"] compass = data0["compass"] nowTime = time.time() - init_time self.allData = [n] + \ [nowTime] + \ [acceleration[0]] + \ [acceleration[1]] + \ [acceleration[2]] + \ [gyro[0]] + \ [gyro[1]] + \ [gyro[2]] + \ [compass[0]] + \ [compass[1]] + \ [compass[2]] + \ [quaternion[0]] + \ [quaternion[1]] + \ [quaternion[2]] + \ [quaternion[3]] self.csv.csvWrite(self.allData) #print(self.allData) time.sleep(self.pollInterval * 1.0 / 1000.0) except (KeyboardInterrupt, SystemExit): print("Finishing Thread bad") self.exit.set() print("Finished Adquisition") return