GithubHelp home page GithubHelp logo

built1n / rastercarve Goto Github PK

View Code? Open in Web Editor NEW
74.0 6.0 11.0 1.42 MB

Generate G-code to engrave raster images on CNC machines

Home Page: https://rastercarve.live

License: GNU General Public License v2.0

Python 100.00%
engrave-raster-images cnc toolpath engraving cnc-machine computer-aided-machining shopbot

rastercarve's Introduction

RasterCarve

PyPI version PyPI license PyPI status

This is a little Python script I wrote to generate G-code toolpaths to engrave raster images.

A hosted version of the script is available at https://rastercarve.live (Github). There is also a standalone custom G-code previewer available at https://github.com/built1n/rastercarve-preview.

It takes bitmap images and produces commands (G-code) for a CNC machine to engrave that image onto a piece of material. For the uninitiated, a CNC machine is essentially a robotic carving machine -- think robot drill: you 1) put in a piece of wood/foam/aluminum stock; 2) program the machine; and 3) out comes a finished piece with the right patterns cut into it.

This program comes in during step 2 -- it takes an image and outputs the right sequence of commands for your machine to engrave it. This is not the first program that can do this, but existing solutions are unsuitable due to their high cost.

The program's output has been thoroughly tested on a ShopBot Desktop MAX, which produced the results shown below, and a ShopBot PRTalpha. Various users have reported successful results on X-Carve and Shapeoko machines, among others.

Installation

$ pip install rastercarve

Running straight from the source tree works fine, too:

$ python -m rastercarve -h

Usage

$ rastercarve --width 10 examples/test.png > out.nc
Generating G-code: 100%|██████████████████| 278/278 [00:04<00:00, 57.10 lines/s]
=== Statistics ===
Input resolution: 512x512 px
Output dimensions: 10.00" wide by 10.00" tall = 100.0 in^2
Max line depth: 0.080 in
Max line width: 0.043 in (30.0 deg V-bit)
Line spacing: 0.047 in (110% stepover)
Line angle: 22.5 deg
Number of lines: 277
Input resolution:  51.2 PPI
Output resolution: 100.0 PPI
Scaled image by f=3.91 (200.0 PPI)
Total toolpath length: 2202.6 in
 - Rapids:  34.6 in (8.6 s)
 - Plunges: 29.8 in (59.6 s)
 - Moves:   2138.2 in (1282.9 s)
Feed rate: 100.0 in/min
Plunge rate: 30.0 in/min
Estimated machining time: 1351.2 sec
1 suppressed debug message(s).

This command generates G-code to engrave examples/test.png into an piece of material 10 inches wide. Exactly one of the --width or --height parameters must be specified on the command line; the other will be calculated automatically.

The engraving parameters can be safely left at their defaults, though fine-tuning is possible depending on material and machine characteristics.

The output G-code will be piped to out.nc, which any CNC machine should accept as input.

Machining Process

With the toolpath generated, it is time to run the job. Presumably you know the specifics of your particular machine, so I'll only outline the high-level steps here:

  1. Load the right tool. An engraving bit is best, though ordinary V-bits give acceptable results. Make sure that the tool angle matches that used to generate the toolpath (30 degrees is the default -- change this if needed).

  2. Load the material. MDF seems to work best; plywood and ordinary lumber are too prone to chipping. Plastics have a tendency to melt and stick to the bit.

  3. Zero X and Y axes at the top left corner of the eventual image location. Double check that the bottom right corner is in bounds.

  4. Zero the Z axis to the top surface of the material.

  5. Load and run the toolpath. The engraving will begin in the top right corner and work its way down to the bottom right in a serpentine fashion.

Ramping

Some tools (e.g. ShopBot) have an option to control acceleration ramping speeds. The intricate nature of many raster engraving toolpaths generated with this program tend to trigger unneccessary speed ramping on these machines, leading to very slow cycle times. The solution to this is to set more aggressive ramping values. (ShopBot users can use [VR].)

Advanced

