|
| 1 | +# SPDX-FileCopyrightText: 2021 Phil Burgess for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +""" |
| 6 | +Lola Droid Shoulder Robot with servo and NeoPixel |
| 7 | +""" |
| 8 | + |
| 9 | +# pylint: disable=import-error |
| 10 | +import time |
| 11 | +import random |
| 12 | +import board |
| 13 | +import pwmio |
| 14 | +import neopixel |
| 15 | +from adafruit_motor import servo |
| 16 | +from adafruit_led_animation.animation.comet import Comet |
| 17 | +from adafruit_led_animation.animation.SparklePulse import SparklePulse |
| 18 | +from adafruit_led_animation.sequence import AnimationSequence |
| 19 | +from adafruit_led_animation.color import RED, BLUE |
| 20 | + |
| 21 | +PIXEL_PIN = board.D5 |
| 22 | +SERVO_PIN = board.A2 |
| 23 | +NUM_PIXELS = 12 |
| 24 | +ORDER = neopixel.GRB |
| 25 | +BRIGHTNESS = 0.1 |
| 26 | + |
| 27 | +# Initialize servo |
| 28 | +PWM = pwmio.PWMOut(SERVO_PIN, frequency=50) |
| 29 | +SERVO = servo.Servo(PWM) |
| 30 | + |
| 31 | +# Initialize NeoPixels and animations |
| 32 | +PIXELS = neopixel.NeoPixel(PIXEL_PIN, NUM_PIXELS, auto_write=False, |
| 33 | + pixel_order=ORDER) |
| 34 | +LARSON = Comet(PIXELS, bounce=True, speed=0.6/NUM_PIXELS, |
| 35 | + tail_length=NUM_PIXELS//2, |
| 36 | + color=(RED[0] * BRIGHTNESS, # This is a little faster than |
| 37 | + RED[1] * BRIGHTNESS, # using the NeoPixel brightness |
| 38 | + RED[2] * BRIGHTNESS)) # setting. |
| 39 | +SPARKLE = SparklePulse(PIXELS, period=2, speed=0.15, |
| 40 | + max_intensity=BRIGHTNESS, color=BLUE) |
| 41 | +ANIMATIONS = AnimationSequence(LARSON, SPARKLE, advance_interval=7, |
| 42 | + auto_clear=False) |
| 43 | + |
| 44 | +SERVO.angle = POSITION = NEXT_POSITION = 7 |
| 45 | +MOVING = False # Initial state = paused |
| 46 | +START_TIME = time.monotonic() # Initial time |
| 47 | +DURATION = 1.0 # Hold initial position for 1 sec |
| 48 | + |
| 49 | +while True: # Loop forever... |
| 50 | + |
| 51 | + # Move turret -- randomly looks around and pauses |
| 52 | + NOW = time.monotonic() |
| 53 | + ELAPSED = NOW - START_TIME # Seconds since start of motion or pause |
| 54 | + if ELAPSED >= DURATION: # End motion/pause? |
| 55 | + MOVING = not MOVING # Toggle between those two states |
| 56 | + START_TIME = NOW # and record the new starting time |
| 57 | + ELAPSED = 0.0 |
| 58 | + if MOVING: # Switching from paused to moving |
| 59 | + POSITION = NEXT_POSITION |
| 60 | + while abs(POSITION - NEXT_POSITION) < 10: # Min +/- 10 degrees |
| 61 | + NEXT_POSITION = random.uniform(0, 90) # Try, try again |
| 62 | + DURATION = 0.2 + 0.6 * abs(POSITION - NEXT_POSITION) / 80 |
| 63 | + else: # Switching from moving to paused |
| 64 | + SERVO.angle = NEXT_POSITION # Move to end of sweep |
| 65 | + DURATION = random.uniform(0.5, 2.5) # Pause time |
| 66 | + if MOVING: |
| 67 | + FRACTION = ELAPSED / DURATION # Linear 0 to 1 |
| 68 | + FRACTION = (3 * FRACTION ** 2) - (2 * FRACTION ** 3) # Ease in/out |
| 69 | + SERVO.angle = POSITION + (NEXT_POSITION - POSITION) * FRACTION |
| 70 | + |
| 71 | + ANIMATIONS.animate() # Cycle through NeoPixel animations |
0 commit comments