Running code at 1 ms interval #2697
Replies: 4 comments 7 replies
-
Hi @shodan8192! I've shared this piece of code on Discord before and maybe it is useful in this context too:
It can be improved in various ways - maybe It isn't hard to build a firmware envelope with |
Beta Was this translation helpful? Give feedback.
-
(Kasper responded while I was writing my reply. So some here is repeating what he wrote). Unfortunately that's currently the best approach. Instead of yielding a specific amount of times you could use Since the control loop is probably the most important thing, consider moving that one into a different container and thus process. This way, there isn't any other task that could take longer (when you yield). Alternatively, you could use Also be aware that garbage collections can take more than 1ms. Depending on your code these slower GCs don't happen often, but they can happen. From your description that sounds acceptable, but be aware of it. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your responses ! So I will stay with my current solution, just wanted to be sure that I wasn't missed something better. I'm aware of some deviation from 1 ms in my loop, rarely it's 2ms but most often no more than 200us - it's acceptable and has no side effects. In my spare time maybe I'll build envelope with As an endnote I want to say that this is my first project with Toit, but I found it absolutely fantastic and by far best from what I've try or use for microcontroller programming for years now. Excellent job - thank you ! |
Beta Was this translation helpful? Give feedback.
-
Let's consider following code: import gpio
output-loop:
out := gpio.Pin.out 11
x := 100
while true:
x.repeat:
out.set 0
out.set 1
sleep --ms=10
main:
spawn:: output-loop What would be the simplest possible way to communicate with such process (eg to change |
Beta Was this translation helpful? Give feedback.
-
Hello,
I'm refurbishing 40 years old welding turntable machine which was driven by 160W brushed DC motoreductor, it's controller is completely damaged.
New control module will be build on ESP32-S3, H-bridge, ACS711EX sensor plus opamp comparators for over- and reverse current, photoelectric tachometer, two TM1650 displays, potentiometers (for diameter and linear speed), direction switch and start/stop foot switch.
One part of program is PWM output control loop, which is responsible for ramp control (eg soft start/stop, rotation reversing) that takes into account overcurrent and reversed current (when decelerating) signals from comparators. To keep PWM steps small and to not miss motor current error conditions this code has to be called frequently, so I choose 1 ms interval for it.
On ESP32 Toit
sleep
(and in turnDuration.periodic
) has real resolution 10ms, which I believe is consequence ofCONFIG_FREERTOS_HZ=100
andxTaskNotify
used by underlying timer implementation. To achieve 1 ms interval, I've ended up with :where
X
was chosen by trials. My program works as expected, but maybe there is more "elegant" solution for this ?Beta Was this translation helpful? Give feedback.
All reactions