Example #1
0
    def optimize(self, statements):
        cur_statement = GStatement()
        nstatements = [cur_statement]
        state = {x:None for x in self.move_desc}
        save = False
        for statement in statements:
            for code in statement:
                if code.address == 'G' and code.command in (0,1):
                    save = True
                elif code.address in ('G', 'M'):
                    state = {x:None for x in self.move_desc}
                    save = False

                if not save:
                    cur_statement.append(code)
                    continue

                if code.address in self.move_desc:
                    if code.command != state[code.address]:
                        cur_statement.append(code)
                        state[code.address] = code.command
                else:
                    cur_statement.append(code)

            cur_statement = GStatement()
            nstatements.append(cur_statement)
        return nstatements
Example #2
0
def parse(code, ifile, dump, ofile, stats, noopt):
    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))
    codes.insert(0, absolute)

    if stats:
        print(generate_stats(codes), file=sys.stderr)

    s = []
    for statement in codes:
        s.append(str(statement))

    if ofile:
        ofile.write('\n'.join(s))
    if dump:
        print('\n'.join(s), )
Example #3
0
    def optimize(self, statements):
        cur_statement = GStatement()
        nstatements = [cur_statement]
        cur_code = None
        for statement in statements:
            for code in statement:
                if code.address in self.move_desc:
                    cur_statement.append(code)
                elif code == cur_code:
                    pass
                elif code.address == 'G' and code.command >= 0 and code.command <= 3:
                    cur_code = code
                    cur_statement.append(code)
                else:
                    cur_code = None
                    cur_statement.append(code)

            cur_statement = GStatement()
            nstatements.append(cur_statement)
        return nstatements
Example #4
0
    def optimize(self, statements):
        cur_statement = GStatement()
        nstatements = [cur_statement]
        for statement in statements:
            for code in statement:
                if code.address == 'M':
                    cur_statement = GStatement()
                    nstatements.append(cur_statement)
                cur_statement.append(code)

            cur_statement = GStatement()
            nstatements.append(cur_statement)
        return nstatements
Example #5
0
    def optimize(self, statements):
        cur_statement = GStatement()
        nstatements = [cur_statement]
        last_val = None
        for statement in statements:
            for code in statement:
                if code.address == 'F':
                    if code.command != last_val:
                        s = GStatement(code)
                        nstatements.insert(-1, s)
                        last_val = code.command
                else:
                    cur_statement.append(code)

            cur_statement = GStatement()
            nstatements.append(cur_statement)
        return nstatements
Example #6
0
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