RevPi AIO sampling rate for 8ms in python

Moderator: RevPiModIO

Post Reply
daisuketme
Posts: 5
Joined: 13 Jul 2021, 16:07

RevPi AIO sampling rate for 8ms in python

Post by daisuketme »

Hello all,

I purchased RevPi AIO module and want to collect +- 10V value.

Data sampling should be max (8ms) according to the datasheet.
So I arranged a test code in python:

Code: Select all

import datetime
import revpimodio2

rpi = revpimodio2.RevPiModIO( autorefresh = True )
rpi.cycletime = 8

def test_loop( cycle_tools ):
  time_stamp = datetime.datetime.utcnow().isoformat()
  ai_value   = rpi.io['InputValue_1'].value / 1000
  print( f'{time_stamp} {ai_value}' )
  rpi.exitsignal.wait(0.003)

rpi.cycleloop( test_loop )
The result below showed, however, the cycle time seemed 800ms:
2021-07-14T08:07:58.093727 0.0
2021-07-14T08:08:01.931883 3.472
2021-07-14T08:08:03.526059 10.244
2021-07-14T08:08:04.323045 10.323
2021-07-14T08:08:05.121934 9.322
2021-07-14T08:08:05.918650 4.895
2021-07-14T08:08:06.715427 0.0

Is any setting wrong or code?

(Spec)
Model: RevPi Connect+ 32GB + RevPi AIO (QTY 1)
RevPi AIO version: 1.4
Python version: 3.7.3
revpimodio2 version: 2.5.7

Any opinion is welcome.
Thank you very much in advance.
Daisuke
User avatar
RevPiModIO
KUNBUS
Posts: 325
Joined: 20 Jan 2017, 08:44
Contact:

Re: RevPi AIO sampling rate for 8ms in python

Post by RevPiModIO »

Hi Daisuke!

The cycleloop function will cycle the given function itself, in the default of 50ms, which will override your 8 ms. You can set other cycle time intervals in the function call of cycleloop:

Code: Select all

import datetime
import revpimodio2

rpi = revpimodio2.RevPiModIO(autorefresh=True )
rpi.handlesignalend()

def test_loop( cycle_tools ):
    # do not use wait functions in here
    time_stamp = datetime.datetime.utcnow().isoformat()
    ai_value   = rpi.io['InputValue_1'].value / 1000
    print( f'{time_stamp} {ai_value}' )

rpi.cycleloop( test_loop, cycletime=10 )  # Set cycle time to 10 milliseconds
You have to check the "ADC_DataRate" MEM value of your AIO module in pictory! If you want so use a fast cycle time, you have to set rise the sample frequency of the AIO to fit your needs. If this ist to slow (which is the default) you will get always the same values for a "long" time ;)

Regards
Sven
python3-RevPiModIO - https://revpimodio.org/ || Der RevPi ist das Beste, was passieren konnte!
daisuketme
Posts: 5
Joined: 13 Jul 2021, 16:07

Re: RevPi AIO sampling rate for 8ms in python

Post by daisuketme »

Hi Sven,

Many thanks for your prompt reply.
I forgot to change ADC_DataRate!

After changed it to higher value (160Hz, 320Hz, 640Hz Max) from the default (5Hz), the program you corrected worked well.

Thank you again for your comment :D

Best wishes
Daisuke
jacob42
Posts: 3
Joined: 19 Apr 2022, 20:26

Re: RevPi AIO sampling rate for 8ms in python

Post by jacob42 »

Sven, can you point me to some literature as to how the above code ensures equally spaced data in time? Python is far from real-time, so how does the specified cycle loop not have jitter (i.e. the cycle loop executes every 10 ms +/- some random small amount of time). Is there some underlying code that ensures that I get all samples recorded for a given sample rate. For example, in Pictory, if I select 640 Hz for my data rate (my highest option), and then specify 1/640 = 0.0015625 or 1.5625 ms as my cycle loop, will I get every data point recorded from the AIO channel? If not, is it possible to do so in another way?

I realize after reading a bit more that the maximum sample rate is 125 Hz, so, a better example would be if I set the data rate to 40 Hz and the cycletime to 25 ms, am I guaranteed to get all sampled data points, in order, for as long as the cycleloop handler is called?

Seems to me the best way to do this would be to have some real-time buffer that all the data is getting stored into, then query that buffer from Python every so often in a loop. Because python is not real time, each query would return a slightly different length buffer depending on how much time passed since the last query.
Post Reply