Ejemplo n.º 1
0
class ArduinoPlotter:
    def __init__(self):
        self.plotHandler = PyPlotHandler(2000, [0, 2000, 0, 5])
        self.arduinoHandler = ArduinoHandler()
        self.consumerThread = ThreadHandler(self.consumer_function)
        self.timerStatus = InfiniteTimer(0.1, self.print_buffers_status)

    def consumer_function(self):
        if self.arduinoHandler.data_waiting():
            self.plotHandler.put(self.arduinoHandler.buffer_acquisition.get() *
                                 5.0 / 1024.0)

    def get_buffers_status(self, separator):
        return self.arduinoHandler.get_buffers_status(
            separator) + separator + self.plotHandler.get_buffers_status()

    def print_buffers_status(self):
        print self.get_buffers_status(" - ")

    def start(self):
        self.timerStatus.start()
        self.consumerThread.start()
        self.arduinoHandler.start_acquisition()

    def stop(self):
        self.arduinoHandler.stop_acquisition()
        self.consumerThread.stop()
        self.timerStatus.stop()
    def __init__(self, parent, app=None, label=None):
        """
        Initializes all the objects for a arduino acquisition and plotting.
        It has:
        plotHandler: Handles the continuous plot.
        arduinoHandler: Handles the arduino acquisition.
        consumerThread: Redirects the acquire data from the arduinoHandler to the plotHandler.

        See the code of the test function in this file for an example.
        :param parent: The parent to be passed to the pyqtPlotWidget.
        :param app: If it is set, it will be passed to the plotwidget to be updated with every plot update.
        :param label: If it is set as a QtLabel, this label will be updated with the status of the buffers.
        """
        self.plotHandler = None
        self.label = label
        self._init_plotHandler(parent, app)
        self.arduinoHandler = ArduinoHandler(qnt_ch=10)
        self.consumerThread = ThreadHandler(self.consumer_function)
        self.timerStatus = InfiniteTimer(0.05, self.print_buffers_status)
        self.started = False
Ejemplo n.º 3
0
def test():
    import sys
    from PyQt4 import QtGui
    app = QtGui.QApplication(sys.argv)
    form = QtGui.QMainWindow()
    form.resize(800, 600)
    central_widget = QtGui.QWidget(form)
    vertical_layout = QtGui.QVBoxLayout(central_widget)

    plot_handler = PyQtGraphHandler(qnt_points=5000, parent=central_widget)

    plot_handler.plotWidget.setYRange(-1, 1)

    from datetime import datetime
    import numpy as np

    def generate_point():
        agr = datetime.now()
        y_value = agr.microsecond / 1000000.0
        plot_handler.series.buffer.put(np.sin(2 * np.pi * y_value))

    from ThreadHandler import InfiniteTimer
    timer = InfiniteTimer(0.001, generate_point)
    timer.start()

    plot_handler.timer.start(0)

    vertical_layout.addWidget(plot_handler.plotWidget)
    form.setCentralWidget(central_widget)
    form.show()
    app.exec_()

    timer.stop()
    plot_handler.timer.stop()
Ejemplo n.º 4
0
def test():
    import sys
    from PyQt4 import QtGui
    app = QtGui.QApplication(sys.argv)
    form = QtGui.QMainWindow()
    form.resize(800, 600)
    central_widget = QtGui.QWidget(form)
    vertical_layout = QtGui.QVBoxLayout(central_widget)

    plot_handler = EMGPlotHandler(qnt_points=5000,
                                  parent=central_widget,
                                  y_range=[-2.5, 2.5])
    plot_handler.process_in_plotter = False
    plot_handler.emg_bruto.visible = True
    plot_handler.hilbert.visible = False
    plot_handler.hilbert_retificado.visible = False
    from datetime import datetime
    import numpy as np

    def generate_point():
        agr = datetime.now()
        y_value = agr.microsecond / 1000000.0
        emg_bruto = np.sin(
            2 * np.pi * y_value) + 0.4 * np.sin(20 * 2 * np.pi * y_value)
        plot_handler.emg_bruto.buffer.put(emg_bruto + 1)

    from ThreadHandler import InfiniteTimer
    timer = InfiniteTimer(0.001, generate_point)
    timer.start()

    plot_handler.timer.start(0)

    vertical_layout.addWidget(plot_handler.plotWidget)
    form.setCentralWidget(central_widget)
    form.show()
    app.exec_()

    timer.stop()
    plot_handler.timer.stop()
