Skip to content

Commit ffbf67c

Browse files
authored
Merge pull request #762 from caternuson/feedback_servo
Update feedback servo with CP examples
2 parents 88dd162 + 0477e9d commit ffbf67c

4 files changed

Lines changed: 191 additions & 0 deletions

File tree

Binary file not shown.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Example code for calibrating analog feedback values to servo range
2+
3+
import time
4+
import board
5+
import pulseio
6+
from adafruit_motor import servo
7+
from analogio import AnalogIn
8+
9+
# Pin setup
10+
SERVO_PIN = board.A1
11+
FEEDBACK_PIN = board.A5
12+
13+
# Calibration setup
14+
ANGLE_MIN = 0
15+
ANGLE_MAX = 180
16+
17+
# Setup servo
18+
pwm = pulseio.PWMOut(SERVO_PIN, duty_cycle=2 ** 15, frequency=50)
19+
servo = servo.Servo(pwm)
20+
servo.angle = None
21+
22+
# Setup feedback
23+
feedback = AnalogIn(FEEDBACK_PIN)
24+
25+
print("Servo feedback calibration.")
26+
# Move to MIN angle
27+
print("Moving to {}...".format(ANGLE_MIN), end="")
28+
servo.angle = ANGLE_MIN
29+
time.sleep(2)
30+
print("Done.")
31+
feedback_min = feedback.value
32+
# Move to MAX angle
33+
print("Moving to {}...".format(ANGLE_MAX), end="")
34+
servo.angle = ANGLE_MAX
35+
time.sleep(2)
36+
print("Done.")
37+
feedback_max = feedback.value
38+
# Print results
39+
print("Feedback MIN = {}".format(feedback_min))
40+
print("Feedback MAX = {}".format(feedback_max))
41+
# Deactivate servo
42+
servo.angle = None
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Example code for recording and playing back servo motion with a
2+
# analog feedback servo
3+
4+
import time
5+
import board
6+
import pulseio
7+
from simpleio import map_range
8+
from adafruit_motor import servo
9+
from analogio import AnalogIn
10+
from digitalio import DigitalInOut, Direction, Pull
11+
12+
# Pin setup
13+
RECORD_PIN = board.D10
14+
PLAY_PIN = board.D9
15+
LED_PIN = board.D13
16+
SERVO_PIN = board.A1
17+
FEEDBACK_PIN = board.A5
18+
19+
# Record setup
20+
CALIB_MIN = 2816
21+
CALIB_MAX = 49632
22+
ANGLE_MIN = 0
23+
ANGLE_MAX = 180
24+
SAMPLE_COUNT = 512
25+
SAMPLE_DELAY = 0.025
26+
27+
# Setup record button
28+
record_button = DigitalInOut(RECORD_PIN)
29+
record_button.direction = Direction.INPUT
30+
record_button.pull = Pull.UP
31+
32+
# Setup play button
33+
play_button = DigitalInOut(PLAY_PIN)
34+
play_button.direction = Direction.INPUT
35+
play_button.pull = Pull.UP
36+
37+
# Setup LED
38+
led = DigitalInOut(LED_PIN)
39+
led.direction = Direction.OUTPUT
40+
led.value = False
41+
42+
# Setup servo
43+
pwm = pulseio.PWMOut(SERVO_PIN, duty_cycle=2 ** 15, frequency=50)
44+
servo = servo.Servo(pwm)
45+
servo.angle = None
46+
47+
# Setup feedback
48+
feedback = AnalogIn(FEEDBACK_PIN)
49+
50+
# Servo positions stored here
51+
position = [None]*SAMPLE_COUNT
52+
53+
print("Servo RecordPlay")
54+
55+
def play_servo():
56+
print("Playing...", end="")
57+
count = 0
58+
while play_button.value:
59+
print(".", end="")
60+
angle = position[count]
61+
if angle is None:
62+
break
63+
servo.angle = angle
64+
count += 1
65+
if count >= SAMPLE_COUNT:
66+
break
67+
time.sleep(SAMPLE_DELAY)
68+
print("Done.")
69+
servo.angle = None
70+
time.sleep(0.250)
71+
72+
def record_servo():
73+
for i in range(len(position)):
74+
position[i] = None
75+
servo.angle = None
76+
led.value = True
77+
print("Recording...", end="")
78+
count = 0
79+
while record_button.value:
80+
print(".", end='')
81+
position[count] = map_range(feedback.value, CALIB_MIN, CALIB_MAX, ANGLE_MIN, ANGLE_MAX)
82+
count += 1
83+
if count >= SAMPLE_COUNT:
84+
break
85+
time.sleep(SAMPLE_DELAY)
86+
led.value = False
87+
print("Done.")
88+
time.sleep(0.250)
89+
90+
while True:
91+
if not record_button.value:
92+
time.sleep(0.01)
93+
# wait for released
94+
while not record_button.value:
95+
pass
96+
time.sleep(0.02)
97+
# OK released!
98+
record_servo()
99+
100+
if not play_button.value:
101+
time.sleep(0.01)
102+
# wait for released
103+
while not play_button.value:
104+
pass
105+
time.sleep(0.02)
106+
# OK released!
107+
play_servo()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Example code for using analog feedback value to seek a position
2+
import board
3+
import pulseio
4+
from simpleio import map_range
5+
from adafruit_motor import servo
6+
from analogio import AnalogIn
7+
8+
# Demo angles
9+
angles = [0, 180, 0, 45, 180]
10+
11+
# Pin setup
12+
SERVO_PIN = board.A1
13+
FEEDBACK_PIN = board.A5
14+
15+
# Calibration setup
16+
CALIB_MIN = 18112
17+
CALIB_MAX = 49408
18+
ANGLE_MIN = 0
19+
ANGLE_MAX = 180
20+
21+
# Setup servo
22+
pwm = pulseio.PWMOut(SERVO_PIN, duty_cycle=2 ** 15, frequency=50)
23+
servo = servo.Servo(pwm)
24+
servo.angle = None
25+
26+
# Setup feedback
27+
feedback = AnalogIn(FEEDBACK_PIN)
28+
29+
def get_position():
30+
return map_range(feedback.value, CALIB_MIN, CALIB_MAX, ANGLE_MIN, ANGLE_MAX)
31+
32+
def seek_position(position, tolerance=2):
33+
servo.angle = position
34+
35+
while abs(get_position() - position) > tolerance:
36+
pass
37+
38+
print("Servo feedback seek example.")
39+
for angle in angles:
40+
print("Moving to {}...".format(angle), end="")
41+
seek_position(angle)
42+
print("Done.")

0 commit comments

Comments
 (0)