|
| 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() |
0 commit comments