34
34
35
35
# We disable the too few public methods check because this is a private base class for the two types
36
36
# of servos.
37
- class _BaseServo : # pylint: disable-msg=too-few-public-methods
37
+ class _BaseServo : # pylint: disable-msg=too-few-public-methods
38
38
"""Shared base class that handles pulse output based on a value between 0 and 1.0
39
39
40
40
:param ~pulseio.PWMOut pwm_out: PWM output object.
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
44
def __init__ (self , pwm_out , * , min_pulse = 750 , max_pulse = 2250 ):
44
45
self ._pwm_out = pwm_out
45
46
self .set_pulse_width_range (min_pulse , max_pulse )
46
47
47
48
def set_pulse_width_range (self , min_pulse = 750 , max_pulse = 2250 ):
48
49
"""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
50
+ self ._min_duty = int ((min_pulse * self ._pwm_out .frequency ) / 1000000 * 0xFFFF )
51
+ max_duty = (max_pulse * self ._pwm_out .frequency ) / 1000000 * 0xFFFF
51
52
self ._duty_range = int (max_duty - self ._min_duty )
52
53
53
54
@property
@@ -56,20 +57,21 @@ def fraction(self):
56
57
For conventional servos, corresponds to the servo position as a fraction
57
58
of the actuation range. Is None when servo is diabled (pulsewidth of 0ms).
58
59
"""
59
- if self ._pwm_out .duty_cycle == 0 : # Special case for disabled servos
60
+ if self ._pwm_out .duty_cycle == 0 : # Special case for disabled servos
60
61
return None
61
62
return (self ._pwm_out .duty_cycle - self ._min_duty ) / self ._duty_range
62
63
63
64
@fraction .setter
64
65
def fraction (self , value ):
65
66
if value is None :
66
- self ._pwm_out .duty_cycle = 0 # disable the motor
67
+ self ._pwm_out .duty_cycle = 0 # disable the motor
67
68
return
68
69
if not 0.0 <= value <= 1.0 :
69
70
raise ValueError ("Must be 0.0 to 1.0" )
70
71
duty_cycle = self ._min_duty + int (value * self ._duty_range )
71
72
self ._pwm_out .duty_cycle = duty_cycle
72
73
74
+
73
75
class Servo (_BaseServo ):
74
76
"""Control the position of a servo.
75
77
@@ -99,6 +101,7 @@ class Servo(_BaseServo):
99
101
the servo mechanism may hit the end stops, buzz, and draw extra current as it stalls.
100
102
Test carefully to find the safe minimum and maximum.
101
103
"""
104
+
102
105
def __init__ (self , pwm_out , * , actuation_range = 180 , min_pulse = 750 , max_pulse = 2250 ):
103
106
super ().__init__ (pwm_out , min_pulse = min_pulse , max_pulse = max_pulse )
104
107
self .actuation_range = actuation_range
@@ -115,18 +118,20 @@ def angle(self):
115
118
116
119
@angle .setter
117
120
def angle (self , new_angle ):
118
- if new_angle is None : # disable the servo by sending 0 signal
121
+ if new_angle is None : # disable the servo by sending 0 signal
119
122
self .fraction = None
120
123
return
121
124
if new_angle < 0 or new_angle > self .actuation_range :
122
125
raise ValueError ("Angle out of range" )
123
126
self .fraction = new_angle / self .actuation_range
124
127
128
+
125
129
class ContinuousServo (_BaseServo ):
126
130
"""Control a continuous rotation servo.
127
131
128
132
:param int min_pulse: The minimum pulse width of the servo in microseconds.
129
133
:param int max_pulse: The maximum pulse width of the servo in microseconds."""
134
+
130
135
@property
131
136
def throttle (self ):
132
137
"""How much power is being delivered to the motor. Values range from ``-1.0`` (full
0 commit comments