def send(code, ifile, device, baudrate, measure, yes, noopt, stats, zero): if not ifile and not code: print("Need either file or code") return -1 if ifile: code = ifile.read() else: code = "\n".join(code.split(";")) codes = parse_and_optimize(code, noopt) absolute = GStatement(GCode("G", 90)) spindle_start = GStatement(GCode("M", 3)) codes.insert(0, absolute) codes.insert(0, spindle_start) if measure == "metric": adjust = GCode("G", 21) else: adjust = GCode("G", 20) codes.insert(0, GStatement(adjust)) if zero: zero = GStatement(GCode("G", 0), GCode("Z", 0), GCode("X", 0), GCode("Y", 0)) codes.append(zero) codes.append(GStatement(GCode("M", 5))) if stats: print(generate_stats(codes), file=sys.stderr) if not yes: print() x = None while x != "y": x = raw_input("Start? (y/n) ") if x == "n": print("Aborting") return -1 cnc = CNC(device, baudrate) cnc.add_codes(*codes) cnc.onprogress, cnc.oncomplete = make_progressbar(len(cnc), "Buffer: ") cnc.onalarm = lambda x: print("\nalarm: %s, %s" % (x, cnc.cur)) cnc.onerror = lambda x: print("\nerror: %s, %s" % (x, cnc.cur)) try: cnc.connect() cnc.send_queue() except KeyboardInterrupt: print() print("Interrupted") print("Raising position alarm") cnc.halt() return -1 return 0
def main(tools = [1] * 8, step = 1, is_error = False): cnc_arr = [CNC(i+1, tools[i], is_error) for i in range(0,8)] rgv = RGV() work_num = 1 for i in range(0, 8*60*60): if rgv.state == 0: # 检查是否持有物料 if rgv.target_work == None: rgv.target_work = Work(work_num,step) work_num = work_num + 1 # 筛选故障cnc和不同加工工序的cnc temp_cnc_arr = [] for cnc in cnc_arr: if cnc.trouble_time != 0: continue if rgv.target_work.step + 1 == cnc.tool: temp_cnc_arr.append(cnc) # 打擂找出最佳目标cnc min_time = float('inf') best_cnc = None if get_has_minus(temp_cnc_arr,rgv): # 有负数情况 for cnc in temp_cnc_arr: # 去除所有正数 if cnc.work_timer - get_move_time(cnc.position, rgv.position) > 0: continue # 求出放置移动时间加上下料时间最小的数 place_time = k_even_place_time if cnc.num % 2 == 0 else k_odd_place_time if get_move_time(cnc.position, rgv.position) + place_time < min_time: min_time = get_move_time(cnc.position, rgv.position) + place_time best_cnc = cnc else: # 全为正数 # 求出剩余工作时间加上上下料时间最短 for cnc in temp_cnc_arr: place_time = k_even_place_time if cnc.num % 2 == 0 else k_odd_place_time temp_time = cnc.work_timer + place_time if temp_time < min_time: min_time = temp_time best_cnc = cnc # 未找到最佳cnc则退出 if not best_cnc:return (rgv,cnc_arr) # 对rgv下达指令 rgv.move_to_position(best_cnc.position) if best_cnc.work_timer > 0: rgv.wait() else: rgv.place(best_cnc) # cnc,rgv 执行指令 for cnc in cnc_arr:cnc.execute() rgv.execute() return (rgv,cnc_arr)
def buildModel(self): cnc = CNC(self.settings.acceleration, self.settings.layerheight) if RECORD_TIMES: self.log("recording g code times in /tmp/gcodeTimes") fp = open("/tmp/gcodeTimes", "w") ln = -1 for gl in self.gcode: ln += 1 p = re.split("\\s+", gl, 1) params = {} if not (p[0].strip() in ["M117", "m117"]): if len(p) >= 2: self.paramStr = p[1] if "X" in self.paramStr: params["X"] = self._get_float("X") if "Y" in self.paramStr: params["Y"] = self._get_float("Y") if "Z" in self.paramStr: params["Z"] = self._get_float("Z") if "E" in self.paramStr: params["E"] = self._get_float("E") if "F" in self.paramStr: params["F"] = self._get_float("F") if "S" in self.paramStr: params["S"] = self._get_float("S") if "P" in self.paramStr: params["P"] = self._get_float("P") t = cnc.execute(p[0], params, ln) if RECORD_TIMES: fp.write("(%s) (%.3f)\n" % (gl, t)) if RECORD_TIMES: fp.close() gobj = cnc.getGObject() gobj.setMaxLine(ln) self.maxTool = cnc.getMaxTool() self.totalTime, self.layerTimes = cnc.getTimes() self.layerMap = [] for lx in range(len(gobj)): self.layerMap.append(gobj.getGCodeLines(lx)) self.totalTimeStr = formatElapsed(self.totalTime) self.layerTimeStr = [formatElapsed(s) for s in self.layerTimes] return gobj
def buildModel(self): rgcode = [s.rstrip() for s in self.gcode] cnc = CNC(self.settings.acceleration, self.settings.layerheight) ln = -1 for gl in rgcode: ln += 1 if ";" in gl: gl = gl.split(";")[0] if gl.strip() == "": continue p = re.split("\\s+", gl, 1) params = {} if not (p[0].strip() in ["M117", "m117"]): if len(p) >= 2: self.paramStr = p[1] if "X" in self.paramStr: params["X"] = self._get_float("X") if "Y" in self.paramStr: params["Y"] = self._get_float("Y") if "Z" in self.paramStr: params["Z"] = self._get_float("Z") if "E" in self.paramStr: params["E"] = self._get_float("E") if "F" in self.paramStr: params["F"] = self._get_float("F") if "S" in self.paramStr: params["S"] = self._get_float("S") cnc.execute(p[0], params, ln) gobj = cnc.getGObject() gobj.setMaxLine(ln) self.totalTime, self.layerTimes = cnc.getTimes() self.totalTimeStr = formatElapsed(self.totalTime) self.layerTimeStr = [formatElapsed(s) for s in self.layerTimes] return gobj
def __clean_data(self): self.path = [] # 当前蚂蚁的路径 self.total_distance = 0.0 # 当前路径的总距离 self.move_count = 0 # 移动次数 self.current_city = -1 # 当前停留的城市 # self.open_table_city = [True for i in xrange(city_num)] # 探索城市的状态 city_index = random.randint(0, 1) # 随机初始出生点 因为rgv是固定0或1位置出发] self.current_city = city_index self.path.append(city_index) # self.open_table_city[city_index] = False self.move_count = 1 self.available_cnc = [i for i in range(0, 8)] self.clock = 0 self.cncs = [] for i in range(0, 8): self.cncs.append(CNC(time_process_single, time_process_single))
app["cnc"].poll() except: log.warning("error handle request") log.warning(traceback.format_exc()) await asyncio.sleep(seconds) loop = asyncio.get_event_loop() app = web.Application(loop=loop) app.add_routes([ web.get("/", index), web.get("/ws", websocket), ]) app.router.add_static("/", path="./static") app["loop"] = loop asyncio.ensure_future(periodic(app, 1.0), loop=loop) with open("config.json", "r") as f: config = json.load(f) try: port = int(sys.argv[sys.argv.index("--port") + 1]) except: port = 8080 with CNC(config, dummy="--dummy" in sys.argv) as cnc: app["cnc"] = cnc web.run_app(app, port=port)
def alarm(device, baudrate): cnc = CNC(device, baudrate) cnc.connect() cnc.halt()
def send(code, ifile, device, baudrate, measure, yes, noopt, stats, zero): if not ifile and not code: print("Need either file or code") return -1 if ifile: code = ifile.read() else: code = '\n'.join(code.split(';')) codes = parse_and_optimize(code, noopt) absolute = GStatement(GCode('G', 90)) spindle_start = GStatement(GCode('M', 3)) codes.insert(0, absolute) codes.insert(0, spindle_start) if measure == 'metric': adjust = GCode('G', 21) else: adjust = GCode('G', 20) codes.insert(0, GStatement(adjust)) if zero: zero = GStatement(GCode('G', 0), GCode('Z', 0), GCode('X', 0), GCode('Y', 0)) codes.append(zero) codes.append(GStatement(GCode('M', 5))) if stats: print(generate_stats(codes), file=sys.stderr) if not yes: print() x = None while x != 'y': x = raw_input('Start? (y/n) ') if x == 'n': print('Aborting') return -1 cnc = CNC(device, baudrate) cnc.add_codes(*codes) cnc.onprogress, cnc.oncomplete = make_progressbar(len(cnc), 'Buffer: ') cnc.onalarm = lambda x: print('\nalarm: %s, %s' % (x, cnc.cur)) cnc.onerror = lambda x: print('\nerror: %s, %s' % (x, cnc.cur)) try: cnc.connect() cnc.send_queue() except KeyboardInterrupt: print() print('Interrupted') print('Raising position alarm') cnc.halt() return -1 return 0