Пример #1
0
 def initPWM(pin):
     """ Sets a pin to PWM """
     pwm = PWM(pin, 0)
     pwm.frequency = 1e3
     pwm.duty_cycle = 0
     pwm.enable()
     return pwm
Пример #2
0
 def initPWM(pin):
     pwm = PWM(pin, 0)
     pwm.frequency = 1e3
     pwm.duty_cycle = 0
     pwm.enable()
     return pwm
Пример #3
0
print(mode)

# Open PWM channel 0, pin 32
pins = range(0, 41)
"""
for pin in pins:
	try:
		pwm = PWM(0, pin)
		print("** Channel 0 pin {} works!".format(pin))
	except:
		print("Pin {} does not work".format(pin))
"""
pwm = PWM(0, 2)
pwm = PWM(0, 0)

#Channel 0 pin 2

# Set frequency to 1 kHz
pwm.frequency = 2e3
# Set duty cycle to 75%
pwm.duty_cycle = 0.25

pwm.enable()

# Change duty cycle to 50%
pwm.duty_cycle = 0.25

time.sleep(5 * 60)

pwm.close()
"""periphery库 PWM测试."""
import time
from periphery import PWM

# 打开 PWM 3, channel 0 ,对应开发板上PWM3外设

try:
    pwm = PWM(2, 0)
    # 设置PWM输出频率为 1 kHz
    pwm.frequency = 1e3
    # 设置占空比为 50%
    pwm.duty_cycle = 0.50
    # 开启PWM输出
    pwm.enable()
    while True:
        for i in range(0, 9):
            time.sleep(0.1)
            pwm.duty_cycle += 0.05
        for i in range(0, 9):
            time.sleep(0.1)
            pwm.duty_cycle -= 0.05
        if pwm.duty_cycle == 0.0:
            time.sleep(1)
finally:
    pwm.close()
Пример #5
0
class PERIPHERY_PWM(PERIPHERY):
    """
    PWM出力ピンを表すクラス。
    指定ピンがハードウェアPWMに対応しない場合は、疑似PWMとして操作する。
    """
    def __init__(self, pin, freq=None, range=None, threshold=0.01, debug=False):
        """
        親クラスのコンストラクタ処理後、指定のピンに対しPWM出力ピンとして設定を行う。
        初期値としてPWMサイクル値をゼロに指定する。
        引数:
            pin     int     PWM番号、必須(1~3)
            freq    int     PWM Frequency値(Hz)
            range   int     PWMサイクル値の範囲(25から40,000までの整数)
            threshold   float   入力値を0として認識するしきい値(-threshold < value< threshold => 0)
        """
        super().__init__(pin, mode=OUT, debug=debug)
        self.freq = freq or DEFAULT_FREQ
        self.threshold = threshold
        if self.pin == 1:
            self.chip_number = 0
            self.channel_number = 0
        elif self.pin == 2:
            self.chip_number = 1
            self.channel_number = 0
        elif self.pin == 3:
            self.chip_number = 2
            self.channel_number = 0
        else:
            raise ValueError('[PERIPHERY_PWM] pwm:{} is out of range(1.. 3)'.format(str(self.pin)))
        try:
            from periphery import PWM
        except ImportError:
            exit('[PERIPHERY_PWM] This code requires periphery package')
        self.gpio = PWM(self.chip_number, self.channel_number)
        self.gpio.frequency = self.freq
        self.gpio.duty_cycle = 0
        self.gpio.enable()
        self.run(0)
        if self.debug:
            print('[PERIPHERY_PWM] pwm:{} set freq:{}/dutycycle:{}, enabled and set value 0'.format(
                str(self.pin), str(self.freq), str(self.gpio.duty_cycle)))

    def run(self, input_value):
        """
        引数で渡されたpulse値をPWMサイクル値に変換し、指定ピンへ出力する。
        引数:
            input_value float       コントローラ/AIからの入力値
        戻り値:
            なし
        """
        cycle = self.to_duty_cycle(input_value)
        self.gpio.duty_cycle(cycle)
        if self.debug:
            print('[PERIPHERY_PWM] pwm:{} set cycle {}(input_value:{})'.format(
                str(self.pin), str(cycle), str(input_value)))


    def to_duty_cycle(self, input_value):
        """
        コントローラ/AIからの入力値をPWMサイクル値に変換する。
        しきい値範囲内の場合やNoneの場合は、0として扱う。
        引数:
            input_value     float   コントローラ/AIからの入力値(None含む)
        戻り値:
            cycle           float     PWM duty_cycle値
        """
        if input_value is None or (abs(float(input_value)) < self.threshold):
            if self.debug:
                print('[PERIPHERY_PWM] gpio:{} input_value None to zero'.format(str(self.pin)))
            return float(0)
        input_value = abs(input_value)
        if input_value >= 1.0:
            return float(1.0)
        return float(input_value)
    
    def shutdown(self):
        self.run(0)
        self.gpio.disable()
        if self.debug:
            print('[PERIPHERY_PWM] set duty_cycle to 0 and disabled')
        super().shutdown()