def run(): daemon = DaemonManager() daemon.start() # Stuff that should clearly be elsewhere. subprocess.call(['aconnect', '130:0', '129:1']) subprocess.call(['aconnect', '24:0', '129:1']) subprocess.call(['jack_connect', 'fluidsynth:l_00', 'system:playback_1']) subprocess.call(['jack_connect', 'fluidsynth:r_00', 'system:playback_2']) comm = Comm() # Start the proactor thread. We do this after Comm() has been created so # there are connections to manage, otherwise the proactor will just # immediately terminate. proactorThread = threading.Thread(target = getProactor().run) proactorThread.start() try: commands = Commands(comm) win = MyWin(Tk(), commands) win.mainloop() finally: comm.close() proactorThread.join()
duplicates = list() duplicate_boxes = list() classIds_o = list() boxes_o = list() lowest_y_box = None print("Registrering cards on webcam") if not capture_from_webcam: frame = cv2.imread(img_name) blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (1280, 1280), (0, 0, 0), True, crop=False) net.setInput(blob) outs = net.forward(output_layers) postprocess(frame, outs) cv2.imwrite("./registrered.png", frame) comm.send(registrer_piles(frame.shape[1])) # cv2.imwrite("final.png", image) print("Final image saved") except: loop = True print("Error in recognition, taking image again") ret, frame = cap.read() cv2.waitKey(50) comm.close() cap.release() cv2.destroyAllWindows()
class Robot: def __init__(self, x, y, heading): self.x = x self.y = y self.heading = heading self.weed_volume = 0 self.manual_control = True self.auto_turn_chance = 0.1 self.auto_turn_amount = 15 self.auto_move_amount = 10 self.auto_col_turn_max = 90 self.communicator = Comm(12345, 12346) self.start() def start(self): while True: while self.communicator.has_read_data(): data = self.communicator.read( ) # cmd will be a JSON object from the controller. cmd = json.loads(data) self.process(cmd) if not self.manual_control: self.auto() self.communicator.send(self.get_state_string()) time.sleep(1) # cmd is a JSON string. def process(self, cmd): print("Processing {0}".format(cmd)) # Receive message to change control type if cmd["type"] == "mode_msg": if cmd["mode"] == "manual": print("Setting manual control mode") self.manual_control = True elif cmd["mode"] == "auto": print("Setting autonomous control mode") self.manual_control = False else: print("Unsupported control mode") # Receive message to apply manual controls elif cmd["type"] == "ctrl_msg": self.move(int(cmd["move"])) # Read move amount from json self.turn(int(cmd["turn"])) # Read turn amount from json else: print("Unsupported message type") # Move the robot around autonomously def auto(self): if random.random() < self.auto_turn_chance: dir = 1 if random.randrange(2) == 1 else -1 # + left, - right self.turn(self.auto_turn_amount * dir) self.move(self.auto_move_amount) self.fix_collision() def fix_collision(self): # Keep reversing and turning until the robot is free while self.detect_collision(): self.move(-self.auto_move_amount) self.turn(random.randrange(self.auto_col_turn_max)) def detect_collision(self): b = self.x < 0 or self.x > 100 or self.y < 0 or self.y > 100 if b: print("Collision Detected!") print("Collision Detected!") print("Collision Detected!") return b def move(self, amount): radHeading = self.heading * math.pi / 180 self.x += amount * math.cos(radHeading) self.y += amount * math.sin(radHeading) weeds = random.randrange(5) self.weed_volume += weeds print("Robot collected {0}".format(weeds)) def turn(self, amount): self.heading += amount self.heading %= 360 def get_state_string(self): dict = { "timestamp": str(time.ctime()), "position_x": str(self.x), "position_y": str(self.y), "heading": str(self.heading), "weeds": str(self.weed_volume), } s = "{" for key in dict: s += "\"" + key + "\":\"" + str(dict[key]) + "\", " s = s[:-2] s += "}" return s def shutdown(self): self.communicator.close()