@@ -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_width_range (min_pulse , max_pulse )
46
+
47
+ def set_pulse_width_range (self , min_pulse = 750 , max_pulse = 2250 ):
48
+ """Change min and max 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 ):
@@ -56,6 +60,8 @@ def fraction(self):
56
60
57
61
@fraction .setter
58
62
def fraction (self , value ):
63
+ if not 0.0 <= value <= 1.0 :
64
+ raise ValueError ("Must be 0.0 to 1.0" )
59
65
duty_cycle = self ._min_duty + int (value * self ._duty_range )
60
66
self ._pwm_out .duty_cycle = duty_cycle
61
67
@@ -68,6 +74,13 @@ class Servo(_BaseServo):
68
74
:param int min_pulse: The minimum pulse width of the servo in microseconds.
69
75
:param int max_pulse: The maximum pulse width of the servo in microseconds.
70
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
+
71
84
The specified pulse width range of a servo has historically been 1000-2000us,
72
85
for a 90 degree range of motion. But nearly all modern servos have a 170-180
73
86
degree range, and the pulse widths can go well out of the range to achieve this
@@ -80,22 +93,23 @@ class Servo(_BaseServo):
80
93
get a wider range of movement. But if you go too low or too high,
81
94
the servo mechanism may hit the end stops, buzz, and draw extra current as it stalls.
82
95
Test carefully to find the safe minimum and maximum.
83
- """
96
+ """
84
97
def __init__ (self , pwm_out , * , actuation_range = 180 , min_pulse = 750 , max_pulse = 2250 ):
85
98
super ().__init__ (pwm_out , min_pulse = min_pulse , max_pulse = max_pulse )
86
- self ._actuation_range = actuation_range
99
+ self .actuation_range = actuation_range
100
+ """The physical range of motion of the servo in degrees."""
87
101
self ._pwm = pwm_out
88
102
89
103
@property
90
104
def angle (self ):
91
105
"""The servo angle in degrees. Must be in the range ``0`` to ``actuation_range``."""
92
- return self ._actuation_range * self .fraction
106
+ return self .actuation_range * self .fraction
93
107
94
108
@angle .setter
95
109
def angle (self , new_angle ):
96
- if new_angle < 0 or new_angle > self ._actuation_range :
110
+ if new_angle < 0 or new_angle > self .actuation_range :
97
111
raise ValueError ("Angle out of range" )
98
- self .fraction = new_angle / self ._actuation_range
112
+ self .fraction = new_angle / self .actuation_range
99
113
100
114
class ContinuousServo (_BaseServo ):
101
115
"""Control a continuous rotation servo.
0 commit comments