From RepRap
Jump to: navigation, search

3D-to-5D-Gcode.php is A GPL'd script written and maintained by Erik de Bruijn. It has many features, the primary function is to convert skeinforge's GCode to GCode that the 5D Firmware understands. It adds the E-codes and removes M101, M102 and M103 codes. This frees the microcontroller firmware from having to spend time doing square root calculations. The script can also manipulate GCode according to rules that you define and it can compensate for backlash in your cartesian bot.

The code for this script can be found in subversion, here (in /reprap/trunk/users/erik/). You can also use subversion to check out just my files, in the following way:

# svn co http://reprap.svn.sourceforge.net/svnroot/reprap/trunk/users/erik

Feature list

  • Converts 3D GCode into 5D Gcode. Compatible with Skeinforge and the 5D Firmware.
  • Backlash compensation: Get better print quality without changing your hardware.
  • String replacement: Need to routinely change some piece of GCode? Have it changed automatically.
  • Rule based GCode manipulation: manipulate X, Y and Z when conditions are met.
  • Automatic write to SD card and safe removal (Linux only)
  • automated input file search will perform a search for the input file in varous directories: You don't have to type long paths every time, just the part of the filename without .gcode.
  • Automatic determination of output file name
  • Can rescale and offset g-code (just add parameters: "--scale 1.2" (scales X and Y) or "--scale_z 0.9" and/or "--offset_x 10" --offset_y -10")
  • Support for different configurable device profiles (with offsets and scale tuning)
  • Anti-ooze or extruder pullback support: this is experimental. It will not reverse if the head only travel a very short distance before extruding again. Disabled in the default version.
  • Rudimentary dual extruder support. It can be configured to print with the second extruder (BfB GCode only, I think)
  • Several workarounds to make it easier to use it with custom RepRaps based on BitsFromBytes electronics and firmware.

Feature: 3D to 5D GCode conversion

The OLD, 3D (lame!) way:

 M108S200 ; extruder motor speed at 200 PWM
 M101 ; extruder motor forward
 G1 ... ; make your moves...
 M103 ; extruder off

New, Now 5 Dimensionally!

 G1 X0.0 Y0.0 Z0.0
 G1 X1.0 Y1.0 E1.4142 ; Extrude the pythagorian distance (X^2+Y^2)^.5. Assuming just XY plain (2D) moves...
 G1 X0.0 Y0.0  ; Go back, no extrusion

Adding extrusion lengths (E codes) is now supported in Skeinforge too, but you cannot precisely control the amount of extrusion with parameters (such as when you're changing materials).

Feature: Backlash compensation

A picture says a thousand words...


On the left, you see a chess pawn printed without backlash compensation. On the right, this "software" setting was turned on. Reducing backlash becomes disproportionately harder when you need to go further. You hit constraints like exceeding a healthy belt tension, running the motors with too much power, etc.

This play is not there on my X axis, because that one doesn't use microstepping and has more resonance/vibrations. My theory is that this vibration causes the XY-carriage to settle closer to where it's supposed to be, instead of lagging a bit behind when it was supposed to move.

Now, the script adds an amount of movement that you can configure per axis. It was actually easier than I thought, so it's definitely something to add to the Java host as well. You need to set up 4 numbers, backlash_X_forward, backlash_X_reverse, backlash_Y_forward, backlash_Y_reverse. I've made the unit milimeters. The compensation simply needs to know the direction in which it was moving, and add the backlash. The static friction seems to be symmetrical, but the length of the belt (that stretches slightly) connecting the pulley to the Y axis, is asymmetrical.

Anti-backlash is configured per axis, as follows:

$setting['anti-backlash'] = array(
       'X'=>array('fwd_dynamic'=>0.10/*mm*/, 'rev_dynamic'=>0.10/*mm*/,'fwd_static'=>0.1/*mm*/, 'rev_static'=>0.1/*mm*/ ), // Backlash on X-axis
       'Y'=>array('fwd_dynamic'=>0.45/*mm*/, 'rev_dynamic'=>0.45/*mm*/,'fwd_static'=>0.1/*mm*/, 'rev_static'=>0.1/*mm*/ ), // Backlash on Y-axis

These are the settings on my machine. Adjust experimentally.

Feature: String replacement

It can also do simple find- and replace's on Gcode lines, which I currently use to speed up the raft making (of which the base layer is slow in Skeinforge, with a reason I now learned). All of the features can be turned on or off in a configuration file or in the top section of the php script.

Feature: GCode manipulation

In the configuration header, you will find this example line:

$setting['actions'] = array(
        array('while'=>array('match','F330.0'),'actions'=>array('E_mul'=>7,'F_mul'=>0.7)), // raft making: slow down XY-moves, speed up extrusion.

Once the script finds a line matching the string F330.0, it will perform the actions on that line. With my settings, the raft is set for feedrate 330.0 in skeinforge, so this will apply to the raft. There can be several actions, which are applied in that order. E_mul will multiply the E value on that line (the extrusion axis). It accepts, X, Y, Z, E and F. This example slows down XY movements while increasing the amount of extrusion in that area. This is meant to create a better raft.

In addition to a 'while' condition, start and stop conditions are possible.

If you agree that using the feedrate to match against a raft is not very robust, there is a better way:

$setting['actions'] = array(
  array( // raft making: slow down XY-moves, speed up extrusion. 
         'condition_start'=>array('match','<layer> 0.5'), // set it to match the foundation layer of your raft.

This does the same as the earlier while condition if your foundation layer is at 0.5.


Ubuntu/Debian Linux

$ sudo apt-get install php5-cli   # if you don't have php
$ svn co http://reprap.svn.sourceforge.net/svnroot/reprap/trunk/users/erik/3D-to-5D-G-code.php

Make it executable:

$ chmod +x 3D-to-5D-G-code.php


[Download the Windows PHP executable]. Select either "IIS CGI" or "no server" during install. Make sure the PHP bin directory is added to you environmental variable "PATH"; The installer should do this automatically.

Open a command prompt window by clicking Start->Run and typing "cmd" in the box without the quotes. Navigate to the directory where you downloaded the script, e.g.

cd C:\Users\username\Downloads

To run the script, use the same command line as on linux, but add "php" to the front of it.

php 3D-to-5D-G-code.php [input_file] [--output_file filename]

Please add instructions if you use Mac and you've figured them out.


To run it, remove the output file if it already exists. Otherwise it will be appended. I find this useful for doing batches.

$ ./3D-to-5D-G-code.php {input file.gcode} [--output_file output.gcode]

If it doesn't run, correct the path on the first line to include the correct path to your PHP binary. Alternatively, run php in front of the script's name, e.g. "php 3D-to-5D-G-code.php {commands}"