Skip to content

Commit 2348322

Browse files
authored
Merge pull request #1502 from adafruit/Terrako_Robot
Code for Terrako
2 parents bdc80aa + d4d2ab0 commit 2348322

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

Terrako_Robot/code.py

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

0 commit comments

Comments
 (0)