本文介绍了如何使用ADC在Raspbery Pi中获得可能的最高采样率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个使用Raspberry Pi 3 B的项目中,在该项目中,我通过ADC MPC3008从IR传感器(Sharp GP2Y0A21YK0F)获取数据,并使用PyQtgraph库实时显示.

I am working in a project using Raspberry Pi 3 B where I get data from a IR sensor(Sharp GP2Y0A21YK0F) through a ADC MPC3008 and display it in real-time using PyQtgraph library.

ADC的数据表显示,在5.0V时,采样率为200khz.但是我每秒只能得到大约30个样本.

The datasheet of the ADC says that at 5.0V, the sampling rate is 200khz. However I am only getting about 30 samples per second.

使用Raspberry pi是否有可能达到200khz?

Is it possible to achieve 200khz using Raspberry pi?

如果是,我应该学习或实施什么以获得它?

If yes, what should I study or implement in order to obtain it?

如果没有,我应该怎么做才能获得尽可能高的采样率?如何找出最高采样率?

If not, what should I do to obtain the highest sample rate possible and how can I find out what is the highest sample rate?

这是我的代码:

# -*- coding: utf-8 -*-

import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
from collections import deque
import serial
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np

SPI_PORT   = 0
SPI_DEVICE = 0
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))

win = pg.GraphicsWindow()
win.setWindowTitle('pyqtgraph example: Scrolling Plots')

nsamples=600 #tamanho das matrizes para os dados
tx_aq = 0 #velocidade da aquisição
intervalo_sp = 0.5 #intervalo para secao de poincare

# 1) Simplest approach -- update data in the array such that plot appears to scroll
#    In these examples, the array size is fixed.
p1 = win.addPlot()
p1.setRange(yRange=[0,35])

p2 = win.addPlot()
p2.setRange(yRange=[-100,100])

p3 = win.addPlot()
p3.setRange(yRange=[-100,100])
p3.setRange(xRange=[-0,35])

#p3.plot(np.random.normal(size=100), pen=(200,200,200), symbolBrush=(255,0,0), symbolPen='w')
'''
p3.setDownsampling(mode='peak')
p3.setClipToView(True)
p3.setRange(xRange=[-100, 0])
p3.setLimits(xMax=0)
'''

data1= np.zeros((nsamples,2),float) #ARMAZENAR POSICAO
vec_0=deque()
vec_1=deque()
vec_2=deque()
ptr1 = 0

data2= np.zeros((nsamples,2),float) #ARMAZENAR VELOCIDADE
diff=np.zeros((2,2),float)
diff_v=deque()

data3= np.zeros((nsamples,2),float)
data3_sp=np.zeros((1,2),float)

ptr3=0

curve1 = p1.plot(data1)
curve2 = p2.plot(data2)
curve3 = p3.plot(data3)

#Coeficientes da calibração do IR
c1=-7.246
c2=44.17
c3=-95.88
c4=85.28

tlast=time.clock()
tlast_sp=time.clock()
#print tlast

def getdata():
    global vec_0, vec_1, vec_2, tlast
    timenow=time.clock()

    if timenow-tlast>=tx_aq:
        #name=input("HUGO")

        tlast=timenow

        t0=float(time.clock())
        str_0 =mcp.read_adc(0)
        t1=float(time.clock())
        str_1 =mcp.read_adc(0)
        t2=float(time.clock())
        str_2 =mcp.read_adc(0)

        d0x=(float(str_0))*(3.3/1023)
        d0= c1*d0x**3+c2*d0x**2+c3*d0x+c4
        vec_0=(t0, d0)

        d1x=(float(str_1))*(3.3/1023)
        d1= c1*d1x**3+c2*d1x**2+c3*d1x+c4
        vec_1=(t1, d1)

        d2x=(float(str_2))*(3.3/1023)
        d2= c1*d2x**3+c2*d2x**2+c3*d2x+c4
        vec_2=(t2, d2)

        functions()

def diferenciar():
    global data2


    diff=(data1[-1,1]-data1[-3,1])/(data1[-1,0]-data1[-3,0])

    data2[:-1] = data2[1:]
    data2[-1,1] = diff
    data2[-1,0] = data1[-2,0]


def organizar():
    global data1, data3

    data1[:-1] = data1[1:]
    vec_x1=np.array(vec_1)
    data1[-1]=vec_x1

def EF(): #ESPACO DE FASE
    global data3, ptr3

    data3[:-1] = data3[1:]
    data3[-1,0]=data1[-1,1]
    data3[-1,1]=data2[-1,1]

def SP():
    global timenow_sp, tlast_sp

    timenow_sp=time.clock()

    if timenow_sp-tlast_sp>=intervalo_sp:

        tlast_sp=timenow_sp

        data3_sp[0,0]=data3[-2,0]
        data3_sp[0,1]=data3[-2,1]
        p3.plot(data3_sp, pen=None, symbol='o', symbolPen=None, symbolSize=4, symbolBrush=('r'))
        #print data3_sp

def plotar():
    global ptr1
    curve1.setData(data1)
    ptr1 += 1
    curve2.setData(data2)
    #curve2.setPos(ptr1, 0)

    #p3.plot(data3)

def functions():

    diferenciar()
    organizar()
    EF()
    SP()
    plotar()

def update1():
    global data1, curve1, ptr1

    getdata()


# update all plots
def update():
    update1()

timer = pg.QtCore.QTimer()
timer.timeout.connect(update)
timer.start(50)



## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

我正在尝试找到一种解决方法,但是到目前为止,我失败了.

I am trying to find a way to solve it, but I have failed so far.

你们会帮助我吗?或者至少指出我在哪里可以找到有关此信息的信息?

Would you guys help me with that or at least point me out where I can find information about this?

推荐答案

使用Raspberry Pi之类的通用计算机(尤其是MCP3008)无法实现这种采样率.原因是MCP系列ADC在~2.7Mhz的SPI时钟5V处达到最高值.

This kind of Sampling rate is not achievable with a general-purpose computer like Raspberry Pi, especially with MCP3008. The reason being the MCP series of ADC's tops out at ~2.7Mhz SPI clock at 5V.

要以200KHz速率阅读,您需要一块专用板.

In order to read at 200KHz rate, you would need a dedicated board.

但是,您可以尝试PCM1803A,它可以明显达到采样率,最多为96 kHz

However, you can try PCM1803A which could evidently achieve sampling rate of up to 96 kHz,

此处中对此进行了讨论,

2个通道* 150ksps = 300ksps

2 channels * 150ksps = 300ksps

有开销,假设每个样本大约32位, 9.6mbps的原始数据

with overhead, assuming about 32 bit per sample, you are looking at 9.6mbps of raw data

仅凭Pi和ADC是不可能的.

NO WAY with just a Pi and ADC.

您需要一个外部微控制器/ADC将数据发送到Pi 通过USB或以太网

You need an external microcontroller / adc sending the data to the Pi over USB or Ethernet

此处

  • Raspberry Pi不是为高速数据收集而设计的
  • MCP系列ADC的最高输出电压为5V时的〜2.7Mhz SPI时钟
  • RPi的SPI延迟
  • the Raspberry Pi is NOT designed for high speed data collection
  • the MCP series of ADC's tops out at ~2.7Mhz SPI clock at 5V
  • SPI latency with the RPi

Pi上的SPI接口根本无法准确 以精确的间隔从ADC读取100,000个样本.

The SPI interface on the Pi is simply not capable of accurately reading 100,000 samples from an ADC at precise intervals.

这篇关于如何使用ADC在Raspbery Pi中获得可能的最高采样率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-13 14:26