APIs, concepts, guides, and more
G-Code

Use G-Code to specify coordinated motions in Cartesian space for specialized machines, with support for basic codes and a user interface for building or using pre-built configurations.

Note
The RapidCode G-Code Parser and Executor are not a replacement for a true CNC controller. It is not a simple drop-in replacement for a full function CNC provided by companies who offer turn-key solutions in this field. However, the flexibility of the RMP motion controller as well as interoperability with PathMotion and arbitrary kinematic robot models make it ideal for designing specialized machines that use G-Code as the motion command source.

🔹 What is G-Code?

G-Code is a programming language for specifying coordinated motions in cartesian space used by many manufacturing machines.

RapidCode API G-Code Sample

🔹 How does G-Code work?

In the RapidCode API, G-Code parsing requires the creation of a Robot instance, which requires the kinematic model to be specified, and a MultiAxis instance that contains all of the axes used during the execution of the G-Code file. Machines with linear prime or redundant axes can also be configured by specifying the axis to link their motion to using LinearModelBuilder.

You can find the full set of G-Code functions in our Gcode class.

🔹 User Interface

Build your user interface using our Gcode class or use our G-Code UI built into RapidSetup.

RapidSetup: G-Code UI

🔹 Supported Codes

Note
We currently support a set of G-codes focused mainly on path motion.
For additional G-code support, please Contact Us.
Code Description Explanation
G0 Rapid positioning Rapid motion to a point in Cartesian space via the quickest path (not always linear), using default axis settings (DefaultVelocitySet)
G1 Linear interpolation Coordinated linear motion between points in cartesian space
G2 Circular interpolation, clockwise Clockwise arc motion about a center point to an end point
G3 Circular interpolation, counter clockwise Counter clockwise arc motion about a center point to an end point
G4 Dwell Pauses all axes for the specified time in seconds, which may be fractional.
G9 Exact stop check (non-modal) Placed before a motion group command, the machine will come to a complete stop at end of the move before continuing to the next. Only remains active for one line.
G17 XY plane selection G2/3 commands happen on the XY plane (Default mode)
G18 ZX plane selection G2/3 commands happen on the ZX plane
G19 YZ plane selection G2/3 commands happen on the YZ plane
G20 Programming in inches (in) Distances after this command will be interpreted as inches. Rotations are always in degrees
G21 Programming in millimeters (mm) Distances after this command will be interpreted as mm. Rotations are always in degrees
G54-G59 Work Offset Applies a pre-configured XYZ offset relative to home(origin) to all motions.
G61 Exact Stop Check (modal) The machine will come to a complete stop at end of the move before continuing to the next for all future moves until G64 is called.
G64 Cancel Exact Stop Reset to default cutting mode (cancel G61)
G90 Absolute programming Distances after this command will be interpreted as absolute (relative to origin 0,0,0)
G91 Incremental programming (relative) Distances after this command will be interpreted as incremental (relative to current position at start of move)
G92 Coordinate System Offset Sets the current (absolute) position to have the specified coordinate values by moving the origin.

🔹 M-Code Support

Currently, all M-codes are supported by calling a user-defined callback, as specified in GcodeCallback.

When running a G-Code file (Gcode.Run), when the M-code is encountered, it completes the motion preceding it, executes the callback, and then continues the motion in the remainder of the file.

🔹 Not Yet Supported

Helical G2/3 motions
Features not listed above

Warning
G-Code is not running in the RMP firmware but instead, an internal RapidCode thread that buffers motions to the RMP
(Getters and setters will not work across multiple applications at the same time)

📜 Sample Code

G-Code with Free Axis

