forked from pimoroni/inventorhatmini-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_easing.py
More file actions
76 lines (55 loc) · 2.31 KB
/
simple_easing.py
File metadata and controls
76 lines (55 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import time
import math
import random
from inventorhatmini import InventorHATMini, SERVO_1
"""
An example of how to move a servo smoothly between random positions.
Press "User" to exit the program.
"""
# Constants
UPDATES = 50 # How many times to update Servos per second
UPDATE_RATE = 1 / UPDATES
TIME_FOR_EACH_MOVE = 2 # The time to travel between each random value
UPDATES_PER_MOVE = TIME_FOR_EACH_MOVE * UPDATES
SERVO_EXTENT = 70 # How far from zero to move the servo
USE_COSINE = True # Whether or not to use a cosine path between values
# Create a new InventorHATMini and get a servo from it
board = InventorHATMini(init_leds=False)
s = board.servos[SERVO_1]
# 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
# Sleep until a specific time in the future. Use this instead of time.sleep() to correct for
# inconsistent timings when dealing with complex operations or external communication
def sleep_until(end_time):
time_to_sleep = end_time - time.monotonic()
if time_to_sleep > 0.0:
time.sleep(time_to_sleep)
# Continually move the servo until the user button is pressed
while not board.switch_pressed():
# Record the start time of this loop
start_time = time.monotonic()
# 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)
# Sleep until the next update, accounting for how long the above operations took to perform
sleep_until(start_time + UPDATE_RATE)
# Disable the servo
s.disable()