|
| 1 | +import time |
| 2 | +import json |
| 3 | +import threading |
| 4 | +import zmq |
| 5 | +from nav_service import NavigationService |
| 6 | + |
| 7 | +class ChoreExecutor: |
| 8 | + def __init__(self): |
| 9 | + self.nav = NavigationService() |
| 10 | + self.nav.start() |
| 11 | + |
| 12 | + self.running = False |
| 13 | + self.current_chore = None |
| 14 | + self.chore_thread = None |
| 15 | + |
| 16 | + # Robot State |
| 17 | + self.detections = {} |
| 18 | + |
| 19 | + # Subscribe to obs for detections |
| 20 | + self.nav.sub_socket.setsockopt(zmq.SUBSCRIBE, b"") |
| 21 | + # We need a way to read detections. NavigationService already reads obs. |
| 22 | + # Let's piggyback or just read from nav service state? |
| 23 | + # Nav service only stores pose and rooms. |
| 24 | + # Let's subclass or monkeypatch NavService to expose detections, |
| 25 | + # OR just read from the socket in NavService and store it. |
| 26 | + # Actually, let's just modify NavService to store full obs or detections. |
| 27 | + |
| 28 | + def start_chore(self, chore_name): |
| 29 | + if self.running: |
| 30 | + print("[CHORE] Already running a chore.") |
| 31 | + return |
| 32 | + |
| 33 | + self.running = True |
| 34 | + self.current_chore = chore_name |
| 35 | + self.chore_thread = threading.Thread(target=self._run_chore, args=(chore_name,)) |
| 36 | + self.chore_thread.start() |
| 37 | + |
| 38 | + def stop_chore(self): |
| 39 | + self.running = False |
| 40 | + if self.chore_thread: |
| 41 | + self.chore_thread.join() |
| 42 | + self.nav.stop() |
| 43 | + |
| 44 | + def _run_chore(self, chore_name): |
| 45 | + print(f"[CHORE] Starting: {chore_name}") |
| 46 | + |
| 47 | + if chore_name == "clean_up": |
| 48 | + self._do_clean_up() |
| 49 | + else: |
| 50 | + print(f"[CHORE] Unknown chore: {chore_name}") |
| 51 | + |
| 52 | + self.running = False |
| 53 | + print(f"[CHORE] Finished: {chore_name}") |
| 54 | + |
| 55 | + def _do_clean_up(self): |
| 56 | + # 1. Go to Kitchen |
| 57 | + print("[CHORE] Step 1: Go to Kitchen") |
| 58 | + self.nav.go_to_room("Kitchen") |
| 59 | + self._wait_until_idle() |
| 60 | + |
| 61 | + # 2. Look for Trash |
| 62 | + print("[CHORE] Step 2: Scan for Trash") |
| 63 | + trash = self._wait_for_detection("trash") |
| 64 | + |
| 65 | + if trash: |
| 66 | + print(f"[CHORE] Found trash at {trash.get('box')}!") |
| 67 | + # 3. Pick it up (Mock action) |
| 68 | + print("[CHORE] Step 3: Picking up trash...") |
| 69 | + time.sleep(2) |
| 70 | + |
| 71 | + # 4. Go to Bin (Let's say Bin is in Hallway) |
| 72 | + print("[CHORE] Step 4: Go to Hallway (Trash Bin)") |
| 73 | + self.nav.go_to_room("Hallway") |
| 74 | + self._wait_until_idle() |
| 75 | + |
| 76 | + # 5. Drop it |
| 77 | + print("[CHORE] Step 5: Dropping trash") |
| 78 | + time.sleep(1) |
| 79 | + else: |
| 80 | + print("[CHORE] No trash found.") |
| 81 | + |
| 82 | + def _wait_for_detection(self, label, timeout=5.0): |
| 83 | + # Poll self.nav.detections |
| 84 | + start = time.time() |
| 85 | + while time.time() - start < timeout: |
| 86 | + dets = self.nav.detections |
| 87 | + for cam, items in dets.items(): |
| 88 | + for item in items: |
| 89 | + if item.get("label") == label: |
| 90 | + return item |
| 91 | + time.sleep(0.1) |
| 92 | + return None |
| 93 | + |
| 94 | + def _wait_until_idle(self): |
| 95 | + # Wait for nav service to report idle |
| 96 | + # (It might briefly be idle before starting move, so wait a tiny bit first if needed) |
| 97 | + time.sleep(0.5) |
| 98 | + while not self.nav.is_idle(): |
| 99 | + time.sleep(0.1) |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + executor = ChoreExecutor() |
| 103 | + |
| 104 | + # Wait for connection |
| 105 | + time.sleep(2) |
| 106 | + |
| 107 | + print("Available Rooms:", executor.nav.rooms.keys()) |
| 108 | + |
| 109 | + executor.start_chore("clean_up") |
| 110 | + |
| 111 | + # Keep main thread alive |
| 112 | + while executor.running: |
| 113 | + time.sleep(1) |
| 114 | + |
| 115 | + executor.stop_chore() |
0 commit comments