def ProcessStepSate(self): # send data to serial port ---------------------------------------------- # check if we are done with gcode if self.workingProgramCounter >= len(self.gcodeDataLines): self.swState = gc.gSTATE_IDLE self.progExecOutQueue.put(gc.threadEvent(gc.gEV_STEP_END, None)) if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread reach last PC, swState->gc.gSTATE_IDLE" return # update PC self.progExecOutQueue.put(gc.threadEvent(gc.gEV_PC_UPDATE, self.workingProgramCounter)) # end IDLE state if self.workingProgramCounter > self.initialProgramCounter: self.swState = gc.gSTATE_IDLE self.progExecOutQueue.put(gc.threadEvent(gc.gEV_STEP_END, None)) if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread finish STEP cmd, swState->gc.gSTATE_IDLE" return gcode = self.gcodeDataLines[self.workingProgramCounter] # don't sent unnecessary data save the bits for speed for reComments in gReGcodeComments: gcode = reComments.sub("", gcode) self.RunStepSendGcode(gcode)
def SerialRead(self): serialData = "" if not self.progExecSerialRxInQueue.empty(): # get item from queue e = self.progExecSerialRxInQueue.get() if e.event_id == gc.gEV_ABORT: # make sure we stop processing any states... self.swState = gc.gSTATE_ABORT # add data to queue and signal main window to consume self.progExecOutQueue.put(gc.threadEvent(gc.gEV_ABORT, e.data)) wx.PostEvent(self.notifyWindow, gc.threadQueueEvent(None)) elif e.event_id == gc.gEV_SER_RXDATA: if len(e.data) > 0: # add data to queue and signal main window to consume self.progExecOutQueue.put(gc.threadEvent(gc.gEV_DATA_IN, e.data)) serialData = e.data self.DecodeStatusData(serialData) return serialData
def SerialRead(self): serialData = "" if not self.progExecSerialRxInQueue.empty(): # get item from queue e = self.progExecSerialRxInQueue.get() if e.event_id == gc.gEV_ABORT: # make sure we stop processing any states... self.swState = gc.gSTATE_ABORT # add data to queue and signal main window to consume self.progExecOutQueue.put(gc.threadEvent(gc.gEV_ABORT, e.data)) wx.PostEvent(self.notifyWindow, gc.threadQueueEvent(None)) elif e.event_id == gc.gEV_SER_RXDATA: if len(e.data) > 0: # add data to queue and signal main window to consume self.progExecOutQueue.put( gc.threadEvent(gc.gEV_DATA_IN, e.data)) serialData = e.data self.DecodeStatusData(serialData) return serialData
def ProcessStepSate(self): # send data to serial port ---------------------------------------------- # check if we are done with gcode if self.workingProgramCounter >= len(self.gcodeDataLines): self.swState = gc.gSTATE_IDLE self.progExecOutQueue.put(gc.threadEvent(gc.gEV_STEP_END, None)) if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread reach last PC, swState->gc.gSTATE_IDLE" return # update PC self.progExecOutQueue.put( gc.threadEvent(gc.gEV_PC_UPDATE, self.workingProgramCounter)) # end IDLE state if self.workingProgramCounter > self.initialProgramCounter: self.swState = gc.gSTATE_IDLE self.progExecOutQueue.put(gc.threadEvent(gc.gEV_STEP_END, None)) if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread finish STEP cmd, swState->gc.gSTATE_IDLE" return gcode = self.gcodeDataLines[self.workingProgramCounter] # don't sent unnecessary data save the bits for speed for reComments in gReGcodeComments: gcode = reComments.sub("", gcode) self.RunStepSendGcode(gcode)
def ProcessRunSate(self): # send data to serial port ---------------------------------------------- # check if we are done with gcode if self.workingProgramCounter >= len(self.gcodeDataLines): self.swState = gc.gSTATE_IDLE self.progExecOutQueue.put(gc.threadEvent(gc.gEV_RUN_END, None)) wx.PostEvent(self.notifyWindow, gc.threadQueueEvent(None)) if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread reach last PC, swState->gc.gSTATE_IDLE" return # update PC self.progExecOutQueue.put(gc.threadEvent(gc.gEV_PC_UPDATE, self.workingProgramCounter)) wx.PostEvent(self.notifyWindow, gc.threadQueueEvent(None)) # check for break point hit if (self.workingProgramCounter in self.breakPointSet) and \ (self.workingProgramCounter != self.initialProgramCounter): self.swState = gc.gSTATE_BREAK self.progExecOutQueue.put(gc.threadEvent(gc.gEV_HIT_BRK_PT, None)) wx.PostEvent(self.notifyWindow, gc.threadQueueEvent(None)) if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread encounter breakpoint PC[%s], swState->gc.gSTATE_BREAK" % \ (self.workingProgramCounter) return # get gcode line gcode = self.gcodeDataLines[self.workingProgramCounter] # check for msg line reMsgSearch = gReGcodeMsg.search(gcode) if (reMsgSearch is not None) and \ (self.workingProgramCounter != self.initialProgramCounter): self.swState = gc.gSTATE_BREAK self.progExecOutQueue.put(gc.threadEvent(gc.gEV_HIT_MSG, reMsgSearch.group(1))) wx.PostEvent(self.notifyWindow, gc.threadQueueEvent(None)) if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread encounter MSG line PC[%s], swState->gc.gSTATE_BREAK, MSG[%s]" % \ (self.workingProgramCounter, reMsgSearch.group(1)) return # don't sent unnecessary data save the bits for speed for reComments in gReGcodeComments: gcode = reComments.sub("", gcode) # send g-code command self.RunStepSendGcode(gcode)
def RunStepSendGcode(self, gcodeData): gcode = gcodeData.strip() if len(gcode) > 0: if self.machineAutoStatus: if self.deviceID == gc.gDEV_TINYG2 or self.deviceID == gc.gDEV_TINYG: gcode = "%s%s" % (gcode, gc.gTINYG_CMD_GET_STATUS) elif self.deviceID == gc.gDEV_GRBL: gcode = "%s%s" % (gcode, gc.gGRBL_CMD_GET_STATUS) else: gcode = "%s\n" % (gcode) # write data self.SerialWrite(gcode) # wait for response #responseData = self.WaitForResponse() self.WaitForAcknowledge() self.workingProgramCounter += 1 # if we stop early make sure to update PC to main UI if self.swState == gc.gSTATE_IDLE: self.progExecOutQueue.put( gc.threadEvent(gc.gEV_PC_UPDATE, self.workingProgramCounter))
def StopCapture(self): if self.cmdLineOptions.vverbose: print "** gsatCV2Panel StopCapture." if self.capture: self.capture = False if self.captureTimer is not None: self.captureTimer.Stop() if self.visionThread is not None: self.cvw2tQueue.put(gc.threadEvent(gEV_CMD_CV_EXIT, None)) goitem = False while not self.t2cvwQueue.empty(): te = self.t2cvwQueue.get() goitem = True # make sure to unlock thread if goitem: self.t2cvwQueue.task_done() # self.cvw2tQueue.join() self.visionThread = None
def RunStepSendGcode(self, gcodeData): gcode = gcodeData.strip() if len(gcode) > 0: if self.machineAutoStatus: if self.deviceID == gc.gDEV_TINYG2 or self.deviceID == gc.gDEV_TINYG: gcode = "%s%s" % (gcode, gc.gTINYG_CMD_GET_STATUS) elif self.deviceID == gc.gDEV_GRBL: gcode = "%s%s" % (gcode, gc.gGRBL_CMD_GET_STATUS) else: gcode = "%s\n" % (gcode) # write data self.SerialWrite(gcode) # wait for response #responseData = self.WaitForResponse() self.WaitForAcknowledge() self.workingProgramCounter += 1 # if we stop early make sure to update PC to main UI if self.swState == gc.gSTATE_IDLE: self.progExecOutQueue.put(gc.threadEvent(gc.gEV_PC_UPDATE, self.workingProgramCounter))
def ProcessRunSate(self): # send data to serial port ---------------------------------------------- # check if we are done with gcode if self.workingProgramCounter >= len(self.gcodeDataLines): self.swState = gc.gSTATE_IDLE self.progExecOutQueue.put(gc.threadEvent(gc.gEV_RUN_END, None)) if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread reach last PC, swState->gc.gSTATE_IDLE" return # update PC self.progExecOutQueue.put( gc.threadEvent(gc.gEV_PC_UPDATE, self.workingProgramCounter)) # check for break point hit if (self.workingProgramCounter in self.breakPointSet) and \ (self.workingProgramCounter != self.initialProgramCounter): self.swState = gc.gSTATE_BREAK self.progExecOutQueue.put(gc.threadEvent(gc.gEV_HIT_BRK_PT, None)) if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread encounter breakpoint PC[%s], swState->gc.gSTATE_BREAK" % \ (self.workingProgramCounter) return # get gcode line gcode = self.gcodeDataLines[self.workingProgramCounter] # check for msg line reMsgSearch = gReGcodeMsg.search(gcode) if (reMsgSearch is not None) and \ (self.workingProgramCounter != self.initialProgramCounter): self.swState = gc.gSTATE_BREAK self.progExecOutQueue.put( gc.threadEvent(gc.gEV_HIT_MSG, reMsgSearch.group(1))) if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread encounter MSG line PC[%s], swState->gc.gSTATE_BREAK, MSG[%s]" % \ (self.workingProgramCounter, reMsgSearch.group(1)) return # don't sent unnecessary data save the bits for speed for reComments in gReGcodeComments: gcode = reComments.sub("", gcode) # send g-code command self.RunStepSendGcode(gcode)
def run(self): """ Worker Thread. This is the code executing in the new thread context. """ import cv2.cv as cv self.cv = cv #set up camera self.captureDevice = self.cv.CaptureFromCAM(self.cv2CaptureDevice) # let camera hardware settle time.sleep(1) # init sensor frame size self.cv.SetCaptureProperty(self.captureDevice, self.cv.CV_CAP_PROP_FRAME_WIDTH, self.cv2CaptureWidth) self.cv.SetCaptureProperty(self.captureDevice, self.cv.CV_CAP_PROP_FRAME_HEIGHT, self.cv2CaptureHeight) # init before work loop self.endThread = False if self.cmdLineOptions.vverbose: print "** gcscomputerVisionThread start." while (self.endThread != True): # capture frame frame = self.CaptureFrame() # sned frame to window, and wait... #wx.PostEvent(self.notifyWindow, gc.threadQueueEvent(None)) self.t2cvwQueue.put(gc.threadEvent(gEV_CMD_CV_IMAGE, frame)) self.t2cvwQueue.join() # sleep for a period time.sleep(self.cv2CapturePeriod / 1000) # process input queue for new commands or actions self.ProcessQueue() # check if we need to exit now if self.endThread: break if self.cmdLineOptions.vverbose: print "** gcscomputerVisionThread exit."
def run(self): """ Worker Thread. This is the code executing in the new thread context. """ import cv2 as cv2 self.cv2 = cv2 # set up camera self.captureDevice = cv2.VideoCapture(self.cv2CaptureDevice) # let camera hardware settle time.sleep(1) # init sensor frame size self.captureDevice.set(self.cv2.cv.CV_CAP_PROP_FRAME_WIDTH, self.cv2CaptureWidth) self.captureDevice.set(self.cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, self.cv2CaptureHeight) # init before work loop self.endThread = False if self.cmdLineOptions.vverbose: print "** gsatcomputerVisionThread start." while self.endThread != True: # capture frame frame = self.CaptureFrame() # sned frame to window, and wait... # wx.PostEvent(self.notifyWindow, gc.threadQueueEvent(None)) self.t2cvwQueue.put(gc.threadEvent(gEV_CMD_CV_IMAGE, frame)) self.t2cvwQueue.join() # sleep for a period time.sleep(self.cv2CapturePeriod / 1000) # process input queue for new commands or actions self.ProcessQueue() # check if we need to exit now if self.endThread: break self.captureDevice.release() if self.cmdLineOptions.vverbose: print "** gsatcomputerVisionThread exit."
def run(self): """Run Worker Thread.""" # This is the code executing in the new thread. self.endThread = False if self.cmdLineOptions.vverbose: print "** gsatSerialPortThread start." while(self.endThread != True): # process input queue for new commands or actions self.ProcessQueue() # check if we need to exit now if self.endThread: break if self.serPort.isOpen(): if self.swState == gc.gSTATE_RUN: self.SerialRead() elif self.swState == gc.gSTATE_ABORT: # do nothing, wait to be terminated pass else: if self.cmdLineOptions.verbose: print "** gsatSerialPortThread unexpected state [%d], moving back to IDLE." \ ", swState->gc.gSTATE_IDLE " % (self.swState) self.ProcessIdleSate() self.swState = gc.gSTATE_IDLE else: message ="** Serial Port is close, gsatSerialPortThread terminating.\n" if self.cmdLineOptions.verbose: print message.strip() # make sure we stop processing any states... self.swState = gc.gSTATE_ABORT # add data to queue self.serialThreadOutQueue.put(gc.threadEvent(gc.gEV_ABORT, "")) wx.LogMessage(message) break time.sleep(0.01) if self.cmdLineOptions.vverbose: print "** gsatSerialPortThread exit."
def run(self): """Run Worker Thread.""" # This is the code executing in the new thread. self.endThread = False if self.cmdLineOptions.vverbose: print "** gsatSerialPortThread start." while (self.endThread != True): # process input queue for new commands or actions self.ProcessQueue() # check if we need to exit now if self.endThread: break if self.serPort.isOpen(): if self.swState == gc.gSTATE_RUN: self.SerialRead() elif self.swState == gc.gSTATE_ABORT: # do nothing, wait to be terminated pass else: if self.cmdLineOptions.verbose: print "** gsatSerialPortThread unexpected state [%d], moving back to IDLE." \ ", swState->gc.gSTATE_IDLE " % (self.swState) self.ProcessIdleSate() self.swState = gc.gSTATE_IDLE else: message = "** Serial Port is close, gsatSerialPortThread terminating.\n" if self.cmdLineOptions.verbose: print message.strip() # make sure we stop processing any states... self.swState = gc.gSTATE_ABORT # add data to queue self.serialThreadOutQueue.put(gc.threadEvent(gc.gEV_ABORT, "")) wx.LogMessage(message) break time.sleep(0.01) if self.cmdLineOptions.vverbose: print "** gsatSerialPortThread exit."
def SerialRead(self): exFlag = False exMsg = "" serialData = "" try: inDataCnt = self.serPort.inWaiting() while inDataCnt > 0 and not exFlag: # read data from port # Was running with performance issues using readline(), move to read() # Using "".join() as performance is much better then "+=" #serialData = self.serPort.readline() #self.rxBuffer += self.serPort.read(inDataCnt) self.rxBuffer = "".join( [self.rxBuffer, self.serPort.read(inDataCnt)]) while '\n' in self.rxBuffer: serialData, self.rxBuffer = self.rxBuffer.split('\n', 1) if len(serialData) > 0: #pdb.set_trace() if self.cmdLineOptions.vverbose: print "[%03d] <- ASCII:{%s} HEX:{%s}" % ( len(serialData), serialData.strip(), ':'.join( x.encode('hex') for x in serialData)) elif self.cmdLineOptions.verbose: print "[%03d] <- %s" % (len(serialData), serialData.strip()) # add data to queue self.serialThreadOutQueue.put( gc.threadEvent(gc.gEV_SER_RXDATA, "%s\n" % serialData)) inDataCnt = self.serPort.inWaiting() except serial.SerialException, e: exMsg = "** PySerial exception: %s\n" % e.message exFlag = True
def StopCapture(self): self.capture = False if self.captureTimer is not None: self.captureTimer.Stop() if self.visionThread is not None: self.cvw2tQueue.put(gc.threadEvent(gEV_CMD_CV_EXIT, None)) goitem = False while (not self.t2cvwQueue.empty()): te = self.t2cvwQueue.get() goitem = True # make sure to unlock thread if goitem: self.t2cvwQueue.task_done() #self.cvw2tQueue.join() self.visionThread = None
def SerialWrite(self, serialData): exFlag = False exMsg = "" # sent data to UI self.progExecOutQueue.put(gc.threadEvent(gc.gEV_DATA_OUT, serialData)) try: # send command self.serPort.write(serialData.encode('ascii')) if self.cmdLineOptions.vverbose: print "[%03d] -> ASCII:{%s} HEX:{%s}" % (len(serialData), serialData.strip(), ':'.join(x.encode('hex') for x in serialData)) elif self.cmdLineOptions.verbose: print "[%03d] -> %s" % (len(serialData), serialData.strip()) except serial.SerialException, e: exMsg = "** PySerial exception: %s\n" % e.message exFlag = True
def RunStepSendGcode(self, gcodeData): gcode = gcodeData.strip() if len(gcode) > 0: gcode = "%s\n" % (gcode) # write data self.SerialWrite(gcode) # wait for response #responseData = self.WaitForResponse() self.WaitForAcknowledge() self.workingProgramCounter += 1 # if we stop early make sure to update PC to main UI if self.swState == gc.gSTATE_IDLE: self.progExecOutQueue.put(gc.threadEvent(gc.gEV_PC_UPDATE, self.workingProgramCounter)) wx.PostEvent(self.notifyWindow, gc.threadQueueEvent(None))
def SerialWrite(self, serialData): exFlag = False exMsg = "" # sent data to UI self.progExecOutQueue.put(gc.threadEvent(gc.gEV_DATA_OUT, serialData)) try: # send command self.serPort.write(serialData.encode('ascii')) if self.cmdLineOptions.vverbose: print "[%03d] -> ASCII:{%s} HEX:{%s}" % ( len(serialData), serialData.strip(), ':'.join( x.encode('hex') for x in serialData)) elif self.cmdLineOptions.verbose: print "[%03d] -> %s" % (len(serialData), serialData.strip()) except serial.SerialException, e: exMsg = "** PySerial exception: %s\n" % e.message exFlag = True
def SerialRead(self): exFlag = False exMsg = "" serialData = "" try: inDataCnt = self.serPort.inWaiting() while inDataCnt > 0 and not exFlag: # read data from port # Was running with performance issues using readline(), move to read() # Using "".join() as performance is much better then "+=" #serialData = self.serPort.readline() #self.rxBuffer += self.serPort.read(inDataCnt) self.rxBuffer = "".join([self.rxBuffer, self.serPort.read(inDataCnt)]) while '\n' in self.rxBuffer: serialData, self.rxBuffer = self.rxBuffer.split('\n', 1) if len(serialData) > 0: #pdb.set_trace() if self.cmdLineOptions.vverbose: print "[%03d] <- ASCII:{%s} HEX:{%s}" % (len(serialData), serialData.strip(), ':'.join(x.encode('hex') for x in serialData)) elif self.cmdLineOptions.verbose: print "[%03d] <- %s" % (len(serialData), serialData.strip()) # add data to queue self.serialThreadOutQueue.put(gc.threadEvent(gc.gEV_SER_RXDATA, "%s\n" % serialData)) inDataCnt = self.serPort.inWaiting() except serial.SerialException, e: exMsg = "** PySerial exception: %s\n" % e.message exFlag = True
def run(self): """Run Worker Thread.""" # This is the code executing in the new thread. self.endThread = False if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread start." # inti serial RX thread self.serialRxThread = gsatSerialPortThread(self, self.serPort, self.progExecSerialRxOutQueue, self.progExecSerialRxInQueue, self.cmdLineOptions) # init communication with device (helps to force tinyG into txt mode if self.deviceID == gc.gDEV_TINYG2 or self.deviceID == gc.gDEV_TINYG: self.SerialWrite(gc.gTINYG_CMD_GET_STATUS) elif self.deviceID == gc.gDEV_GRBL: self.SerialWrite(gc.gGRBL_CMD_GET_STATUS) self.SerialWrite(gc.gGRBL_CMD_GET_STATUS) while(self.endThread != True): # process input queue for new commands or actions self.ProcessQueue() # process write queue from UI cmds self.ProcessSerialWriteQueue() # check if we need to exit now if self.endThread: break if self.serPort.isOpen(): if self.swState == gc.gSTATE_RUN: self.ProcessRunSate() elif self.swState == gc.gSTATE_STEP: self.ProcessStepSate() elif self.swState == gc.gSTATE_IDLE: self.ProcessIdleSate() elif self.swState == gc.gSTATE_BREAK: self.ProcessIdleSate() elif self.swState == gc.gSTATE_ABORT: # do nothing, wait to be terminated pass else: if self.cmdLineOptions.verbose: print "** gsatProgramExecuteThread unexpected state [%d], moving back to IDLE." \ ", swState->gc.gSTATE_IDLE " % (self.swState) self.ProcessIdleSate() self.swState = gc.gSTATE_IDLE else: message ="** Serial Port is close, gsatProgramExecuteThread terminating.\n" if self.cmdLineOptions.verbose: print message.strip() # make sure we stop processing any states... self.swState = gc.gSTATE_ABORT # add data to queue and signal main window to consume self.progExecOutQueue.put(gc.threadEvent(gc.gEV_ABORT, "")) wx.PostEvent(self.notifyWindow, gc.threadQueueEvent(None)) wx.LogMessage(message) break time.sleep(0.02) if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread exit."
exMsg = "** OSError exception: %s\n" % str(e) exFlag = True except IOError, e: exMsg = "** IOError exception: %s\n" % str(e) exFlag = True if exFlag: # make sure we stop processing any states... self.swState = gc.gSTATE_ABORT if self.cmdLineOptions.verbose: print exMsg.strip() # add data to queue and signal main window self.progExecOutQueue.put(gc.threadEvent(gc.gEV_ABORT, exMsg)) wx.PostEvent(self.notifyWindow, gc.threadQueueEvent(None)) def DecodeStatusData(self, serialData): # ----------------------------------------------------------------- # Grbl if self.deviceID == gc.gDEV_GRBL: # GRBL status data rematch = gReGRBLMachineStatus.match(serialData) # data is expected to be an array of strings as follows # statusData[0] : Machine state # statusData[1] : Machine X # statusData[2] : Machine Y # statusData[3] : Machine Z
def DecodeStatusData (self, serialData): # ----------------------------------------------------------------- # Grbl if self.deviceID == gc.gDEV_GRBL: # GRBL status data rematch = gReGRBLMachineStatus.match(serialData) # data is expected to be an array of strings as follows # statusData[0] : Machine state # statusData[1] : Machine X # statusData[2] : Machine Y # statusData[3] : Machine Z # statusData[4] : Work X # statusData[5] : Work Y # statusData[6] : Work Z if rematch is not None: statusData = rematch.groups() machineStatus = dict() machineStatus['stat'] = statusData[0] machineStatus['posx'] = statusData[1] machineStatus['posy'] = statusData[2] machineStatus['posz'] = statusData[3] machineStatus['wposx'] = statusData[4] machineStatus['wposy'] = statusData[5] machineStatus['wposz'] = statusData[6] if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread re GRBL status match %s" % str(statusData) print "** gsatProgramExecuteThread str match from %s" % str(serialData.strip()) self.progExecOutQueue.put(gc.threadEvent(gc.gEV_DATA_STATUS, machineStatus)) elif self.deviceDetected == False: rematch = gReGrblVersion.match(serialData) if rematch is not None: self.deviceDetected = True self.progExecOutQueue.put(gc.threadEvent(gc.gEV_DEVICE_DETECTED, None)) # ----------------------------------------------------------------- # TinyG and TinyG2 if self.deviceID == gc.gDEV_TINYG or self.deviceID == gc.gDEV_TINYG2: if self.deviceDetected == False: rematch = gReTinyGDetect.findall(serialData) if len(rematch) > 0: self.deviceDetected = True self.progExecOutQueue.put(gc.threadEvent(gc.gEV_DEVICE_DETECTED, None)) # tinyG verbose/status rematch = gReTinyGVerbose.findall(serialData) if len(rematch) > 0: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread re tinyG verbose match %s" % str(rematch) print "** gsatProgramExecuteThread str match from %s" % str(serialData) machineStatus = dict(rematch) status = machineStatus.get('stat') if status is not None: if '0' in status: machineStatus['stat'] = 'Init' elif '1' in status: machineStatus['stat'] = 'Ready' elif '2' in status: machineStatus['stat'] = 'Alarm' elif '3' in status: machineStatus['stat'] = 'Stop' elif '4' in status: machineStatus['stat'] = 'End' elif '5' in status: machineStatus['stat'] = 'Run' elif '6' in status: machineStatus['stat'] = 'Hold' elif '7' in status: machineStatus['stat'] = 'Probe' elif '8' in status: machineStatus['stat'] = 'Run' elif '9' in status: machineStatus['stat'] = 'Home' self.progExecOutQueue.put(gc.threadEvent(gc.gEV_DATA_STATUS, machineStatus)) else: rematch = [] if self.deviceID == gc.gDEV_TINYG2: rematch = gReTinyG2PosStatus.findall(serialData) # newer version (g2core) moved to TinyG style if len(rematch) == 0: rematch = gReTinyGPosStatus.findall(serialData) elif self.deviceID == gc.gDEV_TINYG: rematch = gReTinyGPosStatus.findall(serialData) if len(rematch) > 0: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread re tinyG status match %s" % str(rematch) print "** gsatProgramExecuteThread str match from %s" % str(serialData) machineStatus = dict() if self.deviceID == gc.gDEV_TINYG2: machineStatus["mpo%s" % rematch[0][0].lower()] = rematch[0][1] elif self.deviceID == gc.gDEV_TINYG: machineStatus["pos%s" % rematch[0][0].lower()] = rematch[0][1] self.progExecOutQueue.put(gc.threadEvent(gc.gEV_DATA_STATUS, machineStatus)) else: rematch = gReTinyGStateStatus.findall(serialData) if len(rematch) > 0: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread re tinyG status match %s" % str(serialData) machineStatus = dict() machineStatus["stat"] = rematch[0][1] self.progExecOutQueue.put(gc.threadEvent(gc.gEV_DATA_STATUS, machineStatus))
def ProcessQueue(self): # check output queue and notify UI if is not empty if not self.progExecOutQueue.empty(): if self.okToPostEvents: self.okToPostEvents = False wx.PostEvent(self.notifyWindow, gc.threadQueueEvent(None)) # process events from queue --------------------------------------------- if not self.progExecInQueue.empty(): # get item from queue e = self.progExecInQueue.get() self.lastEventID = e.event_id if e.event_id == gc.gEV_CMD_EXIT: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got event gc.gEV_CMD_EXIT." self.endThread = True self.swState = gc.gSTATE_IDLE if self.serialRxThread is not None: self.progExecSerialRxOutQueue.put(gc.threadEvent(gc.gEV_CMD_EXIT, None)) elif e.event_id == gc.gEV_CMD_RUN: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got event gc.gEV_CMD_RUN, swState->gc.gSTATE_RUN" self.gcodeDataLines = e.data[0] self.initialProgramCounter = e.data[1] self.workingProgramCounter = self.initialProgramCounter self.breakPointSet = e.data[2] self.swState = gc.gSTATE_RUN elif e.event_id == gc.gEV_CMD_STEP: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got event gc.gEV_CMD_STEP, swState->gc.gSTATE_STEP" self.gcodeDataLines = e.data[0] self.initialProgramCounter = e.data[1] self.workingProgramCounter = self.initialProgramCounter self.breakPointSet = e.data[2] self.swState = gc.gSTATE_STEP elif e.event_id == gc.gEV_CMD_STOP: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got event gc.gEV_CMD_STOP, swState->gc.gSTATE_IDLE" self.swState = gc.gSTATE_IDLE elif e.event_id == gc.gEV_CMD_SEND: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got event gc.gEV_CMD_SEND." self.serialWriteQueue.append((e.data, False)) elif e.event_id == gc.gEV_CMD_SEND_W_ACK: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got event gc.gEV_CMD_SEND_W_ACK." self.serialWriteQueue.append((e.data, True)) elif e.event_id == gc.gEV_CMD_AUTO_STATUS: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got event gc.gEV_CMD_AUTO_STATUS." self.machineAutoStatus = e.data elif e.event_id == gc.gEV_CMD_OK_TO_POST: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got event gc.gEV_CMD_OK_TO_POST." self.okToPostEvents = True else: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got unknown event!! [%s]." % str(e.event_id)
exMsg = "** OSError exception: %s\n" % str(e) exFlag = True except IOError, e: exMsg = "** IOError exception: %s\n" % str(e) exFlag = True if exFlag: # make sure we stop processing any states... self.swState = gc.gSTATE_ABORT if self.cmdLineOptions.verbose: print exMsg.strip() # add data to queue and signal main window self.progExecOutQueue.put(gc.threadEvent(gc.gEV_ABORT, exMsg)) wx.PostEvent(self.notifyWindow, gc.threadQueueEvent(None)) def DecodeStatusData (self, serialData): # ----------------------------------------------------------------- # Grbl if self.deviceID == gc.gDEV_GRBL: # GRBL status data rematch = gReGRBLMachineStatus.match(serialData) # data is expected to be an array of strings as follows # statusData[0] : Machine state # statusData[1] : Machine X # statusData[2] : Machine Y
def ProcessQueue(self): # check output queue and notify UI if is not empty if not self.progExecOutQueue.empty(): if self.okToPostEvents: self.okToPostEvents = False wx.PostEvent(self.notifyWindow, gc.threadQueueEvent(None)) # process events from queue --------------------------------------------- if not self.progExecInQueue.empty(): # get item from queue e = self.progExecInQueue.get() self.lastEventID = e.event_id if e.event_id == gc.gEV_CMD_EXIT: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got event gc.gEV_CMD_EXIT." self.endThread = True self.swState = gc.gSTATE_IDLE if self.serialRxThread is not None: self.progExecSerialRxOutQueue.put( gc.threadEvent(gc.gEV_CMD_EXIT, None)) elif e.event_id == gc.gEV_CMD_RUN: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got event gc.gEV_CMD_RUN, swState->gc.gSTATE_RUN" self.gcodeDataLines = e.data[0] self.initialProgramCounter = e.data[1] self.workingProgramCounter = self.initialProgramCounter self.breakPointSet = e.data[2] self.swState = gc.gSTATE_RUN elif e.event_id == gc.gEV_CMD_STEP: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got event gc.gEV_CMD_STEP, swState->gc.gSTATE_STEP" self.gcodeDataLines = e.data[0] self.initialProgramCounter = e.data[1] self.workingProgramCounter = self.initialProgramCounter self.breakPointSet = e.data[2] self.swState = gc.gSTATE_STEP elif e.event_id == gc.gEV_CMD_STOP: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got event gc.gEV_CMD_STOP, swState->gc.gSTATE_IDLE" self.swState = gc.gSTATE_IDLE elif e.event_id == gc.gEV_CMD_SEND: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got event gc.gEV_CMD_SEND." self.serialWriteQueue.append((e.data, False)) elif e.event_id == gc.gEV_CMD_SEND_W_ACK: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got event gc.gEV_CMD_SEND_W_ACK." self.serialWriteQueue.append((e.data, True)) elif e.event_id == gc.gEV_CMD_AUTO_STATUS: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got event gc.gEV_CMD_AUTO_STATUS." self.machineAutoStatus = e.data elif e.event_id == gc.gEV_CMD_OK_TO_POST: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got event gc.gEV_CMD_OK_TO_POST." self.okToPostEvents = True else: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got unknown event!! [%s]." % str( e.event_id)
def DecodeStatusData(self, serialData): # ----------------------------------------------------------------- # Grbl if self.deviceID == gc.gDEV_GRBL: # GRBL status data rematch = gReGRBLMachineStatus.match(serialData) # data is expected to be an array of strings as follows # statusData[0] : Machine state # statusData[1] : Machine X # statusData[2] : Machine Y # statusData[3] : Machine Z # statusData[4] : Work X # statusData[5] : Work Y # statusData[6] : Work Z if rematch is not None: statusData = rematch.groups() machineStatus = dict() machineStatus['stat'] = statusData[0] machineStatus['posx'] = statusData[1] machineStatus['posy'] = statusData[2] machineStatus['posz'] = statusData[3] machineStatus['wposx'] = statusData[4] machineStatus['wposy'] = statusData[5] machineStatus['wposz'] = statusData[6] if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread re GRBL status match %s" % str( statusData) print "** gsatProgramExecuteThread str match from %s" % str( serialData.strip()) self.progExecOutQueue.put( gc.threadEvent(gc.gEV_DATA_STATUS, machineStatus)) elif self.deviceDetected == False: rematch = gReGrblVersion.match(serialData) if rematch is not None: self.deviceDetected = True self.progExecOutQueue.put( gc.threadEvent(gc.gEV_DEVICE_DETECTED, None)) # ----------------------------------------------------------------- # TinyG and TinyG2 if self.deviceID == gc.gDEV_TINYG or self.deviceID == gc.gDEV_TINYG2: if self.deviceDetected == False: rematch = gReTinyGDetect.findall(serialData) if len(rematch) > 0: self.deviceDetected = True self.progExecOutQueue.put( gc.threadEvent(gc.gEV_DEVICE_DETECTED, None)) # tinyG verbose/status rematch = gReTinyGVerbose.findall(serialData) if len(rematch) > 0: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread re tinyG verbose match %s" % str( rematch) print "** gsatProgramExecuteThread str match from %s" % str( serialData) machineStatus = dict(rematch) status = machineStatus.get('stat') if status is not None: if '0' in status: machineStatus['stat'] = 'Init' elif '1' in status: machineStatus['stat'] = 'Ready' elif '2' in status: machineStatus['stat'] = 'Alarm' elif '3' in status: machineStatus['stat'] = 'Stop' elif '4' in status: machineStatus['stat'] = 'End' elif '5' in status: machineStatus['stat'] = 'Run' elif '6' in status: machineStatus['stat'] = 'Hold' elif '7' in status: machineStatus['stat'] = 'Probe' elif '8' in status: machineStatus['stat'] = 'Run' elif '9' in status: machineStatus['stat'] = 'Home' self.progExecOutQueue.put( gc.threadEvent(gc.gEV_DATA_STATUS, machineStatus)) else: rematch = [] if self.deviceID == gc.gDEV_TINYG2: rematch = gReTinyG2PosStatus.findall(serialData) # newer version (g2core) moved to TinyG style if len(rematch) == 0: rematch = gReTinyGPosStatus.findall(serialData) elif self.deviceID == gc.gDEV_TINYG: rematch = gReTinyGPosStatus.findall(serialData) if len(rematch) > 0: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread re tinyG status match %s" % str( rematch) print "** gsatProgramExecuteThread str match from %s" % str( serialData) machineStatus = dict() if self.deviceID == gc.gDEV_TINYG2: machineStatus["mpo%s" % rematch[0][0].lower()] = rematch[0][1] elif self.deviceID == gc.gDEV_TINYG: machineStatus["pos%s" % rematch[0][0].lower()] = rematch[0][1] self.progExecOutQueue.put( gc.threadEvent(gc.gEV_DATA_STATUS, machineStatus)) else: rematch = gReTinyGStateStatus.findall(serialData) if len(rematch) > 0: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread re tinyG status match %s" % str( serialData) machineStatus = dict() machineStatus["stat"] = rematch[0][1] self.progExecOutQueue.put( gc.threadEvent(gc.gEV_DATA_STATUS, machineStatus))
def run(self): """Run Worker Thread.""" # This is the code executing in the new thread. self.endThread = False if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread start." # inti serial RX thread self.serialRxThread = gsatSerialPortThread( self, self.serPort, self.progExecSerialRxOutQueue, self.progExecSerialRxInQueue, self.cmdLineOptions) # init communication with device (helps to force tinyG into txt mode if self.deviceID == gc.gDEV_TINYG2 or self.deviceID == gc.gDEV_TINYG: self.SerialWrite(gc.gTINYG_CMD_GET_STATUS) elif self.deviceID == gc.gDEV_GRBL: self.SerialWrite(gc.gGRBL_CMD_GET_STATUS) self.SerialWrite(gc.gGRBL_CMD_GET_STATUS) while (self.endThread != True): # process input queue for new commands or actions self.ProcessQueue() # process write queue from UI cmds self.ProcessSerialWriteQueue() # check if we need to exit now if self.endThread: break if self.serPort.isOpen(): if self.swState == gc.gSTATE_RUN: self.ProcessRunSate() elif self.swState == gc.gSTATE_STEP: self.ProcessStepSate() elif self.swState == gc.gSTATE_IDLE: self.ProcessIdleSate() elif self.swState == gc.gSTATE_BREAK: self.ProcessIdleSate() elif self.swState == gc.gSTATE_ABORT: # do nothing, wait to be terminated pass else: if self.cmdLineOptions.verbose: print "** gsatProgramExecuteThread unexpected state [%d], moving back to IDLE." \ ", swState->gc.gSTATE_IDLE " % (self.swState) self.ProcessIdleSate() self.swState = gc.gSTATE_IDLE else: message = "** Serial Port is close, gsatProgramExecuteThread terminating.\n" if self.cmdLineOptions.verbose: print message.strip() # make sure we stop processing any states... self.swState = gc.gSTATE_ABORT # add data to queue and signal main window to consume self.progExecOutQueue.put(gc.threadEvent(gc.gEV_ABORT, "")) wx.PostEvent(self.notifyWindow, gc.threadQueueEvent(None)) wx.LogMessage(message) break time.sleep(0.02) if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread exit."
def ProcessQueue(self): # process events from queue --------------------------------------------- if not self.progExecInQueue.empty(): # get item from queue e = self.progExecInQueue.get() self.lastEventID = e.event_id if e.event_id == gc.gEV_CMD_EXIT: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got event gc.gEV_CMD_EXIT." self.endThread = True self.swState = gc.gSTATE_IDLE if self.serialRxThread is not None: self.progExecSerialRxOutQueue.put(gc.threadEvent(gc.gEV_CMD_EXIT, None)) self.progExecSerialRxOutQueue.join() elif e.event_id == gc.gEV_CMD_RUN: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got event gc.gEV_CMD_RUN, swState->gc.gSTATE_RUN" self.gcodeDataLines = e.data[0] self.initialProgramCounter = e.data[1] self.workingProgramCounter = self.initialProgramCounter self.breakPointSet = e.data[2] self.swState = gc.gSTATE_RUN elif e.event_id == gc.gEV_CMD_STEP: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got event gc.gEV_CMD_STEP, swState->gc.gSTATE_STEP" self.gcodeDataLines = e.data[0] self.initialProgramCounter = e.data[1] self.workingProgramCounter = self.initialProgramCounter self.breakPointSet = e.data[2] self.swState = gc.gSTATE_STEP elif e.event_id == gc.gEV_CMD_STOP: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got event gc.gEV_CMD_STOP, swState->gc.gSTATE_IDLE" self.swState = gc.gSTATE_IDLE elif e.event_id == gc.gEV_CMD_SEND: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got event gc.gEV_CMD_SEND." self.SerialWrite(e.data) elif e.event_id == gc.gEV_CMD_SEND_W_ACK: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got event gc.gEV_CMD_SEND_W_ACK." self.SerialWrite(e.data) #responseData = self.WaitForResponse() self.WaitForAcknowledge() elif e.event_id == gc.gEV_CMD_AUTO_STATUS: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got event gc.gEV_CMD_AUTO_STATUS." self.machineAutoStatus = e.data else: if self.cmdLineOptions.vverbose: print "** gsatProgramExecuteThread got unknown event!! [%s]." % str(e.event_id) # item qcknowledge self.progExecInQueue.task_done()