def test():
    import sys
    from PyQt4 import QtGui
    app = QtGui.QApplication(sys.argv)
    form = QtGui.QMainWindow()
    form.resize(800, 600)
    central_widget = QtGui.QWidget(form)
    vertical_layout = QtGui.QVBoxLayout(central_widget)

    plot_handler = IMUGraphHandler(qnt_points=800, parent=central_widget, y_range=[-2, 2])

    from datetime import datetime
    import numpy as np

    def generate_point():
        agr = datetime.now()
        y_value = agr.microsecond / 1000000.0
        accelx = np.sin(2*np.pi*y_value)            + 0.2*np.sin(20*2*np.pi*y_value)
        accely = np.sin(2*np.pi*y_value + 2*np.pi/3)+ 0.2*np.sin(20*2*np.pi*y_value + 2*np.pi/3)
        accelz = np.sin(2*np.pi*y_value - 2*np.pi/3)+ 0.2*np.sin(20*2*np.pi*y_value - 2*np.pi/3)
        plot_handler.accelerometer_x.buffer.put(accelx)
        plot_handler.accelerometer_y.buffer.put(accely)
        plot_handler.accelerometer_z.buffer.put(accelz)

    from ThreadHandler import InfiniteTimer
    timer = InfiniteTimer(0.005, generate_point)
    timer.start()

    plot_handler.timer.start(0)

    vertical_layout.addWidget(plot_handler.plotWidget)
    form.setCentralWidget(central_widget)
    form.show()
    app.exec_()

    timer.stop()
    plot_handler.timer.stop()
Ejemplo n.º 6
0
 def __init__(self):
     self.plotHandler = PyPlotHandler(2000, [0, 2000, 0, 5])
     self.arduinoHandler = ArduinoHandler()
     self.consumerThread = ThreadHandler(self.consumer_function)
     self.timerStatus = InfiniteTimer(0.1, self.print_buffers_status)
Ejemplo n.º 7
0
def test():
    my_arduino_handler = ArduinoHandler(port_name='/dev/ttyUSB0', qnt_ch=10)

    def printer():
        if my_arduino_handler.data_waiting:
            print(my_arduino_handler.buffer_acquisition.get())
            # time.sleep(0.01) # Uncomment if you want to see the buffer_acquisition to get full

    consumer_thr = ThreadHandler(printer)

    # Uncomment this to act as a data logger:
    # from datetime import datetime
    # horario = datetime.now()
    # arq = open("leituras-" + str(horario) + '-dados.txt', 'w')
    # arq.write("Leituras salvas com Arduino Handler - Datetime: " + str(horario) + "\n")
    # arq.write("leituras = [")
    # def saver():
    #    if my_arduino_handler.data_waiting:
    #        arq.write(str(my_arduino_handler.buffer_acquisition.get()))
    #        arq.write(', ')
    # consumer_thr = ThreadHandler(saver)

    def show_status():
        print(my_arduino_handler)

    status_timer = InfiniteTimer(0.5, show_status)
    while True:
        print('-------------------------------')
        print(my_arduino_handler)
        print('-------------------------------')
        print('Menu')
        print('-------------------------------')
        print('start - Automatically Starts Everything')
        print('stop - Automatically Stops Everything')
        print('q - Quit')
        print('-------------------------------')
        print('op - open() ')
        print('cl - close()')
        print('ra - readall()')
        print('sth - start Consumer')
        print('pth - pause Consumer')
        print('rth - resume Consumer')
        print('kth - kill Consumer')
        print('sacq - start Aquisition')
        print('kacq - kill Aquisition')
        print('-------------------------------')

        if sys.version_info.major == 2:
            str_key = raw_input()
        else:
            str_key = input()

        if 'q' in str_key:
            my_arduino_handler.stop_acquisition()
            consumer_thr.stop()
            status_timer.stop()
            break
        elif 'op' in str_key:
            my_arduino_handler.open()
        elif 'cl' in str_key:
            my_arduino_handler.close()
        elif 'ra' in str_key:
            print(my_arduino_handler.serialPort.read_all())
        elif 'sth' in str_key:
            status_timer.start()
            consumer_thr.start()
        elif 'pth' in str_key:
            consumer_thr.pause()
        elif 'rth' in str_key:
            consumer_thr.resume()
        elif 'kth' in str_key:
            status_timer.stop()
            consumer_thr.stop()
        elif 'sacq' in str_key:
            status_timer.start()
            my_arduino_handler.start_acquisition()
        elif 'kacq' in str_key:
            status_timer.stop()
            my_arduino_handler.stop_acquisition()
        elif 'start' in str_key:
            status_timer.start()
            consumer_thr.start()
            my_arduino_handler.start_acquisition()
        elif 'stop' in str_key:
            status_timer.stop()
            my_arduino_handler.stop_acquisition()
            consumer_thr.stop()
