Mercurial > maestro-servo
changeset 6:c6b9391e2d89
Add a Servo class
This class lets you define a number of physical parameters for
your servo, so that you can set it in degrees.
| author | Radomir Dopieralski <sheep-devel@sheep.art.pl> |
|---|---|
| date | Mon, 23 Jun 2014 17:57:02 +0200 |
| parents | ee14101e75a3 |
| children | 942b81279a73 |
| files | maestro.py |
| diffstat | 1 files changed, 21 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/maestro.py Mon Jun 23 09:40:03 2014 +0000 +++ b/maestro.py Mon Jun 23 17:57:02 2014 +0200 @@ -39,7 +39,7 @@ return byte1 + byte2 * 256 def init(self): - """Initializes the communication.""" + """Initialize the communication.""" self._write_command(0xAA) def set_target(self, servo, target): @@ -70,3 +70,23 @@ """True if any servo is still moving, otherwise False.""" self._write_command(0x93) return bool(self._read_byte()) + + +class Servo(object): + min_position = 4000 + max_position = 8000 + + def __init__(self, maestro, index, max_degrees=90, reverse=False): + self.maestro = maestro + self.index = index + self.revers = reverse + self.max_degrees = max_degrees + + def set_angle(self, degrees): + position = ((self.max_position - self.min_position) * + degrees) / self.max_degrees + if self.reverse: + target = self.max_position - position + else: + target = self.min_position + position + self.maestro.set_position(self.index, target)
