def __init__(self, channel): """Allocate a PWM given a channel. :param channel: The PWM channel number. 0-9 are on-board, 10-19 are on the MXP port :type channel: int """ SensorBase.checkPWMChannel(channel) self.channel = channel self._port = hal.initializeDigitalPort(hal.getPort(channel)) self.freq = 1/self.kDefaultPwmPeriod * 1000 print('PWM::__init__ channel: {0}, freq: {1}'.format( channel, self.freq)) if not hal.allocatePWMChannel(self._port): raise IndexError("PWM channel %d is already allocated" % channel) #self.pwmObj = hal_data['pwm'][digital_port.pin]['pwmObj'] # Need this to free on unit test wpilib reset Resource._add_global_resource(self) hal.setPWM(self._port, 0) self.__finalizer = weakref.finalize(self, _freePWM, self._port) self.eliminateDeadband = True hal.HALReport(hal.HALUsageReporting.kResourceType_PWM, channel)
def setRaw(self, value): """Set the PWM value directly to the hardware. Write a raw value to a PWM channel. :param value: Raw PWM value. Range 0 - 255. :type value: int """ hal.setPWM(self.port, value)
def setRaw(self, value): """Set the PWM value directly to the hardware. Write a raw value to a PWM channel. :param value: Raw PWM value. Range 0 - 255. :type value: int """ if self.port is None: raise ValueError("operation on freed port") hal.setPWM(self.port, value)
def __init__(self, channel): """Allocate a PWM given a channel. :param channel: The PWM channel number. 0-9 are on-board, 10-19 are on the MXP port :type channel: int """ SensorBase.checkPWMChannel(channel) self.channel = channel self._port = hal.initializeDigitalPort(hal.getPort(channel)) if not hal.allocatePWMChannel(self._port): raise IndexError("PWM channel %d is already allocated" % channel) # Need this to free on unit test wpilib reset Resource._add_global_resource(self) hal.setPWM(self._port, 0) self._pwm_finalizer = weakref.finalize(self, _freePWM, self._port) self.eliminateDeadband = False hal.HALReport(hal.HALUsageReporting.kResourceType_PWM, channel)
def _freePWM(port): hal.setPWM(port, 0) hal.freePWMChannel(port) hal.freeDIO(port)