Python Forum
I can't get my real-time chart with Python (Help!)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I can't get my real-time chart with Python (Help!)
#1
Question 
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.
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Non-blocking real-time plotting slow_rider 5 3,457 Jan-07-2023, 09:47 PM
Last Post: woooee
  How to change UTC time to local time in Python DataFrame? SamKnight 2 1,528 Jul-28-2022, 08:23 AM
Last Post: Pedroski55
  Real time database satyanarayana 3 1,623 Feb-16-2022, 01:37 PM
Last Post: buran
  Real time data satyanarayana 3 20,357 Feb-16-2022, 07:46 AM
Last Post: satyanarayana
  Real time Detection and Display Gilush 0 1,764 Feb-05-2022, 08:28 PM
Last Post: Gilush
  Real-Time output of server script on a client script. throwaway34 2 2,011 Oct-03-2021, 09:37 AM
Last Post: ibreeden
  Real Time Audio Processing with Python Sound-Device not working Slartybartfast 2 3,885 Mar-14-2021, 07:20 PM
Last Post: Slartybartfast
  Python Matplotlib: Create chart for every 4 columns in Excel file JaneTan 2 2,733 Feb-28-2021, 05:02 AM
Last Post: JaneTan
  how to find difference between fake driving license or real using python? pjaymn 2 2,500 Dec-20-2020, 08:41 PM
Last Post: jefsummers
  Real Time signal processing tagalog 2 2,603 Dec-12-2020, 05:23 AM
Last Post: tagalog

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020