Python/Arduino:PySerial、Matplotlib、numpy;Raspbian

2024-06-25 23:01:25 发布

您现在位置:Python中文网/ 问答频道 /正文

**编辑:我收到以下错误消息:* “检索辅助功能总线地址时出错:或.freedesktop.DBus.错误。服务未知:名称org.a11y.Bus不是由任何.service文件提供的”

我用Python编写了PC平台的代码,它运行得很好。我想让这个程序运行在一个连接到监视器的rasberry pi上。在

我为上面需要的函数安装了Raspbian和所有的库。在

代码如下:

############################################################
# Visualizing data from the Arduino serial monotor         #
# and creating a graph in a nice format. Later work will   #
# look into outputting this to a Android device            #
############################################################
############################################################

############################################################
############################################################

#Becuase we can always use more time
import time

#Inport the fancy plotting library 
import matplotlib.pyplot as plt

#Inport the serial library
import serial

#We want to do array math so we need Numpy
import numpy

#We also want to draw things so we need drawnow and
#everything that comes with it
from drawnow import *


############################################################
############################################################

#Creating an array to hold data for plotting. See several
#lines below for more information

tempArray = []
pressureArray = []
altitudeArray = []

#The object that is created to interact with the Serial Port
arduinoSerialInput = serial.Serial('/dev/ttyACM0', 230400)

time.sleep(3)

#Here we set the definition oto look for interactive mode
#and plot live data
plt.ion()

############################################################
############################################################

clippingCounter = 0

#Here we define a function to make a plot, or figure of
#the data aquired

def plotTempData():

    plt.ylim(150,320)
    plt.title('Plots from data of the BMP Sensor')
    plt.grid(True)
    plt.ylabel('Tempature')
    plt.subplot(2, 2, 1)
    plt.plot(tempArray, 'ro-', label='Degrees K', linewidth=2)
    plt.legend(loc='upper left')

    #plt2 = plt.twinx()
    #plt2.plot(pressureArray, 'bx-')

def plotPresData():

    #plt.ylim(260,320)
    plt.title('Plots from data of the BMP Sensor')
    plt.grid(True)
    plt.ylabel('Pressure')
    plt.subplot(2, 2, 2)
    plt.plot(pressureArray, 'bo-', label='Pascals', linewidth=2)
    plt.legend(loc='upper left')
    plt.plot(pressureArray, 'bo-')
    plt.show()

############################################################
############################################################

while (1==1):
    #First things first, lets wait for data prior to reading
    if (arduinoSerialInput.inWaiting()>0):
        myArduinoData = arduinoSerialInput.readline()
#        print myArduinoData -Line commented out, see below

#The arduino should be set up to proovide one string of data
#from here we will seperate that one string into two or more
#to make it easier to plot

        #This will create an array seperated by the comma
        #we specified in the Arduino IDE aasign it to a
        #variable of our choice and convert the string
        #back into a number using the float command
        dataArray = myArduinoData.split(',')
        temp =  float(dataArray[0])
        pressure = float(dataArray[1])

        #Used to test the printing of values to output
        #print temp, " , ", pressure, " , ", altitude

#We need to create an array to to hold the values in question
#Looking at the top, you will see the empty array we need to
#create that will hold the values we need to plot
        tempArray.append(temp)
        pressureArray.append(pressure)

        plt.figure(1)
        drawnow(plotTempData)
        plt.pause(.000001)

        #plt.figure(2)
        drawnow(plotPresData)
        plt.pause(.000001)



#We want to clip some data off since the graph keeps
#expanding and too many points are building up.
        clippingCounter = clippingCounter + 1
        if(clippingCounter>50):
          tempArray.pop(0)
          pressureArray.pop(0)



############################################################
############################################################

Tags: andofthetofromimportdataplot
1条回答
网友
1楼 · 发布于 2024-06-25 23:01:25

你提到的错误是我认为“at-spi2-core”模块有问题。这个问题似乎已经在ubuntu2.9.90-0ubuntu2上修复了,你有没有尝试升级'at-spi2-core'模块?或者强制启用它可能会解决您的问题。在

有关确保启用SPI的信息如下: http://www.raspberrypi-spy.co.uk/2014/08/enabling-the-spi-interface-on-the-raspberry-pi/

会在评论中添加这个,因为我不是100%对此,但我没有代表。在

相关问题 更多 >