串行

时间:2018-07-28 12:01:58

标签: python matplotlib arduino

我正在将2个Arduino组合在一起。第一个控制来自鼠标传感器的数据,第二个控制来自超声波传感器的数据,以及将来的伺服系​​统。我创建了一个简单的GUI来显示两个Arduino的2个图形。

我的问题是来自鼠标的数据存在一些延迟。如果我使用传感器5秒钟,则在接下来的15秒钟内,我将从该时间接收数据。那么,您知道这将如何顺利进行吗?我需要它是实时的,以便可以计算速度和加速度。我尝试在Arduino代码上更改延迟,波特率。同时,在Arduino串行监视器上,这真的很好。   在我从Arduino发送dx,sumX,dy,sumY之前,但是更改为仅发送2个值并没有帮助。

dx,dy-求和/求和轴上的位移

X,Y-X,Y的总位移

负责此部分的Python代码

def update_figure(self):
            t = time.time()
            reading=str(ser.readline().decode("utf-8"))
            dx,dy=reading.split(',')
            dx=float(dx) # displaycement in x axis
            dy=float(dy) # displaycement in y axis
            Y=outputdy[-1]  # this is very non pro method to gather sum of movement in Y axis, and below X axis.
            global YSum
            YSum=YSum+Y 
            plt.pause(0.000001)     
            if (len(outputdx)>75):
                outputdx.pop(0)
            if (len(outputdy)>75):
                outputdy.pop(0)         

            lcdx.display(XSum)
            lcdy.display(YSum)
            self.minor.plot(outputdy,  color="red", linewidth=0.5, linestyle="-", label = 'dy')
            self.minor.plot(outputdx,  color="blue", linewidth=0.5, linestyle="-",label='dx')
            self.minor.legend(fontsize=8,loc='upper left', shadow=True,)

            self.draw()
            elapsed = time.time() - t
            print(elapsed)

Here,您可以看到我的应用程序。我打印在每个循环中花费的时间 Movement.update_figure。

Python代码

from __future__ import unicode_literals
import time
import sys
import os
import matplotlib
# Make sure that we are using QT5
matplotlib.use('Qt5Agg')
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import  QLCDNumber, QLabel
from numpy import arange, sin, pi
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import serial
progname = os.path.basename(sys.argv[0])
progversion = "0.1"
ser = serial.Serial('COM3',115200)
arduinoData=serial.Serial('COM4',115200)
output=[]
outputy=[]
outputx=[]
outputdx=[]
outputdy=[]
mean10=0
mean5=0
numbers10=0
X=0
XSum=0
Y=0
YSum=0
class MyMplCanvas(FigureCanvas):
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)


        self.minor=fig.add_subplot(111)
        self.compute_initial_figure()
        FigureCanvas.__init__(self, fig)
        self.setParent(parent)
        FigureCanvas.setSizePolicy(self,
        QtWidgets.QSizePolicy.Expanding,
        QtWidgets.QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)
    def compute_initial_figure(self):
        pass

class Mousemovement(MyMplCanvas):
    def __init__(self, *args, **kwargs):
        MyMplCanvas.__init__(self, *args, **kwargs)
        timer = QtCore.QTimer(self)
        timer.timeout.connect(self.update_figure)
        timer.start(1)      

    def compute_initial_figure(self):
        self.minor.plot([0, 1, 2, 3], [1, 2, 0, 4], 'r')
    def update_figure(self):
        t = time.time()
        reading=str(ser.readline().decode("utf-8"))
        dx,dy=reading.split(',')
        dx=float(dx) # displaycement in x axis
        dy=float(dy) # displaycement in y axis
        Y=outputdy[-1]  
        global YSum
        YSum=YSum+Y 
        plt.pause(0.000001)     
        if (len(outputdx)>75):
            outputdx.pop(0)
        if (len(outputdy)>75):
            outputdy.pop(0)         

        lcdx.display(XSum)
        lcdy.display(YSum)
        self.minor.plot(outputdy,  color="red", linewidth=0.5, linestyle="-", label = 'dy')
        self.minor.plot(outputdx,  color="blue", linewidth=0.5, linestyle="-",label='dx')
        self.minor.legend(fontsize=8,loc='upper left', shadow=True,)

        self.draw()
        elapsed = time.time() - t
        print(elapsed)

