Skip to content

Instantly share code, notes, and snippets.

@tannewt
Created January 5, 2018 19:45
Show Gist options
  • Save tannewt/840fbf01aac078bcf6527624cc365e43 to your computer and use it in GitHub Desktop.
Save tannewt/840fbf01aac078bcf6527624cc365e43 to your computer and use it in GitHub Desktop.
motor featherwing
# The MIT License (MIT)
#
# Copyright (c) 2017 Scott Shawcroft for Adafruit Industries LLC
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
`adafruit_motor.motor_featherwing`
====================================================
Helper for using motors with the `Motor FeatherWing <https://www.adafruit.com/product/2927>`_.
* Author(s): Scott Shawcroft
"""
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWings.git"
from adafruit_motor import motor, stepper
from adafruit_pca9685 import PCA9685
class MotorFeatherWing:
def __init__(self, i2c):
self._motor1 = None
self._motor2 = None
self._motor3 = None
self._motor4 = None
self._stepper12 = None
self._stepper34 = None
self._pca = PCA9685(i2c, address=0x60)
self._pca.frequency = 1600
@property
def motor1(self):
"""`DCMotor` controls for motor 1."""
if not self._motor1:
if self._stepper12:
raise RuntimeError("Cannot use motor1 at the same time as stepper12.")
self._pca.channels[8].duty_cycle = 0xffff
self._motor1 = motor.DCMotor(self._pca.channels[9], self._pca.channels[10])
return self._motor1
@property
def motor2(self):
"""`DCMotor` controls for motor 2."""
if not self._motor2:
if self._stepper12:
raise RuntimeError("Cannot use motor2 at the same time as stepper12.")
self._pca.channels[13].duty_cycle = 0xffff
self._motor2 = motor.DCMotor(self._pca.channels[11], self._pca.channels[12])
return self._motor2
@property
def motor3(self):
"""`DCMotor` controls for motor 3."""
if not self._motor3:
if self._stepper34:
raise RuntimeError("Cannot use motor3 at the same time as stepper34.")
self._pca.channels[2].duty_cycle = 0xffff
self._motor3 = motor.DCMotor(self._pca.channels[3], self._pca.channels[4])
return self._motor3
@property
def motor4(self):
"""`DCMotor` controls for motor 4."""
if not self._motor4:
if self._stepper34:
raise RuntimeError("Cannot use motor4 at the same time as stepper34.")
self._pca.channels[7].duty_cycle = 0xffff
self._motor4 = motor.DCMotor(self._pca.channels[5], self._pca.channels[6])
return self._motor4
@property
def stepper12(self):
"""`StepperMotor` controls for one connected to motor 1 and motor 2."""
if not self._stepper12:
if self._motor1 or self._motor2:
raise RuntimeError("Cannot use stepper12 at the same time as motor1 or motor2.")
self._pca.channels[8].duty_cycle = 0xffff
self._pca.channels[13].duty_cycle = 0xffff
self._stepper12 = stepper.StepperMotor(self._pca.channels[10], self._pca.channels[9], self._pca.channels[11], self._pca.channels[12])
return self._stepper12
@property
def stepper34(self):
"""`StepperMotor` controls for one connected to motor 3 and motor 4."""
if not self._stepper34:
if self._motor3 or self._motor4:
raise RuntimeError("Cannot use stepper34 at the same time as motor3 or motor4.")
self._pca.channels[7].duty_cycle = 0xffff
self._pca.channels[2].duty_cycle = 0xffff
self._stepper34 = stepper.StepperMotor(self._pca.channels[4], self._pca.channels[3], self._pca.channels[5], self._pca.channels[6])
return self._stepper34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment