-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsequence_simple_servo.py
More file actions
46 lines (35 loc) · 1.26 KB
/
Copy pathsequence_simple_servo.py
File metadata and controls
46 lines (35 loc) · 1.26 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
# examples/async/sequence_simple_servo.py
#
# Demonstrates: running a servo through a sequence of moves using sequence()
# API style: async for loop (sequence)
# Sync equivalent: examples/basic/sequence_simple_servo.py
#
# Each tuple: (target_degrees, time_secs, steps, easing[, delay_start])
# delay_start (5th element, optional) pauses before that move begins.
#
# CircuitPython note: asyncio.run(main()) works with adafruit_asyncio v3+.
# On older CircuitPython, use: asyncio.get_event_loop().run_until_complete(main())
import asyncio
import board
import pwmio
from adafruit_motor import servo
from varspeed_async import Vspeed
MIN = 0
MAX = 180
vs = Vspeed(init_position=MIN, result="int", debug=False)
vs.set_bounds(lower_bound=MIN, upper_bound=MAX)
pwm = pwmio.PWMOut(board.D13, duty_cycle=2 ** 15, frequency=50)
my_servo = servo.Servo(pwm)
my_servo.angle = MIN
my_sequence = [
(180, 3.0, 60, "SineEaseInOut"),
(90, 2.0, 40, "QuadEaseOut", 1.0),
(0, 3.0, 60, "SineEaseInOut"),
]
async def main():
print("running servo sequence...")
async for position, running, changed in vs.sequence(my_sequence, loop_max=1):
if changed:
my_servo.angle = position
print("done, now at " + str(vs.position) + " degrees")
asyncio.run(main())