-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathanimation_explorer.py
executable file
·148 lines (120 loc) · 3.95 KB
/
animation_explorer.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python3
"""
List all Cozmo animations on a web page with buttons to try the animations.
In order to run this script, you also need all the other files inside the project.
If that is the case, running this script will load the interface.
"""
from flask import Flask, render_template, request
import flask_helpers
import cozmo
import json
import random
import time
import logging
logging.basicConfig(format='%(asctime)s animation explorer %(levelname)s %(message)s', level=logging.INFO)
robot = None
cozmoEnabled = True
return_to_pose = False
flask_app = Flask(__name__)
rndID = random.randrange(1000000000, 9999999999)
animations = ''
triggers = ''
behaviors = ''
action = []
pose = None
@flask_app.route('/')
def index():
return render_template('index.html', randomID=rndID, animations=animations, triggers=triggers, behaviors=behaviors)
@flask_app.route('/toggle_pose', methods=['POST'])
def toggle_pose():
global return_to_pose
# Toggle for returning to pose after finishing animation
return_to_pose = not return_to_pose
logging.info('return_to_pose is set to: ' + str(return_to_pose))
return str(return_to_pose)
@flask_app.route('/play_animation', methods=['POST'])
def play_animation():
# Handling of received animation
global pose
animation = json.loads(request.data.decode('utf-8'))
if cozmoEnabled:
pose = robot.pose
robot.play_anim(animation).wait_for_completed()
logging.info('Animation \'' + animation + '\' started')
check_pose_return()
else:
time.sleep(2)
return 'true'
@flask_app.route('/play_trigger', methods=['POST'])
def play_trigger():
# Handling of received trigger
global pose
trigger = json.loads(request.data.decode('utf-8'))
if cozmoEnabled:
pose = robot.pose
robot.play_anim_trigger(getattr(cozmo.anim.Triggers, trigger)).wait_for_completed()
logging.info('Trigger \'' + trigger + '\' started')
check_pose_return()
else:
time.sleep(2)
return 'true'
@flask_app.route('/play_behavior', methods=['POST'])
def play_behavior():
# Handling of received behavior
global pose
global action
behavior = json.loads(request.data.decode('utf-8'))
if cozmoEnabled:
pose = robot.pose
action = [robot.start_behavior(getattr(cozmo.behavior.BehaviorTypes, behavior)), behavior]
logging.info('Behavior \'' + behavior + '\' started')
else:
time.sleep(2)
return 'true'
@flask_app.route('/stop', methods=['POST'])
def stop():
global action
if action is not []:
robot.stop_freeplay_behaviors()
logging.info('behavior \'' + action[1] + '\' stopped')
action = []
check_pose_return()
else:
robot.abort_all_actions()
return 'false'
def check_pose_return():
if return_to_pose:
robot.go_to_pose(pose)
logging.info('Cozmo returning to pose he had before animation started')
def cozmo_program(_robot: cozmo.robot.Robot):
global robot
robot = _robot
try:
global animations
global triggers
global behaviors
for a in robot.conn.anim_names:
animations += a + ','
animations = animations[:-1]
for t in dir(cozmo.anim.Triggers):
if '__' not in t:
triggers += t + ','
triggers = triggers[:-1]
for b in dir(cozmo.behavior.BehaviorTypes):
if '__' not in b:
behaviors += b + ','
behaviors = behaviors[:-1]
logging.info('Attempting to open browser window at 127.0.0.1:5000')
flask_helpers.run_flask(flask_app)
except KeyboardInterrupt:
print("\nExit requested by user")
try:
cozmo.run_program(cozmo_program)
except SystemExit as e:
cozmoEnabled = False
try:
flask_helpers.run_flask(flask_app)
except KeyboardInterrupt:
print("\nExit requested by user")
print('e = "%s"' % e)
print('\nNo Cozmo detected')