forked from DarkCasterX/ErrselaPie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errpy.py
214 lines (181 loc) · 6.88 KB
/
errpy.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import requests
import time
#Helper class to contain all of Errsela's functionality
class errsela:
def __init__(self):
print("Initializing errsela client...\n")
self.success_status = "SS_SUCCESS"
#Right motor power
self.right_motor = 180
#Left motor power
self.left_motor = 180
#Speech command for Errsela to talk
self.speech = ''
#Eye 1 power
self.servo1 = 180
#Eye 2 power
self.servo2 = 180
#Duration of movement
self.duration = 0
#Base URL which will be concatenated for every request
self.url= 'http://nyitetic.nyit.edu/errsela/botcommand.aspx?'
print("Initialized.")
#Prints the current success status
def print_success_status(self):
print("\nPrevious operation's success status: " + self.success_status + "\n")
return self
#Shows response information
def showresponseinfo(self, command):
print('\nResponse Info:\n')
print(command.status_code)
print(command.headers)
#Function to handle commands and send to errsela
def sendcommand(self):
if(self.success_status == "SS_FAILURE"):
self.print_success_status()
print('Sending commands to ERRSELA...\n')
try:
command = requests.get(self.url + 'rm=' + str(self.right_motor) + '&lm=' + str(self.left_motor) + '&speech=' + str(self.speech) + '&servo1=' + str(self.servo1) + '&servo2=' + str(self.servo2) + '&duration=' + str(self.duration))
print('Command successfuly sent.\n')
#Handle an unsatisfactory response
if(command.status_code != 200):
print('\nCommand wasn\'t properly received by ERRSELA.')
self.showresponseinfo(command)
yesorno = input('Would you like to resend this commmand? Enter \'Yes\' to resend, otherwise enter \'No\' ').lower()
if(yesorno == 'yes'):
print('You decided to resend the command.\nResending...')
command = requests.get(self.url + 'rm=' + str(self.right_motor) + '&lm=' + str(self.left_motor) + '&speech=' + str(self.speech) + '&servo1=' + str(self.servo1) + '&servo2=' + str(self.servo2) + '&duration=' + str(self.duration))
print('Command resent.')
if(yesorno == 'no'):
print('Cancelled the command.')
return self
except Exception as e:
self.success_status = "SS_FAILURE"
print('Something went wrong...')
choice = input("Would you like to see the error output?").lower()
if(choice == 'yes'):
print(e)
else:
print("Thoroughly review your code. This is a critical error in a builtin command.\n")
print("It is highly encouraged to look at the command's success status\n")
print("And look at the error output.\n")
self.getdefaults()
return 0
#Resets all values to their defaults, helps prevent
#Accidentally reusing a value or command
def getdefaults(self):
try:
self.right_motor = 180
self.left_motor = 180
self.speech =''
self.servo1 = 180
self.servo2 = 180
self.duration = 0
return self
except:
print("Failed to reset values to their defaults. Returning...")
return 0
#Send a speech command to Errsela
def talk(self, message):
try:
self.speech = message
return self.sendcommand()
except:
print("Command failed.")
self.success_status = "SS_FAILURE"
return self
#Commands Errsela to move in one direction for an amount of time
def moveforward(self, time):
try:
self.duration = time
self.right_motor = 0
self.left_motor = 0
return self.sendcommand()
except:
print("Command failed.")
self.success_status = "SS_FAILURE"
return self
#Commands Errsela to turn right
#Note: You can control the angle which Errsela turns by modifying the power
#And duration arguments when calling this function
def turnright(self, power=180, duration=3):
try:
self.right_motor = 180
self.left_motor = 180 - power
self.duration = 3
return self.sendcommand()
except:
print("Command failed.")
self.success_status = "SS_FAILURE"
return self
#Commands Errsela to turn left
#Note: You can control the angle which Errsela turns by modifying the power
#And duration arguments when calling this function
def turnleft(self, power=180, duration=3):
try:
self.right_motor = 180 - power
self.left_motor = 180
self.duration = 3
return self.sendcommand()
except:
print("Command failed.")
self.success_status = "SS_FAILURE"
return self
#Commands Errsela to make a U-turn towards the right
def turnaroundr(self):
try:
self.right_motor = 0
self.left_motor = 180
self.duration = 5
return self.sendcommand()
except:
print("Command failed.")
self.success_status = "SS_FAILURE"
return self
#Commands Errsela to make a U-turn towards the left
def turnaroundl(self):
try:
self.right_motor = 180
self.left_motor = 0
self.duration = 5
return self.sendcommand()
except:
print("Command failed.")
self.success_status = "SS_FAILURE"
return self
#Dims Errsela's lights
def blink(self, times):
try:
time.sleep(2.5)
counter = 0
if(counter < times):
self.servo1 = 0
self.servo2 = 0
self.blink(times - 1)
return self.sendcommand()
else:
return self
except:
print("Command failed.")
self.success_status = "SS_FAILURE"
return self
#Dims one of Errsela's lights
def winkl(self):
try:
self.servo1 = 0
self.servo2 = 180
return self.sendcommand()
except:
print("Command failed.")
self.success_status = "SS_FAILURE"
return self
#Dims one of Errsela's lights
def winkr(self):
try:
self.servo1 = 180
self.servo2 = 0
return self.sendcommand()
except:
print("Command failed.")
self.success_status = "SS_FAILURE"
return self