Skip to content

Commit

Permalink
Merge pull request #39 from adafruit/pylint-update
Browse files Browse the repository at this point in the history
Ran black, updated to pylint 2.x
  • Loading branch information
kattni committed Mar 17, 2020
2 parents 6615856 + 07ca9a0 commit e232452
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 138 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
source actions-ci/install.sh
- name: Pip install pylint, black, & Sphinx
run: |
pip install --force-reinstall pylint==1.9.2 black==19.10b0 Sphinx sphinx-rtd-theme
pip install --force-reinstall pylint black==19.10b0 Sphinx sphinx-rtd-theme
- name: Library version
run: git describe --dirty --always --tags
- name: PyLint
Expand Down
3 changes: 2 additions & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ spelling-store-unknown-words=no
[MISCELLANEOUS]

# List of note tags to take in consideration, separated by a comma.
notes=FIXME,XXX,TODO
# notes=FIXME,XXX,TODO
notes=FIXME,XXX


[TYPECHECK]
Expand Down
8 changes: 5 additions & 3 deletions adafruit_motor/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Motor.git"


class DCMotor:
"""DC motor driver. ``positive_pwm`` and ``negative_pwm`` can be swapped if the motor runs in
the opposite direction from what was expected for "forwards".
Expand All @@ -48,6 +49,7 @@ class DCMotor:
when high and the other is low.
:param ~pulseio.PWMOut negative_pwm: The motor input that causes the motor to spin backwards
when high and the other is low."""

def __init__(self, positive_pwm, negative_pwm):
self._positive = positive_pwm
self._negative = negative_pwm
Expand All @@ -70,10 +72,10 @@ def throttle(self, value):
self._positive.duty_cycle = 0
self._negative.duty_cycle = 0
elif value == 0:
self._positive.duty_cycle = 0xffff
self._negative.duty_cycle = 0xffff
self._positive.duty_cycle = 0xFFFF
self._negative.duty_cycle = 0xFFFF
else:
duty_cycle = int(0xffff * abs(value))
duty_cycle = int(0xFFFF * abs(value))
if value < 0:
self._positive.duty_cycle = 0
self._negative.duty_cycle = duty_cycle
Expand Down
17 changes: 11 additions & 6 deletions adafruit_motor/servo.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,21 @@

# We disable the too few public methods check because this is a private base class for the two types
# of servos.
class _BaseServo: # pylint: disable-msg=too-few-public-methods
class _BaseServo: # pylint: disable-msg=too-few-public-methods
"""Shared base class that handles pulse output based on a value between 0 and 1.0
:param ~pulseio.PWMOut pwm_out: PWM output object.
:param int min_pulse: The minimum pulse length of the servo in microseconds.
:param int max_pulse: The maximum pulse length of the servo in microseconds."""

def __init__(self, pwm_out, *, min_pulse=750, max_pulse=2250):
self._pwm_out = pwm_out
self.set_pulse_width_range(min_pulse, max_pulse)

def set_pulse_width_range(self, min_pulse=750, max_pulse=2250):
"""Change min and max pulse widths."""
self._min_duty = int((min_pulse * self._pwm_out.frequency) / 1000000 * 0xffff)
max_duty = (max_pulse * self._pwm_out.frequency) / 1000000 * 0xffff
self._min_duty = int((min_pulse * self._pwm_out.frequency) / 1000000 * 0xFFFF)
max_duty = (max_pulse * self._pwm_out.frequency) / 1000000 * 0xFFFF
self._duty_range = int(max_duty - self._min_duty)

