This repository has been archived by the owner on May 29, 2019. It is now read-only.
/
RobotController.py
290 lines (250 loc) · 10.8 KB
/
RobotController.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python
from Vision_SSD300 import Vision
from Navigator import Navigator
from QRReader import QRReader
import threading
import logging as log
import sys
from scheduler import Scheduler, Event
from remote import Remote, RPCType
import config
import asyncio
import os
import time
import base64
import cv2
from serial_io import SerialIO
import json
class RobotController:
model_xml = '/home/student/ssd300.xml'
model_bin = '/home/student/ssd300.bin'
def __init__(self):
self.vision = Vision(
RobotController.model_xml,
RobotController.model_bin,
self,
is_headless=True,
live_stream=True,
confidence_interval=0.5)
self.received_frame = None
self.qr_reader = QRReader()
self.last_qr_approached = None
self.current_qr_approached = None
self.approach_complete = True
self.retrying_approach = False
self.standby_mode = True
self.standby_invoked = True
self.serial_io = SerialIO('/dev/ttyACM0', 115200, self)
self.actions = {}
self.watered = False
if config.RESPOND_TO_API:
host = config.API_HOST
if config.API_SECURE:
host = "wss://"+host
else:
host = "ws://"+host
self.remote = Remote(config.UUID, host)
self.remote.add_callback(
RPCType.MOVE_IN_DIRECTION, self.remote_move)
self.remote.add_callback(
RPCType.EVENTS, self.on_events_received)
self.remote.add_callback(
RPCType.SET_STANDBY, self.set_standby)
rm_thread = threading.Thread(target=self.thread_remote,
name="remote", daemon=True)
rm_thread.start()
# rm_thread.join()
# Create the navigation system
self.navigator = Navigator(self, verbose=True)
threading.Thread(target=self.vision.start, name="vision").start()
def remote_move(self, direction):
self.navigator.remote_move(direction)
def run_event(self, event):
if self.standby_mode and len(self.actions.keys()) == 0:
self.set_standby(False, justMove=True)
for action in event.actions:
pid = action["plant_id"]
if pid not in self.actions:
self.actions[pid] = []
self.actions[pid].append(action["name"])
def thread_remote(self):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
self.sched = Scheduler()
self.sched.run_event_cb = self.run_event
loop.run_until_complete(self.remote.connect())
def enabled(self):
return len(self.actions) > 0 or not self.standby_mode
def clean_actions(self):
new_actions = {}
for key in self.actions:
if self.actions[key] != []:
new_actions[key] = self.actions[key]
self.actions = new_actions
def process_visual_data(self, predictions, frame):
"""
Forwards messages to navigator instance.
:param predictions: List of predictions produced by the VPU
:return:
"""
# If the standby is currently undergoing, but standby mode is False, stop standby mode here
if self.standby_invoked and not self.standby_mode:
self.standby_invoked = False
self.random_search_mode = True
self.navigator.remote_motor_controller.random()
self.standby_invoked = False
# If the sensor's last read time is long enough (1 hour), attempt to read the sensor
if time.time() - self.serial_io.sensor_last_read > 3600 and not self.serial_io.value_reading:
threading.Thread(name="serial_read", target=self.serial_io.read_value).start()
if self.enabled():
log.info("self.actions: {}, standby_mode: {}".format(self.actions, self.standby_mode))
self.clean_actions()
self.received_frame = frame
self.navigator.on_new_frame(predictions)
else:
if self.standby_mode:
log.info("\033[0;34m[Pi] Standby mode flag detected")
if not self.approach_complete:
log.info("\033[1;37;44m[Pi] Robot approaching, ignoring flag")
elif self.retrying_approach:
log.info("\033[1;37;44m[Pi] Robot retrying approach, ignoring flag")
else:
if not self.standby_invoked:
self.navigator.remote_motor_controller.stop()
log.info("\033[1;37;44m[Pi] Invoking standby mode")
# Any other switches to flip?
# Reset read QR codes
self.current_qr_approached = None
self.last_qr_approached = None
# Stop the motor
self.standby_invoked = True
elif len(self.actions) == 0:
log.info("\033[0;34m[Pi] Robot has no event left to complete")
# Stop immediately? Wait until the jobs to finish to stop?
def read_qr_code(self):
# Read the QR code
tries = 3
qr_codes = self.qr_reader.identify(self.received_frame)
while tries > 0:
if len(qr_codes) == 0:
log.warning("No plant QR found.")
tries -= 1
else:
for qr in qr_codes:
self.current_qr_approached = qr
log.info("Plant QR found: {}".format(qr))
break
def on_plant_found(self):
# Send message to initiate approach command, until instructed to continue
if self.current_qr_approached is None:
log.warning("No QR found, retrying approach")
self.retrying_approach = True
self.navigator.remote_motor_controller.retry_approach()
elif self.current_qr_approached.startswith("gbpl:"):
# Parse the QR
plant_id = int(self.current_qr_approached[5:])
if not self.standby_invoked:
# If robot is not in standby mode, go forward anyways
self.approach_complete = False
self.navigator.remote_motor_controller.approached()
elif plant_id in self.actions:
if len(self.actions[plant_id]) == 0:
log.info("Plant {} has no task left to complete, leaving...".format(str(plant_id)))
self.last_qr_approached = self.current_qr_approached
self.current_qr_approached = None
self.navigator.remote_motor_controller.approach_escape()
else:
self.approach_complete = False
if "PLANT_WATER" in self.actions[plant_id] or not self.standby_invoked:
self.navigator.remote_motor_controller.approached()
else:
self.navigator.remote_motor_controller.approached(raise_arm=False)
else:
log.info("Plant {} has no task assigned, leaving...".format(str(plant_id)))
self.last_qr_approached = self.current_qr_approached
self.current_qr_approached = None
self.navigator.remote_motor_controller.approach_escape()
else:
log.warning("Invalid QR code {}".format(self.current_qr_approached))
self.last_qr_approached = self.current_qr_approached
self.current_qr_approached = None
self.navigator.remote_motor_controller.approach_escape()
def on_approach_complete(self):
# Take a picture here
plant_id = None
if self.current_qr_approached is not None:
if self.current_qr_approached.startswith("gbpl:"):
plant_id = int(self.current_qr_approached[5:])
if "PLANT_CAPTURE_PHOTO" in self.actions.get(plant_id, []) or not self.standby_invoked:
self.remote.plant_capture_photo(int(self.current_qr_approached[5:]), base64.b64encode(cv2.imencode(".jpg", self.received_frame)[1]).decode("utf-8"))
else:
log.warning("[Pi] No QR code found during this approach, photo will not be sent.")
self.last_qr_approached = self.current_qr_approached
self.current_qr_approached = None
try:
if plant_id is not None:
if self.watered:
self.actions[plant_id].remove("PLANT_WATER")
self.watered = False
if self.actions[plant_id] == []:
self.actions.pop(plant_id, None)
self.actions[plant_id].remove("PLANT_CAPTURE_PHOTO")
except:
pass
finally:
self.navigator.remote_motor_controller.approach_escape()
def on_approach_escape_complete(self):
self.navigator.random_search_mode = True # Flip on the random search
self.navigator.remote_motor_controller.random_walk()
self.clean_actions()
self.approach_complete = True
def on_retry_complete(self):
self.retrying_approach = False
self.navigator.approach_frame_counter = 8
def on_plant_seen(self):
pass
def on_events_received(self, data):
events = list(map(Event.from_dict, data))
non_ephemeral = []
for e in events:
if e.ephemeral:
self.run_event(e)
else:
non_ephemeral.append(e)
self.sched.push_events(non_ephemeral)
pass
def set_standby(self, mode, justMove=False):
if mode:
self.standby_mode = True
while not hasattr(self, "navigator"):
pass
self.navigator.remote_motor_controller.stop()
return
while not hasattr(self, "navigator"):
pass
# Start random search
self.navigator.random_search_mode = True
self.navigator.remote_motor_controller.random_walk()
if not justMove:
# Turn off standby mode
self.standby_mode = False
def get_state(self):
if self.standby_mode:
return "Standby Mode"
elif self.retrying_approach:
return "Retrying approach"
elif self.navigator.get_random_search_mode():
return "Random Search Mode"
elif self.navigator.get_follow_mode():
return "Follow Mode"
elif self.navigator.get_escape_mode():
return "Escape Mode"
def main():
if os.getenv("http_proxy") is not None:
print("You are a monster.")
print("Use start.sh. Do not run this Python file yourself.")
return
log.basicConfig(format="[ %(asctime)s ] [ %(levelname)s ] %(message)s\033[0m", level=log.DEBUG, stream=sys.stdout)
RobotController()
if __name__ == "__main__":
main()