Skip to content

Commit ab547b7

Browse files
committed
Continuous servos and DC motors work.
1 parent a32d2d2 commit ab547b7

File tree

5 files changed

+90
-17
lines changed

5 files changed

+90
-17
lines changed

adafruit_motor/motor.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
Shield for Arduino v2 Kit - v2.3 <https://www.adafruit.com/product/1438>`_ do this for popular form
3131
factors already.
3232
33+
.. note:: The TB6612 boards feature three inputs XIN1, XIN2 and PWMX. Since we PWM the INs directly
34+
its expected that the PWM pin is consistently high.
3335
3436
* Author(s): Scott Shawcroft
3537
"""
3638

37-
import math
38-
3939
__version__ = "0.0.0-auto.0"
4040
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_motor.git"
4141

@@ -63,9 +63,9 @@ def throttle(self):
6363
if self._positive.duty_cycle > 0 and self._negative.duty_cycle > 0:
6464
raise RuntimeError("PWMs in invalid state")
6565
value = max(self._positive.duty_cycle, self._negative.duty_cycle) / 0xffff
66-
if self._negative > 0:
66+
if self._negative.duty_cycle > 0:
6767
return -1 * value
68-
return values
68+
return value
6969

7070
@throttle.setter
7171
def throttle(self, value):
@@ -78,7 +78,7 @@ def throttle(self, value):
7878
self._positive.duty_cycle = 0xffff
7979
self._negative.duty_cycle = 0xffff
8080
else:
81-
duty_cycle = 0xffff * math.abs(value)
81+
duty_cycle = int(0xffff * abs(value))
8282
if value < 0:
8383
self._positive.duty_cycle = 0
8484
self._negative.duty_cycle = duty_cycle

adafruit_motor/servo.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ def __init__(self, pwm_out, *, min_pulse=1000, max_pulse=2000):
4949
self._duty_range = int(max_duty - self._min_duty)
5050
print(self._min_duty, self._duty_range)
5151
self._pwm_out = pwm_out
52-
self._min_pulse = min_pulse
53-
self._pulse_range = max_pulse - min_pulse
5452

5553
@property
5654
def _fraction(self):
@@ -60,7 +58,6 @@ def _fraction(self):
6058
def _fraction(self, value):
6159
"""The fraction of pulse high."""
6260
duty_cycle = self._min_duty + int(value * self._duty_range)
63-
print(self._min_pulse + int(value * self._pulse_range))
6461
self._pwm_out.duty_cycle = duty_cycle
6562

6663
class Servo(_BaseServo):

examples/continuous_servo.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,27 @@
33
from board import *
44
import busio
55

6-
# Import the PCA9685 module.
6+
# Import the PCA9685 module. Available in the bundle and here:
7+
# https://github.com/adafruit/Adafruit_CircuitPython_PCA9685
78
from adafruit_pca9685 import PCA9685
89

9-
# This example also relies on the Adafruit motor library available here:
10-
# https://github.com/adafruit/Adafruit_CircuitPython_Motor
1110
from adafruit_motor import servo
1211

1312
i2c = busio.I2C(SCL, SDA)
1413

1514
# Create a simple PCA9685 class instance.
1615
pca = PCA9685(i2c)
16+
# You can optionally provide a finer tuned reference clock speed to improve the accuracy of the
17+
# timing pulses. This calibration will be specific to each board and its environment. See the
18+
# calibration.py example in the PCA9685 driver.
19+
# pca = PCA9685(i2c, reference_clock_speed=25630710)
1720
pca.frequency = 50
1821

1922
# The pulse range is 1000 - 2000 by default.
20-
servo7 = servo.ContinuousServo(pca.channels[7], min_pulse=600, max_pulse=2500)
23+
servo7 = servo.ContinuousServo(pca.channels[7])
24+
# If your servo doesn't stop once the script is finished you may need to tune the
25+
# reference_clock_speed above or the min_pulse and max_pulse timings below.
26+
# servo7 = servo.ContinuousServo(pca.channels[7], min_pulse=1000, max_pulse=2000)
2127

2228
print("Forwards")
2329
servo7.throttle = 1

examples/dc_motor.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# This example uses an Adafruit Stepper and DC Motor FeatherWing to run a DC Motor.
2+
# https://www.adafruit.com/product/2927
3+
4+
import time
5+
6+
from board import *
7+
import busio
8+
9+
# Import the PCA9685 module. Available in the bundle and here:
10+
# https://github.com/adafruit/Adafruit_CircuitPython_PCA9685
11+
from adafruit_pca9685 import PCA9685
12+
13+
from adafruit_motor import motor
14+
15+
i2c = busio.I2C(SCL, SDA)
16+
17+
# Create a simple PCA9685 class instance for the Motor FeatherWing's default address.
18+
pca = PCA9685(i2c, address=0x60)
19+
pca.frequency = 100
20+
21+
# Motor 1 is channels 9 and 10 with 8 held high.
22+
# Motor 2 is channels 11 and 12 with 13 held high.
23+
# Motor 3 is channels 3 and 4 with 2 held high.
24+
# Motor 4 is channels 5 and 6 with 7 held high.
25+
26+
# DC Motors generate electrical noise when running that can reset the microcontroller in extreme
27+
# cases. A capacitor can be used to help prevent this. The demo uses motor 4 because it worked ok
28+
# in testing without a capacitor.
29+
# See here for more info: https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino/faq#faq-13
30+
pca.channels[7].duty_cycle = 0xffff
31+
motor4 = motor.DCMotor(pca.channels[5], pca.channels[6])
32+
33+
print("Forwards slow")
34+
motor4.throttle = 0.5
35+
print("throttle:", motor4.throttle)
36+
time.sleep(1)
37+
38+
print("Forwards")
39+
motor4.throttle = 1
40+
print("throttle:", motor4.throttle)
41+
time.sleep(1)
42+
43+
print("Backwards")
44+
motor4.throttle = -1
45+
print("throttle:", motor4.throttle)
46+
time.sleep(1)
47+
48+
print("Backwards slow")
49+
motor4.throttle = -0.5
50+
print("throttle:", motor4.throttle)
51+
time.sleep(1)
52+
53+
print("Stop")
54+
motor4.throttle = 0
55+
print("throttle:", motor4.throttle)
56+
time.sleep(1)
57+
58+
print("Spin freely")
59+
motor4.throttle = None
60+
print("throttle:", motor4.throttle)
61+
62+
pca.deinit()

examples/servo_sweep.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,42 @@
33
from board import *
44
import busio
55

6-
# Import the PCA9685 module.
6+
# Import the PCA9685 module. Available in the bundle and here:
7+
# https://github.com/adafruit/Adafruit_CircuitPython_PCA9685
78
from adafruit_pca9685 import PCA9685
89
from adafruit_motor import servo
910

1011
i2c = busio.I2C(SCL, SDA)
1112

1213
# Create a simple PCA9685 class instance.
1314
pca = PCA9685(i2c)
15+
# You can optionally provide a finer tuned reference clock speed to improve the accuracy of the
16+
# timing pulses. This calibration will be specific to each board and its environment. See the
17+
# calibration.py example in the PCA9685 driver.
18+
# pca = PCA9685(i2c, reference_clock_speed=25630710)
1419
pca.frequency = 50
1520

1621
# To get the full range of the servo you will likely need to adjust the min_pulse and max_pulse to
1722
# match the stall points of the servo.
1823
# This is an example for the Sub-micro servo: https://www.adafruit.com/product/2201
19-
# servo7 = servo.Servo(pca.channels[7], min_pulse=580, max_pulse=2480)
24+
# servo7 = servo.Servo(pca.channels[7], min_pulse=580, max_pulse=2350)
2025
# This is an example for the Micro Servo - High Powered, High Torque Metal Gear:
2126
# https://www.adafruit.com/product/2307
22-
# servo7 = servo.Servo(pca.channels[7], min_pulse=600, max_pulse=2400)
27+
# servo7 = servo.Servo(pca.channels[7], min_pulse=500, max_pulse=2600)
2328
# This is an example for the Standard servo - TowerPro SG-5010 - 5010:
2429
# https://www.adafruit.com/product/155
25-
# servo7 = servo.Servo(pca.channels[7], min_pulse=600, max_pulse=2500)
30+
# servo7 = servo.Servo(pca.channels[7], min_pulse=400, max_pulse=2400)
2631
# This is an example for the Analog Feedback Servo: https://www.adafruit.com/product/1404
27-
# servo7 = servo.Servo(pca.channels[7], min_pulse=600, max_pulse=2600)
32+
# servo7 = servo.Servo(pca.channels[7], min_pulse=600, max_pulse=2500)
2833

2934
# The pulse range is 1000 - 2000 by default.
3035
servo7 = servo.Servo(pca.channels[7])
3136

37+
# We sleep in the loops to give the servo time to move into position.
3238
for i in range(180):
3339
servo7.angle = i
40+
time.sleep(0.03)
3441
for i in range(180):
3542
servo7.angle = 180 - i
43+
time.sleep(0.03)
3644
pca.deinit()

0 commit comments

Comments
 (0)