فهرست منبع

add a time out to home

tuanchris 5 ماه پیش
والد
کامیت
fbc781015b
2فایلهای تغییر یافته به همراه79 افزوده شده و 23 حذف شده
  1. 9 1
      main.py
  2. 70 22
      modules/connection/connection_manager.py

+ 9 - 1
main.py

@@ -388,8 +388,16 @@ async def send_home():
         if not (state.conn.is_connected() if state.conn else False):
         if not (state.conn.is_connected() if state.conn else False):
             logger.warning("Attempted to move to home without a connection")
             logger.warning("Attempted to move to home without a connection")
             raise HTTPException(status_code=400, detail="Connection not established")
             raise HTTPException(status_code=400, detail="Connection not established")
-        connection_manager.home()
+        
+        # Run homing with 15 second timeout
+        success = await asyncio.to_thread(connection_manager.home)
+        if not success:
+            logger.error("Homing failed or timed out")
+            raise HTTPException(status_code=500, detail="Homing failed or timed out after 15 seconds")
+        
         return {"success": True}
         return {"success": True}
+    except HTTPException:
+        raise
     except Exception as e:
     except Exception as e:
         logger.error(f"Failed to send home command: {str(e)}")
         logger.error(f"Failed to send home command: {str(e)}")
         raise HTTPException(status_code=500, detail=str(e))
         raise HTTPException(status_code=500, detail=str(e))

+ 70 - 22
modules/connection/connection_manager.py

@@ -146,7 +146,9 @@ def device_init(homing=True):
         logger.info(f'x, y; {machine_x}, {machine_y}')
         logger.info(f'x, y; {machine_x}, {machine_y}')
         logger.info(f'State x, y; {state.machine_x}, {state.machine_y}')
         logger.info(f'State x, y; {state.machine_x}, {state.machine_y}')
         if homing:
         if homing:
-            home()
+            success = home()
+            if not success:
+                logger.error("Homing failed during device initialization")
     else:
     else:
         logger.info('Machine position known, skipping home')
         logger.info('Machine position known, skipping home')
         logger.info(f'Theta: {state.current_theta}, rho: {state.current_rho}')
         logger.info(f'Theta: {state.current_theta}, rho: {state.current_rho}')
@@ -374,32 +376,78 @@ def get_machine_steps(timeout=10):
         logger.error(f"Failed to get all machine parameters after {timeout}s. Missing: {', '.join(missing)}")
         logger.error(f"Failed to get all machine parameters after {timeout}s. Missing: {', '.join(missing)}")
         return False
         return False
 
 
-def home():
+def home(timeout=15):
     """
     """
     Perform homing by checking device configuration and sending the appropriate commands.
     Perform homing by checking device configuration and sending the appropriate commands.
+    
+    Args:
+        timeout: Maximum time in seconds to wait for homing to complete (default: 15)
     """
     """
-    try:
-        if state.homing:
-            logger.info("Using sensorless homing")
-            state.conn.send("$H\n")
-            state.conn.send("G1 Y0 F100\n")
-        else:
-            homing_speed = 400
-            if state.table_type == 'dune_weaver_mini':
-                homing_speed = 120
-            logger.info("Sensorless homing not supported. Using crash homing")
-            logger.info(f"Homing with speed {homing_speed}")
-            if state.gear_ratio == 6.25:
-                send_grbl_coordinates(0, - 30, homing_speed, home=True)
-                state.machine_y -= 30
+    import threading
+    
+    # Flag to track if homing completed
+    homing_complete = threading.Event()
+    homing_success = False
+    
+    def home_internal():
+        nonlocal homing_success
+        try:
+            if state.homing:
+                logger.info("Using sensorless homing")
+                state.conn.send("$H\n")
+                state.conn.send("G1 Y0 F100\n")
             else:
             else:
-                send_grbl_coordinates(0, -22, homing_speed, home=True)
-                state.machine_y -= 22
-
-        state.current_theta = state.current_rho = 0
-    except Exception as e:
-        logger.error(f"Error homing: {e}")
+                homing_speed = 400
+                if state.table_type == 'dune_weaver_mini':
+                    homing_speed = 120
+                logger.info("Sensorless homing not supported. Using crash homing")
+                logger.info(f"Homing with speed {homing_speed}")
+                if state.gear_ratio == 6.25:
+                    result = send_grbl_coordinates(0, - 30, homing_speed, home=True)
+                    if result == False:
+                        logger.error("Homing failed - send_grbl_coordinates returned False")
+                        homing_complete.set()
+                        return
+                    state.machine_y -= 30
+                else:
+                    result = send_grbl_coordinates(0, -22, homing_speed, home=True)
+                    if result == False:
+                        logger.error("Homing failed - send_grbl_coordinates returned False")
+                        homing_complete.set()
+                        return
+                    state.machine_y -= 22
+
+            state.current_theta = state.current_rho = 0
+            homing_success = True
+            homing_complete.set()
+        except Exception as e:
+            logger.error(f"Error during homing: {e}")
+            homing_complete.set()
+    
+    # Start homing in a separate thread
+    homing_thread = threading.Thread(target=home_internal)
+    homing_thread.daemon = True
+    homing_thread.start()
+    
+    # Wait for homing to complete or timeout
+    if not homing_complete.wait(timeout):
+        logger.error(f"Homing timeout after {timeout} seconds")
+        # Try to stop any ongoing movement
+        try:
+            if state.conn and state.conn.is_connected():
+                state.conn.send("!\n")  # Send feed hold
+                time.sleep(0.1)
+                state.conn.send("\x18\n")  # Send reset
+        except Exception as e:
+            logger.error(f"Error stopping movement after timeout: {e}")
+        return False
+    
+    if not homing_success:
+        logger.error("Homing failed")
         return False
         return False
+    
+    logger.info("Homing completed successfully")
+    return True
 
 
 def check_idle():
 def check_idle():
     """
     """