1
0

pattern_manager.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. import os
  2. import threading
  3. import time
  4. import random
  5. import logging
  6. from datetime import datetime
  7. from tqdm import tqdm
  8. from modules.connection import connection_manager
  9. from modules.core.state import state
  10. from math import pi
  11. import asyncio
  12. import json
  13. from modules.led.led_controller import effect_playing, effect_idle
  14. # Configure logging
  15. logger = logging.getLogger(__name__)
  16. # Global state
  17. THETA_RHO_DIR = './patterns'
  18. os.makedirs(THETA_RHO_DIR, exist_ok=True)
  19. # Create an asyncio Event for pause/resume
  20. pause_event = asyncio.Event()
  21. pause_event.set() # Initially not paused
  22. # Create an asyncio Lock for pattern execution
  23. pattern_lock = asyncio.Lock()
  24. # Progress update task
  25. progress_update_task = None
  26. async def cleanup_pattern_manager():
  27. """Clean up pattern manager resources"""
  28. global progress_update_task, pattern_lock, pause_event
  29. try:
  30. # Cancel progress update task if running
  31. if progress_update_task and not progress_update_task.done():
  32. try:
  33. progress_update_task.cancel()
  34. # Wait for task to actually cancel
  35. try:
  36. await progress_update_task
  37. except asyncio.CancelledError:
  38. pass
  39. except Exception as e:
  40. logger.error(f"Error cancelling progress update task: {e}")
  41. # Clean up pattern lock
  42. if pattern_lock:
  43. try:
  44. if pattern_lock.locked():
  45. pattern_lock.release()
  46. pattern_lock = None
  47. except Exception as e:
  48. logger.error(f"Error cleaning up pattern lock: {e}")
  49. # Clean up pause event
  50. if pause_event:
  51. try:
  52. pause_event.set() # Wake up any waiting tasks
  53. pause_event = None
  54. except Exception as e:
  55. logger.error(f"Error cleaning up pause event: {e}")
  56. # Clean up pause condition from state
  57. if state.pause_condition:
  58. try:
  59. with state.pause_condition:
  60. state.pause_condition.notify_all()
  61. state.pause_condition = threading.Condition()
  62. except Exception as e:
  63. logger.error(f"Error cleaning up pause condition: {e}")
  64. # Clear all state variables
  65. state.current_playing_file = None
  66. state.execution_progress = 0
  67. state.is_running = False
  68. state.pause_requested = False
  69. state.stop_requested = True
  70. state.is_clearing = False
  71. # Reset machine position
  72. await connection_manager.update_machine_position()
  73. logger.info("Pattern manager resources cleaned up")
  74. except Exception as e:
  75. logger.error(f"Error during pattern manager cleanup: {e}")
  76. finally:
  77. # Ensure we always reset these
  78. progress_update_task = None
  79. pattern_lock = None
  80. pause_event = None
  81. def list_theta_rho_files():
  82. files = []
  83. for root, _, filenames in os.walk(THETA_RHO_DIR):
  84. for file in filenames:
  85. relative_path = os.path.relpath(os.path.join(root, file), THETA_RHO_DIR)
  86. files.append(relative_path)
  87. logger.debug(f"Found {len(files)} theta-rho files")
  88. return files
  89. def parse_theta_rho_file(file_path):
  90. """Parse a theta-rho file and return a list of (theta, rho) pairs."""
  91. coordinates = []
  92. try:
  93. logger.debug(f"Parsing theta-rho file: {file_path}")
  94. with open(file_path, 'r') as file:
  95. for line in file:
  96. line = line.strip()
  97. if not line or line.startswith("#"):
  98. continue
  99. try:
  100. theta, rho = map(float, line.split())
  101. coordinates.append((theta, rho))
  102. except ValueError:
  103. logger.warning(f"Skipping invalid line: {line}")
  104. continue
  105. except Exception as e:
  106. logger.error(f"Error reading file: {e}")
  107. return coordinates
  108. # Normalization Step
  109. if coordinates:
  110. first_theta = coordinates[0][0]
  111. normalized = [(theta - first_theta, rho) for theta, rho in coordinates]
  112. coordinates = normalized
  113. logger.debug(f"Parsed {len(coordinates)} coordinates from {file_path}")
  114. return coordinates
  115. def get_clear_pattern_file(clear_pattern_mode, path=None):
  116. """Return a .thr file path based on pattern_name and table type."""
  117. if not clear_pattern_mode or clear_pattern_mode == 'none':
  118. return
  119. # Define patterns for each table type
  120. clear_patterns = {
  121. 'dune_weaver': {
  122. 'clear_from_out': './patterns/clear_from_out.thr',
  123. 'clear_from_in': './patterns/clear_from_in.thr',
  124. 'clear_sideway': './patterns/clear_sideway.thr'
  125. },
  126. 'dune_weaver_mini': {
  127. 'clear_from_out': './patterns/clear_from_out_mini.thr',
  128. 'clear_from_in': './patterns/clear_from_in_mini.thr',
  129. 'clear_sideway': './patterns/clear_sideway_mini.thr'
  130. },
  131. 'dune_weaver_pro': {
  132. 'clear_from_out': './patterns/clear_from_out_pro.thr',
  133. 'clear_from_out_Ultra': './patterns/clear_from_out_Ultra.thr',
  134. 'clear_from_in': './patterns/clear_from_in_pro.thr',
  135. 'clear_from_in_Ultra': './patterns/clear_from_in_Ultra.thr',
  136. 'clear_sideway': './patterns/clear_sideway_pro.thr'
  137. }
  138. }
  139. # Get patterns for current table type, fallback to standard patterns if type not found
  140. table_patterns = clear_patterns.get(state.table_type, clear_patterns['dune_weaver'])
  141. logger.debug(f"Clear pattern mode: {clear_pattern_mode} for table type: {state.table_type}")
  142. if clear_pattern_mode == "random":
  143. return random.choice(list(table_patterns.values()))
  144. if clear_pattern_mode == 'adaptive':
  145. if not path:
  146. logger.warning("No path provided for adaptive clear pattern")
  147. return random.choice(list(table_patterns.values()))
  148. coordinates = parse_theta_rho_file(path)
  149. if not coordinates:
  150. logger.warning("No valid coordinates found in file for adaptive clear pattern")
  151. return random.choice(list(table_patterns.values()))
  152. first_rho = coordinates[0][1]
  153. if first_rho < 0.5:
  154. return table_patterns['clear_from_out']
  155. else:
  156. return random.choice([table_patterns['clear_from_in'], table_patterns['clear_sideway']])
  157. else:
  158. if clear_pattern_mode not in table_patterns:
  159. return False
  160. return table_patterns[clear_pattern_mode]
  161. def is_clear_pattern(file_path):
  162. """Check if a file path is a clear pattern file."""
  163. # Get all possible clear pattern files for all table types
  164. clear_patterns = []
  165. for table_type in ['dune_weaver', 'dune_weaver_mini', 'dune_weaver_pro']:
  166. clear_patterns.extend([
  167. f'./patterns/clear_from_out{("_" + table_type.split("_")[-1]) if table_type != "dune_weaver" else ""}.thr',
  168. f'./patterns/clear_from_in{("_" + table_type.split("_")[-1]) if table_type != "dune_weaver" else ""}.thr',
  169. f'./patterns/clear_sideway{("_" + table_type.split("_")[-1]) if table_type != "dune_weaver" else ""}.thr'
  170. ])
  171. # Normalize paths for comparison
  172. normalized_path = os.path.normpath(file_path)
  173. normalized_clear_patterns = [os.path.normpath(p) for p in clear_patterns]
  174. # Check if the file path matches any clear pattern path
  175. return normalized_path in normalized_clear_patterns
  176. async def run_theta_rho_file(file_path, is_playlist=False):
  177. """Run a theta-rho file by sending data in optimized batches with tqdm ETA tracking."""
  178. if pattern_lock.locked():
  179. logger.warning("Another pattern is already running. Cannot start a new one.")
  180. return
  181. async with pattern_lock: # This ensures only one pattern can run at a time
  182. # Start progress update task only if not part of a playlist
  183. global progress_update_task
  184. if not is_playlist and not progress_update_task:
  185. progress_update_task = asyncio.create_task(broadcast_progress())
  186. coordinates = parse_theta_rho_file(file_path)
  187. total_coordinates = len(coordinates)
  188. if total_coordinates < 2:
  189. logger.warning("Not enough coordinates for interpolation")
  190. if not is_playlist:
  191. state.current_playing_file = None
  192. state.execution_progress = None
  193. return
  194. state.execution_progress = (0, total_coordinates, None, 0)
  195. # stop actions without resetting the playlist
  196. stop_actions(clear_playlist=False)
  197. state.current_playing_file = file_path
  198. state.stop_requested = False
  199. logger.info(f"Starting pattern execution: {file_path}")
  200. logger.info(f"t: {state.current_theta}, r: {state.current_rho}")
  201. reset_theta()
  202. start_time = time.time()
  203. if state.led_controller:
  204. effect_playing(state.led_controller)
  205. with tqdm(
  206. total=total_coordinates,
  207. unit="coords",
  208. desc=f"Executing Pattern {file_path}",
  209. dynamic_ncols=True,
  210. disable=False,
  211. mininterval=1.0
  212. ) as pbar:
  213. for i, coordinate in enumerate(coordinates):
  214. theta, rho = coordinate
  215. if state.stop_requested:
  216. logger.info("Execution stopped by user")
  217. if state.led_controller:
  218. effect_idle(state.led_controller)
  219. break
  220. if state.skip_requested:
  221. logger.info("Skipping pattern...")
  222. connection_manager.check_idle()
  223. if state.led_controller:
  224. effect_idle(state.led_controller)
  225. break
  226. # Wait for resume if paused
  227. if state.pause_requested:
  228. logger.info("Execution paused...")
  229. if state.led_controller:
  230. effect_idle(state.led_controller)
  231. await pause_event.wait()
  232. logger.info("Execution resumed...")
  233. if state.led_controller:
  234. effect_playing(state.led_controller)
  235. move_polar(theta, rho)
  236. # Update progress for all coordinates including the first one
  237. pbar.update(1)
  238. elapsed_time = time.time() - start_time
  239. estimated_remaining_time = (total_coordinates - (i + 1)) / pbar.format_dict['rate'] if pbar.format_dict['rate'] and total_coordinates else 0
  240. state.execution_progress = (i + 1, total_coordinates, estimated_remaining_time, elapsed_time)
  241. # Add a small delay to allow other async operations
  242. await asyncio.sleep(0.001)
  243. # Update progress one last time to show 100%
  244. elapsed_time = time.time() - start_time
  245. state.execution_progress = (total_coordinates, total_coordinates, 0, elapsed_time)
  246. # Give WebSocket a chance to send the final update
  247. await asyncio.sleep(0.1)
  248. if not state.conn:
  249. logger.error("Device is not connected. Stopping pattern execution.")
  250. return
  251. connection_manager.check_idle()
  252. # Only clear state if not part of a playlist
  253. if not is_playlist:
  254. state.current_playing_file = None
  255. state.execution_progress = None
  256. logger.info("Pattern execution completed and state cleared")
  257. else:
  258. logger.info("Pattern execution completed, maintaining state for playlist")
  259. # Only cancel progress update task if not part of a playlist
  260. if not is_playlist and progress_update_task:
  261. progress_update_task.cancel()
  262. try:
  263. await progress_update_task
  264. except asyncio.CancelledError:
  265. pass
  266. progress_update_task = None
  267. async def run_theta_rho_files(file_paths, pause_time=0, clear_pattern=None, run_mode="single", shuffle=False):
  268. """Run multiple .thr files in sequence with options."""
  269. state.stop_requested = False
  270. # Set initial playlist state
  271. state.playlist_mode = run_mode
  272. state.current_playlist_index = 0
  273. # Start progress update task for the playlist
  274. global progress_update_task
  275. if not progress_update_task:
  276. progress_update_task = asyncio.create_task(broadcast_progress())
  277. if shuffle:
  278. random.shuffle(file_paths)
  279. logger.info("Playlist shuffled")
  280. if shuffle:
  281. random.shuffle(file_paths)
  282. logger.info("Playlist shuffled")
  283. try:
  284. while True:
  285. # Construct the complete pattern sequence
  286. pattern_sequence = []
  287. for path in file_paths:
  288. # Add clear pattern if specified
  289. if clear_pattern and clear_pattern != 'none':
  290. clear_file_path = get_clear_pattern_file(clear_pattern, path)
  291. if clear_file_path:
  292. pattern_sequence.append(clear_file_path)
  293. # Add main pattern
  294. pattern_sequence.append(path)
  295. # Shuffle if requested
  296. if shuffle:
  297. # Get pairs of patterns (clear + main) to keep them together
  298. pairs = [pattern_sequence[i:i+2] for i in range(0, len(pattern_sequence), 2)]
  299. random.shuffle(pairs)
  300. # Flatten the pairs back into a single list
  301. pattern_sequence = [pattern for pair in pairs for pattern in pair]
  302. logger.info("Playlist shuffled")
  303. # Set the playlist to the first pattern
  304. state.current_playlist = pattern_sequence
  305. # Execute the pattern sequence
  306. for idx, file_path in enumerate(pattern_sequence):
  307. state.current_playlist_index = idx
  308. if state.stop_requested:
  309. logger.info("Execution stopped")
  310. return
  311. # Update state for main patterns only
  312. logger.info(f"Running pattern {file_path}")
  313. # Execute the pattern
  314. await run_theta_rho_file(file_path, is_playlist=True)
  315. # Handle pause between patterns
  316. if idx < len(pattern_sequence) - 1 and not state.stop_requested and pause_time > 0 and not state.skip_requested:
  317. # Check if current pattern is a clear pattern
  318. if is_clear_pattern(file_path):
  319. logger.info("Skipping pause after clear pattern")
  320. else:
  321. logger.info(f"Pausing for {pause_time} seconds")
  322. pause_start = time.time()
  323. while time.time() - pause_start < pause_time:
  324. if state.skip_requested:
  325. logger.info("Pause interrupted by stop/skip request")
  326. break
  327. await asyncio.sleep(1) # Check every 100ms
  328. state.skip_requested = False
  329. if run_mode == "indefinite":
  330. logger.info("Playlist completed. Restarting as per 'indefinite' run mode")
  331. if pause_time > 0:
  332. logger.debug(f"Pausing for {pause_time} seconds before restarting")
  333. await asyncio.sleep(pause_time)
  334. continue
  335. else:
  336. logger.info("Playlist completed")
  337. break
  338. finally:
  339. # Clean up progress update task
  340. if progress_update_task:
  341. progress_update_task.cancel()
  342. try:
  343. await progress_update_task
  344. except asyncio.CancelledError:
  345. pass
  346. progress_update_task = None
  347. # Clear all state variables
  348. state.current_playing_file = None
  349. state.execution_progress = None
  350. state.current_playlist = None
  351. state.current_playlist_index = None
  352. state.playlist_mode = None
  353. if state.led_controller:
  354. effect_idle(state.led_controller)
  355. logger.info("All requested patterns completed (or stopped) and state cleared")
  356. def stop_actions(clear_playlist = True):
  357. """Stop all current actions."""
  358. try:
  359. with state.pause_condition:
  360. state.pause_requested = False
  361. state.stop_requested = True
  362. state.current_playing_file = None
  363. state.execution_progress = None
  364. state.is_clearing = False
  365. if clear_playlist:
  366. # Clear playlist state
  367. state.current_playlist = None
  368. state.current_playlist_index = None
  369. state.playlist_mode = None
  370. # Cancel progress update task if we're clearing the playlist
  371. global progress_update_task
  372. if progress_update_task and not progress_update_task.done():
  373. progress_update_task.cancel()
  374. state.pause_condition.notify_all()
  375. connection_manager.update_machine_position()
  376. except Exception as e:
  377. logger.error(f"Error during stop_actions: {e}")
  378. # Ensure we still update machine position even if there's an error
  379. connection_manager.update_machine_position()
  380. def move_polar(theta, rho):
  381. """
  382. This functions take in a pair of theta rho coordinate, compute the distance to travel based on current theta, rho,
  383. and translate the motion to gcode jog command and sent to grbl.
  384. Since having similar steps_per_mm will make x and y axis moves at around the same speed, we have to scale the
  385. x_steps_per_mm and y_steps_per_mm so that they are roughly the same. Here's the range of motion:
  386. X axis (angular): 50mm = 1 revolution
  387. Y axis (radial): 0 => 20mm = theta 0 (center) => 1 (perimeter)
  388. Args:
  389. theta (_type_): _description_
  390. rho (_type_): _description_
  391. """
  392. # Adding soft limit to reduce hardware sound
  393. # soft_limit_inner = 0.01
  394. # if rho < soft_limit_inner:
  395. # rho = soft_limit_inner
  396. # soft_limit_outter = 0.015
  397. # if rho > (1-soft_limit_outter):
  398. # rho = (1-soft_limit_outter)
  399. if state.table_type == 'dune_weaver_mini':
  400. x_scaling_factor = 2
  401. y_scaling_factor = 3.7
  402. else:
  403. x_scaling_factor = 2
  404. y_scaling_factor = 5
  405. delta_theta = theta - state.current_theta
  406. delta_rho = rho - state.current_rho
  407. x_increment = delta_theta * 100 / (2 * pi * x_scaling_factor) # Added -1 to reverse direction
  408. y_increment = delta_rho * 100 / y_scaling_factor
  409. x_total_steps = state.x_steps_per_mm * (100/x_scaling_factor)
  410. y_total_steps = state.y_steps_per_mm * (100/y_scaling_factor)
  411. offset = x_increment * (x_total_steps * x_scaling_factor / (state.gear_ratio * y_total_steps * y_scaling_factor))
  412. if state.table_type == 'dune_weaver_mini':
  413. y_increment -= offset
  414. else:
  415. y_increment += offset
  416. new_x_abs = state.machine_x + x_increment
  417. new_y_abs = state.machine_y + y_increment
  418. # dynamic_speed = compute_dynamic_speed(rho, max_speed=state.speed)
  419. connection_manager.send_grbl_coordinates(round(new_x_abs, 3), round(new_y_abs,3), state.speed)
  420. state.current_theta = theta
  421. state.current_rho = rho
  422. state.machine_x = new_x_abs
  423. state.machine_y = new_y_abs
  424. def pause_execution():
  425. """Pause pattern execution using asyncio Event."""
  426. logger.info("Pausing pattern execution")
  427. state.pause_requested = True
  428. pause_event.clear() # Clear the event to pause execution
  429. return True
  430. def resume_execution():
  431. """Resume pattern execution using asyncio Event."""
  432. logger.info("Resuming pattern execution")
  433. state.pause_requested = False
  434. pause_event.set() # Set the event to resume execution
  435. return True
  436. def reset_theta():
  437. logger.info('Resetting Theta')
  438. state.current_theta = 0
  439. connection_manager.update_machine_position()
  440. def set_speed(new_speed):
  441. state.speed = new_speed
  442. logger.info(f'Set new state.speed {new_speed}')
  443. def get_status():
  444. """Get the current status of pattern execution."""
  445. status = {
  446. "current_file": state.current_playing_file,
  447. "is_paused": state.pause_requested,
  448. "is_running": bool(state.current_playing_file and not state.stop_requested),
  449. "progress": None,
  450. "playlist": None,
  451. "speed": state.speed
  452. }
  453. # Add playlist information if available
  454. if state.current_playlist and state.current_playlist_index is not None:
  455. next_index = state.current_playlist_index + 1
  456. status["playlist"] = {
  457. "current_index": state.current_playlist_index,
  458. "total_files": len(state.current_playlist),
  459. "mode": state.playlist_mode,
  460. "next_file": state.current_playlist[next_index] if next_index < len(state.current_playlist) else None
  461. }
  462. if state.execution_progress:
  463. current, total, remaining_time, elapsed_time = state.execution_progress
  464. status["progress"] = {
  465. "current": current,
  466. "total": total,
  467. "remaining_time": remaining_time,
  468. "elapsed_time": elapsed_time,
  469. "percentage": (current / total * 100) if total > 0 else 0
  470. }
  471. return status
  472. async def broadcast_progress():
  473. """Background task to broadcast progress updates."""
  474. from app import active_status_connections
  475. while True:
  476. # Send status updates regardless of pattern_lock state
  477. status = get_status()
  478. disconnected = set()
  479. # Create a copy of the set for iteration
  480. active_connections = active_status_connections.copy()
  481. for websocket in active_connections:
  482. try:
  483. await websocket.send_json(status)
  484. except Exception as e:
  485. logger.warning(f"Failed to send status update: {e}")
  486. disconnected.add(websocket)
  487. # Clean up disconnected clients
  488. if disconnected:
  489. active_status_connections.difference_update(disconnected)
  490. # Check if we should stop broadcasting
  491. if not state.current_playlist:
  492. # If no playlist, only stop if no pattern is being executed
  493. if not pattern_lock.locked():
  494. logger.info("No playlist or pattern running, stopping broadcast")
  495. break
  496. # Wait before next update
  497. await asyncio.sleep(1)