-
Notifications
You must be signed in to change notification settings - Fork 1
/
Planner.py
166 lines (124 loc) · 5.18 KB
/
Planner.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import asyncio
import cozmo
from Common.woc import WOC
from Common.colors import Colors
from os import system
import random
import _thread
import sys
from threading import Timer
import speech_recognition as sr
import os
from datetime import datetime
from dateutil import parser
import pytz
from GoogleCalendar import GoogleCalendar
# pip3 install python-dateutil
# pip3 install pyowm
'''
Planner Module
@class Planner
@author - Team Wizards of Coz
'''
#SET UP THE GOOGLE PROJECT FIRST AND DOWNLOAD THE CLIENT_ID JSON TO THIS FOLDER
SCOPES = 'https://www.googleapis.com/auth/calendar.readonly'
CLIENT_SECRET_FILE = '<CLIENT SECRET JSON FILE LOCATION>'
APPLICATION_NAME = '<APPLICATION_NAME>'
class Planner(WOC):
cl = None
exit_flag = False
audioThread = None
cubes = None
timeRemaining = 100
curEvent = None
owm = None
calendar = None
idleAnimations = ['anim_sparking_idle_03','anim_sparking_idle_02','anim_sparking_idle_01']
attractAttentionAnimations = ['anim_keepaway_pounce_02','reacttoblock_triestoreach_01']
animCtr = 0
face = None
faceFound = False
messageDelivered = False
def __init__(self, *a, **kw):
sys.setrecursionlimit(0x100000)
cozmo.setup_basic_logging()
cozmo.connect(self.startResponding)
def startResponding(self, coz_conn):
asyncio.set_event_loop(coz_conn._loop)
self.coz = coz_conn.wait_for_robot()
self.playIdle()
self.audioThread = _thread.start_new_thread(self.startAudioThread, ())
while not self.exit_flag:
asyncio.sleep(0)
self.coz.abort_all_actions()
def accessGoogleCalendar(self):
# GOOGLE CALENDAR
self.calendar = GoogleCalendar(SCOPES,CLIENT_SECRET_FILE,APPLICATION_NAME,TZ)
self.calendar.pollCalendar()
event,timeToEvent = self.calendar.todaysEventAndTimeToEvent()
if event is not None:
start = event['start'].get('dateTime', event['start'].get('date'))
# print(start)
if timeToEvent < self.timeRemaining:
self.coz.play_anim('anim_sparking_getin_01').wait_for_completed()
self.findFaceAndInform(timeToEvent)
def findFaceAndInform(self,timeToEvent):
find_face = self.coz.start_behavior(cozmo.behavior.BehaviorTypes.FindFaces)
try:
self.face = self.coz.world.wait_for_observed_face(timeout=5)
print("Found a face!", self.face)
find_face.stop()
except asyncio.TimeoutError:
find_face.stop()
self.coz.say_text("Look at me!",duration_scalar=1.5,voice_pitch=-1,in_parallel=True).wait_for_completed()
self.coz.play_anim(random.choice(self.attractAttentionAnimations)).wait_for_completed()
self.findFaceAndInform(timeToEvent)
if self.faceFound == False:
self.faceFound = True
if self.face is not None:
find_face.stop()
self.coz.play_anim("anim_greeting_happy_01").wait_for_completed()
self.coz.say_text("You have a meeting in "+str(timeToEvent)+" minutes ! Check you calendar!",duration_scalar=2,voice_pitch=0,in_parallel=True).wait_for_completed()
self.messageDelivered = True
def startAudioThread(self):
try:
print("Take input");
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(self.startListening())
except Exception as e:
print(e)
async def startListening(self):
if self.faceFound:
print("Taking input");
r = sr.Recognizer()
r.energy_threshold = 5000
print(r.energy_threshold)
with sr.Microphone(chunk_size=512) as source:
audio = r.listen(source)
try:
speechOutput = r.recognize_google(audio)
if self.messageDelivered == True:
self.processSpeech(speechOutput)
await asyncio.sleep(1);
await self.startListening()
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
await asyncio.sleep(0);
await self.startListening()
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
def processSpeech(self,speechOutput):
print(speechOutput)
if 'thanks' in speechOutput or 'thank' in speechOutput:
self.coz.play_anim("anim_greeting_happy_01").wait_for_completed()
self.messageDelivered = False
def playIdle(self):
self.coz.play_anim(self.idleAnimations[self.animCtr]).wait_for_completed()
self.animCtr += 1
if self.animCtr==3:
self.accessGoogleCalendar()
else:
self.playIdle()
if __name__ == '__main__':
Planner()