當前位置: 首頁>>代碼示例>>Python>>正文


Python pyb.Timer類代碼示例

本文整理匯總了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
開發者ID:CRAWlab,項目名稱:ARLISS,代碼行數:25,代碼來源:motor_class.py

示例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()		
開發者ID:old-blighty,項目名稱:de1-electonics-group-project,代碼行數:33,代碼來源:full.py

示例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
開發者ID:CRAWlab,項目名稱:ARLISS,代碼行數:9,代碼來源:motor_class.py

示例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)
開發者ID:pymagic-org,項目名稱:pymagic_driver,代碼行數:56,代碼來源:motors.py

示例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)
開發者ID:old-blighty,項目名稱:de1-electonics-group-project,代碼行數:10,代碼來源:full.py

示例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)
開發者ID:SolitonNew,項目名稱:pyhome,代碼行數:55,代碼來源:timethread.py

示例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
開發者ID:CRAWlab,項目名稱:ARLISS,代碼行數:12,代碼來源:motor.py

示例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()
開發者ID:sbollaerts,項目名稱:barequadx,代碼行數:53,代碼來源:Motor.py

示例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
開發者ID:CRAWlab,項目名稱:ARLISS,代碼行數:13,代碼來源:motor_class.py

示例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
開發者ID:CRAWlab,項目名稱:ARLISS,代碼行數:13,代碼來源:motor.py

示例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
開發者ID:CRAWlab,項目名稱:ARLISS,代碼行數:14,代碼來源:motor.py

示例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)
開發者ID:pymagic-org,項目名稱:pymagic_driver,代碼行數:14,代碼來源:motors.py

示例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()
開發者ID:wagnerc4,項目名稱:flight_controller,代碼行數:14,代碼來源:esc.py

示例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)
開發者ID:CRAWlab,項目名稱:ARLISS,代碼行數:16,代碼來源:motor.py

示例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)
開發者ID:Kurtizl,項目名稱:Balancing-Robot,代碼行數:29,代碼來源:motor.py


注:本文中的pyb.Timer類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。