@@ -7,227 +7,18 @@ GCODE Parser for Python
77Currently in development, ``pygcode `` is a low-level GCode interpreter
88for python.
99
10+
1011Installation
1112============
1213
13- Using ` PyPi < https://pypi.python.org/pypi/pydemia >`__:
14+ Install using `` pip ``
1415
1516``pip install pygcode ``
1617
17- Usage
18- =====
19-
20- Just brainstorming here...
21-
22- Writing GCode
23- -------------
24-
25- Writing gcode from python object instances to text
26-
27- ::
28-
29- >>> from pygcode import *
30- >>> gcodes = [
31- ... GCodeRapidMove(Z=5),
32- ... GCodeStartSpindleCW(),
33- ... GCodeRapidMove(X=10, Y=20),
34- ... GCodeFeedRate(200),
35- ... GCodeLinearMove(Z=-1.5),
36- ... GCodeRapidMove(Z=5),
37- ... GCodeStopSpindle(),
38- ... ]
39- >>> print('\n'.join(str(g) for g in gcodes))
40-
41- G00 Z5
42- M03
43- G00 X10 Y20
44- F200
45- G01 Z-1.5
46- G00 Z5
47- M05
48-
49-
50- To plot along a lines of vectors, you could write...
51-
52- ::
53-
54- >>> from pygcode import *
55- >>> from euclid import Vector3
56-
57- >>> vectors = [
58- ... Vector3(0, 0, 0),
59- ... Vector3(10, 0, 0),
60- ... Vector3(10, 20, 0),
61- ... Vector3(10, 20, 3),
62- ... Vector3(0, 20, 3),
63- ... Vector3(0, 0, 3),
64- ... Vector3(0, 0, 0)
65- ... ]
66-
67- >>> to_coords = lambda v: {'X': v.x, 'Y': v.y, 'Z': v.z}
68- >>> for v in vectors:
69- ... print("%s" % GCodeLinearMove(**to_coords(v)))
70-
71- G01 X0 Y0 Z0
72- G01 X10 Y0 Z0
73- G01 X10 Y20 Z0
74- G01 X10 Y20 Z3
75- G01 X0 Y20 Z3
76- G01 X0 Y0 Z3
77- G01 X0 Y0 Z0
78-
79-
80- Reading / Interpreting GCode
81- ----------------------------
82-
83- To read gcode from a file, utilise the ``Line `` class.
84- Each ``Line `` instance contains a ``Block `` and an optional ``Comment ``.
85- The ``Block `` contains a list of gcodes you're after.
86-
87- ::
88-
89- from pygcode import Line
90-
91- with open('part.gcode', 'r') as fh:
92- for line_text in fh.readlines():
93- line = Line(line_text)
94-
95- print(line) # will print the line (with cosmetic changes)
96- line.block.gcodes # is your list of gcodes
97- line.block.modal_params # are all parameters not assigned to a gcode, assumed to be motion modal parameters
98- if line.comment:
99- line.comment.text # your comment text
100-
101- To elaborate, here are some line examples
102-
103- ::
104-
105- >>> from pygcode import Line
106-
107- >>> line = Line('G01 x1 y2 f100 s1000 ; blah')
108- >>> print(line)
109- G01 X1 Y2 F100 S1000 ; blah
110- >>> print(line.block)
111- G01 X1 Y2 F100 S1000
112- >>> print(line.comment)
113- ; blah
114-
115- >>> line = Line('G0 x1 y2 (foo) f100 (bar) s1000')
116- >>> print(line)
117- G00 X1 Y2 F100 S1000 (foo. bar)
118- >>> print(line.comment)
119- (foo. bar)
120-
121-
122- Interpreting what a line of gcode does depends on the machine it's running on,
123- and also that machine's state (or 'mode')
124-
125- The simple line of a rapid move to ``x=10, y=10 `` may be ``G00 X10 Y10 ``.
126- However, if the machine in question is in "Incremental Motion" mode ``G91 `` then
127- the machine will only end up at ``x=10, y=10 `` if it started at ``x=0, y=0 ``
128-
129- So, GCode interpretation is done via a virtual machine:
130-
131- ::
132-
133- >>> from pygcode import Machine, GCodeRapidMove
134-
135- >>> m = Machine()
136- >>> m.pos
137- <Position: X0 Y0 Z0>
138- >>> g = GCodeRapidMove(X=10, Y=20)
139- >>> m.process_gcodes(g)
140- >>> m.pos
141- <Position: X10 Y20 Z0>
142- >>> m.process_gcodes(g)
143- >>> m.pos
144- <Position: X10 Y20 Z0> # same position; machine in absolute mode
145- >>> m.mode.distance
146- <GCodeAbsoluteDistanceMode: G90> # see
147-
148- >>> m.process_gcodes(GCodeIncrementalDistanceMode())
149- >>> m.process_gcodes(g) # same gcode as above
150- >>> m.pos
151- <Position: X20 Y40 Z0>
152-
153- all valid ``m.mode `` attributes can be found with ``from pygcode.gcodes import MODAL_GROUP_MAP; MODAL_GROUP_MAP.keys() ``
154-
155- Also note that the order codes are interpreted is important.
156- For example, the following code is WRONG
157-
158- ::
159-
160- from pygcode import Machine, Line
161- m = Machine()
162- line = Line('G0 x10 y10 G91')
163- m.process_gcodes(*line.block.gcodes) # WRONG!
164-
165- This will process the movement to ``x=10, y=10 ``, and **then ** it will change the
166- distance mode to *Incremental *... there are 2 ways to do this correctly.
167-
168- - ``m.process_gcodes(*sorted(line.block.gcodes)) ``, or simply
169- - ``m.process_block(line.block) ``
170-
171- sorting a list of gcodes will sort them in execution order (as specified by
172- `LinuxCNC's order of execution <http://linuxcnc.org/docs/html/gcode/overview.html#_g_code_order_of_execution >`__).
173- ``process_block `` does this automatically.
174-
175- If you need to process & change one type of gcode (usually a movement),
176- you must split a list of gcodes into those executed before, and after the one
177- in question.
178-
179- ::
180-
181- from pygcode import GCodeRapidMove, GCodeLinearMove
182- from pygcode import Machine, Line, split_gcodes
183- m = Machine()
184- line = Line('M0 G0 x10 y10 G91')
185- (befores, (g,), afters) = split_gcodes(line.block.gcodes, (GCodeRapidMove, GCodeLinearMove))
186- m.process_gcodes(*sorted(befores))
187- if g.X is not None:
188- g.X += 100 # shift linear movements (rapid or otherwise)
189- m.process_gcodes(g)
190- m.process_gcodes(*sorted(afters))
191-
192-
193- For a more practical use of machines & interpreting gcode, have a look at
194- `pygcode-normalize.py <https://github.com/fragmuffin/pygcode/blob/master/scripts/pygcode-normalize.py >`__
195-
196- At the time of writing this, that script converts arcs to linear codes, and
197- expands drilling cycles to basic movements (so my
198- `GRBL <https://github.com/gnea/grbl >`__ machine can understand them)
199-
200-
201- Development
202- ===========
203-
204- This library came from my own needs to interpret and convert erroneous
205- arcs to linear segments, and to expand canned drilling cycles, but also
206- as a means to *learn * GCode.
207-
208- As such there is no direct plan for further development, however I'm
209- interested in what you'd like to use it for, and cater for that.
210-
211- Generally, in terms of what to support, I'm following the lead of:
212-
213- - `GRBL <https://github.com/gnea/grbl >`__ and
214- - `LinuxCNC <http://linuxcnc.org/ >`__
215-
216- More support will come with increased interest.
217- So that is... if you don't like what it does, or how it's documented, make some
218- noise in the `issue section <https://github.com/fragmuffin/pygcode/issues >`__.
219- if you get in early, you may get some free labour out of me ;)
220-
221-
222- Supported G-Codes
223- -----------------
18+ or `download directly from PyPi <https://pypi.python.org/pypi/pygcode >`__
22419
225- All GCodes supported by `LinuxCNC <http://linuxcnc.org >`__ can be written, and
226- parsed by ``pygcode ``.
22720
228- Few GCodes are accurately interpreted by a virtual CNC `` Machine `` instance.
229- Supported movements are currently;
21+ Documentation
22+ =============
23023
231- - linear movements
232- - arc movements
233- - canned drilling cycles
24+ `Check out the wiki <https://github.com/fragmuffin/pygcode/wiki >`__ for documentation.
0 commit comments