Mercurial > circuitpython-spiderwing
comparison python/servos.py @ 0:cf2cd5a35d24 draft
Initial commit
| author | Radomir Dopieralski <sheep-devel@sheep.art.pl> |
|---|---|
| date | Mon, 15 May 2017 16:36:08 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:cf2cd5a35d24 |
|---|---|
| 1 import pca9685 | |
| 2 import math | |
| 3 | |
| 4 | |
| 5 TRIMS = [ | |
| 6 0, -15, 0, # front left | |
| 7 0, 0, | |
| 8 10, -20, 0, # hind left | |
| 9 0, -10, .0, # hind right | |
| 10 0, 0, | |
| 11 0, -5, 0, # front right | |
| 12 ] | |
| 13 | |
| 14 | |
| 15 class Servos: | |
| 16 def __init__(self, i2c, address=0x40, freq=50, min_us=600, max_us=2400, | |
| 17 degrees=180): | |
| 18 self.period = 1000000 / freq | |
| 19 self.min_duty = self._us2duty(min_us) | |
| 20 self.max_duty = self._us2duty(max_us) | |
| 21 self.degrees = degrees | |
| 22 self.freq = freq | |
| 23 self.pca9685 = pca9685.PCA9685(i2c, address) | |
| 24 self.pca9685.freq(freq) | |
| 25 | |
| 26 def _us2duty(self, value): | |
| 27 return int(4095 * value / self.period) | |
| 28 | |
| 29 def position(self, index, degrees=None, radians=None, us=None, duty=None): | |
| 30 span = self.max_duty - self.min_duty | |
| 31 if degrees is not None: | |
| 32 duty = self.min_duty + span * degrees / self.degrees | |
| 33 elif radians is not None: | |
| 34 duty = self.min_duty + span * radians / math.radians(self.degrees) | |
| 35 elif us is not None: | |
| 36 duty = self._us2duty(us) | |
| 37 elif duty is not None: | |
| 38 pass | |
| 39 else: | |
| 40 return self.pca9685.duty(index) | |
| 41 duty = min(self.max_duty, max(self.min_duty, int(duty + TRIMS[index]))) | |
| 42 self.pca9685.duty(index, duty) | |
| 43 | |
| 44 def release(self, index): | |
| 45 self.pca9685.duty(index, 0) |
