本文整理汇总了Python中pyb.Timer类的典型用法代码示例。如果您正苦于以下问题:Python Timer类的具体用法?Python Timer怎么用?Python Timer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Timer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: start
def start(self, speed, direction):
PWM_py_pin = Pin(self.PWMpin)
DIR_py_pin = Pin(self.DIRpin, Pin.OUT_PP)
tim = Timer(self.timer_id, freq=1000)
ch = tim.channel(self.channel_id, Timer.PWM, pin=PWM_py_pin)
if direction in ('cw', 'CW', 'clockwise'):
DIR_py_pin.high()
elif direction in ('ccw', 'CCW', 'counterclockwise'):
DIR_py_pin.low()
else:
raise ValueError('Please enter CW or CCW')
if 0 <= speed <= 100:
ch.pulse_width_percent(speed)
else:
raise ValueError("Please enter a speed between 0 and 100")
self.isRunning = True
self.currentDirection = direction
self.currentSpeed = speed
示例2: motor_control
def motor_control():
# define various I/O pins for ADC
adc_1 = ADC(Pin('X19'))
adc_2 = ADC(Pin('X20'))
# set up motor with PWM and timer control
A1 = Pin('Y9',Pin.OUT_PP)
A2 = Pin('Y10',Pin.OUT_PP)
pwm_out = Pin('X1')
tim = Timer(2, freq = 1000)
motor = tim.channel(1, Timer.PWM, pin = pwm_out)
A1.high() # motor in brake position
A2.high()
# Calibrate the neutral position for joysticks
MID = adc_1.read() # read the ADC 1 value now to calibrate
DEADZONE = 10 # middle position when not moving
# Use joystick to control forward/backward and speed
while True: # loop forever until CTRL-C
speed = int(100*(adc_1.read()-MID)/MID)
if (speed >= DEADZONE): # forward
A1.high()
A2.low()
motor.pulse_width_percent(speed)
elif (speed <= -DEADZONE):
A1.low() # backward
A2.high()
motor.pulse_width_percent(-speed)
else:
A1.low() # stop
A2.low()
示例3: change_speed
def change_speed(self,newspeed):
PWM_py_pin = Pin(self.PWMpin)
DIR_py_pin = Pin(self.DIRpin, Pin.OUT_PP)
tim = Timer(self.timer_id, freq=1000)
ch = tim.channel(self.channel_id, Timer.PWM, pin=PWM_py_pin)
ch.pulse_width_percent(newspeed)
self.currentSpeed = newspeed
self.isRunning = True
示例4: MOTORS
class MOTORS():
def __init__(self):
#设定Pin
self._rForward = Pin('B8')
self._rBackward = Pin('B9')
self._lForward = Pin('B14')
self._lBackward = Pin('B15')
#set right motor pwm
self._rTim = Timer(4, freq=3000)
self._rf_ch = self._rTim.channel(3, Timer.PWM, pin=self._rForward)
self._rb_ch = self._rTim.channel(4, Timer.PWM, pin=self._rBackward)
#set left motor pwm
self._lTim = Timer(12, freq=3000)
self._lf_ch = self._lTim.channel(1, Timer.PWM, pin=self._lForward)
self._lb_ch = self._lTim.channel(2, Timer.PWM, pin=self._lBackward)
#设定右边电机的转速
#-1 < ratio < 1
def set_ratio_r(self, ratio):
#check ratio
if(ratio > 1.0):
ratio = 1.0
elif(ratio < -1.0):
ratio = -1.0
if(ratio > 0):
self._rb_ch.pulse_width_percent(0)
self._rf_ch.pulse_width_percent(ratio*100)
elif(ratio < 0):
self._rf_ch.pulse_width_percent(0)
self._rb_ch.pulse_width_percent(-ratio*100)
else:
self._rf_ch.pulse_width_percent(0)
self._rb_ch.pulse_width_percent(0)
#设定左边电机的转速
#-1 < ratio < 1
def set_ratio_l(self, ratio):
#check ratio
if(ratio > 1.0):
ratio = 1.0
elif(ratio < -1.0):
ratio = -1.0
if(ratio > 0):
self._lb_ch.pulse_width_percent(0)
self._lf_ch.pulse_width_percent(ratio*100)
elif(ratio < 0):
self._lf_ch.pulse_width_percent(0)
self._lb_ch.pulse_width_percent(-ratio*100)
else:
self._lf_ch.pulse_width_percent(0)
self._lb_ch.pulse_width_percent(0)
def all_stop(self):
self.set_ratio_l(0)
self.set_ratio_r(0)
示例5: drive_motor
def drive_motor():
A1 = Pin('Y9',Pin.OUT_PP)
A2 = Pin('Y10',Pin.OUT_PP)
A1.high()
A2.low()
motor = Pin('X1')
tim = Timer(2, freq = 1000)
ch = tim.channel(1, Timer.PWM, pin = motor)
while True:
ch.pulse_width_percent(50)
示例6: TimeThread
class TimeThread(object):
"""
This class allows to make a call of several functions with a specified time
intervals. For synchronization a hardware timer is used. The class contains
main loop to check flags of queue.
"""
def __init__(self, timerNum, showExceptions = False):
self.showExceptions = showExceptions
self.timerQueue = []
self.timer = Timer(timerNum, freq=1000)
self.timer.callback(self._timer_handler)
"""
The method adds a pointer of function and delay time to the event queue.
Time interval should be set in milliseconds.
When the method adds to the queue it verifies uniqueness for the pointer.
If such a pointer already added to an event queue then it is not added again.
When the queue comes to this pointer then entry is removed from the queue.
But the pointer function is run.
"""
def set_time_out(self, delay, function):
b = True
for r in self.timerQueue:
if r[1] == function:
b = False
if b:
self.timerQueue += [[delay, function]]
# The handler of hardware timer
def _timer_handler(self, timer):
q = self.timerQueue
for i in range(len(q)):
if q[i][0] > 0:
q[i][0] -= 1
"""
The method runs an infinite loop in wich the queue is processed.
This method should be accessed after pre-filling queue.
Further work is performed within the specified (by the method
set_time_out()) functions.
"""
def run(self):
q = self.timerQueue
while True:
for i in range(len(q) - 1, -1, -1):
if q[i][0] == 0:
f = q[i][1]
del(q[i])
if self.showExceptions:
f()
else:
try:
f()
except Exception as e:
print(e)
示例7: set_speed
def set_speed(self, newSpeed):
""" Method to change the speed of the motor, direciton is unchanged
Arugments
newSpeed : the desired new speed 0-100 (as percentage of max)
"""
PWMpin = Pin(self.PWMpin)
tim = Timer(self.timer_id, freq=1000)
ch = tim.channel(self.channel_id, Timer.PWM, pin=PWMpin)
ch.pulse_width_percent(newSpeed)
# PWM.set_duty_cycle(self.PWMpin, newSpeed)
self.currentSpeed = newSpeed
示例8: rate
class Motor:
_index = None
_pin = None
_timer = None
_channel = None
_rate = None
_rate_min = None
_rate_max = None
# Each range represents 50% of the complete range
_step = None
_debug = False
# ########################################################################
# ### Properties
# ########################################################################
@property
def rate(self):
return self._rate
@rate.setter
def rate(self, value):
self._rate = max(min(value, 100), 0)
pulse_value = int(self._rate_min + self._rate * self._step)
if not self._debug:
self._channel.pulse_width(pulse_value)
else:
print("<<ESC>> %s: %.3d" % (self._pin, self._rate))
# ########################################################################
# ### Constructors and destructors
# ########################################################################
def __init__(self, esc_index, rate_min=1000, rate_max=2000, debug=False):
if esc_index >= 0 & esc_index <= 3:
self._debug = debug
self._rate_min = rate_min
self._rate_max = rate_max
self._step = (rate_max - rate_min) / 100
self._index = esc_index
self._pin = ESC_PIN[esc_index]
self._timer = Timer(ESC_TIMER[esc_index], prescaler=83, period=19999)
self._channel = self._timer.channel(ESC_CHANNEL[esc_index], Timer.PWM, pin=Pin(self._pin))
self.rate = 0
else:
raise Exception("Invalid ESC index")
def __del__(self):
self.rate(self._rate_min)
self._timer.deinit()
示例9: stop
def stop(self):
PWM_py_pin = Pin(self.PWMpin)
DIR_py_pin = Pin(self.DIRpin, Pin.OUT_PP)
tim = Timer(self.timer_id, freq=1000)
ch = tim.channel(self.channel_id, Timer.PWM, pin=PWM_py_pin)
for i in range(self.currentSpeed):
ch.pulse_width_percent(self.currentSpeed-i)
time.sleep(0.01)
ch.pulse_width_percent(0)
self.isRunning = False
self.currentDirection = None
self.currentSpeed = 0.0
示例10: hard_stop
def hard_stop(self):
""" Method to hard stop an individual motor"""
self.PWMpin = Pin('X4')
tim = Timer(2, freq=1000)
ch = tim.channel(4, Timer.PWM, pin=self.PWMpin)
ch.pulse_width_percent(0)
# PWM.set_duty_cycle(self.PWMpin, 0.0)
# set the status attributes
self.isRunning = True
self.currentDirection = None
self.currentSpeed = 0
示例11: soft_stop
def soft_stop(self):
""" Method to soft stop (coast to stop) an individual motor"""
PWMpin = Pin(self.PWMpin)
tim = Timer(self.timer_id, freq=1000)
ch = tim.channel(self.channel_id, Timer.PWM, pin=PWMpin)
for i in range(self.currentSpeed):
ch.pulse_width_percent(self.currentSpeed-i)
time.sleep(0.01)
ch.pulse_width_percent(0)
# set the status attributes
self.isRunning = False
self.currentDirection = None
self.currentSpeed = 0.0
示例12: __init__
def __init__(self):
#设定Pin
self._rForward = Pin('B8')
self._rBackward = Pin('B9')
self._lForward = Pin('B14')
self._lBackward = Pin('B15')
#set right motor pwm
self._rTim = Timer(4, freq=3000)
self._rf_ch = self._rTim.channel(3, Timer.PWM, pin=self._rForward)
self._rb_ch = self._rTim.channel(4, Timer.PWM, pin=self._rBackward)
#set left motor pwm
self._lTim = Timer(12, freq=3000)
self._lf_ch = self._lTim.channel(1, Timer.PWM, pin=self._lForward)
self._lb_ch = self._lTim.channel(2, Timer.PWM, pin=self._lBackward)
示例13: __init__
class ESC:
freq_min = 950
freq_max = 1950
def __init__(self, index):
self.timer = Timer(esc_pins_timers[index], prescaler=83, period=19999)
self.channel = self.timer.channel(esc_pins_channels[index],
Timer.PWM,
pin=Pin(esc_pins[index]))
self.trim = esc_trim[index]
def move(self, freq):
freq = min(self.freq_max, max(self.freq_min, freq + self.trim))
self.channel.pulse_width(int(freq))
def __del__(self):
self.timer.deinit()
示例14: __init__
def __init__(self, PWMpin, DIRpin, timer_id, channel_id):
self.PWMpin = PWMpin
self.DIRpin = DIRpin
self.timer_id = timer_id
self.channel_id = channel_id
self.isRunning = False
self.currentDirection = None
self.currentSpeed = 0
# Set up the GPIO pins as output
PWMpin = Pin(self.PWMpin)
DIRpin = Pin(self.DIRpin, Pin.OUT_PP)
tim = Timer(self.timer_id, freq=1000)
ch = tim.channel(self.channel_id, Timer.PWM, pin=PWMpin)
示例15: __init__
def __init__(self):
# set up motor with PWM and timer control
self.A1 = Pin('X3',Pin.OUT_PP) # A is right motor
self.A2 = Pin('X4',Pin.OUT_PP)
self.B1 = Pin('X7',Pin.OUT_PP) # B is left motor
self.B2 = Pin('X8',Pin.OUT_PP)
self.PWMA = Pin('X1')
self.PWMB = Pin('X2')
self.speed = 0 # +100 full speed forward, -100 full speed back
self.turn = 0 # turn is +/-100; 0 = left/right same speed,
# ... +50 = left at speed, right stop, +100 = right back full
# Configure counter 2 to produce 1kHz clock signal
self.tim = Timer(2, freq = 1000)
# Configure timer to provide PWM signal
self.motorA = self.tim.channel(1, Timer.PWM, pin = self.PWMA)
self.motorB = self.tim.channel(2, Timer.PWM, pin = self.PWMB)
self.lsf = 0 # left motor speed factor +/- 1
self.rsf = 0 # right motor speed factor +/- 1
self.countA = 0 # speed pulse count for motorA
self.countB = 0 # speed pulse count for motorB
self.speedA = 0 # actual speed of motorA
self.speedB = 0 # actual speed of motorB
# Create external interrupts for motorA and motorB Hall Effect Senors
self.motorA_int = pyb.ExtInt ('Y4', pyb.ExtInt.IRQ_RISING, pyb.Pin.PULL_NONE,self.isr_motorA)
self.motorB_int = pyb.ExtInt ('Y6', pyb.ExtInt.IRQ_RISING, pyb.Pin.PULL_NONE,self.isr_motorB)
self.speed_timer = pyb.Timer(8, freq=10)
self.speed_timer.callback(self.isr_speed_timer)