@property
Expand All @@ -56,20 +57,21 @@ def fraction(self):
For conventional servos, corresponds to the servo position as a fraction
of the actuation range. Is None when servo is diabled (pulsewidth of 0ms).
"""
if self._pwm_out.duty_cycle == 0: # Special case for disabled servos
if self._pwm_out.duty_cycle == 0: # Special case for disabled servos
return None
return (self._pwm_out.duty_cycle - self._min_duty) / self._duty_range

@fraction.setter
def fraction(self, value):
if value is None:
self._pwm_out.duty_cycle = 0 # disable the motor
self._pwm_out.duty_cycle = 0 # disable the motor
return
if not 0.0 <= value <= 1.0:
raise ValueError("Must be 0.0 to 1.0")
duty_cycle = self._min_duty + int(value * self._duty_range)
self._pwm_out.duty_cycle = duty_cycle


class Servo(_BaseServo):
"""Control the position of a servo.
Expand Down Expand Up @@ -99,6 +101,7 @@ class Servo(_BaseServo):
the servo mechanism may hit the end stops, buzz, and draw extra current as it stalls.
Test carefully to find the safe minimum and maximum.
"""

def __init__(self, pwm_out, *, actuation_range=180, min_pulse=750, max_pulse=2250):
super().__init__(pwm_out, min_pulse=min_pulse, max_pulse=max_pulse)
self.actuation_range = actuation_range
Expand All @@ -115,18 +118,20 @@ def angle(self):

@angle.setter
def angle(self, new_angle):
if new_angle is None: # disable the servo by sending 0 signal
if new_angle is None: # disable the servo by sending 0 signal
self.fraction = None
return
if new_angle < 0 or new_angle > self.actuation_range:
raise ValueError("Angle out of range")
self.fraction = new_angle / self.actuation_range


class ContinuousServo(_BaseServo):
"""Control a continuous rotation servo.
:param int min_pulse: The minimum pulse width of the servo in microseconds.
:param int max_pulse: The maximum pulse width of the servo in microseconds."""

@property
def throttle(self):
"""How much power is being delivered to the motor. Values range from ``-1.0`` (full
Expand Down
25 changes: 16 additions & 9 deletions adafruit_motor/stepper.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"""Step a fraction of a step by partially activating two neighboring coils. Step size is determined
by ``microsteps`` constructor argument."""


class StepperMotor:
"""A bipolar stepper motor or four coil unipolar motor.
Expand All @@ -70,10 +71,11 @@ class StepperMotor:
the fourth coil (unipolar) or second input to second coil (bipolar).
:param int microsteps: Number of microsteps between full steps. Must be at least 2 and even.
"""

def __init__(self, ain1, ain2, bin1, bin2, *, microsteps=16):
self._coil = (ain2, bin1, ain1, bin2)

# set a safe pwm freq for each output
# set a safe pwm freq for each output
for i in range(4):
if self._coil[i].frequency < 1500:
self._coil[i].frequency = 2000
Expand All @@ -84,8 +86,10 @@ def __init__(self, ain1, ain2, bin1, bin2, *, microsteps=16):
if microsteps % 2 == 1:
raise ValueError("Microsteps must be even")
self._microsteps = microsteps
self._curve = [int(round(0xffff * math.sin(math.pi / (2 * microsteps) * i)))
for i in range(microsteps + 1)]
self._curve = [
int(round(0xFFFF * math.sin(math.pi / (2 * microsteps) * i)))
for i in range(microsteps + 1)
]
self._update_coils()

def _update_coils(self, *, microstepping=False):
Expand All @@ -98,10 +102,12 @@ def _update_coils(self, *, microstepping=False):

# This ensures DOUBLE steps use full torque. Without it, we'd use partial torque from the
# microstepping curve (0xb504).
if not microstepping and (duty_cycles[leading_coil] == duty_cycles[trailing_coil] and
duty_cycles[leading_coil] > 0):
duty_cycles[leading_coil] = 0xffff
duty_cycles[trailing_coil] = 0xffff
if not microstepping and (
duty_cycles[leading_coil] == duty_cycles[trailing_coil]
and duty_cycles[leading_coil] > 0
):
duty_cycles[leading_coil] = 0xFFFF
duty_cycles[trailing_coil] = 0xFFFF

# Energize coils as appropriate:
for i in range(4):
Expand Down Expand Up @@ -145,8 +151,9 @@ def onestep(self, *, direction=FORWARD, style=SINGLE):
step_size = half_step

current_interleave = self._current_microstep // half_step
if ((style == SINGLE and current_interleave % 2 == 1) or
(style == DOUBLE and current_interleave % 2 == 0)):
if (style == SINGLE and current_interleave % 2 == 1) or (
style == DOUBLE and current_interleave % 2 == 0
):
step_size = half_step
elif style in (SINGLE, DOUBLE):
step_size = full_step
Expand Down

0 comments on commit e232452

Please sign in to comment.