G-code

From RepRap
Revision as of 08:46, 16 February 2010 by Adrianbowyer (talk | contribs) (RepRap G Code Fields)
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 an X coordinate can be integer (X175) or fractional (X17.62), whereas selecting extruder number 2.76 makes no sense.

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.

Individual commands

Checking

N and *

Example: N123 [...G Code 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.

You can leave both of these out - RepRap will still work, but it won't do checking. You have to have both or neither though.

The checksum, cs, 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;  // Defensive programming...

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

Buffered G 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 in a straight line 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

Example: G28

Move to the origin. This causes the RepRap machine to move back to its X, Y and Z zero endstops. It does so accelerating, so as to get there fast. But when it arrives it backs off by 1 mm in each direction slowly, then moves back slowly to the stop. This ensures more accurate positioning.

Unbuffered G 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. Short pauses between these commands and any that might follow them do not affect the performance of the machine.

G4

Example: G4 P200

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

G20

Example: G20

Units from now on are in inches.

G21

Example: G21

Units from now on are in millimeters. (This is the RepRap default.)

G90

Example: G90

All coordinates from now on are absolute relative to the origin of the machine. (This is the RepRap default.)

G91

Example: G91

All coordinates from now on are relative to the last position.


G92

Example: G92 X10 E90

Set the current position to the values specified. This would set the machine's X coordinate to 10, and the extrude coordinate to 90.

Unbuffered M and T commands

M104

Example: M104 S190

Set the temperature of the current extruder to 190oC and return control to the host immediately (i.e. before that temperature has been reached by the extruder).

M105

Example: M105

Request the temperature of the current extruder in degrees Celsius. The temperature is returned to the host computer.

M106

Example: M106

Turn on the cooling fan (if any).

M107

Example: M107

Turn off the cooling fan (if any).

M108

Example: M108 S0.7

Set the power used by the extruder stepper motor. Power is regulated using pulse-width modulation (PWM). In the example the PWM duty cycle would be set to 70%. See also M113.

M109

Example: M109 S190

Set the temperature of the current extruder to 190oC and wait for it to reach that value before sending an acknowledgment to the host. In fact the RepRap firmware waits a while after the temperature has been reached for the extruder to stabilise - typically about 40 seconds. This can be changed by a parameter in the firmware configuration file when the firmware is compiled.

M110

Example: N123 M110

Set the current line number to 123. Thus the expected next line after this command will be 124.

M111

Example: M111 S6

Set the level of debugging information transmitted back to the host to level 6. The level is the OR of three bits:

#define DEBUG_ECHO (1<<0)
#define DEBUG_INFO (1<<1)
#define DEBUG_ERRORS (1<<2)

Thus 6 means send information and errors, but don't echo commands.

M112

Example: M112

Reserved for Immediate Stop. Not yet implemented.


M113

Example: M113

Use the on-board potentiometer on the extruder controller board to set the PWM for the extruder's stepper power. See also M108.

M126

Example: M126 P500

Open the extruder's valve (if it has one) and wait 500 milliseconds for it to do so.

M127

Example: M127 P400

Close the extruder's valve (if it has one) and wait 400 milliseconds for it to do so.

T

Example: T1

Select extruder number 1 to build with. Extruder numbering starts at 0.