@@ -43,11 +43,14 @@ class _BaseServo:
43
43
:param int max_pulse: The maximum pulse length of the servo in microseconds.
44
44
:param int range: The physical range of the servo corresponding to the signal's duty in
45
45
degrees."""
46
- def __init__ (self , pwm_out , * , min_pulse = 1000 , max_pulse = 2000 , trim = 0 ):
47
- self ._min_duty = (min_pulse * 0xffff * pwm_out .frequency ) / 1000000
48
- max_duty = (max_pulse * 0xffff * pwm_out .frequency ) / 1000000
49
- self ._duty_range = max_duty - self ._min_duty
46
+ def __init__ (self , pwm_out , * , min_pulse = 1000 , max_pulse = 2000 ):
47
+ self ._min_duty = int ((min_pulse * pwm_out .frequency ) / 1000000 * 0xffff )
48
+ max_duty = (max_pulse * pwm_out .frequency ) / 1000000 * 0xffff
49
+ self ._duty_range = int (max_duty - self ._min_duty )
50
+ print (self ._min_duty , self ._duty_range )
50
51
self ._pwm_out = pwm_out
52
+ self ._min_pulse = min_pulse
53
+ self ._pulse_range = max_pulse - min_pulse
51
54
52
55
@property
53
56
def _fraction (self ):
@@ -56,7 +59,9 @@ def _fraction(self):
56
59
@_fraction .setter
57
60
def _fraction (self , value ):
58
61
"""The fraction of pulse high."""
59
- self ._pwm_out .duty_cycle = self .min_duty + value * self ._duty_range
62
+ duty_cycle = self ._min_duty + int (value * self ._duty_range )
63
+ print (self ._min_pulse + int (value * self ._pulse_range ))
64
+ self ._pwm_out .duty_cycle = duty_cycle
60
65
61
66
class Servo (_BaseServo ):
62
67
"""Control the position of a servo.
@@ -66,7 +71,8 @@ class Servo(_BaseServo):
66
71
:param int min_pulse: The minimum pulse length of the servo in microseconds.
67
72
:param int max_pulse: The maximum pulse length of the servo in microseconds.
68
73
:param int trim: Slight shift of values to calibrate stopped value in microseconds."""
69
- def __init__ (self , pwm_out , * , actuation_range = 180 , min_pulse = 1000 , max_pulse = 2000 , trim = 0 ):
74
+ def __init__ (self , pwm_out , * , actuation_range = 180 , min_pulse = 1000 , max_pulse = 2000 ):
75
+ super ().__init__ (pwm_out , min_pulse = min_pulse , max_pulse = max_pulse )
70
76
self ._actuation_range = actuation_range
71
77
self ._pwm = pwm_out
72
78
@@ -75,10 +81,11 @@ def angle(self):
75
81
return self ._actuation_range * self ._fraction
76
82
77
83
@angle .setter
78
- def angle (self , value ):
84
+ def angle (self , new_angle ):
79
85
"""The servo angle in degrees."""
80
- if value < 0 or value > self ._actuation_range :
86
+ if new_angle < 0 or new_angle > self ._actuation_range :
81
87
raise ValueError ("Angle out of range" )
88
+ self ._fraction = new_angle / self ._actuation_range
82
89
83
90
class ContinuousServo (_BaseServo ):
84
91
"""Control a continuous rotation servo.
@@ -99,7 +106,7 @@ def throttle(self, value):
99
106
raise ValueError ("Throttle must be between -1.0 and 1.0" )
100
107
if value is None :
101
108
raise ValueError ("Continuous servos cannot spin freely" )
102
- self ._fraction = int (( value + 1 ) / 2 )
109
+ self ._fraction = ( value + 1 ) / 2
103
110
104
111
def __enter__ (self ):
105
112
return self
0 commit comments