cycle time of 10 ms exceeded - can not hold cycle time!

Moderator: RevPiModIO

Post Reply
smn
Posts: 4
Joined: 06 May 2022, 15:13

cycle time of 10 ms exceeded - can not hold cycle time!

Post by smn »

Hello All,

i know questions about this warning are asked a lot, and I read a lot about them but have no answer yet to my specific case.

I use Revpi Core 3 with 4 MIO Modules to get 30 1-10 V Analog Inputs.

I want to achive a frequency of 100 Hz => a cycletime of 10 ms in my thread that reads the inputs. (I read that 8 ms can be possible with the Hardware.)

For that I use the cycleloop method as follows. I tried too keep the called function very short to not waste any time and do the further processing of the values in a different thread.


Code: Select all

#...
q = queue.Queue(maxsize)

def measure(revpi=0):
            measures = [datetime.now(timezone.utc)]  # first column is time
            
            for inputName in inputNames:
                measures.append(
                    # The analog inputs are sampled with 15-bit resolution and are available in the process image as a millivolt value (0 to 10000).
                    revpi.io[inputName]
                )
            q.put(measures, block=False)

    	   return

 revpi = revpimodio2.RevPiModIO(autorefresh=True)

# start thread to listen to "Prozessabbild" managed by revpimodio2 library
 revpi.cycleloop(measure, cycletime=int(10), blocking=False)
 
 # do other things... 
I get the cycle time exceeded error even with 30 ms, but not anymore with 50 ms but that is to slow.

When the cycletime is exceeded is the function interrupted? Or if not, is the next call of the function started in a parallel thread? (i guess it is interrupted because it does not work with shorter cycletimes than 50 ms)

As my function "measure" is so short all of the time that leads to the warning/error is needed for building the prozessabbild?

Do i have to change the autorefresh parameter? Or what can i do to speed things up?

Thank you for your help,
smn
User avatar
RevPiModIO
KUNBUS
Posts: 335
Joined: 20 Jan 2017, 08:44
Contact:

Re: cycle time of 10 ms exceeded - can not hold cycle time!

Post by RevPiModIO »

Could it be, that the queue consumer is to slow?

I can not see your value for `maxsize`, but could you try to set that to `0`.

If the consumer of the queue is to slow, the `.put(...)` function will block inside of the cycle function.

I would change the function parameter name `revpi` to `ct` or something else, because `revpi` is your global RevPiModIO object and inside of the cycle function the object is CycleTools.
So change `def measure(revpi=0):` -> `def measure(ct):` and use the `ct` instead of `revpi`.

Just for debugging you can get the actual runtime of the cycle function by `ct.runtime`. So you could use a `print(ct.runtime, q.qsize())` on the end (!) of the cycle function, what will also slow down the function, but you should see the first values will be very little, till the queue runs to maxsize parameter and than the values will be bigger than cycle time and the warnings will raise.

Sven
python3-RevPiModIO - https://revpimodio.org/ || Der RevPi ist das Beste, was passieren konnte!
smn
Posts: 4
Joined: 06 May 2022, 15:13

Re: cycle time of 10 ms exceeded - can not hold cycle time!

Post by smn »

Thank you for your response!

Yes, you are right, it is indeed the consuming thread. While reading the queue and doing further processing the warning is thrown multiple times (300?). Apart of that it seems to work with 10 ms.
With the print(ct.runtime, q.qsize()) i always get around 0.22 (milliseconds?) no matter how big the queue is.

So the CPU of the RevPi cannot hold the cycletime while doing other procesings. The consuming thread is started with https://docs.python.org/3/library/sched.html . Maybe the cycleloop thread can be prioritized to hold the time?
Post Reply