GCodeAnalyzer.py 7.28 KB
Newer Older
fsantini's avatar
fsantini committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
# This file is part of the Printrun suite.
#
# Copyright 2013 Francesco Santini francesco.santini@gmail.com
#
# Printrun is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Printrun is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Printrun.  If not, see <http://www.gnu.org/licenses/>.
#
# This code is imported from RepetierHost - Original copyright and license:
# Copyright 2011 repetier repetierdev@gmail.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import re
34
import gcoder
fsantini's avatar
fsantini committed
35 36

class GCodeAnalyzer():
37 38 39 40
    def __init__(self):
        self.x = 0
        self.y = 0
        self.z = 0
41
        self.e = 0
42 43 44 45 46 47
        self.emax = 0
        self.f = 1000
        self.lastX = 0
        self.lastY = 0
        self.lastZ = 0
        self.lastE = 0
48 49 50
        self.xOffset = 0
        self.yOffset = 0
        self.zOffset = 0
51 52 53
        self.eOffset = 0
        self.lastZPrint = 0
        self.layerZ = 0
54
        self.imperial = False
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
        self.relative = False
        self.eRelative = False
        self.homeX = 0
        self.homeY = 0
        self.homeZ = 0
        self.maxX = 150
        self.maxY = 150
        self.maxZ = 150
        self.minX = 0
        self.minY = 0
        self.minZ = 0
        self.hasHomeX = False
        self.hasHomeY = False
        self.hasHomeZ = False

    def Analyze(self, gcode):
        gline = gcoder.Line(gcode)
72
        split_raw = gcoder.split(gline)
73
        if gline.command.startswith(";@"): return # code is a host command
74
        gcoder.parse_coordinates(gline, split_raw, self.imperial)
75 76
        code_g = int(gline.command[1:]) if gline.command.startswith("G") else None
        code_m = int(gline.command[1:]) if gline.command.startswith("M") else None
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

        #get movement codes
        if gline.is_move:
            self.lastX = self.x
            self.lastY = self.y
            self.lastZ = self.z
            self.lastE = self.e
            eChanged = False;
            code_f = gline.f
            if code_f != None:
                self.f = code_f

            code_x = gline.x
            code_y = gline.y
            code_z = gline.z
            code_e = gline.e

            if self.relative:
                if code_x != None: self.x += code_x
                if code_y != None: self.y += code_y
                if code_z != None: self.z += code_z
                if code_e != None:
                    if code_e != 0:
                        eChanged = True
                        self.e += code_e
            else:
103
                # absolute coordinates
104 105 106 107 108 109 110 111 112 113 114 115 116 117
                if code_x != None: self.x = self.xOffset + code_x
                if code_y != None: self.y = self.yOffset + code_y
                if code_z != None: self.z = self.zOffset + code_z
                if code_e != None:
                    if self.eRelative:
                        if code_e != 0:
                            eChanged = True
                            self.e += code_e
                    else:
                    # e is absolute. Is it changed?
                        if self.e != self.eOffset + code_e:
                            eChanged = True
                            self.e = self.eOffset + code_e
            #limit checking
118
            """
119 120 121 122 123 124 125
            if self.x < self.minX: self.x = self.minX
            if self.y < self.minY: self.y = self.minY
            if self.z < self.minZ: self.z = self.minZ

            if self.x > self.maxX: self.x = self.maxX
            if self.y > self.maxY: self.y = self.maxY
            if self.z > self.maxZ: self.z = self.maxZ
126
            """
127
            #Repetier has a bunch of limit-checking code here and time calculations: we are leaving them for now
128 129
        elif code_g == 20: self.imperial = True
        elif code_g == 21: self.imperial = False
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
        elif code_g == 28 or code_g == 161:
            self.lastX = self.x
            self.lastY = self.y
            self.lastZ = self.z
            self.lastE = self.e
            code_x = gline.x
            code_y = gline.y
            code_z = gline.z
            code_e = gline.e
            homeAll = False
            if code_x == None and code_y == None and code_z == None: homeAll = True
            if code_x != None or homeAll:
                self.hasHomeX = True
                self.xOffset = 0
                self.x = self.homeX
            if code_y != None or homeAll:
                self.hasHomeY = True
                self.yOffset = 0
                self.y = self.homeY
            if code_z != None or homeAll:
                self.hasHomeZ = True
                self.zOffset = 0
                self.z = self.homeZ
            if code_e != None:
                self.eOffset = 0
                self.e = 0
        elif code_g == 162:
            self.lastX = self.x
            self.lastY = self.y
            self.lastZ = self.z
            self.lastE = self.e
            code_x = gline.x
            code_y = gline.y
            code_z = gline.z
            homeAll = False
            if code_x == None and code_y == None and code_z == None: homeAll = True
            if code_x != None or homeAll:
                self.hasHomeX = True
                self.xOffset = 0
                self.x = self.maxX
            if code_y != None or homeAll:
                self.hasHomeY = True
                self.yOffset = 0
                self.y = self.maxY
            if code_z != None or homeAll:
                self.hasHomeZ = True
                self.zOffset = 0
                self.z = self.maxZ
        elif code_g == 90: self.relative = False
        elif code_g == 91: self.relative = True
        elif code_g == 92:
            code_x = gline.x
            code_y = gline.y
            code_z = gline.z
            code_e = gline.e
            if code_x != None:
                self.xOffset = self.x - float(code_x)
                self.x = self.xOffset
            if code_y != None:
                self.yOffset = self.y - float(code_y)
                self.y = self.yOffset
            if code_z != None:
                self.zOffset = self.z - float(code_z)
                self.z = self.zOffset
            if code_e != None:
                self.xOffset = self.e - float(code_e)
                self.e = self.eOffset
            #End code_g != None
            if code_m != None:
                if code_m == 82: self.eRelative = False
                elif code_m == 83: self.eRelative = True

    def print_status(self):
        attrs = vars(self)
        print '\n'.join("%s: %s" % item for item in attrs.items())