How can I make sure that once the callback has been called once, it will not get called again? Preferrably the timer should be completely disabled after the callback has been called.
It would be helpful if there was a oneshot timer for the purpose below. It is a class that makes it possible to use the continous rotation servo to rotate to an angle. Works but needs calibration.
Code: Select all
import pyb
import math
class ServoConverter( object ):
def __init__( self, servo, timer ):
self._servo = pyb.Servo( servo )
self._speed = 100
self._timer = pyb.Timer( timer )
def _initTimer( self, frequency ):
self._timer.init( freq=frequency )
def _timePassed( self, t ):
self._servo.speed( 0 )
def rotate( self, degrees ):
time = abs( degrees * 1.94 )
freq = int( 1000 / time )
self._timer.callback( None )
self._timer.deinit()
self._initTimer( freq )
self._timer.callback( self._timePassed )
if degrees >= 0:
self._servo.speed( self._speed )
else:
self._servo.speed( -self._speed )