-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_behaviour.py
More file actions
executable file
·53 lines (40 loc) · 1.75 KB
/
async_behaviour.py
File metadata and controls
executable file
·53 lines (40 loc) · 1.75 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
import anki_vector
from anki_vector.util import degrees
"""
This program is to test my understanding of Vector's asynchronous behaviour.
It turns out it is not as complicated as I first thought (I think!).
The most important finding is to control the flow of the program so it doesn't
start then immediately finish before any task has completed.
This program asks Vector to spin around twice whilst speaking.
"""
#Define main program
def main():
#Get Vectors serial number
args = anki_vector.util.parse_command_args()
#Create an AsyncRobot instance
with anki_vector.AsyncRobot(args.serial) as robot:
#Move Vector off his charger. The .result() is a blocking function ensuring
#this action is completed before moving on to the next step
print("Driving off charger...")
robot.behavior.drive_off_charger().result()
#Two asynchronous actions... Chew gum and walk;)
#1
print("Turn in place...")
action = robot.behavior.turn_in_place(degrees(720), speed=degrees(80))
#2
print("Speak text...")
robot.say_text("Weee! This is fun")
#This step ensures the actions complete before the program has ended
while True:
print("Running...")
#Break out of loop and end program once Vector has finished spinning
if action.done():
print("Action complete!")
break
#Or touch Vectors back to stop him mid spin
if robot.touch.last_sensor_reading.is_being_touched:
print("Has been touched!")
break
print("Complete")
#Run main program
main()