Hello,

I am currently trying to plot manipulated joystick serial data from an 
Arduino. I am running into the problem that I can create the graph canvas, 
but there is no real-time plotting occurring.

-- 
You received this message because you are subscribed to the Google Groups 
"pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pyqtgraph/d27c4812-c7d1-43ad-8aba-9000c8d3e429n%40googlegroups.com.

Attachment: aiming.ino
Description: Binary data

import sys
import numpy as np
import pyqtgraph as pg
import pyqtgraph.exporters
from PyQt6.QtGui import *
from PyQt6.QtWidgets import *
from PyQt6.QtCore import *
import serial
import math

arduinoData = serial.Serial("COM3", 115200)  # read the text line from serial port
app = QApplication(sys.argv)


class joystickPlot(QMainWindow):
    def __init__(self, p=0.0):
        super(joystickPlot, self).__init__()

        # Input
        self.p = p / 145

        # Given
        self.m = 0.00435
        self.mu = 0.5
        self.g = 9.81
        self.s = 0.125

        # thetas
        self.theta_x = 0
        self.theta_y = 0
        # distance
        self.x_z = 0

        # coord
        self.x_coord = 0
        self.y_coord = 0
        # Set up GUI configuration
        self.mainbox = QWidget()
        self.setCentralWidget(self.mainbox)
        self.mainbox.setLayout(QVBoxLayout())

        self.canvas = pg.GraphicsLayoutWidget()  # create GrpahicsLayoutWidget obejct
        self.mainbox.layout().addWidget(self.canvas)

        self.label = QLabel()  # placeholder Qlabel object to display framerate
        self.mainbox.layout().addWidget(self.label)

        #  Set up plot
        self.analogPlot = self.canvas.addPlot(title='Real-time joystick analog signal plot')
        self.analogPlot.setYRange(0, 1000)  # set axis range
        self.analogPlot.setXRange(-1000, 1000)
        self.analogPlot.showGrid(x=True, y=True, alpha=0.5)

        x_axis = self.analogPlot.getAxis('bottom')
        y_axis = self.analogPlot.getAxis('left')

        ticks_x = [[(-1000, '-1000'), (0, '0'), (1000, '1000')], [(-800, '-800'), (-600, '-600'), (-400, '-400'),
                                                                  (-200, '-200'), (200, '200'), (400, '400'),
                                                                  (600, '600'),
                                                                  (800, '800')]]
        ticks_y = [[(0, '0'), (1000, '1000')], [(200, '200'), (400, '400'), (600, '600'), (800, '800')]]
        x_axis.setTicks(ticks_x)  # set ticks manually (first major-ticks than minor-ticks)
        y_axis.setTicks(ticks_y)

        self.drawplot = self.analogPlot.plot(pen='y')  # yellow line plot

        # initialize sensor data variables
        self.numPoints = 20  # number of points that should be plottet at the same time stamp
        self.x = np.array([], dtype=float)  # create empty numpy array to store xAxis data
        self.y = np.array([], dtype=float)  # create empty numpy array to store yAxis data

        # start updating
        self._update()

    def get_dist(self):
        if 0 <= self.theta_x <= 90:
            self.x_z = 4 * self.s * (((89.759 * self.p) / (self.m * self.g)) - (
                    self.mu * self.g * math.cos(self.theta_x * (math.pi / 180)))) * math.sin(
                2 * self.theta_x * (math.pi / 180))

    def get_rect_coord(self):
        self.x_coord = self.x_z * math.cos(self.theta_y * math.pi / 180)
        self.y_coord = self.x_z * math.sin(self.theta_y * math.pi / 180)

    def _update(self):

        while (arduinoData.inWaiting() == 0):  # wait until there is data available
            pass  # do nothing
        arduinoString = arduinoData.readline()  # read the text line from serial port
        if sys.version_info >= (3, 0):
            arduinoString = arduinoString.decode('utf-8', 'backslashreplace')
        dataArray = arduinoString.split('x')  # spcd lit it into an array

        self.theta_x = float(dataArray[0])
        self.theta_y = float(dataArray[1])

        self.get_dist()
        self.get_rect_coord()

        self.x = np.append(self.x, self.x_coord)  # append new data point to the array
        if self.x.size >= self.numPoints:  # make sure that the size of the array includes the most recent data points
            self.x = np.append(self.x[1:self.numPoints], self.x_coord)
        else:
            self.x = np.append(self.x, self.x_coord)

        self.y = np.append(self.y, self.y_coord)  # append new data point to the array
        if self.y.size >= self.numPoints:  # make sure that the size of the array includes the most recent data points
            self.y = np.append(self.y[1:self.numPoints], self.y_coord)
        else:
            self.y = np.append(self.y, self.y_coord)

        self.drawplot.setData(self.x, self.y)  # draw current data set


def main():
    pressure = float(input('Enter the initial pressure in PSI: '))
    plot = joystickPlot(pressure)
    plot.show()
    app.exec()


if __name__ == "__main__":
    main()

Reply via email to