|
|
@@ -2,6 +2,7 @@ import asyncio
|
|
|
import serial_asyncio
|
|
|
from typing import List
|
|
|
from utils.logging import LoggerSingleton
|
|
|
+import datetime
|
|
|
|
|
|
MOVE_RATE = 50
|
|
|
APPROACHE_RATE = 10
|
|
|
@@ -33,9 +34,15 @@ class GRBLHandler:
|
|
|
url = self.port,
|
|
|
baudrate = self.baudrate
|
|
|
)
|
|
|
- await self._process_response()
|
|
|
+ for _ in range(3):
|
|
|
+ await self._process_response()
|
|
|
logger.info("GRBL connected.")
|
|
|
- await self.send_gcode(['$21=1', '$22=1', '$H', 'G90', 'G21', '$21=0'])
|
|
|
+ # Enable homing ($22=1)
|
|
|
+ # Run homing cycle ($H)
|
|
|
+ # Set absolute positioning mode (G90)
|
|
|
+ # Set units to millimeters (G21)
|
|
|
+ await self.send_gcode(['$22=1', '$H', 'G90', 'G21'])
|
|
|
+ # await self.send_gcode(['$22=1', '$X', 'G90', 'G21'])
|
|
|
except Exception as e:
|
|
|
logger.error(f"Failed to connect to GRBL: {e}")
|
|
|
raise
|
|
|
@@ -56,6 +63,35 @@ class GRBLHandler:
|
|
|
finally:
|
|
|
self.controller_active.clear()
|
|
|
|
|
|
+ async def wait_until_idle(self, timeout_s=None):
|
|
|
+ """Wait until GRBL reports idle status"""
|
|
|
+ timeout_s = timeout_s or 50
|
|
|
+ if self.debug:
|
|
|
+ return
|
|
|
+
|
|
|
+ start = datetime.datetime.now()
|
|
|
+ while True:
|
|
|
+ if self.writer:
|
|
|
+ self.writer.write(b"?\n")
|
|
|
+ await self.writer.drain()
|
|
|
+ response = await self._process_response()
|
|
|
+
|
|
|
+ if response and "Idle" in response:
|
|
|
+ logger.debug("Movement complete.\nContinuing...")
|
|
|
+ break
|
|
|
+
|
|
|
+ now = datetime.datetime.now()
|
|
|
+ if (now - start).total_seconds() > timeout_s:
|
|
|
+ logger.error("Waiting on idle took too long!")
|
|
|
+ raise TimeoutError("GRBL did not report idle status")
|
|
|
+
|
|
|
+ await asyncio.sleep(0.2) # Async delay to prevent flooding
|
|
|
+
|
|
|
+ async def send_and_wait_gcode(self, commands: List[str], timeout_s=None):
|
|
|
+ """Send GCODE commands and wait until machine is idle"""
|
|
|
+ await self.send_gcode(commands)
|
|
|
+ await self.wait_until_idle(timeout_s)
|
|
|
+
|
|
|
async def _process_response(self):
|
|
|
"""Process GRBL responses"""
|
|
|
if self.reader:
|