The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Gcode::Interpreter::Ultimaker

SYNOPSIS

  use Gcode::Interpreter::Ultimaker;

  $interpreter = Gcode::Interpreter::Ultimaker->new();

  $interpreter->set_method('table');

  $interpreter->parse_line('G1 X1.0 Y1.0 Z1.0 E1.0');

  $position_ref = $interpreter->position();

  $stats_ref = $interpreter->stats();

DESCRIPTION

This module aims to simulate an Ultimaker 3D printer running the Marlin firmware. It parses the Gcode that the Ultimaker uses and keeps track of the printer's extruder head position. It also keeps track of the time it would likely take to reach the current position based on the various moves already processed. Likewise it keeps track of the length of filament extruded.

The parser works on a line-by-line basis. This allows the caller to inspect the state of the virtual printer throughout the simulated print. Future developments may make this interface less interactive to increase parsing speed.

The calculation of the estimated duration of a print is a relatively difficult task as it can vary depending on specific settings of the printer. However, there are three methods to perform this estimation with varying degrees of accuracy (and speed).

The first method is known as the "infinite acceleration" method. It assumes that the printer head instantly gets to maximum speed, and instantly stops at its destination. Because of this, it usually makes very optimistic estimates, but it has the advantage of being a simple and fast calculation.

The second method is to use a lookup table of speeds for various distances. This again is a series of estimates, and inherently inaccurate. However, it's again a fairly fast calculation and gives better results in most cases than the "infinite acceleration" method.

The third method is to simulate the firmware of the actual printer. This method understands the speed the head is moving throughout a movement, and so can calculate the time the movement will take very accurately. Whilst this method still misses some details and machine differences, it typically gives excellent results. This method is complex, and so relatively slow. It's also not implemented (yet) in this module.

This module is a specialised sub-module of the Gcode::Interpreter module. It would normally not be instantiated directly, although definitely can be if required.

METHODS

new

The constructor takes no arguments and returns a blessed object.

position

Takes no arguments and returns a reference to a four element list which denotes the position of the printer. The list is X,Y,Z and E. Each value will be a float denoting the distance from the origin in the measurement units of the printer (which can be set dynamically between millimeters and inches).

stats

Takes no arguments and returns a reference to a hash containing two elements ('extruded' and 'duration'). These two elements denote the amount of filament extruded (in the measurement units of the printer) and the fractional seconds taken from the start of the print until the end of the last line of gcode processed.

set_method

Takes a string argument related to the calculation method used to work out the print time duration. Currently supported values are 'fast' (for the 'infinite acceleration' method) and 'table' for the calibrated lookup table method.

The method can be changed at any time, but don't take effect retrospectively so may lead to inaccurate results.

parse_line

This method takes a string argument containing a line of Gcode instruction. It returns true on succesful parsing and false if it fails.

FUNCTIONS

These functions are primarily internal to the module and don't really have 'friendly' APIs for external use. They're highly performance optimised (which is why they're functions instead of methods), and so somewhat specialised.

Their inputs and outputs may change from time to time. Caveat programmer ;-)

num_from_code

This function can retrieve the numeric value of a Gcode entity from a string of Gcode. For example, it could be used to extract "220" from 'M104 S220'.

xyze_from_words

This function returns a list of X,Y,Z and E values for a given gcode string. For example, 'G1 E40 X10 Y20 Z30' would return 10,20,30,40.

SEE ALSO

Marlin (Ultimaker firmware): https://github.com/ErikZalm/Marlin Cura (slicer, Gcode generator and sender): https://github.com/daid/Cura Printrun (Gcode sender): https://github.com/kliment/Printrun

A script is provided in the 'scripts' directory of the distibution that can perform the calibration tests on a printer and outputs the results in cut-and-paste format that can be put into this module.

Another script (called 'gin') is also provided which can use this module to calculate the total time and filament required for a print. It can also optionally write out some meta data as it works for use in other Gcode-related applications.