Raster Engraving

From Knox Makers Wiki
Revision as of 16:28, 16 March 2015 by Pestes (talk | contribs) (Created page with "== Rastering an image == I have seen recommendations the PNG files be used for this purpose, but because the raster functionality uses the Python Image Library for all image p...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Rastering an image

I have seen recommendations the PNG files be used for this purpose, but because the raster functionality uses the Python Image Library for all image processing, it seems as if any image format readable by PIL should be useable.

The O145 subroutine is used to trigger the Raster function in Ben Jackson's configuration for driving the Buildlog 2.x lasercutter. Because Linux CNC only allows numeric values to be passed to external programs, the filename of the image is expected to be of the format "*-xxxx.ext" where "xxxx" is any integer value and "ext" is an image file extension. For automatic file location to work, the image file must exist in "/home/jamesbond/Desktop/Gcode" and it must, at the very least, include the hyphen. Some valid filename: "pestes-555.png", "a-12.gif", "-890198283.jpg", "555-12345.gif". Invalid filenames: "555.png", "pestes.gif", "joe456.jpg".

If the file cannot be located in the mandatory location, a GUI window will popup asking the user to select the image file.


Raster Operational Flow

The O145 subroutine is actually defined in the "145.ngc" file. All of the parameters specified in the "O145 call" command are passed to this G-Code subroutine.

The O145 subroutine then runs the M144 command. In LinuxCNC, M100-M199 commands run external scripts. Unfortunately, only 2 parameters are allowed to be passed to external scripts, P and Q, both of which are floating point numbers. When the M144 command is executed, it is simply passed the "pic" value from the "O145 call" command which represents the number in the filename of the image to be rastered.

The M144 script opens the image file, prompting with a window if it cannot be found in the default location (/home/jamesbond/Desktop/GCode). It also opens a UNIX pipe and waits for additional parameters to be passed in through this pipe. Meanwhile, the O145 subroutine has executed 3 M145 commands. The external M145 command is a Python script which simply takes the P and Q parameters passed to it and sends them to a UNIX pipe. The original M144 script receives the other parameters it requires from the UNIX pipe -- x, y, w, h, xscangap, yscangap and overscan.

Now that the M144 command has all of the parameters it requires, it used the Python Image Library to resize the image to the required width and height as well as convert it to a 2 tone image.

Two things happen in conjunction at this point of the flow -- the O145 subroutine moves to the start location and simply scans back and forth across the raster image location.

The M144 script goes line by line through each pixel in the image. It determines when pixel changes occur and produces output indicating at which x values those changes occur. It produces output consisting of 2 boolean values and a floating point value: raster-fire, raster-rev, and raster-pos.

Raster-fire indicates whether or not the laser should start or stop firing at the given position. Raster-rev indicates whether the particular scan line is moving forward or backward in the x direction. Raster-pos is the x position where a change in firing status occurs.

So, rather than telling the laser to fire a series of pulses, the script determines at which x positions the laser should be turned on, and at which x positions the laser should be turned off. For any lines with no pixels to be burned, it will still include output with the raster-rev set to indicate a blank line.

The output of the M144 script is sent through the LinuxCNC "halstream" command. This ties in with a "stream" object defined in the 2x_Laser.hal file. There is logic within the HAL file which ultimately results in using the 3 raster-* output values of the script to determine when a value which indicates that the laser should be on due to the raster function (laser-raster).

The HAL file also includes overall logic that ties in all of the modes which can be used to fire the 2x_Laser in LinuxCNC. For example, the laser can be explicitly fired/disabled with the M64/M65 commmands, or fired in coordination with a move via the M62/M63 commands. And turning off the spindle by setting a spindle speed of "S0" or explicitly disabling the spindle with "M5" serves as a master control over the laser, preventing it from firing from any purpose.

The HAL file using a LinuxCNC logical construct which ultimately declares boolean logic for when pin 17 on the parallel port triggers the laser:

laser-final <= laser-master & (laser-raster | ((laser-magic-z | laser-dout) & laser-pulsed))


The ((laser-magic-z | laser-dout) & laser-pulsed)) portion is for normal G-code operation using the Z-Position, M3/M5, M62/M63 and M64/M65 commands. For raster engraving, you do not want these functions to interfere and trigger extra laser pulses. To make sure that does not happen, the Z position needs to be kept at 0 or higher and digital output pin #0 needs to be kept off -- via the M63 and M64 commands.

The laser-master is tied to whether or not the spindle is turning. So, if the spindle is not turning, the laser will never fire. Therefore, you can't simply set the spindle speed to 0 in the hopes that "laser-pulsed" will have fire 0 pulses per unit.