def __init__(self, fs, buffer_size, filename, questions=[], port="COM3"): self.serialBuffer = SerialBuffer(port, buffer_size) self.serialBuffer.callbacks.append(self.callback) self.filename = filename self.questions = questions self.current_question = 0 self.running = False self.finished = False self.plotter = RealTimePlotter(fs) self.plotter.fig.suptitle('Interactive Data Acquitter', fontsize='12', fontweight='bold') self.plotter.fig.subplots_adjust(left=0.27) questions_axes = plt.axes([0, 0.9, 0.2, 0.8]) questions_axes.get_xaxis().set_visible(False) questions_axes.get_yaxis().set_visible(False) self.next_axes = plt.axes([0, 0.05, 0.2, 0.10]) if (len(self.questions) == 0): self.questions_text = questions_axes.text(0, 0, "No question provided", ha='left', fontsize=11, wrap=True) else: qtext = '\n'.join(wrap(self.questions[0].text, 40)) self.questions_text = questions_axes.text(0, 0, qtext, ha='left', fontsize=11, wrap=True) self.btn_next = Button(self.next_axes, 'Continue') self.btn_next.on_clicked(self.next) for q in self.questions: q.currentSample = 0
def __init__(self, device, verbose=False, baud=9600): """Open a serial connection and wait for the ready signal""" self.device = device self.verbose = verbose self.baud = baud self.last_received = 0 # Create the serial buffer and start it up self.serial = SerialBuffer(device, baud) # Create the logger self.logger = TimedLogger(self.serial.start_time, textcolor=self.LOGGING_COLOR) self.serial.start() if not self.wait_for_ready(): exit(1)
from RealTimePlotter import RealTimePlotter from SerialBuffer import SerialBuffer if __name__ == '__main__': import sys sb = None if (len(sys.argv) <= 1): sb = SerialBuffer('COM3', 1024) else: sb = SerialBuffer('COM3', 1024, sys.argv[1]) p = RealTimePlotter() sb.callbacks.append(p.plot) sb.start() p.show() sb.loop()
class HMTLSerial: # Default logging color LOGGING_COLOR = TimedLogger.WHITE # How long to wait for the ready signal after connection MAX_READY_WAIT = 10 def __init__(self, device, verbose=False, baud=9600): """Open a serial connection and wait for the ready signal""" self.device = device self.verbose = verbose self.baud = baud self.last_received = 0 # Create the serial buffer and start it up self.serial = SerialBuffer(device, baud) # Create the logger self.logger = TimedLogger(self.serial.start_time, textcolor=self.LOGGING_COLOR) self.serial.start() if not self.wait_for_ready(): exit(1) def get_message(self, timeout=None): """Returns the next line of text or a complete HMTL message""" item = self.serial.get(wait=timeout) if not item: return None self.last_received = time.time() return item # Wait for data from device indicating its ready for commands def wait_for_ready(self): """Wait for the Arduino to send its ready signal""" self.logger.log("***** Waiting for ready from Arduino *****") start_wait = time.time() while True: item = self.get_message(1.0) if item and (item.data == HMTLprotocol.HMTL_CONFIG_READY): self.logger.log("***** Recieved ready *****") return True if (time.time() - start_wait) > self.MAX_READY_WAIT: raise Exception("Timed out waiting for ready signal") # Send terminated data and wait for (N)ACK def send_and_confirm(self, data, terminated, timeout=10): """Send a command and wait for the ACK""" self.serial.connection.write(data) if terminated: self.serial.connection.write(HMTLprotocol.HMTL_TERMINATOR) start_wait = time.time() while True: item = self.get_message() if item.data == HMTLprotocol.HMTL_CONFIG_ACK: return True if item.data == HMTLprotocol.HMTL_CONFIG_FAIL: raise HMTLConfigException("Configuration command failed") if (time.time() - start_wait) > timeout: raise Exception("Timed out waiting for ACK signal") # XXX: Here we need a method of getting data back from poll or the like # Send a text command def send_command(self, command): self.logger.log("send_command: %s" % (command)) # data = bytes(command, 'utf-8') # send_and_confirm(data) self.send_and_confirm(command, True) # Send a binary config update def send_config(self, type, config): self.logger.log("send_config: %-10s %s" % (type, hexlify(config))) self.send_and_confirm(config, True)
class InteractiveDataAcquitter: def __init__(self, fs, buffer_size, filename, questions=[], port="COM3"): self.serialBuffer = SerialBuffer(port, buffer_size) self.serialBuffer.callbacks.append(self.callback) self.filename = filename self.questions = questions self.current_question = 0 self.running = False self.finished = False self.plotter = RealTimePlotter(fs) self.plotter.fig.suptitle('Interactive Data Acquitter', fontsize='12', fontweight='bold') self.plotter.fig.subplots_adjust(left=0.27) questions_axes = plt.axes([0, 0.9, 0.2, 0.8]) questions_axes.get_xaxis().set_visible(False) questions_axes.get_yaxis().set_visible(False) self.next_axes = plt.axes([0, 0.05, 0.2, 0.10]) if (len(self.questions) == 0): self.questions_text = questions_axes.text(0, 0, "No question provided", ha='left', fontsize=11, wrap=True) else: qtext = '\n'.join(wrap(self.questions[0].text, 40)) self.questions_text = questions_axes.text(0, 0, qtext, ha='left', fontsize=11, wrap=True) self.btn_next = Button(self.next_axes, 'Continue') self.btn_next.on_clicked(self.next) for q in self.questions: q.currentSample = 0 def start(self): self.serialBuffer.start() self.plotter.show() self.serialBuffer.loop() def next(self, event): if (not self.finished and not self.running): self.running = True qtext = '\n'.join( wrap( self.questions[self.current_question].text + ". Collecting data ...", 40)) self.questions_text.set_text(qtext) if (self.finished and not self.running): self.serialBuffer.stop() plt.close() def callback(self, buffer): self.plotter.plot(buffer) if (self.running): q = self.questions[self.current_question] if q.data is None: q.data = buffer else: q.data = np.vstack((q.data, buffer)) q.currentSample += 1 if (q.currentSample >= q.numSamples): self.running = False self.current_question += 1 if (self.current_question >= len(self.questions)): self.current_question = 0 qtext = '\n'.join( wrap( "Test Finished! Data will be saved to {0}. Press continue to exit!" .format(self.filename), 40)) self.questions_text.set_text(qtext) self.finished = True self.save_data() else: qtext = '\n'.join( wrap(self.questions[self.current_question].text, 40)) self.questions_text.set_text(qtext) def save_data(self): with bz2.BZ2File(self.filename, 'w') as f: pickle.dump(self.questions, f)