From 9fb71fb34fbbe62529d6da80335afc44c611bb38 Mon Sep 17 00:00:00 2001 From: Vasilis Vlachoudis Date: Wed, 2 Nov 2016 22:17:55 +0100 Subject: [PATCH] Loading and setting the values from grbl --- CNCCanvas.py | 7 ++-- Sender.py | 19 ++++++++--- ToolsPage.py | 96 +++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 111 insertions(+), 11 deletions(-) diff --git a/CNCCanvas.py b/CNCCanvas.py index a91ba35c5..4a02713e8 100644 --- a/CNCCanvas.py +++ b/CNCCanvas.py @@ -1750,9 +1750,12 @@ def drawPaths(self): if time.time() - startTime > DRAW_TIME: raise AlarmException() n = 1000 - #cmd = self.cnc.parseLine(line) try: - cmd = CNC.breakLine(self.gcode.evaluate(CNC.compileLine(line))) + cmd = self.gcode.evaluate(CNC.compileLine(line)) + if isinstance(cmd,tuple): + cmd = None + else: + cmd = CNC.breakLine(cmd) except AlarmException: raise except: diff --git a/Sender.py b/Sender.py index bf9dfd02c..98479441c 100644 --- a/Sender.py +++ b/Sender.py @@ -64,8 +64,8 @@ TLOPAT = re.compile(r"^\[(...):([+\-]?\d*\.\d*)\]$") DOLLARPAT = re.compile(r"^\[G\d* .*\]$") FEEDPAT = re.compile(r"^(.*)[fF](\d+\.?\d+)(.*)$") - SPLITPAT = re.compile(r"[:,]") +VARPAT = re.compile(r"^\$(\d+)=(\d*\.?\d*) *\(?.*") CONNECTED = "Connected" NOT_CONNECTED = "Not connected" @@ -830,7 +830,7 @@ def stopRun(self, event=None): def serialIO(self): cline = [] # length of pipeline commands sline = [] # pipeline commands - wait = False # wait for commands to complete + wait = False # wait for commands to complete (status change to Idle) tosend = None # next string to send status = False # waiting for status <...> report tr = tg = time.time() # last time a ? or $G was send to grbl @@ -903,7 +903,7 @@ def serialIO(self): #print "+++",repr(tosend) if isinstance(tosend, tuple): #print "gcount tuple=",self._gcount - # wait to empty the grbl buffer + # wait to empty the grbl buffer and status is Idle if tosend[0] == WAIT: # Don't count WAIT until we are idle! wait = True @@ -1022,6 +1022,11 @@ def serialIO(self): CNC.vars["wcoy"] = float(word[2]) CNC.vars["wcoz"] = float(word[3]) + # Machine is Idle buffer is empty stop waiting and go on + if wait and not cline and fields[0]=="Idle": + wait = False + self._gcount += 1 + else: status = False pat = STATUSPAT.match(line) @@ -1124,7 +1129,7 @@ def serialIO(self): self._stop = True self.runEnded() - elif line[:4]=="Grbl": # and self.running: + elif line[:4]=="Grbl" or line[:13]=="CarbideMotion": # and self.running: tg = time.time() self.log.put((Sender.MSG_RECEIVE, line)) self._stop = True @@ -1148,6 +1153,12 @@ def serialIO(self): # a valid gcode event occurs self._alarm = False + elif line[0] == "$": + self.log.put((Sender.MSG_RECEIVE, line)) + pat = VARPAT.match(line) + if pat: + CNC.vars["grbl_%s"%(pat.group(1))] = pat.group(2) + else: self.log.put((Sender.MSG_RECEIVE, line)) diff --git a/ToolsPage.py b/ToolsPage.py index 19a60778e..ad1082b91 100644 --- a/ToolsPage.py +++ b/ToolsPage.py @@ -17,6 +17,7 @@ from operator import attrgetter import os +import time import glob import Utils import Ribbon @@ -24,6 +25,8 @@ import Unicode import CNCRibbon +from CNC import CNC + _EXE_FONT = ("Helvetica",12,"bold") #=============================================================================== @@ -106,6 +109,10 @@ def update(self): def event_generate(self, msg, **kwargs): self.master.listbox.event_generate(msg, **kwargs) + # ---------------------------------------------------------------------- + def beforeChange(self, app): + pass + # ---------------------------------------------------------------------- def populate(self): self.master.listbox.delete(0,END) @@ -484,7 +491,7 @@ def execute(self, app): #============================================================================== # CNC machine configuration #============================================================================== -class CNC(_Base): +class Config(_Base): def __init__(self, master): _Base.__init__(self, master) self.name = "CNC" @@ -514,8 +521,8 @@ def __init__(self, master): # Update variables after edit command # ---------------------------------------------------------------------- def update(self): - self.master.inches = self["units"] - self.master.digits = int(self["round"]) + self.master.inches = self["units"] + self.master.digits = int(self["round"]) self.master.cnc().decimal = self.master.digits self.master.cnc().startup = self["startup"] self.master.gcode.header = self["header"] @@ -757,6 +764,84 @@ def execute(self,app): app.executeOnSelection("TABS", True, ntabs, dtabs, dx, dy, z) app.setStatus(_("Create tabs on blocks")) +#============================================================================== +# Controller setup +#============================================================================== +class Controller(_Base): + def __init__(self, master): + _Base.__init__(self, master) + self.name = "Controller" + self.variables = [ + ("grbl_0", "int", 10, _("$0 Step pulse time [us]")), + ("grbl_1", "int", 25, _("$1 Step idle delay [ms]")), + ("grbl_2", "int", 0, _("$2 Step pulse invert [mask]")), + ("grbl_3", "int", 0, _("$3 Step direction invert [mask]")), + ("grbl_4", "bool", 0, _("$4 Invert step enable pin")), + ("grbl_5", "bool", 0, _("$5 Invert limit pins")), + ("grbl_6", "bool", 0, _("$6 Invert probe pin")), + ("grbl_10", "int", 1, _("$10 Status report options [mask]")), + ("grbl_11", "float", 0.010, _("$11 Junction deviation [mm]")), + ("grbl_12", "float", 0.002, _("$12 Arc tolerance [mm]")), + ("grbl_13", "bool", 0, _("$13 Report in inches")), + ("grbl_20", "bool", 0, _("$20 Soft limits enable")), + ("grbl_21", "bool", 0, _("$21 Hard limits enable")), + ("grbl_22", "bool", 0, _("$22 Homing cycle enable")), + ("grbl_23", "int", 0, _("$23 Homing direction invert [mask]")), + ("grbl_24", "float", 25., _("$24 Homing locate feed rate [mm/min]")), + ("grbl_25", "float", 500., _("$25 Homing search seek rate [mm/min]")), + ("grbl_26", "int", 250, _("$26 Homing switch debounce delay, ms")), + ("grbl_27", "float", 1., _("$27 Homing switch pull-off distance [mm]")), + ("grbl_30", "float", 1000., _("$30 Maximum spindle speed [RPM]")), + ("grbl_31", "float", 0., _("$31 Minimum spindle speed [RPM]")), + ("grbl_32", "bool", 0, _("$32 Laser-mode enable")), + ("grbl_100", "float", 250., _("$100 X-axis steps per mm")), + ("grbl_101", "float", 250., _("$101 Y-axis steps per mm")), + ("grbl_102", "float", 250., _("$102 Z-axis steps per mm")), + ("grbl_110", "float", 500., _("$110 X-axis maximum rate [mm/min]")), + ("grbl_111", "float", 500., _("$111 Y-axis maximum rate [mm/min]")), + ("grbl_112", "float", 500., _("$112 Z-axis maximum rate [mm/min]")), + ("grbl_120", "float", 10., _("$120 X-axis acceleration [mm/sec^2]")), + ("grbl_121", "float", 10., _("$121 Y-axis acceleration [mm/sec^2]")), + ("grbl_122", "float", 10., _("$122 Z-axis acceleration [mm/sec^2]")), + ("grbl_130", "float", 200., _("$130 X-axis maximum travel [mm]")), + ("grbl_131", "float", 200., _("$131 Y-axis maximum travel [mm]")), + ("grbl_132", "float", 200., _("$132 Z-axis maximum travel [mm]"))] + self.buttons.append("exe") + + # ---------------------------------------------------------------------- + def execute(self, app): + lines = [] + for n,t,d,c in self.variables: + v = self[n] + try: + if t=="float": + if v == float(CNC.vars[n]): continue + else: + if v == int(CNC.vars[n]): continue + except: + continue + lines.append("$%s=%s"%(n[5:],str(v))) + lines.append("%wait") + lines.append("$$") + app.run(lines=lines) + + # ---------------------------------------------------------------------- + def beforeChange(self, app): + app.sendGCode("$$") + time.sleep(1) + + # ---------------------------------------------------------------------- + def populate(self): + for n, t, d, l in self.variables: + try: + if t=="float": + self.values[n] = float(CNC.vars[n]) + else: + self.values[n] = int(CNC.vars[n]) + except KeyError: + pass + _Base.populate(self) + #============================================================================== # Tools container class #============================================================================== @@ -772,7 +857,7 @@ def __init__(self, gcode): self.listbox = None # CNC should be first to load the inches - for cls in [ CNC, Font, Color, Cut, Drill, EndMill, Events, + for cls in [ Config, Font, Color, Controller, Cut, Drill, EndMill, Events, Material, Pocket, Profile, Shortcut, Stock, Tabs]: tool = cls(self) @@ -1316,7 +1401,7 @@ def __init__(self, master, app): self.tools.addButton("exe",b) self.toolList = tkExtra.MultiListbox(self, - ((_("Name"), 16, None), + ((_("Name"), 24, None), (_("Value"), 24, None)), header = False, stretch = "last", @@ -1340,6 +1425,7 @@ def __init__(self, master, app): #---------------------------------------------------------------------- def change(self, a=None, b=None, c=None): tool = self.tools.getActive() + tool.beforeChange(self.app) tool.populate() tool.update() self.tools.activateButtons(tool)