def set_servo(self, gpio, pulse_width_us): """ Sets a pulse-width on a gpio to repeat every subcycle (by default every 20ms). """ # Make sure we can set the exact pulse_width_us _pulse_incr_us = _PWM.get_pulse_incr_us() if pulse_width_us % _pulse_incr_us: # No clean division possible raise AttributeError(("Pulse width increment granularity %sus " "cannot divide a pulse-time of %sus") % (_pulse_incr_us, pulse_width_us)) # Initialize channel if not already done, else check subcycle time if _PWM.is_channel_initialized(self._dma_channel): _subcycle_us = _PWM.get_channel_subcycle_time_us(self._dma_channel) if _subcycle_us != self._subcycle_time_us: raise AttributeError(("Error: DMA channel %s is setup with a " "subcycle_time of %sus (instead of %sus)") % \ (self._dma_channel, _subcycle_us, self._subcycle_time_us)) else: init_channel(self._dma_channel, self._subcycle_time_us) # Add pulse for this GPIO add_channel_pulse(self._dma_channel, gpio, 0, \ int(pulse_width_us / _pulse_incr_us))
def set_servo(self, gpio, pulse_width_us): """ Sets a pulse-width on a gpio to repeat every subcycle (by default every 20ms). """ # Make sure we can set the exact pulse_width_us _pulse_incr_us = _PWM.get_pulse_incr_us() if pulse_width_us % _pulse_incr_us: # No clean division possible raise AttributeError(("Pulse width increment granularity %sus " "cannot divide a pulse-time of %sus") % (_pulse_incr_us, pulse_width_us)) # Initialize channel if not already done, else check subcycle time if _PWM.is_channel_initialized(self._dma_channel): _subcycle_us = _PWM.get_channel_subcycle_time_us(self._dma_channel) if _subcycle_us != self._subcycle_time_us: raise AttributeError(("Error: DMA channel %s is setup with a " "subcycle_time of %sus (instead of %sus)") % \ (self._dma_channel, _subcycle_us, self._subcycle_time_us)) else: init_channel(self._dma_channel, self._subcycle_time_us) # If this GPIO is already used, clear it first if self._gpios_used & 1 << gpio: clear_channel_gpio(self._dma_channel, gpio) self._gpios_used |= 1 << gpio # Add pulse for this GPIO add_channel_pulse(self._dma_channel, gpio, 0, \ int(pulse_width_us / _pulse_incr_us))
def pulse(self): ''' The pulse width for the servo. ''' position_ratio = self.angle / self.range position_ratio *= -1 if self.reverse else 1 pulse_range = max(self.pulse) - min(self.pulse) pulse_incr = _PWM.get_pulse_incr_us() pulse = pulse_range * position_ratio + min(self.pulse) pulse = pulse // pulse_incr pulse *= pulse_incr return pulse
def __init__(self, dma_channel=0, subcycle_time_us=20000, \ pulse_incr_us=10): """ Makes sure PWM is setup with the correct increment granularity and subcycle time. """ self._dma_channel = dma_channel self._subcycle_time_us = subcycle_time_us if _PWM.is_setup(): _pw_inc = _PWM.get_pulse_incr_us() if not pulse_incr_us == _pw_inc: raise AttributeError(("Error: PWM is already setup with pulse-" "width increment granularity of %sus instead of %sus")\ % (_pw_inc, self.pulse_incr_us)) else: setup(pulse_incr_us=pulse_incr_us)
def get_pulse_incr_us(): """ Returns the currently set pulse width increment granularity in us """ return _PWM.get_pulse_incr_us()