• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python pyb.Timer类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中pyb.Timer的典型用法代码示例。如果您正苦于以下问题:Python Timer类的具体用法?Python Timer怎么用?Python Timer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Timer类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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


示例16: remote

def  remote():

	#initialise UART communication
	uart = UART(6)
	uart.init(9600, bits=8, parity = None, stop = 2)

	# 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)

	# Motor in idle state
	A1.high()	
	A2.high()	
	speed = 0
	DEADZONE = 5

	# Use keypad U and D keys to control speed
	while True:				# loop forever until CTRL-C
		while (uart.any()!=10):    #wait we get 10 chars
			n = uart.any()
		command = uart.read(10)
		if command[2]==ord('5'):
			if speed < 96:
				speed = speed + 5
				print(speed)
		elif command[2]==ord('6'):
			if speed > - 96:
				speed = speed - 5
				print(speed)
		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()		# idle
			A2.low()		
开发者ID:old-blighty,项目名称:de1-electonics-group-project,代码行数:47,代码来源:full.py


示例17: __init__

  def __init__( self, p, timernum, afreq = 100 ) :
    isname = type(p) == str
    pinname = p if isname else p.name()
    timernum, channel = PWM.timerandchannel(pinname, timernum)
    if isname:
      p = Pin(pinname)

    self._timer = Timer(timernum, freq = afreq)
    self._channel = self._timer.channel(channel, Timer.PWM, pin = p)
开发者ID:rolandvs,项目名称:GuyCarverMicroPythonCode,代码行数:9,代码来源:PWM.py


示例18: start

    def start(self, speed, direction):
        """ method to start a motor
        Arguments:
          speed : speed of the motor 0-100 (as percentage of max)
          direction : CW or CCW, for clockwise or counterclockwise
        """

        # Standby pin should go high to enable motion
        self.STBYpin.high()
        # STBYpin.high()
        # GPIO.output(self.STBYpin, GPIO.HIGH)

        # x01 and x02 have to be opposite to move, toggle to change direction
        if direction in ('cw','CW','clockwise'):
            self.ControlPin1.low()
            # GPIO.output(self.ControlPin1, GPIO.LOW)
            self.ControlPin2.high()
            # GPIO.output(self.ControlPin2, GPIO.HIGH)
        elif direction in ('ccw','CCW','counterclockwise'):
            self.ControlPin1.high()
            # GPIO.output(self.ControlPin1, GPIO.HIGH)
            self.ControlPin2.low()
            # GPIO.output(self.ControlPin2, GPIO.LOW)
        else:
            raise ValueError('Please enter CW or CCW for direction.')

        # Start the motor
        # PWM.start(channel, duty, freq=2000, polarity=0)
        if 0 <= speed <= 100:
            self.PWMpin = Pin('X4')
            tim = Timer(2, freq=1000)
            ch = tim.channel(4, Timer.PWM, pin=self.PWMpin)
            ch.pulse_width_percent(speed)
            # PWM.start(self.PWMpin, speed)
        else:
            raise ValueError("Please enter speed between 0 and 100, \
                              representing a percentage of the maximum \
                              motor speed.")

        # set the status attributes
        self.isRunning = True
        self.currentDirection = direction
        self.currentSpeed = speed
开发者ID:CRAWlab,项目名称:ARLISS,代码行数:43,代码来源:motor.py


示例19: __init__

 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")
开发者ID:sbollaerts,项目名称:barequadx,代码行数:13,代码来源:Motor.py


示例20: gradient

def gradient(ri,gi,bi,rf,gf,bf,wait,cycles):
  """
  ri = initial,  percent
  rf = final,  percent m
  gradient(0,10,0,0,100,0,10,4)
  for inverting:
  gradient(20,255,255,95,255,255,10,2)
  """
  tim = Timer(2, freq=1000)
  cr = tim.channel(2, Timer.PWM_INVERTED, pin=r.pin)
  cg = tim.channel(3, Timer.PWM_INVERTED, pin=g.pin)
  cb = tim.channel(4, Timer.PWM_INVERTED, pin=b.pin)
  for i in range(cycles):
    for a in range(100):
      cr.pulse_width_percent((rf-ri)*0.01*a+ri)
      cg.pulse_width_percent((gf-gi)*0.01*a+gi)
      cb.pulse_width_percent((bf-bi)*0.01*a+gi)
      pyb.delay(wait)
    for a in range(100):
      cr.pulse_width_percent((rf-ri)*0.01*(100-a)+ri)
      cg.pulse_width_percent((gf-gi)*0.01*(100-a)+gi)
      cb.pulse_width_percent((bf-bi)*0.01*(100-a)+gi)
      pyb.delay(wait)
开发者ID:mbag,项目名称:meetup,代码行数:23,代码来源:tutorial01.py



注:本文中的pyb.Timer类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python pyb.UART类代码示例发布时间:2022-05-25
下一篇:
Python pyb.Pin类代码示例发布时间:2022-05-25
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap