本文整理汇总了Python中pyb.Timer.channel方法的典型用法代码示例。如果您正苦于以下问题:Python Timer.channel方法的具体用法?Python Timer.channel怎么用?Python Timer.channel使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyb.Timer
的用法示例。
在下文中一共展示了Timer.channel方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: MOTORS
# 需要导入模块: from pyb import Timer [as 别名]
# 或者: from pyb.Timer import channel [as 别名]
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)
示例2: motor_control
# 需要导入模块: from pyb import Timer [as 别名]
# 或者: from pyb.Timer import channel [as 别名]
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: start
# 需要导入模块: from pyb import Timer [as 别名]
# 或者: from pyb.Timer import channel [as 别名]
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
示例4: change_speed
# 需要导入模块: from pyb import Timer [as 别名]
# 或者: from pyb.Timer import channel [as 别名]
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
示例5: drive_motor
# 需要导入模块: from pyb import Timer [as 别名]
# 或者: from pyb.Timer import channel [as 别名]
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: set_speed
# 需要导入模块: from pyb import Timer [as 别名]
# 或者: from pyb.Timer import channel [as 别名]
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
示例7: rate
# 需要导入模块: from pyb import Timer [as 别名]
# 或者: from pyb.Timer import channel [as 别名]
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()
示例8: stop
# 需要导入模块: from pyb import Timer [as 别名]
# 或者: from pyb.Timer import channel [as 别名]
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
示例9: hard_stop
# 需要导入模块: from pyb import Timer [as 别名]
# 或者: from pyb.Timer import channel [as 别名]
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
示例10: gradient
# 需要导入模块: from pyb import Timer [as 别名]
# 或者: from pyb.Timer import channel [as 别名]
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)
示例11: soft_stop
# 需要导入模块: from pyb import Timer [as 别名]
# 或者: from pyb.Timer import channel [as 别名]
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__
# 需要导入模块: from pyb import Timer [as 别名]
# 或者: from pyb.Timer import channel [as 别名]
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()
示例13: __init__
# 需要导入模块: from pyb import Timer [as 别名]
# 或者: from pyb.Timer import channel [as 别名]
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)
示例14: remote
# 需要导入模块: from pyb import Timer [as 别名]
# 或者: from pyb.Timer import channel [as 别名]
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()
示例15: start
# 需要导入模块: from pyb import Timer [as 别名]
# 或者: from pyb.Timer import channel [as 别名]
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