So I was a huge fan of the Pico price-point and the fact that it seemed possible to program a microcontroller with Python.
But I ave found a blocker for me to use the Pico with Python in my project. The issue is that the main loop in my sample program below sometimes takes up to 5ms to run. And more specifically, it seems that the pico will miss interrupts during when that happens.
So can someone explain what it is that causes this code to sometimes take a lot longer to run? My obvious theory is that it is a garbage collector or similar, but no complex garbage collection should be needed here as only some very trivial variables are created in each loop.
The below code does not demonstrate the missed interrupts as these require some special hardware to detect, but if I can understand why the loop sometimes takes a long time, I think that might be enough.
Full disclosure: I have posted the identically same question on the Raspberry Pi Pico Micropythong forum as well.
Code: Select all
import utime
lastTime_ms = utime.ticks_ms()
nextTime_ms = utime.ticks_add( lastTime_ms, 100 )
lastTime_us = utime.ticks_us()
intervalMean = 0
intervalMeanSq = 0
callCount = 0
while ( True ):
# This block of code is the problem. It sometimes takes more than 1ms, up to 5ms
thisTime_us = utime.ticks_us()
delta_us = utime.ticks_diff( thisTime_us, lastTime_us )
intervalMean = (callCount*intervalMean + delta_us)/(callCount+1)
intervalMeanSq = (callCount*intervalMeanSq + delta_us*delta_us)/(callCount+1)
lastTime_us = thisTime_us
callCount = callCount + 1
# End of problem block
thisTime_ms = utime.ticks_ms()
if ( utime.ticks_diff( nextTime_ms, thisTime_ms ) < 0 ):
timeDiff_ms = utime.ticks_diff( thisTime_ms, lastTime_ms)
print ( "Time: ", thisTime_ms , " Time diff: ", timeDiff_ms, end='' )
if ( timeDiff_ms != 100 ):
print ( " Warning!" )
else:
print ()
lastTime_ms = thisTime_ms
nextTime_ms = utime.ticks_add( nextTime_ms, 100 )