Răsfoiți Sursa

Add validation for coordinate calculations to prevent GRBL error:2

GRBL error:2 means "bad number format" - happens when coordinates
contain inf, nan, or other invalid values.

Added:
- Protection against division by zero in offset calculation
- Validation that coordinates are finite before sending to GRBL
- Detailed error logging when invalid values detected
- Stops pattern gracefully instead of hanging on repeated errors

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
tuanchris 1 săptămână în urmă
părinte
comite
8c0b628309
1 a modificat fișierele cu 16 adăugiri și 1 ștergeri
  1. 16 1
      modules/core/pattern_manager.py

+ 16 - 1
modules/core/pattern_manager.py

@@ -477,6 +477,8 @@ class MotionControlThread:
 
     def _move_polar_sync(self, theta: float, rho: float, speed: Optional[float] = None):
         """Synchronous version of move_polar for use in motion thread."""
+        import math
+
         # This is the original sync logic but running in dedicated thread
         if state.table_type == 'dune_weaver_mini':
             x_scaling_factor = 2
@@ -493,7 +495,12 @@ class MotionControlThread:
         x_total_steps = state.x_steps_per_mm * (100/x_scaling_factor)
         y_total_steps = state.y_steps_per_mm * (100/y_scaling_factor)
 
-        offset = x_increment * (x_total_steps * x_scaling_factor / (state.gear_ratio * y_total_steps * y_scaling_factor))
+        # Protect against division by zero
+        if state.gear_ratio == 0 or y_total_steps == 0:
+            logger.error(f"Invalid state for offset calculation: gear_ratio={state.gear_ratio}, y_total_steps={y_total_steps}")
+            offset = 0
+        else:
+            offset = x_increment * (x_total_steps * x_scaling_factor / (state.gear_ratio * y_total_steps * y_scaling_factor))
 
         if state.table_type == 'dune_weaver_mini' or state.y_steps_per_mm == 546:
             y_increment -= offset
@@ -506,6 +513,14 @@ class MotionControlThread:
         # Use provided speed or fall back to state.speed
         actual_speed = speed if speed is not None else state.speed
 
+        # Validate values before sending to GRBL
+        if not math.isfinite(new_x_abs) or not math.isfinite(new_y_abs) or not math.isfinite(actual_speed):
+            logger.error(f"Invalid coordinates detected: X={new_x_abs}, Y={new_y_abs}, F={actual_speed}")
+            logger.error(f"State: theta={theta}, rho={rho}, machine_x={state.machine_x}, machine_y={state.machine_y}")
+            logger.error(f"Steps: x_steps_per_mm={state.x_steps_per_mm}, y_steps_per_mm={state.y_steps_per_mm}")
+            state.stop_requested = True
+            return
+
         # Call sync version of send_grbl_coordinates in this thread
         self._send_grbl_coordinates_sync(round(new_x_abs, 3), round(new_y_abs, 3), actual_speed)