Added a servo easing example

pull/259/head
ZodiusInfuser 2022-03-21 16:52:11 +00:00
rodzic 98ccb7698d
commit 676c54aaed
3 zmienionych plików z 64 dodań i 5 usunięć

Wyświetl plik

@ -38,12 +38,12 @@ while user_sw.raw() is not True:
for i in range(len(sensor_addrs)):
mux.select(sensor_addrs[i])
sensor_voltage = sen_adc.read_voltage()
# Calculate the LED's hue, with Green for high voltages and Blue for low
hue = (2.0 - (sensor_voltage / 3.3)) * 0.333
led_bar.set_hsv(i, hue, 1.0, BRIGHTNESS)
led_bar.set_hsv(i, hue, 1.0, BRIGHTNESS)
print("S", i + 1, " = ", round(sensor_voltage, 3), sep="", end=", ")
print()
time.sleep(1.0 / UPDATES)
time.sleep(1.0 / UPDATES)

Wyświetl plik

@ -13,7 +13,7 @@ START_PIN = servo2040.SERVO_1
END_PIN = servo2040.SERVO_4
servos = ServoCluster(pio=0, sm=0, pins=list(range(START_PIN, END_PIN + 1)))
# Enable all servos (this puts it at the middle)
# Enable all servos (this puts them at the middle)
servos.enable_all()
time.sleep(2)

Wyświetl plik

@ -0,0 +1,59 @@
import time
import math
import random
from pimoroni import Button
from servo import Servo, servo2040
# Press "Boot" to exit the program.
UPDATES = 50 # How many times to update Servos per second
TIME_FOR_EACH_MOVE = 2 # The time to travel between each random value
UPDATES_PER_MOVE = TIME_FOR_EACH_MOVE * UPDATES
SERVO_EXTENT = 80 # How far from zero to move the servo
USE_COSINE = True # Whether or not to use a cosine path between values
# Create the user button
user_sw = Button(servo2040.USER_SW)
# Create a servo on pin 0
s = Servo(servo2040.SERVO_0)
# Get the initial value and create a random end value between the extents
start_value = s.mid_value()
end_value = random.uniform(-SERVO_EXTENT, SERVO_EXTENT)
update = 0
# Continually move the servo until the user button is pressed
while user_sw.raw() is not True:
# Calculate how far along this movement to be
percent_along = update / UPDATES_PER_MOVE
if USE_COSINE:
# Move the servo between values using cosine
s.to_percent(math.cos(percent_along * math.pi), 1.0, -1.0, start_value, end_value)
else:
# Move the servo linearly between values
s.to_percent(percent_along, 0.0, 1.0, start_value, end_value)
# Print out the value the servo is now at
print("Value = ", round(s.value(), 3), sep="")
# Move along in time
update += 1
# Have we reached the end of this movement?
if update >= UPDATES_PER_MOVE:
# Reset the counter
update = 0
# Set the start as the last end and create a new random end value
start_value = end_value
end_value = random.uniform(-SERVO_EXTENT, SERVO_EXTENT)
time.sleep(1.0 / UPDATES)
# Disable the servo
s.disable()