forked from nodebox/selfiebot
/
Helpers.pde
172 lines (156 loc) · 6.19 KB
/
Helpers.pde
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
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
// TODO Remove
float fromPixelToPaperValue(float pixelValue, float sr_) {
// normalize the pixel X,Y coordinates to the real paper sheet dimensions expressed in mm
float paperValue = (pixelValue/sr_);
;
return paperValue;
}
// TODO Remove
void convertPixelLinesToPaperLines() {
// for (int i=0; i<pixelLines.size(); i++) {
// // converts the coordinates from pixel to paper values, adjusting for a different origin
// Line2D currentLine = pixelLines.get(i);
// float paperX1 = fromPixelToPaperValue(currentLine.a.x, sr)-pW/sr;
// float paperY1 = fromPixelToPaperValue(currentLine.a.y, sr)-pH/sr;
// float paperX2 = fromPixelToPaperValue(currentLine.b.x, sr)-pW/sr;
// float paperY2 = fromPixelToPaperValue(currentLine.b.y, sr)-pH/sr;
// Line2D linePaper = new Line2D(new Vec2D(paperX1, paperY1), new Vec2D(paperX2, paperY2));
// paperLines.add(linePaper);
// }
}
// Export to Gcode
void exportToGcode(ArrayList<ArrayList<PVector>> lines) {
float scaleRatio = 1.0 / sketchWidth * paperWidth;
String outputFolder = "Export/";
String prefix = "selfie-";
String timeStamp = dateNow();
String fileName = outputFolder + prefix + timeStamp + ".ngc";
PrintWriter output = createWriter(fileName);
writeGcodeHeader(output);
for (ArrayList<PVector>line: lines) {
boolean first = true;
for (PVector pt: line) {
pt.mult(scaleRatio);
pt.sub(paperHeight/2, paperWidth/2, 0);
if (first) {
output.println("G0" + " " + "Z" + penUp);
output.println("G0" + " " + "F" + motorFeedFast);
output.println("G0" + " " + "X" + pt.x + " " + "Y" + -(pt.y));
output.println("G0" + " " + "Z" + penDown);
output.println("G0" + " " + "F" + motorFeedSlow);
first = false;
}
else {
output.println("G1" + " " + "X" + pt.x + " " + "Y" + -(pt.y));
}
}
}
writeGcodeFooter(output);
println("G-code: Exported to " + fileName);
}
// Export to .camm
void exportToCAMM(ArrayList<ArrayList<PVector>> lines) {
float scaleRatio = 13.3;// / sketchWidth * paperWidth;
String outputFolder = "Export/";
String prefix = "selfie-";
String timeStamp = dateNow();
String fileName = outputFolder + prefix + timeStamp + ".camm";
PrintWriter output = createWriter(fileName);
writeCAMMHeader(output);
for (ArrayList<PVector>line: lines) {
boolean first = true;
for (PVector pt: line) {
pt.mult(scaleRatio);
pt.sub(0, sketchHeight, 0);
if (first) {
//output.println("// first");
output.println("PU;"); // pen up
output.println("VS " + rolandFeedFast + ";"); // set fast speed
output.println("PU " + pt.x + "," + -(pt.y) + ";"); // go to coordinates
output.println("PD;"); // pen down
output.println("VS " + rolandFeedSlow + ";"); // set fast speed
first = false;
}
else {
//output.println("// not first");
output.println("PD " + pt.x + "," + -(pt.y) + ";"); // draw line
}
}
}
writeCAMMFooter(output);
println("Roland .camm file: Exported to " + fileName);
}
void writeGcodeHeader(PrintWriter output) {
// writes an header with the required setup instructions for the GCode output file
// output.println("( Made with Processing. Paper size: " + paperWidth + "x" + paperHeight + "mm )");
// basic configuration =>> G21 (millimiters) G90 (absolute mode) G64 (constant velocity mode) G40 (turn off radius compensation)
output.println("G21" + " " + "G90" + " " + "G64" + " " + "G40");
// output.println("( T0 : 0.8 )");
// T0 => tool select
// M6 ==> tool change
// output.println("T0 M6");
// G17 ==> select the XY plane
output.println("G17");
// M3 ==> start spindle clockwise
// S1000 ==> spindle speed
// output.println("M3 S1000");
// F... set stepper motors speed
// G0 X0.0 Y0.0 => send plotter head to 'home' position
// G0 is movement with penup while G1 is movement with pen down -> not so sure about this! #ancheno
// G0 Z... ==> pen UP
output.println("G0" + " " + "Z" + penUp);
output.println("G0" + " " + "F" + motorFeedFast + " " + "X0.0" + " " + "Y0.0");
output.println(" ");
// disegna i due assi X,Y
/*
output.println("G0" + " " + "Z" + penDown);
output.println("G0 X-205 Y0");
output.println("G0 X205 Y0");
output.println("G0" + " " + "Z" + penUp);
output.println("G0 X0 Y140");
output.println("G0" + " " + "Z" + penDown);
output.println("G0 X0 Y-140");
output.println("G0" + " " + "Z" + penUp);
output.println(" ");
*/
}
void writeGcodeFooter(PrintWriter output) {
// writes a footer with the end instructions for the GCode output file
output.println(" ");
// G0 Z90.0
// G0 X0 Y0 => go home
// M5 => stop spindle
// M30 => stop execution
output.println("G0" + " " + "Z" + penUp);
//output.println("G0 Z90.0");
output.println("G0 X0 Y0");
output.println("M5");
output.println("M30");
// finalize the GCode text file and quits the current Processing Sketch
output.flush(); // writes the remaining data to the file
output.close(); // finishes the output file
}
void writeCAMMHeader(PrintWriter output) {
// writes an header with the required setup instructions for the CAMM-GL output file
// output.println("( Made with Processing. Paper size: " + paperWidth + "x" + paperHeight + "mm )");
output.println("PA;!ST1;!FS" + force + ";VS" + velocity + ";");
output.println("PU;"); // Pen up
output.println("PU 0,0;"); // Go to origin
//output.println("// end of header");
output.println(" ");
}
void writeCAMMFooter(PrintWriter output) {
// writes a footer with the end instructions for the CAMM-GL output file
output.println(" ");
// PU0,0: => go home
output.println("PU;"); // pen up
output.println("PU 0.0,0.0;"); // go to origin
// finalize the CAMM text file and quits the current Processing Sketch
output.flush(); // writes the remaining data to the file
output.close(); // finishes the output file
}
// Return a string containing the current date/time.
String dateNow() {
return String.format("%4d%02d%02d-%02d%02d%02d", year(), month(), day(), hour(), minute(), second());
// return(year() + "-" + month() + "-" + day() + "-" + hour() + "-" + minute() + "-" + second());
}