{"id":14978095,"url":"https://github.com/nikolay-kha/pycnc","last_synced_at":"2025-04-12T19:45:45.232Z","repository":{"id":54770000,"uuid":"90806072","full_name":"Nikolay-Kha/PyCNC","owner":"Nikolay-Kha","description":"Python CNC machine controller for Raspberry Pi and other ARM Linux boards","archived":false,"fork":false,"pushed_at":"2022-07-02T21:54:10.000Z","size":168,"stargazers_count":616,"open_issues_count":54,"forks_count":188,"subscribers_count":72,"default_branch":"master","last_synced_at":"2025-04-04T00:24:25.894Z","etag":null,"topics":["3d-printer","3d-printing","cnc","cnc-controller","cnc-machine","cnc-milling-controller","gcode","pycnc","raspberry-pi","raspberry-pi-3"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Nikolay-Kha.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-10T01:05:04.000Z","updated_at":"2025-04-04T00:17:10.000Z","dependencies_parsed_at":"2022-08-14T02:20:13.741Z","dependency_job_id":null,"html_url":"https://github.com/Nikolay-Kha/PyCNC","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nikolay-Kha%2FPyCNC","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nikolay-Kha%2FPyCNC/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nikolay-Kha%2FPyCNC/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nikolay-Kha%2FPyCNC/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Nikolay-Kha","download_url":"https://codeload.github.com/Nikolay-Kha/PyCNC/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248625490,"owners_count":21135513,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["3d-printer","3d-printing","cnc","cnc-controller","cnc-machine","cnc-milling-controller","gcode","pycnc","raspberry-pi","raspberry-pi-3"],"created_at":"2024-09-24T13:56:51.469Z","updated_at":"2025-04-12T19:45:45.193Z","avatar_url":"https://github.com/Nikolay-Kha.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/Nikolay-Kha/PyCNC.svg?branch=master)](https://travis-ci.org/Nikolay-Kha/PyCNC)\n\n\n![](https://cloud.githubusercontent.com/assets/8740775/26766365/14796b54-4999-11e7-8ca2-9428a45878ab.png)  \n\nPyCNC is a free open-source high-performance G-code interpreter and\nCNC/3D-printer controller. It can run on a variety of Linux-powered ARM-based\nboards, such as Raspberry Pi, Odroid, Beaglebone and others. This gives you a\nflexibility to pick a board you are most familiar with, and use everything\nLinux has to offer, while keeping all your G-code runtime on the same board\nwithout a need to have a separate microcontroller for real-time operation.\nOur choice of Python as main programming language significantly reduces code\nbase compared to C/C++ projects, reduces boilerplate and microcontroller-specific\ncode, and makes the project accessible to a broader audience to tinker with.\n\n# Realtime Motor Control in Linux?\nTypically there is no way to control stepper motors from Linux runtime\nenvironment due to the lack of real time GPIO control. Even kernel based\nmodules can not guarantee precise control of pulses for steppers.\nHowever, we can use a separate hardware module, DMA (Direct Memory Access)\nwhich provides high precision for GPIO outputs. This module can copy bytes which\nrepresent GPIO states from RAM buffer directly to GPIO with some clock based\non main chip internal oscillator without using CPU's cores. Using such approach\nthis project generates impulses for moving stepper motors and that is very\nprecise way regardless CPU load and OS time jitter.  \nThis approach also allows to use Python language for this project. Typically,\nPython is not good choice for real time application, but since project just\nneeds to set up DMA buffers and hardware will do the rest, Python become the\nperfect choice for easy development of this project.\n\nVideo demo - [YouTube video](https://youtu.be/41wdmmztTNA)  \nAnd the original video when PyCNC was just a prototype [YouTube video](https://youtu.be/vcedo59raS4)\n\n# Current gcode and features support\n* Commands G0, G1, G2, G3, G4, G17, G18, G19, G20, G21, G28, G53, G90, G91, G92,\nM2, M3, M5, M30, M84, M104, M105, M106, M107, M109, M114, M140, M190 are\nsupported. Commands can be easily added, see [gmachine.py](./cnc/gmachine.py)\nfile.\n* Four axis are supported - X, Y, Z, E.\n* Circular interpolation for XY, ZX, YZ planes is supported.\n* Spindle with rpm control is supported.\n* Extruder and bed heaters are supported.\n* Hardware watchdog.\n\n# Watchdog\nPyCNC uses one of DMA channels as hardware watchdog for safety purpose. If\nboard, OS or PyCNC hangs this watchdog should disable all GPIO pins(by\nswitching them into input state, for RPi this would be GPIO0-29) in 15 seconds.\nSince there is a high current and dangerous devices like heated bed, extruder\nheater, this feature should prevent uncontrollable overheating. But don't count\non such software features too much, they can hang too or output MOSFET become\nshorted, use hardware protection like thermal cutoff switches in your machines.\n\n# Hardware\nCurrently, this project supports Raspberry Pi 1-3. Developed and tested with\nRPI3. And there is a way to add new boards. See [hal.py](./cnc/hal.py) file.  \n_Note: Current Raspberry Pi implementation uses the same resources as on board\n3.5 mm jack(PWM module), so do not use it. HDMI audio works._\n\n# Config\nAll configs are stored in [config.py](./cnc/config.py) and contain hardware\nproperties, limitations and pin names for hardware control.  \nRaspberry Pi implementation should be connected to A4988, DRV8825 or any other\nstepper motor drivers with DIR and STEP pin inputs.\nDefault config is created for Raspberry Pi 2-3 and this wiring config:\n\n| Circuit name    | RPi pin name | RAMPSv1.4 board pin name |  Note           |\n|-----------------|--------------|--------------------------|-----------------|\n| X step          | GPIO21       | A0                       |                 |\n| X dir           | GPIO20       | A1                       |                 |\n| steppers enable | GPIO26       | A2, A8, D24, D30, D38    | all steppers    |\n| Y step          | GPIO16       | A6                       |                 |\n| Y dir           | GPIO19       | A7                       |                 |\n| Z dir           | GPIO13       | D48                      |                 |\n| Z step          | GPIO12       | D46                      |                 |\n| E1 step         | GPIO6        | D36                      | reserve         |\n| E1 dir          | GPIO5        | D34                      | reserve         |\n| E0 dir          | GPIO7        | D28                      |                 |\n| E0 step         | GPIO8        | D26                      |                 |\n| Z max           | GPIO11       | D19                      |                 |\n| Z min           | GPIO25       | D18                      |                 |\n| Y max           | GPIO9        | D15                      |                 |\n| Y min           | GPIO10       | D14                      |                 |\n| X max           | GPIO24       | D2                       |                 |\n| X min           | GPIO23       | D3                       |                 |\n| heater bed      | GPIO22       | D8                       |                 |\n| heater 2        | GPIO27       | D9                       | uses for fan    |\n| heater 1        | GPIO18       | D10                      |                 |\n| ser 1           | GPIO17       | D11                      | reserve         |\n| ser 2           | GPIO15       | D6                       | reserve         |\n| ser 3           | GPIO4        | D5                       | reserve         |\n| ser 4           | GPIO14       | D4                       | reserve         |\n| I2C SCL         | GPIO3        | -                        | to ads111x      |\n| I2C SDA         | GPIO2        | -                        | to ads111x      |\n| ads1115 ch0     | -            | A15                      | heater 2 - nc   |\n| ads1115 ch1     | -            | A14                      | bed sensor      |\n| ads1115 ch2     | -            | A13                      | extruder sensor |\n| ads1115 ch3     | -            | -                        | not connected   |\n\nSo having Raspberry Pi connected this way, there is no need to configure\npin map for project. [RAMPS v1.4](http://reprap.org/wiki/RAMPS_1.4) board can\nbe used for this purpose. Full reference circuit diagram and photos of\nassembled controller(click to enlarge):  \n[![circuit small](https://user-images.githubusercontent.com/8740775/28233810-40773186-6902-11e7-8496-5750babfcf44.jpg)](https://user-images.githubusercontent.com/8740775/28233650-d64060e0-6900-11e7-8605-6475384fd2f7.png)\n[![sample front small](https://user-images.githubusercontent.com/8740775/28233812-407820c8-6902-11e7-8de1-35c03509c0e5.jpg)](https://user-images.githubusercontent.com/8740775/28233649-d6402800-6900-11e7-8dca-cd35c8292e0c.jpg)\n[![sample back small](https://user-images.githubusercontent.com/8740775/28233879-db96ea1c-6902-11e7-9298-11150476084b.jpg)](https://user-images.githubusercontent.com/8740775/28233648-d63fa0c4-6900-11e7-8fab-2055e035a6cb.jpg)\n[![sample mounted small](https://user-images.githubusercontent.com/8740775/28233811-40777e8e-6902-11e7-8899-5991860d182c.jpg)](https://user-images.githubusercontent.com/8740775/28233652-d65f82ea-6900-11e7-9e80-d8b0c9238f95.jpg)\n\n# Usage\nJust clone this repo and run `./pycnc` from repo root. It will start in\ninteractive terminal mode where gcode commands can be entered manually.  \nTo run file with gcode commands, just run `./pycnc filename`.  \nOptionally, `pycnc` can be installed. Run\n```bash\nsudo pip install .\n```\nin repo root directory to install it. After than, `pycnc` command will be added\nto system path. To remove installation, just run:\n```bash\nsudo pip remove pycnc\n```\n\n# Performance notice\nPure Python interpreter would not provide great performance for high speed\nmachines. Overspeeding setting causes motors mispulses and probably lose of\ntrajectory. According to my tests, Raspberry Pi 2 can handle axises with 400\n pulses on mm with top velocity ~800 mm per min. There is always way out! :)\nUse JIT Python implementation like PyPy. RPi2 can handle up to 18000 mm per\nminute on the machine with 80 steps per millimeter motors with PyPy.  \n_Note: Raspbian has outdated PyPy version in repositories(v4.0). Moreover v4.0\nhas issue with `mmap` module implementation. Use PyPy v5.0+, download it for\nyour OS from [here](https://pypy.org/download.html)._  \nPyPy installation:\n```bash\nwget wget https://bitbucket.org/pypy/pypy/downloads/pypy2-v5.7.1-linux-armhf-raspbian.tar.bz2\nsudo mkdir /opt/pypy\nsudo tar xvf pypy2-v5.7.1-linux-armhf-raspbian.tar.bz2 --directory /opt/pypy/ --strip-components=1\nsudo ln -s /opt/pypy/bin/pypy /usr/local/bin/pypy\n```\n\n# Project architecture\n![](https://user-images.githubusercontent.com/8740775/27770129-c8c3592c-5f41-11e7-8a9c-254d5a88ed77.png)\n\n# Dependencies\nNothing for runtime. Just pure Python code.\nFor uploading to PyPi there is a need in `pandoc`:\n```bash\nsudo dnf install pandoc\nsudo pip install pypandoc\n```\n\n# GCode simulation\nJust a link, mostly for myself :), to a nice web software for gcode files\nemulation (very helpful for manual creating of gcode files):\n[https://nraynaud.github.io/webgcode/](https://nraynaud.github.io/webgcode/)\n\n# License\nsee [LICENSE](./LICENSE) file.\n\n# Author\nNikolay Khabarov\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikolay-kha%2Fpycnc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnikolay-kha%2Fpycnc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikolay-kha%2Fpycnc/lists"}