1
- # SPDX-FileCopyrightText: 2017 Scott Shawcroft for Adafruit Industries
1
+ # SPDX-FileCopyrightText: 2021 Scott Shawcroft for Adafruit Industries
2
2
#
3
3
# SPDX-License-Identifier: MIT
4
4
23
23
__version__ = "0.0.0-auto.0"
24
24
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Motor.git"
25
25
26
+ FAST_DECAY = 0
27
+ """Recirculation current fast decay mode (coasting)"""
28
+
29
+ SLOW_DECAY = 1
30
+ """Recirculation current slow decay mode (braking)"""
31
+
26
32
27
33
class DCMotor :
28
34
"""DC motor driver. ``positive_pwm`` and ``negative_pwm`` can be swapped if the motor runs in
29
35
the opposite direction from what was expected for "forwards".
30
36
37
+ Motor controller recirculation current decay mode is selectable and defaults to
38
+ ``motor.FAST_DECAY`` (coasting). ``motor.SLOW_DECAY`` is recommended to improve spin
39
+ threshold, speed-to-throttle linearity, and PWM frequency sensitivity.
40
+
41
+ Decay mode settings only effect the operational performance of controller chips such
42
+ as the DRV8833, DRV8871, and TB6612. Either decay mode setting is compatible
43
+ with discrete h-bridge controller circuitry such as the L9110H and L293D; operational
44
+ performance is not altered.
45
+
31
46
:param ~pwmio.PWMOut positive_pwm: The motor input that causes the motor to spin forwards
32
47
when high and the other is low.
33
48
:param ~pwmio.PWMOut negative_pwm: The motor input that causes the motor to spin backwards
@@ -37,34 +52,59 @@ def __init__(self, positive_pwm, negative_pwm):
37
52
self ._positive = positive_pwm
38
53
self ._negative = negative_pwm
39
54
self ._throttle = None
55
+ self ._decay_mode = FAST_DECAY
40
56
41
57
@property
42
58
def throttle (self ):
43
59
"""Motor speed, ranging from -1.0 (full speed reverse) to 1.0 (full speed forward),
44
- or ``None``.
60
+ or ``None`` (controller off) .
45
61
If ``None``, both PWMs are turned full off. If ``0.0``, both PWMs are turned full on.
46
62
"""
47
63
return self ._throttle
48
64
49
65
@throttle .setter
50
66
def throttle (self , value ):
51
67
if value is not None and (value > 1.0 or value < - 1.0 ):
52
- raise ValueError ("Throttle must be None or between -1.0 and 1.0" )
68
+ raise ValueError ("Throttle must be None or between -1.0 and + 1.0" )
53
69
self ._throttle = value
54
- if value is None :
70
+ if value is None : # Turn off motor controller (high-Z)
55
71
self ._positive .duty_cycle = 0
56
72
self ._negative .duty_cycle = 0
57
- elif value == 0 :
73
+ elif value == 0 : # Brake motor (low-Z)
58
74
self ._positive .duty_cycle = 0xFFFF
59
75
self ._negative .duty_cycle = 0xFFFF
60
76
else :
61
77
duty_cycle = int (0xFFFF * abs (value ))
62
- if value < 0 :
63
- self ._positive .duty_cycle = 0
64
- self ._negative .duty_cycle = duty_cycle
65
- else :
66
- self ._positive .duty_cycle = duty_cycle
67
- self ._negative .duty_cycle = 0
78
+ if self ._decay_mode == SLOW_DECAY : # Slow Decay (Braking) Mode
79
+ if value < 0 :
80
+ self ._positive .duty_cycle = 0xFFFF - duty_cycle
81
+ self ._negative .duty_cycle = 0xFFFF
82
+ else :
83
+ self ._positive .duty_cycle = 0xFFFF
84
+ self ._negative .duty_cycle = 0xFFFF - duty_cycle
85
+ else : # Default Fast Decay (Coasting) Mode
86
+ if value < 0 :
87
+ self ._positive .duty_cycle = 0
88
+ self ._negative .duty_cycle = duty_cycle
89
+ else :
90
+ self ._positive .duty_cycle = duty_cycle
91
+ self ._negative .duty_cycle = 0
92
+
93
+ @property
94
+ def decay_mode (self ):
95
+ """Motor controller recirculation current decay mode. A value of ``motor.FAST_DECAY``
96
+ sets the motor controller to the default fast recirculation current decay mode
97
+ (coasting); ``motor.SLOW_DECAY`` sets slow decay (braking) mode."""
98
+ return self ._decay_mode
99
+
100
+ @decay_mode .setter
101
+ def decay_mode (self , mode = FAST_DECAY ):
102
+ if mode in (FAST_DECAY , SLOW_DECAY ):
103
+ self ._decay_mode = mode
104
+ else :
105
+ raise ValueError (
106
+ "Decay mode value must be either motor.FAST_DECAY or motor.SLOW_DECAY"
107
+ )
68
108
69
109
def __enter__ (self ):
70
110
return self
0 commit comments