Last class, we did a very basic language warm-up to make sure you were able to write some text to a file. This class, we're doing something (just a bit) more complicated: drawing an interesting line.
You are going to be creating a line as a text file that looks like this example.pat file:
N1G00X1.000Y9.008
N2G01X3.700Y6.301
N3G01X3.700Y1.801
N4G01X1.300Y4.207
N5G01X1.300Y7.707
N6G01X3.100Y5.902
N7G01X3.100Y3.402
N8G01X1.900Y4.605
...
N62G01X4.700Y6.300
N63G01X8.700Y6.300
N64G01X6.004Y9.000
N65G01X3.507Y9.004
N66G01X1.004Y9.000
N67M02
In other words, most lines contains a line number, command, X coordinate, and Y coordinate:
N35G01X7.000Y3.000
G00
on the first line and G01
on all subsequent lines.'\r\n'
).This happens to be exactly the pattern format required by our large CNC sewing machine's control software, and is a subset of the G-code language. The strange-seeming restrictions on line numbers and number formats are included because the machine control software's G-code interpreter is prone to errors, and we have found that this subset works. (You can actually use a few other G commands, including circular arcs, but this is not relevant to our current situation.)
Of course, many lines are not particularly interesting. But what does it mean for a line to be interesting? That's up to you to decide; but make sure you do decide, so that you will be able to subjectively evaluate your success.
.pat
file.andrewid-line.pat
file in the google drive folder (link is on the board).This viewer can be used to check that your .pat
file is in the proper format.
You may find this svg2pat.py python script to be helpful; either because you want to create your line by drawing an SVG file (perhaps by hand), or as code reference. Beware that this script outputs exactly what's in the svg file; so you need to make sure your file contains a single <path> containing a single continuous line.
Whether you write code or try to use the svg2pat.py script, make sure that you test that the engineering part of the exercise is working ASAP.
If writing a .pat
file from python, you will need to open your file in 'binary mode' in order to write the '\r\n'
line ending properly:
#python f = open('out.pat', 'wb')
Rounding to exactly three decimal places can be tricky:
#python command = "N{}G01X{:.3f}Y{:.3f}".format( lineNumber, round(X * 1000.0) / 1000.0, round(Y * 1000.0) / 1000.0 )
//javascript command = "N" + lineNumber.toString() + "G01" + "X" + X.toFixed(3) + "Y" + Y.toFixed(3);