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:
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.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
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 |
Thank you very much for your attention.