fps() vs elapsed_micros()

Hello

I’ve run across a behavior of clock.fps() which I don’t really understand: When I run the test script below I get the clock.fps() value and the microseconds between each snapshot (I don’t need exactly us resolution or accuray, but I use it to get a bit more granularity than ms). In parallel I set Pin0 when taking a snapshot and I’m measuring the period of that signal with a scope.

# Test of time.elapsed_micros() vs clock.fps()

import sensor, image, time, pyb

sensor.reset();
sensor.set_pixformat(sensor.RGB565);
sensor.set_framesize(sensor.QVGA);
sensor.skip_frames(time = 2000);
sensor.set_auto_gain(False);
sensor.set_auto_exposure(False, 2000);


snapshot_pin = pyb.Pin(pyb.Pin.board.P0, pyb.Pin.OUT_PP);
vsync_pin = pyb.Pin(pyb.Pin.board.P1, pyb.Pin.OUT_PP);
sensor.set_vsync_output(vsync_pin);

t_start = pyb.micros();
t = t_start;
frames = 0;
clock = time.clock();


while(True):

    clock.tick();
    snapshot_pin.value(1);
    img = sensor.snapshot();
    snapshot_pin.value(0);

    t_old = t;
    t = pyb.elapsed_micros(t_start);

    frames = frames+1;
    if frames > 500:
        frames = 0;
        clock.reset();


    print("{:.1f} fps\t{:d} us\t{:d} us".format(clock.fps(), t-t_old, sensor.get_exposure_us()));

Now, the value I get using elapsed_micros() matches very well with the period measured with the scope, but the clock.fps() is always too high.
A few examples:

T [us]		fps from us		clock.fps()
41617		24.0			25.5
27666		36.1			38.7
20620		48.5			53.2

The way I understand the source of clock.fps() this does calculate an accumulated average based on total elapsed time and total number of frames taken, but then I would expect it to be - if anything - too low, not too high.

I’m also trying to periodically reset the clock to reset the average from clock.fps (I suspected there might be some bias introduced during startup), but when I do a reset(), the fps resets to zero and starts climbing up very slowly.

Can aynone give me any pointers why clock.fps() does what it does?


Thanks
Regards

Felix

Falthaus,

Here’s the code for clock.fps():

https://github.com/openmv/openmv/blob/master/src/omv/py/py_time.c#L30