@@ -41,10 +41,14 @@ class _BaseServo: # pylint: disable-msg=too-few-public-methods
41
41
:param int min_pulse: The minimum pulse length of the servo in microseconds.
42
42
:param int max_pulse: The maximum pulse length of the servo in microseconds."""
43
43
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 )
47
44
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 )
48
52
49
53
@property
50
54
def fraction (self ):
@@ -70,6 +74,13 @@ class Servo(_BaseServo):
70
74
:param int min_pulse: The minimum pulse width of the servo in microseconds.
71
75
:param int max_pulse: The maximum pulse width of the servo in microseconds.
72
76
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
+
73
84
The specified pulse width range of a servo has historically been 1000-2000us,
74
85
for a 90 degree range of motion. But nearly all modern servos have a 170-180
75
86
degree range, and the pulse widths can go well out of the range to achieve this
@@ -85,19 +96,19 @@ class Servo(_BaseServo):
85
96
"""
86
97
def __init__ (self , pwm_out , * , actuation_range = 180 , min_pulse = 750 , max_pulse = 2250 ):
87
98
super ().__init__ (pwm_out , min_pulse = min_pulse , max_pulse = max_pulse )
88
- self ._actuation_range = actuation_range
99
+ self .actuation_range = actuation_range
89
100
self ._pwm = pwm_out
90
101
91
102
@property
92
103
def angle (self ):
93
104
"""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
95
106
96
107
@angle .setter
97
108
def angle (self , new_angle ):
98
109
if new_angle < 0 or new_angle > self ._actuation_range :
99
110
raise ValueError ("Angle out of range" )
100
- self .fraction = new_angle / self ._actuation_range
111
+ self .fraction = new_angle / self .actuation_range
101
112
102
113
class ContinuousServo (_BaseServo ):
103
114
"""Control a continuous rotation servo.
0 commit comments