Python Forum

Full Version: I can't get my real-time chart with Python (Help!)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Good morning everybody,

I am working on a Biomedical project, I am working with two Arduino nano and 2 MPU6050 sensors, an Arduino goes with a mpu sensor and communicates by bluetooth to the other Arduino and mpu sensor. I'm reading angles (X1, Y1; X2, Y2) as a result of applying a filter complement to the data obtained from accelerometers and gyroscopes. I need to graph these 4 angles, but in Python code (version 3.8.5) to perform the data reading and graph I do not get any errors, the graphs come out but do not plot any data. I'm working on Windows 10.

Here is an example of how Arduino data is being read in Python:

[-0.48, -0.02, 1.49, -9.29]
<class 'float'>
[-0.48, -0.03, 1.49, -9.31]
<class 'float'>
[-0.49, -0.02, 1.49, -9.32]
<class 'float'>
[-0.49, 0.0, 1.5, -9.31]
<class 'float'>
[2.0, 2.0, 1.5, -9.31]
<class 'float'>
[-0.49, -0.02, 1.5, -9.33]
<class 'float'>
[-0.49, -0.02, 1.51, -9.32]
<class 'float'>
[-0.5, -0.01, 1.5, -9.31]
<class 'float'>

Here's my Python code:

import serial
import time
import collections
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.lines import Line2D
import numpy as np

Samples = 100  #Muestras / Samples
sampleTime = 100  #Tiempo de muestreo / Sample Time
numData = 4

serialPort = 'COM8' # Puerto serial arduino / Arduino serial port
baudRate = 9600  # Baudios

serial.Serial()
def getSerialData(self,Samples,numData,serialConnection, lines):
    while True:
        with serial.Serial('COM8',9600) as ser:
            while True:
                lista =str(ser.readline()).strip("b'")
                lista = lista.strip("\\r\\n'")
                data = lista.split()
                for i in range(numData):
                    data[i].encode("utf-8").decode("unicode-escape").encode("latin1")
                    value = float(data[i])        #array con datos tipo float
                    data[i].append(value) #Guarda lectura en la ultima posicion / #Save reading in the end position
                    lines[i].set_data(range(Samples),data[i]) # Dibujar nueva linea / Drawn new line

#def getSerialData(self,Samples,numData,serialConnection, lines):
    #for i in range(numData):
        #value  = float(serialConnection.readline().strip())  #Leer sensor / Read sensor
        #data[i].append(value) #Guarda lectura en la ultima posicion / #Save reading in the end position
        #lines[i].set_data(range(Samples),data[i]) # Dibujar nueva linea / Drawn new line
        
try:
  serialConnection = serial.Serial(serialPort, baudRate) # Instanciar objeto Serial / Instance Serial Object
except:
  print('Cannot conect to the port')

# Limites de los ejes / Axis limit
xmin = 0
xmax = Samples
ymin = [-100, -100 , -100 , -100]
ymax = [100, 100 , 100 , 100]
lines = []
data = []

for i in range(numData):
    data.append(collections.deque([0] * Samples, maxlen=Samples)) #Lista donde se almacena las variables del sensor
    lines.append(Line2D([], [], color='blue'))                    #Lineas en 2D para graficar los datos
  
fig = plt.figure()# Crea una nueva figura #Create a new figure.
ax1 = fig.add_subplot(2, 2, 1,xlim=(xmin, xmax), ylim=(ymin[0] , ymax[0]))
ax1.title.set_text('Sensor 1 - X')
ax1.set_xlabel("Samples")
ax1.set_ylabel("Degrees")
ax1.add_line(lines[0])

ax2 = fig.add_subplot(2, 2, 2,xlim=(xmin, xmax), ylim=(ymin[1] , ymax[1]))
ax2.title.set_text('Sensor 1 - Y')
ax2.set_xlabel("Samples")
ax2.set_ylabel("Degrees")
ax2.add_line(lines[1])

ax3 = fig.add_subplot(2, 2, 3,xlim=(xmin, xmax), ylim=(ymin[2] , ymax[2]))
ax3.title.set_text('Sensor 2 - X')
ax3.set_xlabel("Samples")
ax3.set_ylabel("Degrees")
ax3.add_line(lines[2])

ax4 = fig.add_subplot(2, 2, 4,xlim=(xmin, xmax), ylim=(ymin[3] , ymax[3]))
ax4.title.set_text('Sensor 2 - Y')
ax4.set_xlabel("Samples")
ax4.set_ylabel("Degrees")
ax4.add_line(lines[3])
    
anim = animation.FuncAnimation(fig,getSerialData, fargs=(Samples,numData,serialConnection,lines), interval=sampleTime)
plt.show()

serialConnection.close() # cerrar puerto serial/ close serial port
The graph comes out but does not plot any data read from Arduino, I do not know what the problem is. I am new to the management of Arduino and Python and have been doing this project from scratch for a few weeks now and learning at the same time. If anyone can help me I will thank you forever as the real-time graph is the last thing I need to do.

Thank you very much for your attention.
Write a Python function to get the real-time data and put it into an Excel sheet. Write VBA event code to process that data into a table. Write an array formula to fetch the latest 30 data points. Create a chart to plot the data from 4. I've written the Python function for you so you just need to get Python and PyXLL installed and you can be ...i think its helpful if anybody more info share with my throug h visitlink providing i will use to see details.share or guide information