usage: rastercarve [-h] (--width WIDTH | --height HEIGHT) [-f FEED_RATE]
                   [-p PLUNGE_RATE] [--rapid RAPID_RATE] [-z SAFE_Z]
                   [--end-z TRAVERSE_Z] [-t TOOL_ANGLE] [-d MAX_DEPTH]
                   [-a LINE_ANGLE] [-s STEPOVER] [-r LINEAR_RESOLUTION]
                   [--dots] [--no-line-numbers]
                   [--preamble PREAMBLE | --preamble-file PREAMBLE_FILE]
                   [--epilogue EPILOGUE | --epilogue-file EPILOGUE_FILE]
                   [--json JSON_DEST] [--debug] [-q] [--version]
                   filename

Generate G-code to engrave raster images.

positional arguments:
  filename              input image (any OpenCV-supported format)

optional arguments:
  -h, --help            show this help message and exit
  --json JSON_DEST      dump statistics in JSON format
  --debug               print debug messages
  -q                    disable progress and statistics
  --version             show program's version number and exit

output dimensions:
  Exactly one required. Image will be scaled while maintaining aspect ratio.

  --width WIDTH         output width (in)
  --height HEIGHT       output height (in)

machine configuration:
  -f FEED_RATE          engraving feed rate (in/min) (default: 100)
  -p PLUNGE_RATE        engraving plunge rate (in/min) (default: 30)
  --rapid RAPID_RATE    rapid traverse rate (for time estimation only)
                        (default: 240)
  -z SAFE_Z             rapid traverse height (in) (default: 0.1)
  --end-z TRAVERSE_Z    Z height of final traverse (in) (default: 2)
  -t TOOL_ANGLE         included angle of tool (deg) (default: 30)

engraving parameters:
  -d MAX_DEPTH          maximum engraving depth (in) (default: 0.08)
  -a LINE_ANGLE         angle of grooves from horizontal (deg) (default: 22.5)
  -s STEPOVER           stepover percentage (affects spacing between lines)
                        (default: 110)
  -r LINEAR_RESOLUTION  distance between successive G-code points (in)
                        (default: 0.01)
  --dots                engrave using dots instead of lines (experimental)

G-code parameters:
  --no-line-numbers     suppress G-code line numbers (dangerous on ShopBot!)
  --preamble PREAMBLE   override the default G-code preamble; to specify
                        multiple lines on the command line, use $'' strings
                        with \n; each line of the preamble will be prepended
                        with a line number, except when used with --no-line-
                        numbers
  --preamble-file PREAMBLE_FILE
                        like --preamble, but read from a file
  --epilogue EPILOGUE   override the default G-code epilogue; see above notes
                        for --preamble
  --epilogue-file EPILOGUE_FILE
                        like --epilogue, but read from a file

The default feeds have been found to be safe values for medium-density
fiberboard (MDF). Experimenting with the STEPOVER, LINE_ANGLE, and
LINEAR_RESOLUTION may yield improvements in engraving quality at the cost of
increased machining time. On ShopBot machines, the --no-line-numbers flag must
not be used, since the spindle will fail to start and damage the material. Use
this flag with caution on other machines.

G-code Customization

The G-code produced should work out-of-the-box on ShopBot machines. Other machines may need some fine-tuning.

Preamble

The default G-code preamble is

G00 G20
M03

This tells the machine to use inch units (G20) and then starts the spindle (M03).

The default G-code epilogue is

M05

This does nothing but stop the spindle.

The --preamble[-file] and --epilogue[-file] options allow you to specify a custom G-code header or footer to override the default. Note that in writing a custom preamble/epilogue, you should not include line numbers; the program will automatically insert them on each line of the supplied preamble/epilogue.

Metric Units

Passing the --metric flag will replace the default G20 directive with G21 to force metric units. If this is passed, all measurements given will be interpreted as millimeters. E.g., --width 100 will be interpreted as a width of 100mm. (That is to say, the --metric flag is comparatively dumb; no internal unit scaling takes place -- only the preamble is changed.)

Note that the --metric flag cannot be used in conjunction with --preamble[-file]. If a custom preamble is necessary with metric units, just include G21 in the custom preamble.

