Python Forum
MPU6050 +Arduino+Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MPU6050 +Arduino+Python
#11
ya wierd. This is what is says:
Quote:C:\Users\Meena\AppData\Local\Programs\Python\Python36\Scripts>pip install pyserial
Requirement already satisfied: pyserial in c:\users\meena\appdata\local\programs\python\python36\lib\site-packages

I have a query
This code below that i posted earlier:
import serial
import matplotlib.pyplot as ply
import numpy   #used for mathematical operations
import pylab as py
from mpl_toolkits.mplot3d import Axes3D
from drawnow import *
 
ACCX=[]
ACCY=[]
ACCZ=[]
GYRX=[]
GYRY=[]
GYRZ=[]
 
arduinodata=serial.Serial('COM3',9600) #port name and baud rate
 
fig = ply.figure()
ax = Axes3D(fig)
ply.ion()
 
 
def makeplotting():
 
        ply.subplot(611)
        ply.ylabel('ACCX (°/sec)')
        ply.xlabel('Time')
        ply.plot(ACCX)
         
        ply.subplot(612)
        ply.ylabel('ACCY (°/sec)')
        ply.xlabel('Time')
        ply.plot(ACCY)
         
        ply.subplot(613)
        ply.ylabel('ACCZ (°/sec)')
        ply.xlabel('Time')
        ply.plot(ACCZ)
 
while True:
        while(arduinodata.inWaiting()==0):
                pass
        arduinostring=arduinodata.readline()       
        arduinostring=str(arduinostring,encoding="utf-8")
        dataArray=arduinostring.split(',')
        print (dataArray)
        if (len(dataArray)==3):
                if (type(float(dataArray[0]))is float):
                        accxtemp=float(dataArray[0])
                        ACCX.append(accxtemp)
                        print (accxtemp)
                         
                else:
                        print ('Data is not in float and hence marked to zero')
                        dataArray[0]=0.0
                        accxtemp=float(dataArray[0])
                        ACCX.append(accxtemp)
                        print (accxtemp)
 
                if (type(float(dataArray[1]))is float):
                        accytemp=float(dataArray[1])
                        ACCY.append(accytemp)
                        print (accytemp)
                         
                else:
                        print ('Data is not in float and hence marked to zero')
                        dataArray[1]=0.0
                        accytemp=float(dataArray[1])
                        ACCY.append(accytemp)
                        print (accytemp)
 
                if (type(float(dataArray[2]))is float):
                        accztemp=float(dataArray[2])
                        ACCZ.append(accztemp)
                        print (accztemp)
                         
                else:
                        print ('Data is not in float and hence marked to zero')
                        dataArray[2]=0.0
                        accztemp=float(dataArray[2])
                        ACCZ.append(accztemp)
                        print (accztemp)        
                         
                drawnow(makeplotting)        
        else:
                print ('there is some data missing')
why do you think this is not working?
Reply
#12
type(float(dataArray[1]))is float will raise the same error

And even if it worked is just ugly. Plus the code is repetitive and there are redundant thingls like
dataArray[0]=0.0 # here it is already float next line is not needed
accxtemp=float(dataArray[0])
and

ACCX.append(accxtemp)
print (accxtemp)
i.e. there is no need to repeat this in both if and else part, leave it for after the if/else block
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#13
and to update package you need to use

pip install --upgrade pyserial
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#14
Ya. Tried still says the same thing

Quote:C:\Users\Meena\AppData\Local\Programs\Python\Python36\Scripts>pip install --upgrade pyserial
Requirement already up-to-date: pyserial in c:\users\meena\appdata\local\programs\python\python36\lib\site-packages

Okay. I should try minimization of code.
What do you suggest where should I start learning being a beginner?
Reply
#15
(Mar-28-2018, 12:28 PM)MeenakshiChowdhary Wrote: What do you suggest where should I start learning being a beginner?
well, this depends on your overall experience and preferences - i.e. do you have programming experience with other languages, how do you prefer to learn, etc.
Start from basics and expand at pace that is OK for you. There are plenty of resources online: books, videos, tutorials, etc. - check our list of free resources and also tutorials section of the forum.
Having a particular project to work on is good as it gives you hands-on experience and you immediately get satisfaction by seeing the results.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#16
Thanks, that helps.
Will get back to this forum in case of queries.
Reply
#17
Hi,
I tried to write the data to the text file.
I am now trying to read line by line and covert in to float so that i can plot in 3D.
Code is below:
import serial,io
import matplotlib.pyplot as ply
import numpy  as np #used for mathematical operations
import pylab as py
from mpl_toolkits.mplot3d import Axes3D
from drawnow import *
from datetime import datetime
import csv

ACCX=[]
ACCY=[]
ACCZ=[]
accx_list=[]
accy_list=[]
accz_list=[]

arduinodata=serial.Serial('COM3',9600) #port name and baud rate

fig = ply.figure()
ax = Axes3D(fig)
ply.ion()

outputfile='C:/Users/Meena/Desktop/SensorData.txt'

def makeplotting():

        ply.subplot(311)
        ply.ylabel('ACCX (°/sec)')
        ply.xlabel('Time')
        ply.plot(ACCX)
        
        ply.subplot(312)
        ply.ylabel('ACCY (°/sec)')
        ply.xlabel('Time')
        ply.plot(ACCY)
        
        ply.subplot(313)
        ply.ylabel('ACCZ (°/sec)')
        ply.xlabel('Time')
        ply.plot(ACCZ)
        
def convert_float(value):
    try:
        return float(value)
    except ValueError:
        print ('Data {} is not in float and hence marked to zero'.format(value))
        return 0.0
new_list=[]

with open(outputfile) as f:
        string=f.readlines()
        new_list=string[0].split(',')

        accx_list.append(float(new_list[0]))
        accy_list.append(float(new_list[1]))
        accz_list.append(float(new_list[2]))

        print(accx_list)
        print(accy_list)
        print(accz_list)
        ax.scatter(accx_list, accy_list, accy_list)
        ply.show()                


                
ax.scatter(accx_list, accy_list, accy_list)
pyplot.show()

output_file=open(outputfile,"w+")

while True:
        while(arduinodata.inWaiting()==0):
                pass
        arduinostring=arduinodata.readline()
        arduinostring=str(arduinostring,encoding="utf-8")
        output_file.write(arduinostring)      
        dataArray=arduinostring.split(',')
        print (dataArray)
        if (len(dataArray)==3):
                dataArray = map(convert_float, dataArray)
                x, y, z = dataArray
                ACCX.append(x)
                ACCY.append(y)
                ACCZ.append(z)
                drawnow(makeplotting)
                        
        else:
                print ('There is some data format problem: {}'.format(dataArray))
I got the plot for 1st line in the file. Trying to iterate it writing an for loop for new list but does not work

This is what is get as Error
Reply


Forum Jump:

User Panel Messages

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