G-code

From RepRap
Revision as of 07:57, 16 February 2010 by Adrianbowyer (talk | contribs)
Jump to: navigation, search

Introduction

This page describes the G Codes that the RepRap firmware uses, what they mean, and how they work.

The list of what can be done is extensible. But check here first, and add your extension here first before you implement it.

A typical piece of GCode as sent to a RepRap machine might look like this:

N3 T0 *86
N4 G92 E0 *102
N5 G28 *13
N6 G1 F1500.0 *117
N7 G1 X2.0 Y2.0 F3000.0 *71
N8 G1 X3.0 Y3.0 *39


RepRap G Code Fields

This section explains the letter-preceded fields. The numbers in the fields are represented by nnn. Numbers can be integers, or can contain a decimal point, depending on context. For example clearly an X coordinate can be integer or fractional, whereas selecting extruder number 2.76 makes no sense.

In the section below, individual commands are distinguished.

Letter Meaning
Gnnn Standard GCode command, such as move to a point
Mnnn RepRap-defined command, such as turn on a cooling fan
Tnnn Select tool nnn. In RepRap, tools are extruders
Snnn Command parameter, such as the voltage to send to a motor
Pnnn Command parameter, such as a time in milliseconds
Xnnn An X coordinate, usually to move to
Ynnn A Y coordinate, usually to move to
Znnn A Z coordinate, usually to move to
Innn Parameter - not currently used
Jnnn Parameter - not currently used
Fnnn Feedrate in mm per minute.
Rnnn Parameter - not currently used
Qnnn Parameter - not currently used
Ennn Length of extrudate in mm. This is exactly like X, Y and Z, but for the length of filament to extrude.
Nnnn Line number. Used to request repeat transmission in the case of communications errors.
*nnn Checksum. Used to check for communications errors.

Checksums for a GCode cmd (including its line number) are computed as follows:

int cs = 0;
for(int i = 0; i < cmd.length(); i++)
   cs = cs ^ cmd.charAt(i);
cs &= 0xff;

and the value is appended as a decimal integer to the command after a '*' character.


Individual commands

Checking

N and *

Example: N123 ... G Codes in here ... *71

These are the line number and the checksum. The RepRap firmware checks the checksum against a locally-computed value and, if they differ, requests a repeat transmission of the line of the given number.

Buffered Commands

The RepRap firmware stores these commands in a ring buffer internally for execution. This means that there is no (appreciable) delay while a command is acknowledged and the next transmitted. In turn, this means that sequences of line segments can be plotted without a dwell between one and the next. As soon as one of these buffered commands is received it is acknowledged and stored locally. If the local buffer is full, then the acknowledgment is delayed until space for storage in the buffer is available. This is how flow control is achieved.

G0

Example: G0 X12

Rapid move. In this case move rapidly to X = 12 mm. In fact, the RepRap firmware uses exactly the same code for rapid as it uses for controlled moves (see below), as - for the RepRap machine - this is just as efficient as not doing so. (The distinction comes from some old machine tools that used to move faster if the axes were not driven in a straight line. For them G0 allowed any movement in space to get to the destination as fast as possible.)


G1

Example: G1 X90.6 Y13.8 E22.4

Controlled move. Go from the current (X, Y) point to the point (90.6, 13.8), extruding material as the move happens from the current extruded length to a length of 22.4 mm.

RepRap does subtle things with feedrates. Thus:

G1 F1500
G1 X90.6 Y13.8 E22.4

Will set a feedrate of 1500 mm/minute, then do the move described above at that feedrate. But

G1 F1500
G1 X90.6 Y13.8 E22.4 F3000

Will set a feedrate of 1500 mm/minute, then do the move described above accelerating to a feedrate of 3000 mm/minute as it does so. The extrusion will accelerate along with the X, Y movement so everything stays synchronized.

RepRap thus treats feedrate as simply another variable (like X, Y, Z, and E) to be linearly interpolated. This gives complete control over accelerations and decelerations in a way that ensures that everything moves together and the right volume of material is extruded at all points.

The first example shows how to get a constant-speed movement. The second how to accelerate or decelerate. Thus

G1 F1500
G1 X90.6 Y13.8 E22.4 F3000
G1 X80 Y20 E36 F1500

