File size: 5,131 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# SPDX-License-Identifier: LGPL-2.1-or-later
# ***************************************************************************
# *   Copyright (c) 2021 sliptonic shopinthewoods@gmail.com                 *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   This program 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 Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with this program; if not, write to the Free Software   *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# ***************************************************************************

__title__ = "CAM Machine State"
__author__ = "sliptonic (Brad Collette)"
__url__ = "https://www.freecad.org"
__doc__ = "Dataclass to implement a machinestate tracker"
__contributors__ = ""

import Path
import FreeCAD
import Path

if False:
    Path.Log.setLevel(Path.Log.Level.DEBUG, Path.Log.thisModule())
    Path.Log.trackModule(Path.Log.thisModule())
else:
    Path.Log.setLevel(Path.Log.Level.INFO, Path.Log.thisModule())


class MachineState:
    def __init__(self):
        self.WCSLIST = [
            "G53",
            "G54",
            "G55",
            "G56",
            "G57",
            "G58",
            "G59",
            "G59.1",
            "G59.2",
            "G59.3",
            "G59.4",
            "G59.5",
            "G59.6",
            "G59.7",
            "G59.8",
            "G59.9",
        ]

        self.X = 0.0  #: float = field(default=0)
        self.Y = 0.0  #: float = field(default=0)
        self.Z = 0.0  #: float = field(default=0)
        self.A = 0.0  #: float = field(default=0)
        self.B = 0.0  #: float = field(default=0)
        self.C = 0.0  #: float = field(default=0)
        self.F = 0.0  #: float = field(default=None)
        self.Coolant = False  #: bool = field(default=False)
        self.WCS = "G54"  #: str = field(default="G54")
        self.Spindle = "off"  #: str = field(default="off")
        self.S = 0  #: int = field(default=0)
        self.T = None  #: int = field(default=None)

    def addCommand(self, command):
        """Processes a command and updates the internal state of the machine. Returns true if the command has alterned the machine state"""
        oldstate = self.getState()
        if command.Name == "M6":
            self.T = int(command.Parameters["T"])
            return not oldstate == self.getState()

        if command.Name in ["M3", "M4"]:
            self.S = command.Parameters["S"]
            self.Spindle = "CW" if command.Name == "M3" else "CCW"
            return not oldstate == self.getState()

        if command.Name in ["M2", "M5"]:
            self.S = 0
            self.Spindle = "off"
            return not oldstate == self.getState()

        if command.Name in self.WCSLIST:
            self.WCS = command.Name
            return not oldstate == self.getState()

        if command.Name in Path.Geom.CmdMoveDrill:
            oldZ = self.Z
            for p in command.Parameters:
                self.__setattr__(p, command.Parameters[p])
            self.__setattr__("Z", oldZ)
            return not oldstate == self.getState()

        for p in command.Parameters:
            self.__setattr__(p, command.Parameters[p])

        return not oldstate == self.getState()

    def getState(self):
        """
        Returns a dictionary of the current machine state
        """
        state = {}
        state["X"] = self.X
        state["Y"] = self.Y
        state["Z"] = self.Z
        state["A"] = self.A
        state["B"] = self.B
        state["C"] = self.C
        state["F"] = self.F
        state["Coolant"] = self.Coolant
        state["WCS"] = self.WCS
        state["Spindle"] = self.Spindle
        state["S"] = self.S
        state["T"] = self.T

        return state

    def getPosition(self):
        """
        Returns a vector of the current machine position
        """

        # This is technical debt.  The actual position may include a rotation
        # component as well.  We should probably be returning a placement
        return FreeCAD.Vector(self.X, self.Y, self.Z)