fusion_hat.pwm module
PWM class to control a single PWM channel
Example
Simple example to control PWM channel 0 at 50Hz with 50% duty cycle.
>>> from fusion_hat.pwm import PWM
>>> pwm = PWM(0)
>>> pwm.freq(50)
>>> pwm.pulse_width_percent(50)
Advanced example to control PWM channel with 50 Hz frequency, 20000us and pulse width 500-2500us for servos.
>>> from fusion_hat.pwm import PWM
>>> pwm = PWM(0)
>>> pwm.freq(50)
>>> pwm.pulse_width(1500) # servo center position
>>> pwm.pulse_width(2500) # servo max position
>>> pwm.pulse_width(500) # servo min position
- class fusion_hat.pwm.PWM(channel: int, freq: int = 50, *args, **kwargs)[source]
Bases:
_BasePWM class to control a single PWM channel
- Parameters:
channel (int/str) – PWM channel number(0-11/P0-P11)
freq (int, optional) – PWM frequency, default is 50Hz
addr (int, optional) – I2C address, default is 0x17
*args – Additional arguments for
fusion_hat._i2c.I2C**kwargs – Additional keyword arguments for
fusion_hat._i2c.I2C
- Raises:
ValueError – Invalid channel number
- CHANNEL_NUM = 12
- PATH = '/sys/class/fusion_hat/fusion_hat/pwm'
- enable(enable: bool = True) None[source]
Enable/disable PWM channel
- Parameters:
enable (bool, optional) – enable or disable, default is True
- write_duty_cycle(duty_cycle: int) int[source]
Set duty cycle in ms
- Parameters:
duty_cycle (int) – duty cycle in ms
- freq(freq: float | None = None) float[source]
Set/get frequency, leave blank to get frequency
- Parameters:
freq (float, optional) – frequency(0-65535)(Hz), default is 50Hz
- Returns:
frequency
- Return type:
float
- prescaler(prescaler: int | None = None, raw: bool = False) int[source]
[Deprecated] Set/get prescaler, leave blank to get prescaler
- period(period: int | None = None) int[source]
Set/get period, leave blank to get period
- Parameters:
period (int, optional) – period(0-65535), default is 0
raw (bool, optional) – Whether to write period directly, default is False
- Returns:
period
- Return type:
int
- duty_cycle(duty_cycle: int | None = None) int[source]
Set/get duty cycle, in ms
- Parameters:
duty_cycle (int, optional) – duty cycle
- Returns:
duty cycle
- Return type:
int
- pulse_width(pulse_width: int | None = None) int[source]
Set/get pulse width, in ms
- Parameters:
pulse_width (int, optional) – pulse width in ms
- Returns:
pulse width
- Return type:
int