Replies: 2 comments 11 replies
-
Hello @jaguilar, I'm interested in this. Partly because I think we need a flexible and good PID solution. One that is able to interface quadrature encoders as well as pwm controls of ordinary and stepper motors in a clear fashion. Why do you want to have the PID controller loop at 1 kHz? Usually the motor-controlling PWM is in the kHz range. For the code: At a first glance I saw
This probably does not speed up things, because the call of the viper function takes a few microseconds. It may even be slower than having this computation in MP. But you may check that with ordinary profiling: from time import ticks_us, ticks_diff
@micropython.viper
def log2_ratio_mul(n: int, num: int, shr: int) -> int:
return (n * num) >> shr
a = 0
b,c,d = 2, 5, 8
def use_viper():
a = log2_ratio_mul(b, c, d)
a = log2_ratio_mul(b, c, d)
a = log2_ratio_mul(b, c, d)
a = log2_ratio_mul(b, c, d)
a = log2_ratio_mul(b, c, d)
a = log2_ratio_mul(b, c, d)
a = log2_ratio_mul(b, c, d)
a = log2_ratio_mul(b, c, d)
a = log2_ratio_mul(b, c, d)
a = log2_ratio_mul(b, c, d)
def use_mp():
a = (b * c) >> d
a = (b * c) >> d
a = (b * c) >> d
a = (b * c) >> d
a = (b * c) >> d
a = (b * c) >> d
a = (b * c) >> d
a = (b * c) >> d
a = (b * c) >> d
a = (b * c) >> d
start = ticks_us()
use_viper()
stop = ticks_us()
print('use_viper() took ', ticks_diff(stop, start)/10, 'us')
start = ticks_us()
use_mp()
stop = ticks_us()
print('use_mp() took ', ticks_diff(stop, start)/10, 'us') results in:
here, so this use of viper slowed things down. Viper shines if working on arrays of data. So if the processing is done in several data sequentially the function call overhead does not count this much. |
Beta Was this translation helpful? Give feedback.
-
Nahhh.
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
# vim:fileencoding=UTF-8:ts=4
from time import ticks_us, ticks_diff
@micropython.viper
def a(n):
for _ in range(int(n)):
pass
@micropython.viper
def b(n: int):
while n > 0:
n -= 1
t0 = ticks_us()
a(1_000_000)
t1 = ticks_us()
print(f'actual time:{ticks_diff(t1, t0):6d} µs')
t0 = ticks_us()
b(1_000_000)
t1 = ticks_us()
print(f'actual time:{ticks_diff(t1, t0):6d} µs')
>>> actual time:224051 µs
>>> actual time:104035 µs |
Beta Was this translation helpful? Give feedback.
-
Hi, I'm using Micropython on the Lego EV3 brick (Arm9 CPU of some variety). I'm trying to write a PID controller that ideally would loop at around a 1000hz frequency while leaving some processor time for other things. (It's actually three different PID controllers each of which would ideally loop at 333hz.)
I've done those things that are obvious to me, like switching from floating point multiplies/divides to integer scaling. I've scattered
@micropython.native
and@micropython.viper
annotations about wherever I can without an error resulting.At this point, the code is just about fast enough, but it would be more comfortable if it were a little faster. I'm wondering where to direct my efforts. Which lines of code are taking up the most time? Normally at work I would fire up a profiler and I'd be able to get line or even instruction level cpu time counts. But I can't seem to find any similar tooling for Micropython.
So my first question is: is there an instruction-level profiler for Micropython?
If function/method level timing a la
@timed_function
is the only mechanism that is easily available, I have some follow-up questions:@timedfunction
.)native
andviper
, does it matter for performance purposes whether the callee function is also generated with the same code generator? For example, if a viper function calls another viper function, does that introduce a lower amount of overhead compared to a viper function calling a native or unannotated function? Does Viper know how to inline another Viper function?I would also appreciate any other tips or pointers to discussions about optimizing CPU-heavy code. Bear in mind I am using Micropython 1.11 because that is the latest version that pybricks is shipping on ev3dev. Also, for completeness of information, here is the current state of my code.
Beta Was this translation helpful? Give feedback.
All reactions