Periodic time shift problem

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
hdenizgurhan
Posts: 3
Joined: Sat Sep 07, 2019 12:19 pm

Periodic time shift problem

Post by hdenizgurhan » Wed Feb 26, 2020 8:56 am

Hello everyone.

I have a problem when i run the code given below. Here i have a 1000 Hz timer and the periodic work is done in main loop. But when i measure the duration between the successive timer interrupts, there is an additional 2 ms time shift for every 11th measurement. This problem disappears when i comment out sine creation line. What could be the problem here?

Code: Select all

import pyb
import utime
from math import pi, sin

Timer2 = pyb.Timer(2, freq = 1, mode=pyb.Timer.UP) 
sample_flag = 0

def isr_sampling_timer(timer):
	global sample_flag	
	sample_flag = 1
		
def main():
	global sample_flag	  
	time_new = 0
	time_old = 0
	cnt_signal = 0
	time_arr = [0.0 for i in range(100)]
	cnt = 0	 
	signal_input = 0
	Timer2.freq(1000)
	Timer2.callback(isr_sampling_timer)	
	while True:
		if sample_flag == 1:
			if cnt_signal%50 == 0:
				time_new = utime.ticks_us()
				time_arr[cnt] = utime.ticks_diff(time_new, time_old)
				time_old = time_new
				cnt = cnt + 1
				
			signal_input = 3*sin((2*pi*0.2*cnt_signal/1000)+0) + 7
			cnt_signal = cnt_signal + 1			
			if cnt == 100:
				Timer2.callback(None)	
				for i in range(100):
					print(time_arr[i])
			sample_flag = 0		
main()

User avatar
jimmo
Posts: 1465
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Periodic time shift problem

Post by jimmo » Wed Feb 26, 2020 9:20 am

Floating point numbers are stored on the heap, which means periodically you'll have to do a periodic garbage collection, which would possibly explain a short delay while the gc runs automatically.

What board is this running on?

hdenizgurhan
Posts: 3
Joined: Sat Sep 07, 2019 12:19 pm

Re: Periodic time shift problem

Post by hdenizgurhan » Wed Feb 26, 2020 9:56 am

What can i do to overcome this problem? Could you give me any suggestion? I am using Nucleo-L476RG.

Thanks.

User avatar
jimmo
Posts: 1465
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Periodic time shift problem

Post by jimmo » Wed Feb 26, 2020 1:44 pm

The L476 only has 96kiB of RAM, so I guess that's why you see it frequently.

The first step would be to confirm that this is what's happening -- try adding "import gc" then at the start of the main loop add:

Code: Select all

gc.collect()
gc.disable()
and see if you get a MemoryError after ~11 measurements.

The simple but not very helpful answer is "don't use floats". The more complicated answer is to make everything that happens in the main loop work on integer values (i.e. precomputed sine lookup tables, etc).

The other option, is that there is a way to build the firmware such that floats don't go in the heap (MP_OBJ_REPR_C & D). (Set MP_OBJ_REPR in mpconfigboard.h, see the explanation in py/mpconfig.h).

Post Reply