Ejemplo n.º 8
0
            # if new_xlim[1] > self.ax.get_xlim()[1]:
            #    self.ax.set_xlim(new_xlim)
            #    self.ax.figure.canvas.draw()
            # print 'Limits: ' + str([self.x_values[0], self.x_values[len(self.x_values)-1]])

        return self.line,
        '''

    def show(self):
        plt.show()

    def get_buffers_status(self):
        return "Plot: %4d" % (self.plot_buffer.qsize()) + '/' + str(self.plot_buffer.maxsize)

if __name__ == '__main__':
    my_plot = PyPlotHandler(50000, [0, 50000, 0, 1])

    from datetime import datetime

    def generate_point():
        agr = datetime.now()
        y_value = agr.microsecond / 1000000.0
        my_plot.put(y_value)
        #print y_value

    timer = InfiniteTimer(0, generate_point)
    timer.start()

    plt.show()
    timer.stop()
Ejemplo n.º 9
0
        self.app.processEvents()  # force complete redraw for every plot

    def get_buffers_status(self):
        return "Plot: %4d" % (self.plot_buffer.qsize()) + '/' + str(
            self.plot_buffer.maxsize)

    def appear(self):
        self.timer.start(0)
        import sys
        if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
            QtGui.QApplication.instance().exec_()

        self.timer.stop()


if __name__ == '__main__':

    my_plot = pyqtgraphHandler(5000)

    from datetime import datetime

    def generate_point():
        agr = datetime.now()
        y_value = agr.microsecond / 1000000.0
        my_plot.put(np.sin(2 * np.pi * y_value))
        #print y_value

    timer = InfiniteTimer(0.001, generate_point)
    timer.start()
    my_plot.appear()
class QtArduinoPlotter:
    def __init__(self, parent, app=None, label=None):
        """
        Initializes all the objects for a arduino acquisition and plotting.
        It has:
        plotHandler: Handles the continuous plot.
        arduinoHandler: Handles the arduino acquisition.
        consumerThread: Redirects the acquire data from the arduinoHandler to the plotHandler.

        See the code of the test function in this file for an example.
        :param parent: The parent to be passed to the pyqtPlotWidget.
        :param app: If it is set, it will be passed to the plotwidget to be updated with every plot update.
        :param label: If it is set as a QtLabel, this label will be updated with the status of the buffers.
        """
        self.plotHandler = None
        self.label = label
        self._init_plotHandler(parent, app)
        self.arduinoHandler = ArduinoHandler(qnt_ch=10)
        self.consumerThread = ThreadHandler(self.consumer_function)
        self.timerStatus = InfiniteTimer(0.05, self.print_buffers_status)
        self.started = False

    def _init_plotHandler(self, parent, app):
        """
        Only initializes the plotHandler object. It is set as a method to allow override.
        """
        self.plotHandler = PyQtGraphHandler(qnt_points=5000,
                                            parent=parent,
                                            y_range=(0, 5),
                                            app=app)

    def consumer_function(self):
        """
        This method is called automatically by the consumerThread. Do not call by yourself.
        It redirects the read value to the plotter buffers.
        Override if you want to do some processing in the data before plotting.
        """
        if self.arduinoHandler.data_waiting:
            self.plotHandler.series.buffer.put(
                self.arduinoHandler.buffer_acquisition.get()[-1] * 5.0 /
                1024.0)

    def get_buffers_status(self, separator):
        """
        Returns a string like:
            Serial:    4/1024 - Acq:    1/1024 - Plot:  30/1024
        :param separator: Separates the strings, example ' - ', ' | ', '\n'
        :return: A string containing the status of all the buffers involved in the acquisition and plotting.
        """
        return self.arduinoHandler.get_buffers_status(separator) + separator + \
               self.plotHandler.series.get_buffers_status()

    def print_buffers_status(self):
        """
        Prints the buffer status of send it to the Label if it is defined.
        See get_buffers_status for more information.
        """
        if self.label is None:
            print(self.get_buffers_status(" - "))
        else:
            self.label.setText(
                self.arduinoHandler.serial_tools_obj.description +
                "\tBuffers: " + self.get_buffers_status(" - "))

    def start(self):
        """
        Set a started flag to True and starts the following tasks: (in this order)
        timerStatus : for updating the status label.
        plotHandler : timer for updating the plot.
        consumerThread : for redirect the data from the arduino to the plotter.
        arduinoHandler : serial port acquisition with a thread.
        """
        self.started = True
        self.timerStatus.start()
        self.plotHandler.timer.start(0)
        self.consumerThread.start()
        self.arduinoHandler.start_acquisition()

    def stop(self):
        """
        Set a started flag to False and stops the following tasks: (in this order)
        arduinoHandler : serial port acquisition with a thread.
        consumerThread : for redirect the data from the arduino to the plotter.
        timerStatus : for updating the status label.
        plotHandler : timer for updating the plot.
        """
        self.started = False
        self.arduinoHandler.stop_acquisition()
        self.consumerThread.stop()
        self.timerStatus.stop()
        self.plotHandler.timer.stop()