Update deployer/simulator_interface.py
Browse files- deployer/simulator_interface.py +86 -34
deployer/simulator_interface.py
CHANGED
|
@@ -1,43 +1,95 @@
|
|
| 1 |
-
# simulator_interface.py -
|
| 2 |
-
|
| 3 |
-
# This version is placeholder logic using logging only. Replace with PyBullet/Isaac Sim for realism.
|
| 4 |
-
|
| 5 |
import time
|
| 6 |
import logging
|
|
|
|
| 7 |
|
| 8 |
-
logging
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
class VirtualRobot:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def __init__(self):
|
| 12 |
self.state = "IDLE"
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
def
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
-
def
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
logging.
|
| 37 |
-
return "
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# simulator_interface.py - Enhanced Virtual Robot Implementation
|
|
|
|
|
|
|
|
|
|
| 2 |
import time
|
| 3 |
import logging
|
| 4 |
+
from typing import Optional
|
| 5 |
|
| 6 |
+
# Configure logging
|
| 7 |
+
logging.basicConfig(
|
| 8 |
+
level=logging.INFO,
|
| 9 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
| 10 |
+
handlers=[logging.StreamHandler()]
|
| 11 |
+
)
|
| 12 |
|
| 13 |
class VirtualRobot:
|
| 14 |
+
"""Enhanced virtual robot with state management and error handling."""
|
| 15 |
+
|
| 16 |
+
VALID_STATES = {"IDLE", "WAVING", "SPEAKING", "ERROR"}
|
| 17 |
+
VALID_ACTIONS = {"wave", "say"}
|
| 18 |
+
|
| 19 |
def __init__(self):
|
| 20 |
self.state = "IDLE"
|
| 21 |
+
self.last_action_time = time.time()
|
| 22 |
+
logging.info("[π€] Virtual Robot initialized in state: %s", self.state)
|
| 23 |
|
| 24 |
+
def _validate_state(self) -> bool:
|
| 25 |
+
"""Ensure robot is in a valid state."""
|
| 26 |
+
if self.state not in self.VALID_STATES:
|
| 27 |
+
self.state = "ERROR"
|
| 28 |
+
logging.error("Invalid robot state detected!")
|
| 29 |
+
return False
|
| 30 |
+
return True
|
| 31 |
|
| 32 |
+
def wave(self) -> str:
|
| 33 |
+
"""Perform waving action with state management."""
|
| 34 |
+
if not self._validate_state():
|
| 35 |
+
return "β System error - invalid state"
|
| 36 |
+
|
| 37 |
+
try:
|
| 38 |
+
self.state = "WAVING"
|
| 39 |
+
self.last_action_time = time.time()
|
| 40 |
+
logging.info("[ποΈ] Performing wave action")
|
| 41 |
+
time.sleep(0.5) # Simulate action duration
|
| 42 |
+
response = "π€ *waves hello!*"
|
| 43 |
+
return response
|
| 44 |
+
except Exception as e:
|
| 45 |
+
self.state = "ERROR"
|
| 46 |
+
logging.error("Wave action failed: %s", str(e))
|
| 47 |
+
return f"β Wave failed: {str(e)}"
|
| 48 |
+
finally:
|
| 49 |
+
self.state = "IDLE"
|
| 50 |
+
|
| 51 |
+
def speak(self, phrase: str) -> str:
|
| 52 |
+
"""Perform speaking action with validation."""
|
| 53 |
+
if not phrase or not isinstance(phrase, str):
|
| 54 |
+
logging.warning("Invalid phrase received")
|
| 55 |
+
return "β Invalid phrase"
|
| 56 |
+
|
| 57 |
+
if not self._validate_state():
|
| 58 |
+
return "β System error - invalid state"
|
| 59 |
+
|
| 60 |
+
try:
|
| 61 |
+
self.state = "SPEAKING"
|
| 62 |
+
self.last_action_time = time.time()
|
| 63 |
+
logging.info("[π¬] Speaking: '%s'", phrase)
|
| 64 |
+
time.sleep(len(phrase) * 0.05) # Dynamic delay based on phrase length
|
| 65 |
+
return f"π£οΈ {phrase}"
|
| 66 |
+
except Exception as e:
|
| 67 |
+
self.state = "ERROR"
|
| 68 |
+
logging.error("Speak action failed: %s", str(e))
|
| 69 |
+
return f"β Speak failed: {str(e)}"
|
| 70 |
+
finally:
|
| 71 |
+
self.state = "IDLE"
|
| 72 |
+
|
| 73 |
+
def perform_action(self, action: str) -> str:
|
| 74 |
+
"""Main action dispatcher with comprehensive validation."""
|
| 75 |
+
if not action or not isinstance(action, str):
|
| 76 |
+
logging.warning("Invalid action command received")
|
| 77 |
+
return "β Invalid action command"
|
| 78 |
+
|
| 79 |
+
action = action.strip().lower()
|
| 80 |
+
|
| 81 |
+
try:
|
| 82 |
+
if action == "wave":
|
| 83 |
+
return self.wave()
|
| 84 |
+
elif action.startswith("say"):
|
| 85 |
+
phrase = action[3:].strip()
|
| 86 |
+
if not phrase:
|
| 87 |
+
return "β Please provide something to say"
|
| 88 |
+
return self.speak(phrase)
|
| 89 |
+
else:
|
| 90 |
+
logging.warning("Unknown action: %s", action)
|
| 91 |
+
return "β Unknown action - try 'wave' or 'say [message]'"
|
| 92 |
+
except Exception as e:
|
| 93 |
+
self.state = "ERROR"
|
| 94 |
+
logging.error("Action processing failed: %s", str(e))
|
| 95 |
+
return f"β System error: {str(e)}"
|