Pen Plotting

The generated toolpaths produce excellent results when used with pen plotters instead of engraving bits (see above). The machine setup is a little more complicated, though: the Z height must be set to half the maximum engraving depth above the material for the black and white regions to be drawn correctly.

Related

Vectric PhotoVCarve - a similar commercial solution. This program is not derived from PhotoVCarve.

My blog post - writeup on the development process.

rastercarve's People

Contributors

built1n avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

rastercarve's Issues

option to set default preamble in gcode

It's a minor issue, but it would be nice to be able to customize the gcode preamble.
I use linuxcnc, and it doesn't like the command combo on N1, so i have to manually edit it for every file.
Also have to add a S value to the M3, otherwise the spindle doesn't spin up
It complains about no 'program end' as well, so i have to add a M2 or M30 at the end of the file as well.

Again, it's really minor, as it just requires some file editing. But would be a nice to be able to set some defaults for the gcode.
thanks.

Width/Height no larger than 100. Can this limit be raised?

Hi,

Nice job on this little gem. My only issue is that I cannot select a size any bigger than 100 and seen as my CNC is setup as metric changing to metric via M21 means the carving can only be 100mm. I am probably missing something, but if not can this size limit be increased?

cheers
100 size limit

generated gcode always have negative Y

I tried the script to convert an image to gcode, it seems all the Y values are negative, is this expected?

N1624968 G1 F100 X34.799842 Y-21.827097 Z-0.002812
N1624969 G1 F100 X34.809081 Y-21.823270 Z-0.002812
N1624970 G1 F100 X34.818320 Y-21.819443 Z-0.002812
N1624971 G1 F100 X34.827558 Y-21.815616 Z-0.002812
N1624972 G1 F100 X34.836797 Y-21.811790 Z-0.002812
N1624973 G1 F100 X34.846036 Y-21.807963 Z-0.002812
N1624974 G1 F100 X34.855275 Y-21.804136 Z-0.002812
N1624975 G1 F100 X34.864514 Y-21.800309 Z-0.002812
N1624976 G1 F100 X34.873752 Y-21.796482 Z-0.002812
N1624977 G1 F100 X34.882991 Y-21.792655 Z-0.002812
N1624978 G1 F100 X34.892230 Y-21.788829 Z-0.002812

error in cvtColor() call

I am getting an error running this locally with an image that work when I run it on the hosted live version, so pretty sure the image is good, Here is the error. Any ideas on why this assertion is failing? Something likely wrong in my environment (windows 10, python3)?

Traceback (most recent call last):
File "C:\Users\Mikej\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\Scripts\rastercarve-script.py", line 33, in
sys.exit(load_entry_point('rastercarve==1.0.8', 'console_scripts', 'rastercarve')())
File "C:\Users\Mikej\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\rastercarve_main_.py", line 458, in main
doEngrave()
File "C:\Users\Mikej\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\rastercarve_main_.py", line 315, in doEngrave
img = ~cv2.cvtColor(cv2.imread(glob_args.filename), cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-q3d_8t8e\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

PS C:\Users\Mikej\Downloads\rastercarve-master\rastercarve-master> rastercarve --width 5 -d .05 -s 100 --metric --no-line-numbers examples/105-0521_IMG.JPG > out.nc
Traceback (most recent call last):
File "C:\Users\Mikej\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\Scripts\rastercarve-script.py", line 33, in
sys.exit(load_entry_point('rastercarve==1.0.8', 'console_scripts', 'rastercarve')())
File "C:\Users\Mikej\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\rastercarve_main_.py", line 458, in main
doEngrave()
File "C:\Users\Mikej\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\rastercarve_main_.py", line 315, in doEngrave
img = ~cv2.cvtColor(cv2.imread(glob_args.filename), cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-q3d_8t8e\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

Feature request: Support for metric units

Currently all output is labelled as inches. If the gcode output is run on a metric machine the result will be scaled down since G1 will be assuming metric move instructions.

After a quick look at the code I believe simply changing the labels is all that would be needed. It would make most sense to have an option to optionally work in millimetres.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.