Will do the first movement accelerating as before, and the second decelerating from 3000 mm/minute back to 1500 mm/minute.

G28

Unbuffered commands

The following commands are not buffered. When one is received it is stored, but it is not acknowledged to the host until the buffer is exhausted and then the command has been executed. Thus the host will pause at one of these commands until it has been done. Dwell between these commands and any that might follow them does not affect the performance of the machine.

G4

Example: G4 P200

Dwell, or delay. In this case sit still doing nothing for 200 milliseconds.

case 4: delay((int)(gc.P + 0.5)); break;

//Inches for Units case 20:

                               setUnits(false);

break;

//mm for Units case 21:

                               setUnits(true);

break;

//Absolute Positioning case 90: abs_mode = true; break;

//Incremental Positioning case 91: abs_mode = false; break;

//Set position as fp case 92:

                               setPosition(fp);

break;

default: if(SendDebug & DEBUG_ERRORS)

                               {
                               Serial.print("huh? G");

Serial.println(gc.G, DEC);

                               FlushSerialRequestResend();
                               }

} }



//find us an m code. if (gc.seen & GCODE_M) {

           // Wait till the q is empty first
           while(!qEmpty()) delay(WAITING_DELAY);
           //delay(2*WAITING_DELAY);

switch (gc.M) { //TODO: this is a bug because search_string returns 0. gotta fix that. case 0: break; /* case 0: //todo: stop program break;

case 1: //todo: optional stop break;

case 2: //todo: program end break; */

// Now, with E codes, there is no longer any idea of turning the extruder on or off. // (But see valve on/off below.)

/* //turn extruder on, forward case 101: ex[extruder_in_use]->setDirection(1); ex[extruder_in_use]->setSpeed(extruder_speed); break;

//turn extruder on, reverse case 102: ex[extruder_in_use]->setDirection(0); ex[extruder_in_use]->setSpeed(extruder_speed); break;

//turn extruder off

  • /

//custom code for temperature control case 104: if (gc.seen & GCODE_S) { ex[extruder_in_use]->setTemperature((int)gc.S); } break;

//custom code for temperature reading case 105: Serial.print("T:"); Serial.println(ex[extruder_in_use]->getTemperature()); break;

//turn fan on case 106: ex[extruder_in_use]->setCooler(255); break;

//turn fan off case 107: ex[extruder_in_use]->setCooler(0); break;

//set PWM to extruder stepper case 108:

  1. if MOTHERBOARD > 1

if (gc.seen & GCODE_S)

                                       ex[extruder_in_use]->setPWM((int)(255.0*gc.S + 0.5));
  1. endif

break;

                       // Set the temperature and wait for it to get there

case 109: ex[extruder_in_use]->setTemperature((int)gc.S);

                               ex[extruder_in_use]->waitForTemperature();

break;

                       // Starting a new print, reset the gc.LastLineNrRecieved counter

case 110: if (gc.seen & GCODE_N) { gc.LastLineNrRecieved = gc.N;

				  if(SendDebug & DEBUG_INFO)
                                   Serial.println("DEBUG:LineNr set");

} break; case 111: SendDebug = gc.S; break; case 112: // STOP!

                              {
                                int a=50;
                               while(a--)
                                 {blink(); delay(50);}
                              }

cancelAndClearQueue(); break;

                      case 113:
                           #if MOTHERBOARD > 1
                               ex[extruder_in_use]->usePotForMotor();
                           #endif

break;


// The valve (real, or virtual...) is now the way to control any extruder (such as // a pressurised paste extruder) that cannot move using E codes.

                       // Open the valve
                       case 126:
                               ex[extruder_in_use]->valveSet(true, (int)(gc.P + 0.5));
                               break;
                               
                       // Close the valve
                       case 127:
                               ex[extruder_in_use]->valveSet(false, (int)(gc.P + 0.5));
                               break;
                                                               

default: if(SendDebug & DEBUG_ERRORS)

                                 {
                                 Serial.print("Huh? M");

Serial.println(gc.M, DEC);

                                 FlushSerialRequestResend();
                                 }

}


}

// Tool (i.e. extruder) change?

       if (gc.seen & GCODE_T)
       {
           while(!qEmpty()) delay(WAITING_DELAY);
           //delay(2*WAITING_DELAY);
           newExtruder(gc.T);
       }