simplified code

This commit is contained in:
Lucca Pirovano
2026-07-16 13:29:56 -04:00
parent a5cdb4e4bd
commit bf3e197a20
4 changed files with 199 additions and 68 deletions
+24 -63
View File
@@ -147,9 +147,6 @@ class NaoMusicPlayer:
self.current_file = None
self.current_task_id = None
self.is_playing = False
self.paused_position = 0.0
self.play_start_time = None
self.position_at_start = 0.0
self.current_title = ""
self.is_loading = False
self.load_error = None
@@ -182,12 +179,6 @@ class NaoMusicPlayer:
_DL_PROGRESS_RE = re.compile(r'\[download\]\s+([\d.]+)%')
def _current_position(self):
"""Playback position tracked client-side, independent of NAOqi's getCurrentPosition."""
if self.is_playing and self.play_start_time is not None:
return self.position_at_start + (time.time() - self.play_start_time)
return self.paused_position
def play(self, url, title):
self.stop()
self.current_title = title
@@ -260,9 +251,6 @@ class NaoMusicPlayer:
self.progress = 100.0
self.current_task_id = self.player.playFile(self.remote_path)
self.is_playing = True
self.play_start_time = time.time()
self.position_at_start = 0.0
self.paused_position = 0.0
print(f"♪ Playing on NAO: {title} (task {self.current_task_id})")
return True
except Exception as e:
@@ -273,30 +261,24 @@ class NaoMusicPlayer:
self.is_loading = False
def pause(self):
"""Toggle playback. No position tracking: resuming restarts the track from the top."""
if self.current_task_id is None or not self.current_file:
print("⏸ Nothing loaded to pause/resume")
return
if self.is_playing:
pos = self._current_position()
try:
self.player.stopAll()
except Exception as e:
print(f"stopAll error while pausing: {e}")
self.load_error = f"Pause error: {e}"
return
self.paused_position = max(0.0, pos)
self.is_playing = False
self.play_start_time = None
print(f"♪ Paused at {self.paused_position:.1f}s")
print("♪ Paused")
else:
try:
self.current_task_id = self.player.playFileFromPosition(
self.current_file, self.paused_position, 1.0, 0.0
)
self.current_task_id = self.player.playFile(self.current_file)
self.is_playing = True
self.play_start_time = time.time()
self.position_at_start = self.paused_position
print(f"♪ Resumed from {self.paused_position:.1f}s (task {self.current_task_id})")
print(f"♪ Resumed from start (task {self.current_task_id})")
except Exception as e:
print(f"Resume error: {e}")
self.load_error = f"Resume error: {e}"
@@ -310,9 +292,6 @@ class NaoMusicPlayer:
self.current_file = None
self.current_task_id = None
self.current_title = ""
self.paused_position = 0.0
self.play_start_time = None
self.position_at_start = 0.0
# ==========================================
@@ -369,6 +348,14 @@ class NaoTeleop:
self.clock = pygame.time.Clock()
self.update_title()
def _safe(self, label, fn):
"""Run fn(), log+swallow any exception so one failure can't crash the loop."""
try:
return fn()
except Exception as e:
print(f"{label} error: {e}")
return None
def _init_audio_device(self):
try:
self.audio_device = self.session.service("ALAudioDevice")
@@ -439,18 +426,13 @@ class NaoTeleop:
if keys[pygame.K_j]: self.head_yaw = min(self.head_yaw + speed, 2.0); changed = True
if keys[pygame.K_l]: self.head_yaw = max(self.head_yaw - speed, -2.0); changed = True
if changed:
try:
self.motion.setAngles(["HeadYaw", "HeadPitch"], [self.head_yaw, self.head_pitch], 0.3)
except Exception as e:
print(f"Head move error: {e}")
self._safe("Head move", lambda: self.motion.setAngles(
["HeadYaw", "HeadPitch"], [self.head_yaw, self.head_pitch], 0.3))
def reset_head(self):
self.head_yaw = 0.0
self.head_pitch = 0.0
try:
self.motion.setAngles(["HeadYaw", "HeadPitch"], [0.0, 0.0], 0.4)
except Exception as e:
print(f"Head reset error: {e}")
self._safe("Head reset", lambda: self.motion.setAngles(["HeadYaw", "HeadPitch"], [0.0, 0.0], 0.4))
def update_title(self):
mode = "[TYPING] " if self.typing_mode else ""
@@ -459,10 +441,7 @@ class NaoTeleop:
def change_volume(self, delta):
self.volume = int(min(100, max(0, self.volume + delta)))
if self.audio_device:
try:
self.audio_device.setOutputVolume(self.volume)
except Exception as e:
print(f"Volume set error: {e}")
self._safe("Volume set", lambda: self.audio_device.setOutputVolume(self.volume))
print(f"🔊 NAO volume: {self.volume}%")
self.update_title()
@@ -483,10 +462,7 @@ class NaoTeleop:
self.motion.setMoveArmsEnabled(True, True)
print("🦶 Initializing walk engine...")
try:
self.motion.moveInit()
except Exception as e:
print(f"moveInit error: {e}")
self._safe("moveInit", self.motion.moveInit)
print("🦶 Walk engine ready")
carpet_config = [["StepHeight", 0.028], ["MaxStepFrequency", 0.6], ["TorsoWy", 0.05]]
@@ -585,10 +561,7 @@ class NaoTeleop:
self.change_volume(5)
elif event.key == pygame.K_t:
self.typing_mode = True
try:
self.motion.stopMove()
except Exception as e:
print(f"stopMove error: {e}")
self._safe("stopMove", self.motion.stopMove)
if not self.typing_mode and not self.music_search_mode:
keys = pygame.key.get_pressed()
@@ -679,27 +652,15 @@ class NaoTeleop:
print(f"\u26a0\ufe0f Frame error (continuing): {e}")
finally:
print("🛑 Shutting down...")
try:
self.motion.stopMove()
except Exception as e:
print(f"stopMove error: {e}")
try:
self.music.stop()
except Exception as e:
print(f"music stop error: {e}")
self._safe("stopMove", self.motion.stopMove)
self._safe("music stop", self.music.stop)
if self.audio_device:
try: self.audio_device.unsubscribe(self.audio_service_name)
except Exception as e: print(f"audio unsubscribe error: {e}")
self._safe("audio unsubscribe", lambda: self.audio_device.unsubscribe(self.audio_service_name))
if getattr(self, "sound_receiver", None):
try: self.sound_receiver.close()
except Exception as e: print(f"sound receiver close error: {e}")
self._safe("sound receiver close", self.sound_receiver.close)
if hasattr(self, 'video') and self.video_client:
try: self.video.unsubscribe(self.video_client)
except Exception as e: print(f"video unsubscribe error: {e}")
try:
self.motion.rest()
except Exception as e:
print(f"rest error: {e}")
self._safe("video unsubscribe", lambda: self.video.unsubscribe(self.video_client))
self._safe("rest", self.motion.rest)
pygame.quit()