-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathobimovement.py
264 lines (224 loc) · 7.93 KB
/
obimovement.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import obi, time, csv, sys, os
from playsound import playsound
path = os.path.dirname(os.path.abspath(__file__))
with open(path + '/saved-positions/bowls.csv', 'r') as read_obj:
csv_reader = csv.reader(read_obj)
BOWL_COORDS = list(csv_reader)
with open(path + '/saved-positions/bowl0-scoop-refined.csv', 'r') as read_obj:
csv_reader = csv.reader(read_obj)
SCOOP_0 = list(csv_reader)[:-1]
with open(path + '/saved-positions/bowl1-scoop-refined.csv', 'r') as read_obj:
csv_reader = csv.reader(read_obj)
SCOOP_1 = list(csv_reader)[:-1]
with open(path + '/saved-positions/bowl2-scoop-refined.csv', 'r') as read_obj:
csv_reader = csv.reader(read_obj)
SCOOP_2 = list(csv_reader)[:-1]
with open(path + '/saved-positions/bowl3-scoop-refined.csv', 'r') as read_obj:
csv_reader = csv.reader(read_obj)
SCOOP_3 = list(csv_reader)[:-1]
with open(path + '/saved-positions/bowl0-scrape-refined.csv', 'r') as read_obj:
csv_reader = csv.reader(read_obj)
SCRAPE_0 = list(csv_reader)
with open(path + '/saved-positions/bowl1-scrape-refined.csv', 'r') as read_obj:
csv_reader = csv.reader(read_obj)
SCRAPE_1 = list(csv_reader)
with open(path + '/saved-positions/bowl2-scrape-refined.csv', 'r') as read_obj:
csv_reader = csv.reader(read_obj)
SCRAPE_2 = list(csv_reader)
with open(path + '/saved-positions/bowl3-scrape-refined.csv', 'r') as read_obj:
csv_reader = csv.reader(read_obj)
SCRAPE_3 = list(csv_reader)
with open(path + '/saved-positions/bowl0-scoop-deep-refined.csv', 'r') as read_obj:
csv_reader = csv.reader(read_obj)
DEEPSCOOP_0 = list(csv_reader)[:-1]
with open(path + '/saved-positions/bowl1-scoop-deep-refined.csv', 'r') as read_obj:
csv_reader = csv.reader(read_obj)
DEEPSCOOP_1 = list(csv_reader)[:-1]
with open(path + '/saved-positions/bowl2-scoop-deep-refined.csv', 'r') as read_obj:
csv_reader = csv.reader(read_obj)
DEEPSCOOP_2 = list(csv_reader)[:-1]
with open(path + '/saved-positions/bowl3-scoop-deep-refined.csv', 'r') as read_obj:
csv_reader = csv.reader(read_obj)
DEEPSCOOP_3 = list(csv_reader)[:-1]
with open(path + '/saved-positions/mouth-pos.csv', 'r') as read_obj:
csv_reader = csv.reader(read_obj)
MOUTHPOS = list(csv_reader)[0]
class ObiMovement():
def __init__(self):
self.robot = obi.Obi('/dev/cu.usbserial-FTBXXIGY')
self.bowlno = 0
self.speed = 2
self.accel = 2
self.mouthpos = MOUTHPOS #[21274, 17098, 15270, 17810, 19902, 26628] #[23146,15515,18839,19867,18021,26601]
self.scoop_depth = 0
self.flag = 0
print(self.robot.SerialIsOpen())
print(self.robot.VersionInfo())
self.robot.Wakeup()
self.robot.WaitForCMUResponse()
print("I'm up!")
def start(self):
self.flag = 0
print("Started")
def pause_indefinitely(self):
self.flag = 1
print("Paused")
def stop(self):
self.flag = 2
print("Stopped")
playsound(path + "/sounds/stop.mp3")
def time_delay(self, secs):
self.check_for_code()
if self.flag == 2:
return
while self.flag == 1:
time.sleep(.2)
self.check_for_code()
print("Waiting for " + str(secs) + " seconds.")
time.sleep(secs)
def cap_speed_and_accel(self):
if self.speed > 5:
self.speed = 5
elif self.speed < 0:
self.speed = 0
if self.accel > 5:
self.accel = 5
elif self.accel < 0:
self.accel = 0
def check_for_code(self):
with open('obi-code.txt', 'r') as f:
lines = f.readlines()
with open('obi-code.txt', 'w') as f:
for line in lines:
mod_line = line.strip('\n ')
mod_line = mod_line.replace('obirobot.', 'self.')
if mod_line in ['self.start()', 'self.pause_indefinitely()', 'self.stop()']:
ldict = {'self':self}
exec(mod_line, globals(), ldict)
elif 'speed' in line or 'accel' in line or 'scoop_depth' in line:
try:
ldict = {'self':self}
exec(mod_line, globals(), ldict)
except:
print("Code (being excecuted within class): " + mod_line)
if sys.exc_info()[0] != SyntaxError:
print("This code threw the following error: " + str(sys.exc_info()[0]) + ".")
else:
f.write(line)
with open(path + '/obi-code.txt', 'r') as f:
code = f.read()
if code != "":
self.stop()
def scoop_from_bowlno(self, bowlno="previous"):
self.check_for_code()
if self.flag == 2:
return
while self.flag == 1:
time.sleep(.2)
self.check_for_code()
self.cap_speed_and_accel()
print(f"Scooping from bowl {str(self.bowlno)} at max speed {self.speed} and max accel {self.accel} with {'deep scoops' if self.scoop_depth == 1 else 'shallow scoops'}")
playsound(path + "/sounds/scoop.mp3")
if self.scoop_depth == 1:
if bowlno == 0:
waypoints = DEEPSCOOP_0
elif bowlno == 1:
waypoints = DEEPSCOOP_1
elif bowlno == 2:
waypoints = DEEPSCOOP_2
else:
waypoints = DEEPSCOOP_3
else:
if bowlno == 0:
waypoints = SCOOP_0
elif bowlno == 1:
waypoints = SCOOP_1
elif bowlno == 2:
waypoints = SCOOP_2
else:
waypoints = SCOOP_3
if bowlno != "previous":
self.bowlno = bowlno
for i in range(9):
waypoint = waypoints[i] + [self.speed*2000, self.accel*6000, 0]
self.robot.SendOnTheFlyWaypointToObi(i, waypoint)
self.robot.ExecuteOnTheFlyPath()
self.robot.WaitForCMUResponse()
def scrape_then_scoop_bowlno(self, bowlno="previous"):
self.check_for_code()
if self.flag == 2:
return
while self.flag == 1:
time.sleep(.2)
self.check_for_code()
self.cap_speed_and_accel()
if bowlno != "previous":
self.bowlno = bowlno
print(f"Scraping down bowl {str(self.bowlno)} at max speed {self.speed} and max accel {self.accel}")
playsound(path + "/sounds/scrape.mp3")
if self.bowlno == 0:
waypoints = SCRAPE_0
elif self.bowlno == 1:
waypoints = SCRAPE_1
elif self.bowlno == 2:
waypoints = SCRAPE_2
else:
waypoints = SCRAPE_3
for i in range(9):
waypoint = waypoints[i] + [self.speed*2000, self.accel*6000, 0]
self.robot.SendOnTheFlyWaypointToObi(i, waypoint)
self.robot.ExecuteOnTheFlyPath()
self.robot.WaitForCMUResponse()
print(f"Scooping from bowl {str(self.bowlno)} at max speed {self.speed} and max accel {self.accel} with {'deep scoops' if self.scoop_depth == 1 else 'shallow scoops'}")
playsound(path + "/sounds/scoop.mp3")
if self.scoop_depth == 1:
if bowlno == 0:
waypoints = DEEPSCOOP_0
elif bowlno == 1:
waypoints = DEEPSCOOP_1
elif bowlno == 2:
waypoints = DEEPSCOOP_2
else:
waypoints = DEEPSCOOP_3
else:
if bowlno == 0:
waypoints = SCOOP_0
elif bowlno == 1:
waypoints = SCOOP_1
elif bowlno == 2:
waypoints = SCOOP_2
else:
waypoints = SCOOP_3
if bowlno == 0:
waypoints[0] = SCRAPE_0[-1]
elif bowlno == 1:
waypoints[0] = SCRAPE_1[-1]
elif bowlno == 2:
waypoints[0] = SCRAPE_2[-1]
else:
waypoints[0] = SCRAPE_3[-1]
if bowlno != "previous":
self.bowlno = bowlno
for i in range(9):
waypoint = waypoints[i] + [self.speed*2000, self.accel*6000, 0]
self.robot.SendOnTheFlyWaypointToObi(i, waypoint)
self.robot.ExecuteOnTheFlyPath()
self.robot.WaitForCMUResponse()
def move_to_mouth(self):
self.check_for_code()
if self.flag == 2:
return
while self.flag == 1:
time.sleep(.2)
self.check_for_code()
self.cap_speed_and_accel()
print(f"Moving to mouth at max speed {self.speed} and max accel {self.accel}")
waypoint = self.mouthpos + [self.speed*2000, self.accel*6000, 0]
self.robot.SendOnTheFlyWaypointToObi(0, waypoint)
self.robot.ExecuteOnTheFlyPath()
self.robot.WaitForCMUResponse()
def close(self):
self.robot.GoToSleep()
self.robot.Close()
print(self.robot.SerialIsOpen())
print("All done")