Components and supplies
Arduino UNO
Dual H-Bridge motor drivers L293D
Mini Servo motor
Project description
Code
CNC code foR Arduino
c_cpp
Upload this code to Arduino board
1/* 2 https://www.youtube.com/watch?v=opZ9RgmOIpc 3 */ 4 5#include <Servo.h> 6#include <AFMotor.h> 7 8#define LINE_BUFFER_LENGTH 512 9 10char STEP = MICROSTEP ; 11 12// Servo position for Up and Down 13const int penZUp = 115; 14const int penZDown = 83; 15 16// Servo on PWM pin 10 17const int penServoPin =10 ; 18 19// Should be right for DVD steppers, but is not too important here 20const int stepsPerRevolution = 48; 21 22// create servo object to control a servo 23Servo penServo; 24 25// Initialize steppers for X- and Y-axis using this Arduino pins for the L293D H-bridge 26AF_Stepper myStepperY(stepsPerRevolution,1); 27AF_Stepper myStepperX(stepsPerRevolution,2); 28 29/* Structures, global variables */ 30struct point { 31 float x; 32 float y; 33 float z; 34}; 35 36// Current position of plothead 37struct point actuatorPos; 38 39// Drawing settings, should be OK 40float StepInc = 1; 41int StepDelay = 0; 42int LineDelay =0; 43int penDelay = 50; 44 45// Motor steps to go 1 millimeter. 46// Use test sketch to go 100 steps. Measure the length of line. 47// Calculate steps per mm. Enter here. 48float StepsPerMillimeterX = 100.0; 49float StepsPerMillimeterY = 100.0; 50 51// Drawing robot limits, in mm 52// OK to start with. Could go up to 50 mm if calibrated well. 53float Xmin = 0; 54float Xmax = 40; 55float Ymin = 0; 56float Ymax = 40; 57float Zmin = 0; 58float Zmax = 1; 59 60float Xpos = Xmin; 61float Ypos = Ymin; 62float Zpos = Zmax; 63 64// Set to true to get debug output. 65boolean verbose = false; 66 67// Needs to interpret 68// G1 for moving 69// G4 P300 (wait 150ms) 70// M300 S30 (pen down) 71// M300 S50 (pen up) 72// Discard anything with a ( 73// Discard any other command! 74 75/********************** 76 * void setup() - Initialisations 77 ***********************/ 78void setup() { 79 // Setup 80 81 Serial.begin( 9600 ); 82 83 penServo.attach(penServoPin); 84 penServo.write(penZUp); 85 delay(100); 86 87 // Decrease if necessary 88 myStepperX.setSpeed(600); 89 90 myStepperY.setSpeed(600); 91 92 93 // Set & move to initial default position 94 // TBD 95 96 // Notifications!!! 97 Serial.println("Mini CNC Plotter alive and kicking!"); 98 Serial.print("X range is from "); 99 Serial.print(Xmin); 100 Serial.print(" to "); 101 Serial.print(Xmax); 102 Serial.println(" mm."); 103 Serial.print("Y range is from "); 104 Serial.print(Ymin); 105 Serial.print(" to "); 106 Serial.print(Ymax); 107 Serial.println(" mm."); 108} 109 110/********************** 111 * void loop() - Main loop 112 ***********************/ 113void loop() 114{ 115 116 delay(100); 117 char line[ LINE_BUFFER_LENGTH ]; 118 char c; 119 int lineIndex; 120 bool lineIsComment, lineSemiColon; 121 122 lineIndex = 0; 123 lineSemiColon = false; 124 lineIsComment = false; 125 126 while (1) { 127 128 // Serial reception - Mostly from Grbl, added semicolon support 129 while ( Serial.available()>0 ) { 130 c = Serial.read(); 131 if (( c == '\n') || (c == '\r') ) { // End of line reached 132 if ( lineIndex > 0 ) { // Line is complete. Then execute! 133 line[ lineIndex ] = '\\0'; // Terminate string 134 if (verbose) { 135 Serial.print( "Received : "); 136 Serial.println( line ); 137 } 138 processIncomingLine( line, lineIndex ); 139 lineIndex = 0; 140 } 141 else { 142 // Empty or comment line. Skip block. 143 } 144 lineIsComment = false; 145 lineSemiColon = false; 146 Serial.println("ok"); 147 } 148 else { 149 if ( (lineIsComment) || (lineSemiColon) ) { // Throw away all comment characters 150 if ( c == ')' ) lineIsComment = false; // End of comment. Resume line. 151 } 152 else { 153 if ( c <= ' ' ) { // Throw away whitepace and control characters 154 } 155 else if ( c == '/' ) { // Block delete not supported. Ignore character. 156 } 157 else if ( c == '(' ) { // Enable comments flag and ignore all characters until ')' or EOL. 158 lineIsComment = true; 159 } 160 else if ( c == ';' ) { 161 lineSemiColon = true; 162 } 163 else if ( lineIndex >= LINE_BUFFER_LENGTH-1 ) { 164 Serial.println( "ERROR - lineBuffer overflow" ); 165 lineIsComment = false; 166 lineSemiColon = false; 167 } 168 else if ( c >= 'a' && c <= 'z' ) { // Upcase lowercase 169 line[ lineIndex++ ] = c-'a'+'A'; 170 } 171 else { 172 line[ lineIndex++ ] = c; 173 } 174 } 175 } 176 } 177 } 178} 179 180void processIncomingLine( char* line, int charNB ) { 181 int currentIndex = 0; 182 char buffer[ 64 ]; // Hope that 64 is enough for 1 parameter 183 struct point newPos; 184 185 newPos.x = 0.0; 186 newPos.y = 0.0; 187 188 // Needs to interpret 189 // G1 for moving 190 // G4 P300 (wait 150ms) 191 // G1 X60 Y30 192 // G1 X30 Y50 193 // M300 S30 (pen down) 194 // M300 S50 (pen up) 195 // Discard anything with a ( 196 // Discard any other command! 197 198 while( currentIndex < charNB ) { 199 switch ( line[ currentIndex++ ] ) { // Select command, if any 200 case 'U': 201 penUp(); 202 break; 203 case 'D': 204 penDown(); 205 break; 206 case 'G': 207 buffer[0] = line[ currentIndex++ ]; // /!\Dirty - Only works with 2 digit commands 208 // buffer[1] = line[ currentIndex++ ]; 209 // buffer[2] = '\\0'; 210 buffer[1] = '\\0'; 211 212 switch ( atoi( buffer ) ){ // Select G command 213 case 0: // G00 & G01 - Movement or fast movement. Same here 214 case 1: 215 // /!\Dirty - Suppose that X is before Y 216 char* indexX = strchr( line+currentIndex, 'X' ); // Get X/Y position in the string (if any) 217 char* indexY = strchr( line+currentIndex, 'Y' ); 218 if ( indexY <= 0 ) { 219 newPos.x = atof( indexX + 1); 220 newPos.y = actuatorPos.y; 221 } 222 else if ( indexX <= 0 ) { 223 newPos.y = atof( indexY + 1); 224 newPos.x = actuatorPos.x; 225 } 226 else { 227 newPos.y = atof( indexY + 1); 228 indexY = '\\0'; 229 newPos.x = atof( indexX + 1); 230 } 231 drawLine(newPos.x, newPos.y ); 232 // Serial.println("ok"); 233 actuatorPos.x = newPos.x; 234 actuatorPos.y = newPos.y; 235 break; 236 } 237 break; 238 case 'M': 239 buffer[0] = line[ currentIndex++ ]; // /!\Dirty - Only works with 3 digit commands 240 buffer[1] = line[ currentIndex++ ]; 241 buffer[2] = line[ currentIndex++ ]; 242 buffer[3] = '\\0'; 243 switch ( atoi( buffer ) ){ 244 case 300: 245 { 246 char* indexS = strchr( line+currentIndex, 'S' ); 247 float Spos = atof( indexS + 1); 248 // Serial.println("ok"); 249 if (Spos == 30) { 250 penDown(); 251 } 252 if (Spos == 50) { 253 penUp(); 254 } 255 break; 256 } 257 case 114: // M114 - Repport position 258 Serial.print( "Absolute position : X = " ); 259 Serial.print( actuatorPos.x ); 260 Serial.print( " - Y = " ); 261 Serial.println( actuatorPos.y ); 262 break; 263 default: 264 Serial.print( "Command not recognized : M"); 265 Serial.println( buffer ); 266 } 267 } 268 } 269 270 271 272} 273 274 275/********************************* 276 * Draw a line from (x0;y0) to (x1;y1). 277 * int (x1;y1) : Starting coordinates 278 * int (x2;y2) : Ending coordinates 279 **********************************/ 280void drawLine(float x1, float y1) { 281 282 if (verbose) 283 { 284 Serial.print("fx1, fy1: "); 285 Serial.print(x1); 286 Serial.print(","); 287 Serial.print(y1); 288 Serial.println(""); 289 } 290 291 // Bring instructions within limits 292 if (x1 >= Xmax) { 293 x1 = Xmax; 294 } 295 if (x1 <= Xmin) { 296 x1 = Xmin; 297 } 298 if (y1 >= Ymax) { 299 y1 = Ymax; 300 } 301 if (y1 <= Ymin) { 302 y1 = Ymin; 303 } 304 305 if (verbose) 306 { 307 Serial.print("Xpos, Ypos: "); 308 Serial.print(Xpos); 309 Serial.print(","); 310 Serial.print(Ypos); 311 Serial.println(""); 312 } 313 314 if (verbose) 315 { 316 Serial.print("x1, y1: "); 317 Serial.print(x1); 318 Serial.print(","); 319 Serial.print(y1); 320 Serial.println(""); 321 } 322 323 // Convert coordinates to steps 324 x1 = (int)(x1*StepsPerMillimeterX); 325 y1 = (int)(y1*StepsPerMillimeterY); 326 float x0 = Xpos; 327 float y0 = Ypos; 328 329 // Let's find out the change for the coordinates 330 long dx = abs(x1-x0); 331 long dy = abs(y1-y0); 332 int sx = x0<x1 ? StepInc : -StepInc; 333 int sy = y0<y1 ? StepInc : -StepInc; 334 335 long i; 336 long over = 0; 337 338 if (dx > dy) { 339 for (i=0; i<dx; ++i) { 340 myStepperX.onestep(sx,STEP); 341 over+=dy; 342 if (over>=dx) { 343 over-=dx; 344 myStepperY.onestep(sy,STEP); 345 } 346 delay(StepDelay); 347 } 348 } 349 else { 350 for (i=0; i<dy; ++i) { 351 myStepperY.onestep(sy,STEP); 352 over+=dx; 353 if (over>=dy) { 354 over-=dy; 355 myStepperX.onestep(sx,STEP); 356 } 357 delay(StepDelay); 358 } 359 } 360 361 if (verbose) 362 { 363 Serial.print("dx, dy:"); 364 Serial.print(dx); 365 Serial.print(","); 366 Serial.print(dy); 367 Serial.println(""); 368 } 369 370 if (verbose) 371 { 372 Serial.print("Going to ("); 373 Serial.print(x0); 374 Serial.print(","); 375 Serial.print(y0); 376 Serial.println(")"); 377 } 378 379 // Delay before any next lines are submitted 380 delay(LineDelay); 381 // Update the positions 382 Xpos = x1; 383 Ypos = y1; 384} 385 386// Raises pen 387void penUp() { 388 penServo.write(penZUp); 389 delay(penDelay); 390 Zpos=Zmax; 391 digitalWrite(15, LOW); 392 digitalWrite(16, HIGH); 393 if (verbose) { 394 Serial.println("Pen up!"); 395 396 } 397} 398// Lowers pen 399void penDown() { 400 penServo.write(penZDown); 401 delay(penDelay); 402 Zpos=Zmin; 403 digitalWrite(15, HIGH); 404 digitalWrite(16, LOW); 405 if (verbose) { 406 Serial.println("Pen down."); 407 408 409 } 410} 411
GCTRL
java
Run this code in Processing IDE
1import java.awt.event.KeyEvent; 2import javax.swing.JOptionPane; 3import processing.serial.*; 4 5Serial port = null; 6 7// select and modify the appropriate line for your operating system 8// leave as null to use interactive port (press 'p' in the program) 9String portname = null; 10//String portname = Serial.list()[0]; // Mac OS X 11//String portname = "/dev/ttyUSB0"; // Linux 12//String portname = "COM6"; // Windows 13 14boolean streaming = false; 15float speed = 0.001; 16String[] gcode; 17int i = 0; 18 19void openSerialPort() 20{ 21 if (portname == null) return; 22 if (port != null) port.stop(); 23 24 port = new Serial(this, portname, 9600); 25 26 port.bufferUntil('\n'); 27} 28 29void selectSerialPort() 30{ 31 String result = (String) JOptionPane.showInputDialog(this, 32 "Select the serial port that corresponds to your Arduino board.", 33 "Select serial port", 34 JOptionPane.PLAIN_MESSAGE, 35 null, 36 Serial.list(), 37 0); 38 39 if (result != null) { 40 portname = result; 41 openSerialPort(); 42 } 43} 44 45void setup() 46{ 47 size(500, 250); 48 openSerialPort(); 49} 50 51void draw() 52{ 53 background(0); 54 fill(255); 55 int y = 24, dy = 12; 56 text("INSTRUCTIONS", 12, y); y += dy; 57 text("p: select serial port", 12, y); y += dy; 58 text("1: set speed to 0.001 inches (1 mil) per jog", 12, y); y += dy; 59 text("2: set speed to 0.010 inches (10 mil) per jog", 12, y); y += dy; 60 text("3: set speed to 0.100 inches (100 mil) per jog", 12, y); y += dy; 61 text("arrow keys: jog in x-y plane", 12, y); y += dy; 62 text("page up & page down: jog in z axis", 12, y); y += dy; 63 text("$: display grbl settings", 12, y); y+= dy; 64 text("h: go home", 12, y); y += dy; 65 text("0: zero machine (set home to the current location)", 12, y); y += dy; 66 text("g: stream a g-code file", 12, y); y += dy; 67 text("x: stop streaming g-code (this is NOT immediate)", 12, y); y += dy; 68 y = height - dy; 69 text("current jog speed: " + speed + " inches per step", 12, y); y -= dy; 70 text("current serial port: " + portname, 12, y); y -= dy; 71} 72 73void keyPressed() 74{ 75 if (key == '1') speed = 0.001; 76 if (key == '2') speed = 0.01; 77 if (key == '3') speed = 0.1; 78 79 if (!streaming) { 80 if (keyCode == LEFT) port.write("G91\ 81G20\ 82G00 X-" + speed + " Y0.000 Z0.000\ 83"); 84 if (keyCode == RIGHT) port.write("G91\ 85G20\ 86G00 X" + speed + " Y0.000 Z0.000\ 87"); 88 if (keyCode == UP) port.write("G91\ 89G20\ 90G00 X0.000 Y" + speed + " Z0.000\ 91"); 92 if (keyCode == DOWN) port.write("G91\ 93G20\ 94G00 X0.000 Y-" + speed + " Z0.000\ 95"); 96 if (keyCode == KeyEvent.VK_PAGE_UP) port.write("G91\ 97G20\ 98G00 X0.000 Y0.000 Z" + speed + "\ 99"); 100 if (keyCode == KeyEvent.VK_PAGE_DOWN) port.write("G91\ 101G20\ 102G00 X0.000 Y0.000 Z-" + speed + "\ 103"); 104 if (key == 'h') port.write("G90\ 105G20\ 106G00 X0.000 Y0.000 Z0.000\ 107"); 108 if (key == 'v') port.write("$0=75\ 109$1=74\ 110$2=75\ 111"); 112 //if (key == 'v') port.write("$0=100\ 113$1=74\ 114$2=75\ 115"); 116 if (key == 's') port.write("$3=10\ 117"); 118 if (key == 'e') port.write("$16=1\ 119"); 120 if (key == 'd') port.write("$16=0\ 121"); 122 if (key == '0') openSerialPort(); 123 if (key == 'p') selectSerialPort(); 124 if (key == '$') port.write("$$\ 125"); 126 } 127 128 if (!streaming && key == 'g') { 129 gcode = null; i = 0; 130 File file = null; 131 println("Loading file..."); 132 selectInput("Select a file to process:", "fileSelected", file); 133 } 134 135 if (key == 'x') streaming = false; 136} 137 138void fileSelected(File selection) { 139 if (selection == null) { 140 println("Window was closed or the user hit cancel."); 141 } else { 142 println("User selected " + selection.getAbsolutePath()); 143 gcode = loadStrings(selection.getAbsolutePath()); 144 if (gcode == null) return; 145 streaming = true; 146 stream(); 147 } 148} 149 150void stream() 151{ 152 if (!streaming) return; 153 154 while (true) { 155 if (i == gcode.length) { 156 streaming = false; 157 return; 158 } 159 160 if (gcode[i].trim().length() == 0) i++; 161 else break; 162 } 163 164 println(gcode[i]); 165 port.write(gcode[i] + '\n'); 166 i++; 167} 168 169void serialEvent(Serial p) 170{ 171 String s = p.readStringUntil('\n'); 172 println(s.trim()); 173 174 if (s.trim().startsWith("ok")) stream(); 175 if (s.trim().startsWith("error")) stream(); // XXX: really? 176} 177 178
CNC code foR Arduino
c_cpp
Upload this code to Arduino board
1/* 2 https://www.youtube.com/watch?v=opZ9RgmOIpc 3 */ 4 5#include 6 <Servo.h> 7#include <AFMotor.h> 8 9#define LINE_BUFFER_LENGTH 512 10 11char 12 STEP = MICROSTEP ; 13 14// Servo position for Up and Down 15const int penZUp 16 = 115; 17const int penZDown = 83; 18 19// Servo on PWM pin 10 20const int penServoPin 21 =10 ; 22 23// Should be right for DVD steppers, but is not too important here 24const 25 int stepsPerRevolution = 48; 26 27// create servo object to control a servo 28Servo 29 penServo; 30 31// Initialize steppers for X- and Y-axis using this Arduino pins 32 for the L293D H-bridge 33AF_Stepper myStepperY(stepsPerRevolution,1); 34AF_Stepper 35 myStepperX(stepsPerRevolution,2); 36 37/* Structures, global variables */ 38struct 39 point { 40 float x; 41 float y; 42 float z; 43}; 44 45// Current position 46 of plothead 47struct point actuatorPos; 48 49// Drawing settings, should be 50 OK 51float StepInc = 1; 52int StepDelay = 0; 53int LineDelay =0; 54int penDelay 55 = 50; 56 57// Motor steps to go 1 millimeter. 58// Use test sketch to go 100 59 steps. Measure the length of line. 60// Calculate steps per mm. Enter here. 61float 62 StepsPerMillimeterX = 100.0; 63float StepsPerMillimeterY = 100.0; 64 65// Drawing 66 robot limits, in mm 67// OK to start with. Could go up to 50 mm if calibrated well. 68 69float Xmin = 0; 70float Xmax = 40; 71float Ymin = 0; 72float Ymax = 40; 73float 74 Zmin = 0; 75float Zmax = 1; 76 77float Xpos = Xmin; 78float Ypos = Ymin; 79float 80 Zpos = Zmax; 81 82// Set to true to get debug output. 83boolean verbose = false; 84 85// 86 Needs to interpret 87// G1 for moving 88// G4 P300 (wait 150ms) 89// M300 90 S30 (pen down) 91// M300 S50 (pen up) 92// Discard anything with a ( 93// Discard 94 any other command! 95 96/********************** 97 * void setup() - Initialisations 98 99 ***********************/ 100void setup() { 101 // Setup 102 103 Serial.begin( 104 9600 ); 105 106 penServo.attach(penServoPin); 107 penServo.write(penZUp); 108 109 delay(100); 110 111 // Decrease if necessary 112 myStepperX.setSpeed(600); 113 114 115 myStepperY.setSpeed(600); 116 117 118 // Set & move to initial default position 119 120 // TBD 121 122 // Notifications!!! 123 Serial.println("Mini CNC Plotter alive 124 and kicking!"); 125 Serial.print("X range is from "); 126 Serial.print(Xmin); 127 128 Serial.print(" to "); 129 Serial.print(Xmax); 130 Serial.println(" 131 mm."); 132 Serial.print("Y range is from "); 133 Serial.print(Ymin); 134 135 Serial.print(" to "); 136 Serial.print(Ymax); 137 Serial.println(" mm."); 138 139} 140 141/********************** 142 * void loop() - Main loop 143 ***********************/ 144void 145 loop() 146{ 147 148 delay(100); 149 char line[ LINE_BUFFER_LENGTH ]; 150 char 151 c; 152 int lineIndex; 153 bool lineIsComment, lineSemiColon; 154 155 lineIndex 156 = 0; 157 lineSemiColon = false; 158 lineIsComment = false; 159 160 while (1) 161 { 162 163 // Serial reception - Mostly from Grbl, added semicolon support 164 165 while ( Serial.available()>0 ) { 166 c = Serial.read(); 167 if (( 168 c == '\ 169') || (c == '\ ') ) { // End of line reached 170 if 171 ( lineIndex > 0 ) { // Line is complete. Then execute! 172 173 line[ lineIndex ] = '\\0'; // Terminate string 174 if 175 (verbose) { 176 Serial.print( "Received : "); 177 Serial.println( 178 line ); 179 } 180 processIncomingLine( line, lineIndex ); 181 182 lineIndex = 0; 183 } 184 else { 185 // Empty 186 or comment line. Skip block. 187 } 188 lineIsComment = false; 189 190 lineSemiColon = false; 191 Serial.println("ok"); 192 } 193 194 else { 195 if ( (lineIsComment) || (lineSemiColon) ) { // Throw 196 away all comment characters 197 if ( c == ')' ) lineIsComment = false; 198 // End of comment. Resume line. 199 } 200 else { 201 if 202 ( c <= ' ' ) { // Throw away whitepace and control characters 203 204 } 205 else if ( c == '/' ) { // Block delete 206 not supported. Ignore character. 207 } 208 else if ( c == '(' 209 ) { // Enable comments flag and ignore all characters until ')' 210 or EOL. 211 lineIsComment = true; 212 } 213 else if 214 ( c == ';' ) { 215 lineSemiColon = true; 216 } 217 else 218 if ( lineIndex >= LINE_BUFFER_LENGTH-1 ) { 219 Serial.println( "ERROR 220 - lineBuffer overflow" ); 221 lineIsComment = false; 222 lineSemiColon 223 = false; 224 } 225 else if ( c >= 'a' && c <= 'z' ) { // 226 Upcase lowercase 227 line[ lineIndex++ ] = c-'a'+'A'; 228 } 229 230 else { 231 line[ lineIndex++ ] = c; 232 } 233 234 } 235 } 236 } 237 } 238} 239 240void processIncomingLine( char* 241 line, int charNB ) { 242 int currentIndex = 0; 243 char buffer[ 64 ]; // 244 Hope that 64 is enough for 1 parameter 245 struct point newPos; 246 247 newPos.x 248 = 0.0; 249 newPos.y = 0.0; 250 251 // Needs to interpret 252 // G1 for moving 253 254 // G4 P300 (wait 150ms) 255 // G1 X60 Y30 256 // G1 X30 Y50 257 // M300 258 S30 (pen down) 259 // M300 S50 (pen up) 260 // Discard anything with a ( 261 262 // Discard any other command! 263 264 while( currentIndex < charNB ) { 265 switch 266 ( line[ currentIndex++ ] ) { // Select command, if any 267 case 268 'U': 269 penUp(); 270 break; 271 case 'D': 272 penDown(); 273 274 break; 275 case 'G': 276 buffer[0] = line[ currentIndex++ ]; // 277 /!\Dirty - Only works with 2 digit commands 278 // buffer[1] = line[ 279 currentIndex++ ]; 280 // buffer[2] = '\\0'; 281 buffer[1] = '\\0'; 282 283 284 switch ( atoi( buffer ) ){ // Select G command 285 case 286 0: // G00 & G01 - Movement or fast movement. Same 287 here 288 case 1: 289 // /!\Dirty - Suppose that X is before Y 290 291 char* indexX = strchr( line+currentIndex, 'X' ); // Get X/Y position in 292 the string (if any) 293 char* indexY = strchr( line+currentIndex, 'Y' ); 294 295 if ( indexY <= 0 ) { 296 newPos.x = atof( indexX + 1); 297 newPos.y 298 = actuatorPos.y; 299 } 300 else if ( indexX <= 0 ) { 301 newPos.y 302 = atof( indexY + 1); 303 newPos.x = actuatorPos.x; 304 } 305 else 306 { 307 newPos.y = atof( indexY + 1); 308 indexY = '\\0'; 309 newPos.x 310 = atof( indexX + 1); 311 } 312 drawLine(newPos.x, newPos.y ); 313 314 // Serial.println("ok"); 315 actuatorPos.x = newPos.x; 316 317 actuatorPos.y = newPos.y; 318 break; 319 } 320 break; 321 322 case 'M': 323 buffer[0] = line[ currentIndex++ ]; // /!\Dirty 324 - Only works with 3 digit commands 325 buffer[1] = line[ currentIndex++ ]; 326 327 buffer[2] = line[ currentIndex++ ]; 328 buffer[3] = '\\0'; 329 switch 330 ( atoi( buffer ) ){ 331 case 300: 332 { 333 char* indexS = 334 strchr( line+currentIndex, 'S' ); 335 float Spos = atof( indexS + 1); 336 337 // Serial.println("ok"); 338 if (Spos == 30) { 339 340 penDown(); 341 } 342 if (Spos == 50) { 343 penUp(); 344 345 } 346 break; 347 } 348 case 114: // 349 M114 - Repport position 350 Serial.print( "Absolute position : X = " ); 351 352 Serial.print( actuatorPos.x ); 353 Serial.print( " - Y = " ); 354 355 Serial.println( actuatorPos.y ); 356 break; 357 default: 358 359 Serial.print( "Command not recognized : M"); 360 Serial.println( 361 buffer ); 362 } 363 } 364 } 365 366 367 368} 369 370 371/********************************* 372 373 * Draw a line from (x0;y0) to (x1;y1). 374 * int (x1;y1) : Starting coordinates 375 376 * int (x2;y2) : Ending coordinates 377 **********************************/ 378void 379 drawLine(float x1, float y1) { 380 381 if (verbose) 382 { 383 Serial.print("fx1, 384 fy1: "); 385 Serial.print(x1); 386 Serial.print(","); 387 Serial.print(y1); 388 389 Serial.println(""); 390 } 391 392 // Bring instructions within limits 393 394 if (x1 >= Xmax) { 395 x1 = Xmax; 396 } 397 if (x1 <= Xmin) { 398 x1 399 = Xmin; 400 } 401 if (y1 >= Ymax) { 402 y1 = Ymax; 403 } 404 if (y1 <= 405 Ymin) { 406 y1 = Ymin; 407 } 408 409 if (verbose) 410 { 411 Serial.print("Xpos, 412 Ypos: "); 413 Serial.print(Xpos); 414 Serial.print(","); 415 Serial.print(Ypos); 416 417 Serial.println(""); 418 } 419 420 if (verbose) 421 { 422 Serial.print("x1, 423 y1: "); 424 Serial.print(x1); 425 Serial.print(","); 426 Serial.print(y1); 427 428 Serial.println(""); 429 } 430 431 // Convert coordinates to steps 432 x1 433 = (int)(x1*StepsPerMillimeterX); 434 y1 = (int)(y1*StepsPerMillimeterY); 435 float 436 x0 = Xpos; 437 float y0 = Ypos; 438 439 // Let's find out the change for the 440 coordinates 441 long dx = abs(x1-x0); 442 long dy = abs(y1-y0); 443 int sx = 444 x0<x1 ? StepInc : -StepInc; 445 int sy = y0<y1 ? StepInc : -StepInc; 446 447 long 448 i; 449 long over = 0; 450 451 if (dx > dy) { 452 for (i=0; i<dx; ++i) { 453 454 myStepperX.onestep(sx,STEP); 455 over+=dy; 456 if (over>=dx) { 457 458 over-=dx; 459 myStepperY.onestep(sy,STEP); 460 } 461 delay(StepDelay); 462 463 } 464 } 465 else { 466 for (i=0; i<dy; ++i) { 467 myStepperY.onestep(sy,STEP); 468 469 over+=dx; 470 if (over>=dy) { 471 over-=dy; 472 myStepperX.onestep(sx,STEP); 473 474 } 475 delay(StepDelay); 476 } 477 } 478 479 if (verbose) 480 481 { 482 Serial.print("dx, dy:"); 483 Serial.print(dx); 484 Serial.print(","); 485 486 Serial.print(dy); 487 Serial.println(""); 488 } 489 490 if (verbose) 491 492 { 493 Serial.print("Going to ("); 494 Serial.print(x0); 495 Serial.print(","); 496 497 Serial.print(y0); 498 Serial.println(")"); 499 } 500 501 // Delay before 502 any next lines are submitted 503 delay(LineDelay); 504 // Update the positions 505 506 Xpos = x1; 507 Ypos = y1; 508} 509 510// Raises pen 511void penUp() { 512 penServo.write(penZUp); 513 514 delay(penDelay); 515 Zpos=Zmax; 516 digitalWrite(15, LOW); 517 digitalWrite(16, 518 HIGH); 519 if (verbose) { 520 Serial.println("Pen up!"); 521 522 } 523 524} 525// Lowers pen 526void penDown() { 527 penServo.write(penZDown); 528 529 delay(penDelay); 530 Zpos=Zmin; 531 digitalWrite(15, HIGH); 532 digitalWrite(16, 533 LOW); 534 if (verbose) { 535 Serial.println("Pen down."); 536 537 538 539 } 540} 541
GCTRL
java
Run this code in Processing IDE
1import java.awt.event.KeyEvent; 2import javax.swing.JOptionPane; 3import 4 processing.serial.*; 5 6Serial port = null; 7 8// select and modify the 9 appropriate line for your operating system 10// leave as null to use interactive 11 port (press 'p' in the program) 12String portname = null; 13//String portname 14 = Serial.list()[0]; // Mac OS X 15//String portname = "/dev/ttyUSB0"; // Linux 16//String 17 portname = "COM6"; // Windows 18 19boolean streaming = false; 20float speed 21 = 0.001; 22String[] gcode; 23int i = 0; 24 25void openSerialPort() 26{ 27 28 if (portname == null) return; 29 if (port != null) port.stop(); 30 31 port 32 = new Serial(this, portname, 9600); 33 34 port.bufferUntil('\ 35'); 36} 37 38void 39 selectSerialPort() 40{ 41 String result = (String) JOptionPane.showInputDialog(this, 42 43 "Select the serial port that corresponds to your Arduino board.", 44 "Select 45 serial port", 46 JOptionPane.PLAIN_MESSAGE, 47 null, 48 Serial.list(), 49 50 0); 51 52 if (result != null) { 53 portname = result; 54 openSerialPort(); 55 56 } 57} 58 59void setup() 60{ 61 size(500, 250); 62 openSerialPort(); 63} 64 65void 66 draw() 67{ 68 background(0); 69 fill(255); 70 int y = 24, dy = 12; 71 72 text("INSTRUCTIONS", 12, y); y += dy; 73 text("p: select serial port", 12, 74 y); y += dy; 75 text("1: set speed to 0.001 inches (1 mil) per jog", 12, y); 76 y += dy; 77 text("2: set speed to 0.010 inches (10 mil) per jog", 12, y); y 78 += dy; 79 text("3: set speed to 0.100 inches (100 mil) per jog", 12, y); y += 80 dy; 81 text("arrow keys: jog in x-y plane", 12, y); y += dy; 82 text("page 83 up & page down: jog in z axis", 12, y); y += dy; 84 text("$: display grbl settings", 85 12, y); y+= dy; 86 text("h: go home", 12, y); y += dy; 87 text("0: zero machine 88 (set home to the current location)", 12, y); y += dy; 89 text("g: stream a g-code 90 file", 12, y); y += dy; 91 text("x: stop streaming g-code (this is NOT immediate)", 92 12, y); y += dy; 93 y = height - dy; 94 text("current jog speed: " + speed 95 + " inches per step", 12, y); y -= dy; 96 text("current serial port: " + portname, 97 12, y); y -= dy; 98} 99 100void keyPressed() 101{ 102 if (key == '1') speed = 103 0.001; 104 if (key == '2') speed = 0.01; 105 if (key == '3') speed = 0.1; 106 107 108 if (!streaming) { 109 if (keyCode == LEFT) port.write("G91\ 110G20\ 111G00 112 X-" + speed + " Y0.000 Z0.000\ 113"); 114 if (keyCode == RIGHT) port.write("G91\ 115G20\ 116G00 117 X" + speed + " Y0.000 Z0.000\ 118"); 119 if (keyCode == UP) port.write("G91\ 120G20\ 121G00 122 X0.000 Y" + speed + " Z0.000\ 123"); 124 if (keyCode == DOWN) port.write("G91\ 125G20\ 126G00 127 X0.000 Y-" + speed + " Z0.000\ 128"); 129 if (keyCode == KeyEvent.VK_PAGE_UP) 130 port.write("G91\ 131G20\ 132G00 X0.000 Y0.000 Z" + speed + "\ 133"); 134 if (keyCode 135 == KeyEvent.VK_PAGE_DOWN) port.write("G91\ 136G20\ 137G00 X0.000 Y0.000 Z-" + speed 138 + "\ 139"); 140 if (key == 'h') port.write("G90\ 141G20\ 142G00 X0.000 Y0.000 Z0.000\ 143"); 144 145 if (key == 'v') port.write("$0=75\ 146$1=74\ 147$2=75\ 148"); 149 //if (key == 150 'v') port.write("$0=100\ 151$1=74\ 152$2=75\ 153"); 154 if (key == 's') port.write("$3=10\ 155"); 156 157 if (key == 'e') port.write("$16=1\ 158"); 159 if (key == 'd') port.write("$16=0\ 160"); 161 162 if (key == '0') openSerialPort(); 163 if (key == 'p') selectSerialPort(); 164 165 if (key == '$') port.write("$$\ 166"); 167 } 168 169 if (!streaming && key 170 == 'g') { 171 gcode = null; i = 0; 172 File file = null; 173 println("Loading 174 file..."); 175 selectInput("Select a file to process:", "fileSelected", 176 file); 177 } 178 179 if (key == 'x') streaming = false; 180} 181 182void fileSelected(File 183 selection) { 184 if (selection == null) { 185 println("Window was closed or 186 the user hit cancel."); 187 } else { 188 println("User selected " + selection.getAbsolutePath()); 189 190 gcode = loadStrings(selection.getAbsolutePath()); 191 if (gcode == null) 192 return; 193 streaming = true; 194 stream(); 195 } 196} 197 198void stream() 199{ 200 201 if (!streaming) return; 202 203 while (true) { 204 if (i == gcode.length) 205 { 206 streaming = false; 207 return; 208 } 209 210 if (gcode[i].trim().length() 211 == 0) i++; 212 else break; 213 } 214 215 println(gcode[i]); 216 port.write(gcode[i] 217 + '\ 218'); 219 i++; 220} 221 222void serialEvent(Serial p) 223{ 224 String s = 225 p.readStringUntil('\ 226'); 227 println(s.trim()); 228 229 if (s.trim().startsWith("ok")) 230 stream(); 231 if (s.trim().startsWith("error")) stream(); // XXX: really? 232} 233 234
Downloadable files
Wiring drawing for Motor shield to stepper
Wiring drawing for Motor shield to stepper
Wiring drawing for Motor shield to stepper
Wiring drawing for Motor shield to stepper
Complete wiring drawing
Complete wiring drawing
Comments
Only logged in users can leave comments
Mrinnovative
0 Followers
•0 Projects
0