summaryrefslogtreecommitdiff
path: root/trunk/users/metalab/processing/GCode_Exerciser/GCode_Exerciser.pde
blob: 76b3fc3f59d724881cb827dd3adacd76b72ef4c2 (plain)
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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Yep, this is actually -*- Java -*-

import processing.serial.*;


// The serial port
Serial myPort;
String serialLine = "";

boolean initialized = false;
boolean started = true;
boolean finished = false;
boolean commandComplete = true;
int commandCount = 0;

String temperature = "?";
String debug = "";
float feedrate = 950;

String gcode[] = {
};
int gcodeIndex = 0;

void setup()
{
  //init stuff
  size(1024, 600);
  //smooth();

  // List all the available serial ports
  println(Serial.list());
  //open the first port...
  myPort = new Serial(this, Serial.list()[1], 19200);

  //load our gcode lines
  //  gcode = loadStrings("/home/wizard23/projects/reprap/downloads/gcodestuff/cooled_rings.gcode");
  //println(gcode);
}

void draw()
{
  String cmd;

  background(20, 20, 20);

  while (myPort.available() > 0)
  {
    int inByte = myPort.read();

    if (inByte == '\n')
    {
      println("Got: " + serialLine);

      String m[] = match(serialLine, "^T:([0-9]+)");
      if (m != null) temperature = m[0];

      m  = match(serialLine, "^Status:");
      if (m != null) debug = serialLine;
      println(debug);

      if (match(serialLine, "^start") != null)
        started = true;

      if (match(serialLine, "^ok") != null)
      {
        commandComplete = true;

        if (gcodeIndex == gcode.length)
          println("Job's done!");
      }

      serialLine = "";
    }
    else if (inByte > 0)
    {
      serialLine += (char)inByte;
    }
  }

  if (started)
  {
    if (commandComplete)
    {


      if (!initialized)
      {
        initialized = true;
        cmd = "G91";
      }
      else
      { 
        cmd = getNextCommand();
      }
      if (cmd != null)
      {
        commandComplete = false;
        println("Sent: " +cmd);
        myPort.write(cmd + "\n");
      }
    }
  }

  if (started && finished)
  {
    println("Job's done!");
    started = false;
    finished = false;
  }

  PFont font;
  font = loadFont("ArialMT-48.vlw"); 
  textFont(font, 16);
  String temp = "Temperature: " + temperature + "C";
  temp += "\nTarget: " + targetTemp + "C";
  temp += "\nDC Speed: " + motorSpeed + "C";
  temp += "\nFeedrate: " + feedrate + " mm/min.             < >: dec/inc feedrate";
  text(temp, 10, 20);
  temp = "Status: " + debug + "\n";
  text(temp, 300, 20);

  temp =
    "Keys:\n";
  text(temp, 10, 100);

  temp = 
    "a: -X\n" +
    "d: +X\n" +
    "w: +Y\n" +
    "s: -Y\n" +
    "q: +Z\n" +
    "z: -Z\n";
  text(temp, 10, 120);

  temp = 
    "u: Set temp to 225\n" +
    "p: Increase temp\n" +
    "l: Decrease temp\n";
  text(temp, 100, 100);

  temp = 
    "i: Increase motor speed\n" +
    "j: Decrease motor speed\n";
  text(temp, 100, 160);

  temp = 
    "m: Extruder on\n" +
    "n: Extruder off\n" +
    "b: Extruder reverse\n";
  text(temp, 100,200);
}

int targetTemp = 20;
int motorSpeed = 130;

String getNextCommand()
{
  String c = null;
  if (keyPressed) { 
    switch (key) { 
    case '>':
      feedrate+=10;
      break;
    case '<':
      feedrate-=10;
      break;

    case 'e':
      c = "G21 (mm)";
      break;

    case 'r':
      c = "G90 (abs)";
      break;

      //case 'y':
      //  c = "G92 (set home)";
      //  break;

      // X axis
    case 'a':
      c = "G1 X-10 Y0 Z0 F" + int(feedrate);
      break;
    case 'd':
      c = "G1 X10 Y0 Z0 F" + int(feedrate);
      break;
    case 'A':
      c = "G1 X-1 Y0 Z0 F150";
      break;
    case 'D':
      c = "G1 X1 Y0 Z0 F150";
      break;
      // Y axis
    case 'w':
      c = "G1 X0 Y10 Z0 F" + int(feedrate);
      break;
    case 's':
      c = "G1 X0 Y-10 Z0 F" + int(feedrate);
      break;
    case 'W':
      c = "G1 X0 Y1 Z0 F150";
      break;
    case 'S':
      c = "G1 X0 Y-1 Z0 F150";
      break;
      // Z axis
    case 'q':
      c = "G1 X0 Y0 Z2 F450";
      break;
    case 'Q':
      c = "G1 X0 Y0 Z0.05 F150";
      break;
    case 'z':
      c = "G1 X0 Y0 Z-2 F450";
      break;
    case 'Z':
      c = "G1 X0 Y0 Z-0.05 F150";
      break;

      // Temp    
    case 'u':
      targetTemp = 205;
      c = "M104 S" + targetTemp;
      break;
    case 'p':
      targetTemp += 5;
      c = "M104 S" + targetTemp;
      break;
    case 'l':
      targetTemp -= 10;
      c = "M104 S" + targetTemp;
      break;    
    case 't':
    case 'T':
      c = "M105 (get current temp)";
      break;

      // Motor
    case 'i':
      motorSpeed += 1;
      c = "M108 S" + motorSpeed;
      break;
    case 'j':
      motorSpeed -= 1;
      c = "M108 S" + motorSpeed;
      break;

      // Extruder    
    case 'm':
      c = "M101 (extruder on)";
      break;
    case 'n':
    case ' ':
      c = "M103 (extruder off)";
      break;
    case 'b':
      c = "M102 (extruder reverse)";
      break;

      // Cooling
    case 'f':
      c = "M106 (cooling on)";
      break;

    case 'F':
      c = "M107 (cooling off)";
      break;

      // Home
    case 'h':
      c = "G30 X0 Y0 Z0";
      break;

      // Set zero
    case 'o':
      c = "G92";
      break;

      // Goto zero
    case '0':
      c = "G28 F" + int(feedrate);
      break;

    case 'y':
    case 'Y':
      c = "M120 (get debug)";
      break;

    }
  }

  return c;
}