2

I am trying to run balance() function on one thread and use Timer on a separate one. Both methods when run separately work fine, but when I try to use them both at once the issr() function which is called by a Timer just stops working after few calls. There is no error on REPL from Raspberry. The motor.do_step() method just changes the Pin values from 0 to 1 when called. Any ideas what is the problem?

def issr(timer):
    global motor1, motor2, i
    motor1.do_step()
    motor2.do_step()


def balance():
    while True:
        global motor1, motor2
        motor1.set_speed(1000)
        motor2.set_speed(1000)


_thread.start_new_thread(balance, ())

tim = Timer()
tim.init(freq=3000, mode=Timer.PERIODIC, callback=issr)
Jonas
  • 121,568
  • 97
  • 310
  • 388
AntCwo
  • 21
  • 3
  • 1
    you should move your `global` statement away from `while` loop, and have some sleep in balance function – Lixas Nov 23 '21 at 11:30
  • I encountered a similar problem. I just posted a question on the MicroPython forum [Timer gets stuck when thread is running - RP2040](https://forum.micropython.org/viewtopic.php?f=21&t=12639) with a simple repro. So far, no solution. – Eliahu Aaron Jul 05 '22 at 13:06
  • @EliahuAaron, Please use the [MicroPython Discussions](https://github.com/micropython/micropython/discussions). The forums have been archived. – Jos Verlinde Feb 08 '23 at 14:11
  • 1
    @JosVerlinde: I posted a MicroPython discussion: [Timer callback gets stuck when thread is running - RP2040](https://github.com/micropython/micropython/discussions/10700) – Eliahu Aaron Feb 09 '23 at 14:18

1 Answers1

0

Threads in RP2040 and micropython have shown several errors, as well as some functions with timers.

I recommend using uasyncio, or in the case of timers, generating a deinit(), performing a small task, and init() again.

I already had problems trying to save files, if the timer started in the middle of the task, it crashed both in the file and in the whole system, and to remedy issues such as system crash I used WDT.

double-beep
  • 5,031
  • 17
  • 33
  • 41
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 04 '23 at 17:37