Skip to content

Commit d214497

Browse files
committed
add access to servo actuation_range and pulse widths after construction
1 parent 4168b4e commit d214497

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

adafruit_motor/servo.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,14 @@ class _BaseServo: # pylint: disable-msg=too-few-public-methods
4141
:param int min_pulse: The minimum pulse length of the servo in microseconds.
4242
:param int max_pulse: The maximum pulse length of the servo in microseconds."""
4343
def __init__(self, pwm_out, *, min_pulse=750, max_pulse=2250):
44-
self._min_duty = int((min_pulse * pwm_out.frequency) / 1000000 * 0xffff)
45-
max_duty = (max_pulse * pwm_out.frequency) / 1000000 * 0xffff
46-
self._duty_range = int(max_duty - self._min_duty)
4744
self._pwm_out = pwm_out
45+
self.set_pulse_widths(min_pulse, max_pulse)
46+
47+
def set_pulse_widths(min_pulse=750, max_pulse=2250):
48+
"""Change pulse widths."""
49+
self._min_duty = int((min_pulse * self._pwm_out.frequency) / 1000000 * 0xffff)
50+
max_duty = (max_pulse * self._pwm_out.frequency) / 1000000 * 0xffff
51+
self._duty_range = int(max_duty - self._min_duty)
4852

4953
@property
5054
def fraction(self):
@@ -70,6 +74,13 @@ class Servo(_BaseServo):
7074
:param int min_pulse: The minimum pulse width of the servo in microseconds.
7175
:param int max_pulse: The maximum pulse width of the servo in microseconds.
7276
77+
``actuation_range`` is an exposed property and can be changed at any time:
78+
79+
.. code-block:: python
80+
81+
servo = Servo(pwm)
82+
servo.actuation_range = 135
83+
7384
The specified pulse width range of a servo has historically been 1000-2000us,
7485
for a 90 degree range of motion. But nearly all modern servos have a 170-180
7586
degree range, and the pulse widths can go well out of the range to achieve this
@@ -85,19 +96,19 @@ class Servo(_BaseServo):
8596
"""
8697
def __init__(self, pwm_out, *, actuation_range=180, min_pulse=750, max_pulse=2250):
8798
super().__init__(pwm_out, min_pulse=min_pulse, max_pulse=max_pulse)
88-
self._actuation_range = actuation_range
99+
self.actuation_range = actuation_range
89100
self._pwm = pwm_out
90101

91102
@property
92103
def angle(self):
93104
"""The servo angle in degrees. Must be in the range ``0`` to ``actuation_range``."""
94-
return self._actuation_range * self.fraction
105+
return self.actuation_range * self.fraction
95106

96107
@angle.setter
97108
def angle(self, new_angle):
98109
if new_angle < 0 or new_angle > self._actuation_range:
99110
raise ValueError("Angle out of range")
100-
self.fraction = new_angle / self._actuation_range
111+
self.fraction = new_angle / self.actuation_range
101112

102113
class ContinuousServo(_BaseServo):
103114
"""Control a continuous rotation servo.

0 commit comments

Comments
 (0)