class MyDynamicMplCanvas(MyMplCanvas):
#"""A canvas that updates itself every second with a new plot."""
    def __init__(self, *args, **kwargs):
        MyMplCanvas.__init__(self, *args, **kwargs)
        timer = QtCore.QTimer(self)
        timer.timeout.connect(self.update_figure)
        timer.start(1)
    def compute_initial_figure(self):
        self.minor.plot([0, 1, 2, 3], [1, 2, 0, 4], 'r')
    def update_figure(self):
        arduinoString=arduinoData.readline()
        str_value = arduinoString.strip()
        if str_value:
            measurement = int(str_value)
        plt.pause(0.000001)     
        self.minor.cla()
        self.minor.set_ylim(0,20)
        output.append(measurement)
        numbers=output[-5:]
        mean5=sum(numbers)/5
        numbers10=output[-10:]
        mean10=sum(numbers10)/10

        if (len(output)>50):
            output.pop(0)
        self.minor.plot( output, color="blue", linewidth=0.5, linestyle="-")

        lcd5.display(mean5)
        lcd10.display(mean10)
        self.draw()

class ApplicationWindow(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.setWindowTitle("application main window")
        self.file_menu = QtWidgets.QMenu('&File', self)
        self.file_menu.addAction('&Quit', self.fileQuit,
        QtCore.Qt.CTRL + QtCore.Qt.Key_Q)
        self.menuBar().addMenu(self.file_menu)
        self.help_menu = QtWidgets.QMenu('&Help', self)
        self.menuBar().addSeparator()
        self.menuBar().addMenu(self.help_menu)
        self.help_menu.addAction('&About', self.about)
        self.main_widget = QtWidgets.QWidget(self)
        label = QtWidgets.QLabel()
        label.setText("mean from 5 measurement")
        label.setAlignment(QtCore.Qt.AlignCenter)
        labelm10 = QtWidgets.QLabel()
        labelm10.setText("mean from 10 measurement")
        labelm10.setAlignment(QtCore.Qt.AlignCenter)
        posx=QtWidgets.QLabel()
        posx.setText("Position x in total")
        posx.setAlignment(QtCore.Qt.AlignCenter)
        posy=QtWidgets.QLabel()
        posy.setText("Position y in total")
        posy.setAlignment(QtCore.Qt.AlignCenter)
        global lcd5, lcd10 ,lcdx ,lcdy
        lcd5 =  QLCDNumber(self)
        lcd10 = QLCDNumber(self)
        lcdx=QLCDNumber(self)
        lcdy=QLCDNumber(self)
        l = QtWidgets.QGridLayout(self.main_widget)


        dc = Mousemovement(self.main_widget, width=6, height=5, dpi=100)
        se=MyDynamicMplCanvas(self.main_widget, width=6, height=5, dpi=100)

        l.addWidget(dc,0,1,1,4)
        l.addWidget(posx,1,0)
        l.addWidget(lcdx,1,1)
        l.addWidget(posy,1,2)
        l.addWidget(lcdy,1,3)
        l.addWidget(label,2,0)      
        l.addWidget(lcd5,2,1)
        l.addWidget(labelm10,2,2)
        l.addWidget(lcd10,2,3)
        l.addWidget(se,3,0,1,5)


        self.main_widget.setFocus()
        self.setCentralWidget(self.main_widget)
        self.statusBar().showMessage("Program ", 2000)
    def fileQuit(self):
        self.close()
    def closeEvent(self, ce):
        self.fileQuit()
    def about(self):
        QtWidgets.QMessageBox.about(self, "About",
    """Program to control movement and velocity"""
    )
qApp = QtWidgets.QApplication(sys.argv)
aw = ApplicationWindow()
aw.setWindowTitle("%s" % progname)
aw.show()
sys.exit(qApp.exec_())
#qApp.exec_(

如果您认为这可以帮忙,请使用Arduino代码。

#include <hidboot.h>
#include <usbhub.h>

// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>

class MouseRptParser : public MouseReportParser
{
protected:
 void OnMouseMove (MOUSEINFO *mi);
};

void MouseRptParser::OnMouseMove(MOUSEINFO *mi)
{

    Serial.print(mi ->dX, DEC);
    Serial.print(",");
    Serial.println(mi ->dY, DEC);
    delay(20);
};

USB     Usb;
USBHub     Hub(&Usb);
HIDBoot<USB_HID_PROTOCOL_MOUSE>    HidMouse(&Usb);

MouseRptParser Prs;

void setup()
{
    Serial.begin( 115200 );
#if !defined(__MIPSEL__)
    while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
    //Serial.println("Start");
    if (Usb.Init() == -1)
        Serial.println("OSC did not start.");
    delay( 20 );
     HidMouse.SetReportParser(0, &Prs);
}

void loop()
{  
  Usb.Task();
  delay(20);
}

0 个答案:

没有答案