ticks = 0 # start a timer for the main loop timer = Timer(0) timer.init(period=int(dt * 1000), mode=Timer.PERIODIC, callback=tick) # turn off garbage collection for deterministic loop times # requires gc.collect() to be called periodically, without it # MemoryErrors will occur after some time # the collect is done inside balancer.do_balance() gc.disable() balancer = Balance(pidL, pidR, imu, dt) balancer.set_balance_pi(bp, bi) balancer.set_motor_pi(mp, mi) # use an infinite loop with flag so that the callback is short # in case code inside takes too long # instead of directly calling everything needed in the callback while (True): if loopReady: loopReady = False led(sec % 2) # status LED, toggle every second if (sec != old_sec): old_sec = sec balancer.increment_count() balancer.do_balance()