Setup and run a simple G-Code program.

  • C#

    string gcodeProgram = "G91; Sets the programming mode to RELATIVE\n" +
    "G64; Turns off exact stop mode(Default)\n" +
    "G1 X1.0 Y0.0 Z0.0 A1.0 F60.0; Move on USERUNIT in positive x direction at 60in/min. Moves Free axis A to position 1.0.\n" +
    "G3 X1 Y1 I0 J1; Counter clockwise arc with a center point of 0,1,0 and end point of 1,1,0 relative to the current position\n" +
    "M80; Show how to use an M-code with GcodeCallback!\n";
    // We assume the axes have been confgured and homed properly prior this this program.
    const string xLabel = "X-Axis";
    const string yLabel = "Y-Axis";
    const string zLabel = "Z-Axis";
    const string aLabel = "A-Axis";
    x_axis.UserLabelSet(xLabel);
    y_axis.UserLabelSet(yLabel);
    z_axis.UserLabelSet(zLabel);
    a_axis.UserLabelSet(aLabel);
    // The joint index of each axis is the index within the MultiAxis object.
    // "X-Axis" has joint index 0
    // "Y-Axis" has joint index 1
    // "Z-Axis" has joint index 2
    // "A-Axis" has joint index 3
    Axis[] axes = new Axis[] { x_axis, y_axis, z_axis, a_axis };
    jointsMultiAxis.AxesAdd(axes, axes.Length);
    jointsMultiAxis.ClearFaults();
    jointsMultiAxis.AmpEnableSet(true);
    const string modelName = "RSI_XYZA";
    const double scaling = 1.0, offset = 0.0;
    LinearModelBuilder builder = new LinearModelBuilder(modelName);
    builder.JointAdd(new LinearJointMapping(0, CartesianAxis.X) { ExpectedLabel = xLabel, Scaling = scaling, Offset = offset });
    builder.JointAdd(new LinearJointMapping(1, CartesianAxis.Y) { ExpectedLabel = yLabel, Scaling = scaling, Offset = offset });
    builder.JointAdd(new LinearJointMapping(2, CartesianAxis.Z) { ExpectedLabel = zLabel, Scaling = scaling, Offset = offset });
    builder.FreeAxisAdd(new ModelAxisMapping(3) { ExpectedLabel = aLabel, Scaling = scaling, Offset = offset }); // Add a free axis.
    // Create a Robot object with a multi axis containing all joints and the kinematic type
    robot = Robot.RobotCreate(controller, jointsMultiAxis, builder, MotionController.AxisFrameBufferSizeDefault);
    robot.Gcode.AccelerationRateSet(1000); // UserUnits per minute squared
    // The free axis index here refers to the index in the RobotPosition freeAxes array. We use index 0 here to refer to the first position in that array.
    robot.Gcode.FreeAxisLetterSet('A', 0);
    robot.Gcode.CallbackRegister(callback); // Register a callback to be called when the Gcode encounters any M-code command
    try
    {
    robot.Gcode.Load(gcodeProgram); // Loads in the string and prepairs it for execution
    }
    catch (Exception e)
    {
    Console.WriteLine("Error loading G-Code: " + e.Message);
    // get any additional G-Code errors (will give you details of every line number that has an error in parsing or processing)
    throw e; // exit the program
    }
    // print some details about the upcoming motion
    Console.WriteLine("G-Code Line Count: " + robot.Gcode.LineCountGet());
    Console.WriteLine("G-Code Error Log Count: " + robot.Gcode.ErrorLogCountGet());
    Console.WriteLine("G-code estimated run time: " + robot.Gcode.DurationGet() + " seconds");
    robot.Gcode.Run(); // Starts the motion. Calling with the false arguement for non blocking behavior
    Int64 activeLineNumber = 0;
    do
    {
    Thread.Sleep(200);
    if (activeLineNumber != robot.Gcode.ExecutingLineNumberGet()) // only write if we are on a new line
    {
    activeLineNumber = robot.Gcode.ExecutingLineNumberGet();
    Console.WriteLine("G-Code Line Number: " + activeLineNumber);
    }
    } while (robot.Gcode.IsRunning());
    HelperFunctions.CheckErrors(robot.Gcode); // Check for any errors that may have occured during the motion

Changing Linear Units G-Code

How changing linear units affects vel and accel setters.