Skip to content

User Manual

rktech-de edited this page Aug 23, 2024 · 21 revisions

GCode generator for raster images

Installation

Simply copy all the *.py and *.inx files into the folder "Extensions" of Inkscape. Where this is located, take a look into "Edit => Settings => System" and use one of the folders configured in "User extensions" or "Inkscape extensions"

e.g. Linux "/usr/share/inkscape/extensions"

for unix (& mac maybe) change the permission on the file:

chmod 755 *.py
chmod 644 *.inx

The extension can be started with: Extension => GCode Laser Tools => Raster2Laser NG

The pull down entry can be changed in the *.inx file, look for <submenu name="GCode Laser tools"/>

Usage of "Raster2Laser NG"

[Required file: png.py / raster2laser_gcode_ng.inx / raster2laser_gcode_ng.py]

  • Resize the inkscape document to match the dimension of your working area on the laser cutter/engraver (Shift+Ctrl+D) or disable the Use page outline setting
  • Draw or import the image
  • To run the extension go to: Extension => GCode Laser Tools => Raster2Laser NG
  • Play!

Motivation

My setup and why I did this fork:

For Laser engraving and cutting I use this gantry cutter Sorotec AL-1110 with this laser Opt Laser PLH3D-6W controlled by LinuxCNC and a MESA 7I95 FPGA Ethernet IO interfaced . And because it needs some time (distance) to accelerate to the set feed rate I have add the "Distance for acceleration" value which is added to the beginning and end of a scan line, so the engraving process is done with constant velocity.

In my machine configuration I'm using not the spindle PWM and enable but I use a separate controller which is connected to the analog and digital in- and outputs which are available via LinuxCNC M62 to M68 codes for synchronous value changes while moving. Therefore I made the GCode generation more flexible, so it should be possible to use any kind of GCode (dialect) by configuration.

Even if I tested it only with my setup it should be now possible to use any other machine setup.

Detailed description

The Configuration is split into two parts, image processing and GCode generation.

Image Processing

Here the output directory and filename are configured also the Laser-Pixel size and image processing to get a black and white or gray scale image out of an colored picture.

The settings within this tab are related to produce a B/W or Grayscale image which is stored as a preview PNG file. If you check the Preview only checkbox processing will stop after creating this preview and the GCode parser will be not started.

Convert a color image into grayscale

First you need grayscale data if you have an color image source. This can be done with Color to Grayscale conversion. Here the examples and differences

color source MonaLisa
0.21R + 0.71G + 0.07B Natural brightness perception of the human eye
(R+G+B)/3 Arithmetic mean
R Use only the red channel
G Use only the green channel
B Use only the blue channel
Max Color Use the cannel with the highest value
This give the brightest image
Min Color Use the cannel with the lowest value
This give the darkest image

If non of this settings fits your need, you can do this also with an external Image Manipulation Program (e.g. GIMP) and use that grayscale result, then this configuration has no effect.

Black and White processing

For the laser engraving process the B/W processing is the simplest, the laser has only to change the color or surface, or even not. And some material only allow such processing e.g. glass.

But getting good B/W date from a grayscale image is quite complex. Here are some examples

B/W threshold => 64 128 192
B/W fixed threshold
B/W random threshold
Halftone
Halftone row
Halftone column
Simple2D
Floyd–Steinberg
Jarvis-Judice-Ninke

You also can do this externally with an Image Manipulation Program. If you do so and your source is already B/W you should use this setting:

