-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathadafruit_motorkit.py
312 lines (236 loc) · 10.7 KB
/
adafruit_motorkit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# SPDX-FileCopyrightText: 2017 Scott Shawcroft for Adafruit Industries
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
`adafruit_motorkit`
====================================================
CircuitPython helper library for DC & Stepper Motor FeatherWing, Shield, and Pi Hat kits.
* Author(s): Scott Shawcroft, Kattni Rembor
Implementation Notes
--------------------
**Hardware:**
* `DC Motor + Stepper FeatherWing <https://www.adafruit.com/product/2927>`_
* `Adafruit Motor/Stepper/Servo Shield for Arduino v2 Kit <https://www.adafruit.com/product/1438>`_
* `Adafruit DC & Stepper Motor HAT for Raspberry Pi - Mini Kit
<https://www.adafruit.com/product/2348>`_
**Software and Dependencies:**
* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
* Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
* Adafruit's PCA9685 library: https://github.com/adafruit/Adafruit_CircuitPython_PCA9685
* Adafruit's Motor library: https://github.com/adafruit/Adafruit_CircuitPython_Motor
"""
import board
from adafruit_pca9685 import PCA9685
try:
from typing import Optional, Tuple
from busio import I2C
import adafruit_motor.motor
import adafruit_motor.stepper
except ImportError:
pass
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MotorKit.git"
class MotorKit:
"""Class representing an Adafruit DC & Stepper Motor FeatherWing, Shield or Pi Hat kit.
:param int address: I2C address of PCA9685 PWM controller. Default address is ``0x60``.
:param busio.I2C i2c: I2C bus object to use. If not specified, use ``board.I2C()``.
.. note::
``board.I2C()`` uses the default I2C bus frequency of 100 kHz. To speed up
motor control, use an I2C bus frequency of 400 KHz, or if available, 1 MHz.
The PCA9685 controller supports both of these higher speeds.
This will noticeably speed up stepper motor operation when many steps are requested.
:param int steppers_microsteps: Number of microsteps per step for stepper motors. Default is 16.
:param float pwm_frequency: defaults to 1600 Hz
"""
def __init__(
self,
address: int = 0x60,
i2c: Optional[I2C] = None,
steppers_microsteps: int = 16,
pwm_frequency: float = 1600.0,
) -> None:
self._motor1 = None
self._motor2 = None
self._motor3 = None
self._motor4 = None
self._stepper1 = None
self._stepper2 = None
if i2c is None:
i2c = board.I2C()
self._pca = PCA9685(i2c, address=address)
self._pca.frequency = pwm_frequency
self._steppers_microsteps = steppers_microsteps
# We can save memory usage (~300 bytes) by deduplicating the construction of the objects for
# each motor. This saves both code size and the number of raw strings (the error message)
# stored. The same technique is a net loss for stepper because there is less duplication.
def _motor(
self, motor_name: int, channels: Tuple[int, int, int], stepper_name: int
) -> adafruit_motor.motor.DCMotor:
from adafruit_motor import motor # pylint: disable=import-outside-toplevel
motor_name = "_motor" + str(motor_name)
stepper_name = "_stepper" + str(stepper_name)
if not getattr(self, motor_name):
if getattr(self, stepper_name):
raise RuntimeError(
"Cannot use {} at the same time as {}.".format(
motor_name[1:], stepper_name[1:]
)
)
self._pca.channels[channels[0]].duty_cycle = 0xFFFF
setattr(
self,
motor_name,
motor.DCMotor(
self._pca.channels[channels[1]], self._pca.channels[channels[2]]
),
)
return getattr(self, motor_name)
@property
def motor1(self) -> adafruit_motor.motor.DCMotor:
""":py:class:``~adafruit_motor.motor.DCMotor`` controls for motor 1.
The following image shows the location of the M1 terminal on the DC/Stepper FeatherWing.
The label on the FeatherWing is found on the bottom of the board.
The terminal is labeled on the top of the Shield and Pi Hat.
.. image :: ../docs/_static/motor_featherwing/m1.jpg
:alt: Motor 1 location
This example moves the motor forwards for one fifth of a second at full speed.
.. code-block:: python
import time
from adafruit_motorkit import motorkit
kit = MotorKit()
kit.motor1.throttle = 1.0
time.sleep(0.2)
kit.motor1.throttle = 0
"""
return self._motor(1, (8, 9, 10), 1)
@property
def motor2(self) -> adafruit_motor.motor.DCMotor:
""":py:class:``~adafruit_motor.motor.DCMotor`` controls for motor 2.
The following image shows the location of the M2 terminal on the DC/Stepper FeatherWing.
The label on the FeatherWing is found on the bottom of the board.
The terminal is labeled on the top of the Shield and Pi Hat.
.. image :: ../docs/_static/motor_featherwing/m2.jpg
:alt: Motor 2 location
This example moves the motor forwards for one fifth of a second at full speed.
.. code-block:: python
import time
from adafruit_motorkit import motorkit
kit = MotorKit()
kit.motor2.throttle = 1.0
time.sleep(0.2)
kit.motor1.throttle = 0
"""
return self._motor(2, (13, 11, 12), 1)
@property
def motor3(self) -> adafruit_motor.motor.DCMotor:
""":py:class:``~adafruit_motor.motor.DCMotor`` controls for motor 3.
The following image shows the location of the M2 terminal on the DC/Stepper FeatherWing.
The label on the FeatherWing is found on the bottom of the board.
The terminal is labeled on the top of the Shield and Pi Hat.
.. image :: ../docs/_static/motor_featherwing/m3.jpg
:alt: Motor 3 location
This example moves the motor forwards for one fifth of a second at full speed.
.. code-block:: python
import time
from adafruit_motorkit import motorkit
kit = MotorKit()
kit.motor3.throttle = 1.0
time.sleep(0.2)
kit.motor1.throttle = 0
"""
return self._motor(3, (2, 3, 4), 2)
@property
def motor4(self) -> adafruit_motor.motor.DCMotor:
""":py:class:``~adafruit_motor.motor.DCMotor`` controls for motor 4.
.. image :: ../docs/_static/motor_featherwing/m4.jpg
:alt: Motor 4 location
This example moves the motor forwards for one fifth of a second at full speed.
.. code-block:: python
import time
from adafruit_motorkit import motorkit
kit = MotorKit()
kit.motor4.throttle = 1.0
time.sleep(0.2)
kit.motor1.throttle = 0
"""
return self._motor(4, (7, 5, 6), 2)
@property
def stepper1(self) -> adafruit_motor.stepper.StepperMotor:
""":py:class:``~adafruit_motor.stepper.StepperMotor`` controls for one connected to stepper
1 (also labeled motor 1 and motor 2).
The following image shows the location of the stepper1 terminals on the DC/Stepper
FeatherWing. stepper1 is made up of the M1 and M2 terminals.
The labels on the FeatherWing are found on the bottom of the board.
The terminals are labeled on the top of the Shield and Pi Hat.
.. image :: ../docs/_static/motor_featherwing/stepper1.jpg
:alt: Stepper 1 location
This example moves the stepper motor 100 steps forwards.
.. code-block:: python
from adafruit_motorkit import MotorKit
kit = MotorKit()
for i in range(100):
kit.stepper1.onestep()
"""
if not self._stepper1:
from adafruit_motor import ( # pylint: disable=import-outside-toplevel
stepper,
)
if self._motor1 or self._motor2:
raise RuntimeError(
"Cannot use stepper1 at the same time as motor1 or motor2."
)
self._pca.channels[8].duty_cycle = 0xFFFF
self._pca.channels[13].duty_cycle = 0xFFFF
self._stepper1 = stepper.StepperMotor(
self._pca.channels[10],
self._pca.channels[9],
self._pca.channels[11],
self._pca.channels[12],
microsteps=self._steppers_microsteps,
)
return self._stepper1
@property
def stepper2(self) -> adafruit_motor.stepper.StepperMotor:
""":py:class:``~adafruit_motor.stepper.StepperMotor`` controls for one connected to stepper
2 (also labeled motor 3 and motor 4).
The following image shows the location of the stepper2 terminals on the DC/Stepper
FeatherWing. stepper2 is made up of the M3 and M4 terminals.
The labels on the FeatherWing are found on the bottom of the board.
The terminals are labeled on the top of the Shield and Pi Hat.
.. image :: ../docs/_static/motor_featherwing/stepper2.jpg
:alt: Stepper 2 location
This example moves the stepper motor 100 steps forwards.
.. code-block:: python
from adafruit_motorkit import MotorKit
kit = MotorKit()
for i in range(100):
kit.stepper2.onestep()
"""
if not self._stepper2:
from adafruit_motor import ( # pylint: disable=import-outside-toplevel
stepper,
)
if self._motor3 or self._motor4:
raise RuntimeError(
"Cannot use stepper2 at the same time as motor3 or motor4."
)
self._pca.channels[7].duty_cycle = 0xFFFF
self._pca.channels[2].duty_cycle = 0xFFFF
self._stepper2 = stepper.StepperMotor(
self._pca.channels[4],
self._pca.channels[3],
self._pca.channels[5],
self._pca.channels[6],
microsteps=self._steppers_microsteps,
)
return self._stepper2
@property
def frequency(self) -> float:
"""The overall PCA9685 PWM frequency in Hertz."""
return self._pca.frequency
@frequency.setter
def frequency(self, pwm_frequency: float = 1600.0) -> None:
self._pca.frequency = pwm_frequency