Speed calculation

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
Philosophix
Posts: 21
Joined: Wed Jan 02, 2019 11:45 am

Speed calculation

Post by Philosophix » Fri Jan 04, 2019 8:00 pm

I was thinking of calculating vehicle speed in a timer callback-function set to be called at 1 hz, but am unsure what I'm allowed to do inside the callback. Can I declare and use local variables, call other functions, ...? Or is there a better way to implement speed calculation when I know how many pulses the inductive sensor gives per distance unit?

jickster
Posts: 574
Joined: Thu Sep 07, 2017 8:57 pm

Re: Speed calculation

Post by jickster » Fri Jan 04, 2019 8:33 pm

There are rules about ISRs:

https://docs.micropython.org/en/latest/ ... rules.html

Unless you have a time-sensitive action to take, you should consider using micropython.schedule

https://docs.micropython.org/en/latest/ ... n.schedule

With this, you can schedule a callback-function to be executed “as soon as possible” and it will be NOT in ISR context so there’s no restrictions like memory allocation.


Sent from my iPhone using Tapatalk Pro

Philosophix
Posts: 21
Joined: Wed Jan 02, 2019 11:45 am

Re: Speed calculation

Post by Philosophix » Fri Jan 04, 2019 8:59 pm

So I schedule a callback-function in the timer callback-function?
What do I do with the arg-argument of the schedule-function if I'm scheduling a function with no args?
Thanx!

jickster
Posts: 574
Joined: Thu Sep 07, 2017 8:57 pm

Re: Speed calculation

Post by jickster » Fri Jan 04, 2019 10:05 pm

Philosophix wrote:So I schedule a callback-function in the timer callback-function?
What do I do with the arg-argument of the schedule-function if I'm scheduling a function with no args?
Thanx!

Pass in 0 like the timer example here

https://docs.micropython.org/en/latest/ ... #isr-rules



Sent from my iPhone using Tapatalk Pro

Post Reply