Color to Grayscale conversion = (R+G+B)/3 (But this doesn't matter)

B/W conversion algorithm = B/W fixed threshold

B/W threshold = 128

Grayscale processing

The grayscale mage processing is much more easy then getting a nice B/W picture, you only have to select the numbers of gray levels. With a number of 2 levels you have the same as an B/W image with a threshold of 128, with three you have 0%, 50% and 100% intensity and so on.

To get nice grayscale result you have to choose the right material and configure good power levels and feed rate.

But the most important thing is, that your engraving hardware will move with a constant velocity during the active scan line phase and will not reduce speed or stop when changing the laser power.

Number of grey levels => 2 3 4 8 128
Grayscale

The number of gray level had an influence of the number of GCode commands for a scan line, because with an high level a little gray scale difference will create a new command. If you see sometimes unwanted black dots in the result, reducing the gray levels could help.

GCode Parser

The CGode generation is done completely different then in the fork master repository.

The GCode generator didn't "produce" any kind of G or M commands in the python script (only some lines with comments with the settings at the beginning). It only generate the values for positions and power which can be used in this GCode configurations.

  • Init code: put in here everithing you need to setup your machine once at startup
  • Start of line code: put here the commands you need at the beginning of a new scanline
  • Laser off travel code: put here the commands you need to travel (fast) to a new position with laser off
  • Power level change code: this code is executed whenever the laser power value changes. It will be always set to minimum power level at the end of a scanline
  • Post code: put in here everithing you need to stop your machine once at the end

Possible variables to be used for this four parameters

Variable description
{NL} new line in the GCode file at this position, currently hard-coded with '\n'. At the end of an config string an newline is automatically added
{XPOS} calculated X position
{YPOS} calculated Y position
{ZPOS} always the configured "Z level" value
{APOS} calculated A position of an rotary axis, based on the Y position and the workpiece diameter
{BPOS} calculated B position of an rotary axis, based on the X position and the workpiece diameter
{FEED} always the configured "Feed rate" value
{POWT} calculated laser power, based on gray level and laser power min/max configuration
{PCMT} substituted by the Laser ON command or Laser OFF command string depending on grey level
{SCNL} actual scan line number for comment purpose
{SCNC} actual column number for comment purpose
{PDIR} depends on current scan line move direction. Will be: "->", "<-", "/\" or "\/"
{POWM} configured value from Minimum laser power value
{POWX} configured value from Maximum laser power value
{POWL} this represents the maximum power level within the current scan line
{PIXV} pixel value, intensity of the current pixel, 0 = Black, 255 = white, 256 = outside engraving path

Image slicing

Depending on the Scan image lines configuration the slicing is done horizontal (X or B) or vertical (Y or A). The slicing will always start top-left from the inkskape image. I will describe it for the horizontal slicing, for vertical it is the same but 90° rotated.

This is an example image to show how a 15x8 pixel image will be processed, where the zero point is located depending on the configuration.

Zero point configuration
examples
GCode X dimension [mm]
5 pixel/mm
GCode Y dimension [mm]
5 pixel/mm
Bottom, Left 0.0 .. 3.0 0.0 .. 1.6
Bottom, Center -1.5 .. 1.5 0.0 .. 1.6
Bottom, Right -3.0 .. 0.0 0.0 .. 1.6
Middle, Right -3.0 .. 0.0 -0.8 .. 0.8
Top, Right -3.0 .. 0.0 -1.6 .. 0.0

The Acceleration distance is outside this dimensions , so be aware of it when setting your machine zero position.

The next image show how a image line (scan line 3) is processed.

Configuration used for this example
Scan image lines Left to Right
Zero point for width Left
Resolution 5 pixel/mm
Acceleration distance 15 mm
Minimum laser power value 10
Maximum laser power value 100
Laser on threshold 254

With Optimize scan line set to remove blank lines, reduce scan line length in the scan line the most left and right pixel is searched where the pixel value is equal or below Laser on threshold (Laser on condition). otherwise the first and the last pixel is used. This is the beginning and end position of the engraving path and the laser should move here with constant velocity (feed rate). So the most left and right withe area is not used for the path calculation. With the setting Distance for acceleration an additional distance is added to the left and right of the engraving path with laser power set to Minimum laser power value to allow the machine to accelerate and decelerate. This will prevent darker/deeper engraving at the left and right side of the engraved picture. Even if this setting is set to 0, it will be added one pixel with laser power minimum on both sides.

In this example the minim laser power is set to 10 so the laser is not turned off during acceleration and next line traveling. This could result in unwanted lines if the laser feed rate is below the engraving feed rate and happens for sure wen the laser change the direction. to avoid this you can use the Laser on/off command and the Laser on threshold parameter. Set the Laser on threshold parameter to 254, the Laser on command to {POWT}, the Laser off command to 0, and replace the {POWT} in the Power level change code to {PCMT} this will use 0 when the image color is white or the the laser is accelerating or traveling and use the laser power value if the image color is not white.

If the complete scan line is white no GCode is generated for this line. The start position of the next line will be calculated depending on the Scan image lines setting.

Left to Right: laser engraving will always start on the left side and travel to the right.

Right to Left: laser engraving will always start on the right side and travel to the left.

Zig Zag X: the laser will always move left to right at even and right to left at odd scan lines (start counting with line 0 as even) no matter if there are blank lines in between.

Fastest X: with every new line it is calculated if the left/right or right/left start point is the nearest on from the current end position. If a distance of more then 3x the Distance for acceleration has to be passed with laser off within a can line. Travel started after with Distance for acceleration when the Laser was switched off and ended with Distance for acceleration before the laser is switched on again.

Configuration Page "General setup"

Setting Description
Export directory Directory path
File Name File name for the preview and GCode file. The Filename will be extended with additional information which conversion and additional settings where selected.
Add numeric suffix to filename Sdd a four digit increasing number to the filename so an already created preview and GCode file will not be overwriten
debug Write all configuration settings into the GCode file
Preview only Only create the preview image with the given parameters, but do not crate the GCode file

Configuration Page "Image setup"

Setting Description
Replace transparency with This coulour will be used when your object(s) is smaler then the page size to fill the background.
Be aware, the the Inkscape document setting for the background coulor did not take affect here, the page background is always handled as "transparnt" coulor.
Resolution Select here the number of laser lines per mm you want to engrave depending on your laser spot size, or select Use laser spot size to define it by the spot diameter.
Laser spot size[mm] Size of the laser spot diameter to calculate the number of scanlines per mm. This setting will only be used if you select Use laser spot size in the Resolution configuration.
Color to Grayscale conversion Type of algorithm how to convert the three RGB values in into one grayscale value. If your source image is already grayscale this setting has no effect.
0.21R + 0.71G + 0.07B - Natural brightness perception of the human eye
(R+G+B)/3 - Arithmetic mean
R - Use only the red channel values
G - Use only the green channel values
B - Use only the blue channel values
Max Color - Use the cannel with the highest value
Min Color - Use the cannel with the lowest value
B/W conversion algorithm B/W fixed threshold
B/W random threshold
Halftone
Halftone row
Halftone column
Simple2D
Floyd–Steinberg
Jarvis-Judice-Ninke
Grayscale
B/W threshold This will specify the threshold when using B/W fixed threshold, Simple2D, Floyd–Steinberg or Jarvis-Judice-Ninke as conversion algorithm. For all other algorithms it has no effect
Number of grey levels This will specify the number of gray levels when using Grayscale as conversion algorithm. For all other algorithms it has no effect
Workpiece diameter for rotary axis [mm] This value is used to calculate the values for the rotary axis {APOS} and {BPOS}, do convert mm into degree values.
Use page outline If this check box is set the engraving size and zero point calculation is done by the Inkscape pagesize setting.
If not set, this calculation is done by the object size. If there are more then one object at the Inkscape page a bound box around all this objects is used.

Configuration Page "G-Code Laser"

Setting Description
Name of this GCode setting The idea is, that in the final version there are more G-Code pages planned to easily switch between different setups, this field give you the possibility to give it a meaningful name. it is also put in the comment part of the GCode file.
Init code Anything you need to setup your machine, you can add the homing sequence here.
Among other things I'm starting the laser power supply and wait for the ready signal.
You can insert {NL} to seperate the commands in different lines.
Post code This is the code placed at the end of the GCode file, the M2 command has to be placed here. I also switch off the laser power here.
Start of line code This code line is generated at the beginning of a new scan line. {PDIR} will give an arrow with the path direction, and {SCNL} will give the scan line number. I use this for a comment in the GCode file. Also {NL} is possible.
After a Start of line code always a Laser off travel code is generated.
Laser off travel code This code is used to travel with the maximum speed to the given position. The variables {XPOS} and {YPOS} are substituted with the coordinates. The acceleration distance is included here. you can use {APOS} and {BPOS} when you are using a rotary A or B axis, this values are in degree and calculated with the workpice diameter setting. Here A corespondent with Y and B with X.
This code is always executed after the Start of line code but is also used if in the Scan image lines the Fastest X or Fastest Y mode is enabled and a distance of more then 3x the Distance for acceleration has to be passed with laser off. Travel started with Distance for acceleration when the Laser was switched off and ended with Distance for acceleration before the laser is switched on again.
Power level change code This code line is generated anytime the laser power level changes. Here you should use the {PCMT} variable to set the laser ON/OFF command or {POWT} variable to set the laser power value defined by the MIN/MAX value for white/black. Also {XPOS}, {YPOS}, {APOS} and {BPOS} can (must) be used here.
Laser ON command When using the {PCMT} variable in the code config, it is substituted with this command setting when the image pixel color is below or equal the Laser on threshold setting.
Laser OFF command When using the {PCMT} variable in the code config, it is substituted with this command setting when the image pixel color is above the Laser on threshold setting.
Laser on threshold This value decide whether the Laser ON command or Laser OFF command setting is used. If the image pixel value is below or equal the threshold the Laser ON command setting is used for the {PCMT} variable otherwise the Laser OFF command setting. A value of 0 means black and 255 is for a white pixel color. It is also used for detection the engraving path borders.
engraving feed rate This value is put into the {FEED} variable.
Minimum laser power value This value is put into the {POWT} variable if the image pixel is set white.
Maximum laser power value This value is put into the {POWT} variable if the image pixel is set black.
Distance for acceleration This is the distance added to the left/right or top/bottom to allow the machine to accelerate and decelerate without affect to the engraving.
Z position This value is put into the {ZPOS} variable.
Optimize scan line remove blank lines, reduce scan line length this will skip lines with all pixel white, and reduce the scan line to the size between the most left and right not white pixel
remove blank lines only skip lines with all pixels withe, but use the full length of the scan line as engraving path
no movement optimization use all given scan lines in full length as engraving path
Scan image lines Define the processing of the direction of the engraving path as described above in the slicing section.
Zig zag offset when using Zig Zag X or Fastest X the offset value is added to X position calculation when move is right to left, same for Y position on bottom to top moves. this can be used for backlash compensation and/or laser power time delays.
Interleaved line scan with fixed laser power This can help on machines, where the power level change is not fast enough or will stop the laser movement so a dark "edge" is visible on gray level changes.
With this configuration switched on a scan line is repeated with all gray levels seperatly. The laser power level for this line is stored in {POWL} and can be used in the Start of line code so the power level change can be done "outside" the image. In the Power level change code the laser is only switched on and off any more. But switching the laser on and off must be possible without stopping the movement. Be aware, that with activating this, every gray scale level need it's own line move and can increase the number of line moves dramatically.

Configuration Page "G-Code Image"

Setting Description
Flip X The pixel image data is flipped at the X axis (upside down) before prozessing the GCode (Preview image is not flipped). This is useful if your machine coordinate system is different from otheres, if the rotary axis is mounted 180° turned or if you engrave on the backside of glass. To get the final result in the same format then the inkscape image
Flip Y The pixel image data is flipped at the Y axis before prozessing the GCode (Preview image is not flipped).
Invert (Black/White) If set to false (normal), a black pixel had the highest laser power and white the lowest. If invert is active the white pixel had the highest laser power and black the lowest. Use this setting if your engraving material has an inverse color change, e.g. slate.
Gamma value for laser power output calculate the gamma onto the laser power value, the Minimum laser power value and Maximum laser power value will not change, but the image will be darker for gamma values grater and brighter for values lower then 1. This can help to compensate the laser and/or material characteristic.
Zero point for width This will set the zero point for the GCode coordinate system to Left, Center or Right. Flipping the image has no affect to this setting.
Zero point for height This will set the zero point for the GCode coordinate system to Top, Middle or Bottom. Flipping the image has no affect to this setting.

Machine Setup Configuration Examples

LinuxCNC

Calibration images
Gray wedge to find white and black level and also check the gamma