python 3d打印机
by Nikolay Khabarov
通过尼古拉·哈巴罗夫(Nikolay Khabarov)
如何使用Python构建自己的CNC控制器和3D打印机 (How you can use Python to build your own CNC controller and 3D printer)
This article discusses the process I used to build the first ever CNC machine controller implementation on pure Python.
本文讨论了我用来在纯Python上构建第一个CNC机床控制器实现的过程。
Computer numerical control (CNC) machine controllers are typically implemented using the C or C++ programming language. They run on OS-less or real-time operating systems with simple microcontrollers.
计算机数控(CNC)机器控制器通常使用C或C ++编程语言来实现。 它们在无操作系统或具有简单微控制器的实时操作系统上运行。
In this article, I’ll describe how to build a CNC controller — a 3D printer in particular — using modern ARM boards (Raspberry Pi) with a modern high level language (Python).
在本文中,我将介绍如何使用具有现代高级语言(Python)的现代ARM板(树莓派)来构建CNC控制器(尤其是3D打印机)。
Such a modern approach opens a wide range of integration options with other cutting edge technologies, solutions, and infrastructures. This makes the whole project developer-friendly.
这种现代方法为与其他前沿技术,解决方案和基础架构的集成提供了广泛的选择。 这使整个项目对开发人员友好。
关于该项目 (About the Project)
Modern ARM boards typically use Linux as a reference operating system. This gives us access to the entire Linux infrastructure with all the Linux software packages. We can host a web server on a board, use Bluetooth connectivity, use OpenCV for image recognition, and build a cluster of boards, among other things.
现代ARM板通常使用Linux作为参考操作系统。 这使我们可以使用所有Linux软件包访问整个Linux基础结构。 我们可以在板上托管Web服务器,使用蓝牙连接,使用OpenCV进行图像识别以及构建板集群等。
These are well-known tasks that can be implemented on ARM boards, and they can be really useful for custom CNC machines. For example, auto-positioning using compuvision can be very handy for some machines.
这些是可以在ARM板上执行的众所周知的任务,它们对于自定义CNC机器非常有用。 例如,对于某些机器,使用compupvision自动定位可能非常方便。
Linux is not a real-time operating system. This means we can’t generate pulses with the required timings to control stepper motors directly from the board pins with running software, even as a kernel module. So, how can we use steppers and high-level Linux features? We can use two chips — one microcontroller with a classic CNC implementation, and an ARM board connected to this microcontroller via UART (universal asynchronous receiver-transmitter).
Linux不是实时操作系统。 这意味着我们无法使用运行软件(甚至是内核模块)从具有所需时序的脉冲直接从板针直接控制步进电机。 那么,我们如何使用步进器和高级Linux功能? 我们可以使用两块芯片-一个具有经典CNC实现的微控制器,以及一个通过UART(通用异步收发器)连接到该微控制器的ARM板。
What if there are no suitable firmware features for this microcontroller? What if we need to control additional axes that are not implemented in the microcontroller? Any modifications to the existing C/C++ firmware will require plenty of development time and efforts. Let’s see if we can make it easier and even save money on microcontrollers by simply removing them.
如果该微控制器没有合适的固件功能怎么办? 如果我们需要控制微控制器中未实现的其他轴怎么办? 对现有C / C ++固件的任何修改都将需要大量的开发时间和精力。 让我们看看是否可以通过简单地删除微控制器来使它变得更容易甚至省钱。
氯化萘 (PyCNC)
PyCNC is a free open-source high-performance G-code interpreter and CNC/3D-printer controller. It can run on various Linux-powered, ARM-based boards, such as Raspberry Pi, Odroid, Beaglebone, and others. This gives you the flexibility to pick any board and use everything that Linux offers. And you can keep the entire G-code runtime on one board without the need for a separate microcontroller for real-time operation.
PyCNC是一个免费的开源高性能G代码解释器和CNC / 3D打印机控制器。 它可以在各种基于Linux的基于ARM的板上运行,例如Raspberry Pi,Odroid,Beaglebone等。 这使您可以灵活地选择任何板并使用Linux提供的所有功能。 而且,您可以将整个G代码运行时保持在一块板上,而无需单独的微控制器进行实时操作。
Choosing Python as the main programming language significantly reduces the code base compared to C/C++ projects. It also reduces the boilerplate and microcontroller-specific code, and makes the project accessible to a wider audience.
与C / C ++项目相比,选择Python作为主要编程语言会大大减少代码库。 它还减少了样板代码和微控制器特定的代码,并使该项目可供更广泛的受众使用。
这个怎么运作 (How it works)
The project uses DMA (Direct Memory Access) on the chip hardware module. It simply copies the GPIO (General Purpose Input Output) states buffer allocated in RAM to the actual GPIO registers. This copying process is synchronized by the system clock and works completely independently from the CPU cores. Thus, a sequence of pulses for the stepper motor’s axis is generated in memory and then the DMA precisely sends them out.
该项目在芯片硬件模块上使用DMA(直接内存访问)。 它仅将RAM中分配的GPIO(通用输入输出)状态缓冲区复制到实际的GPIO寄存器。 该复制过程由系统时钟同步,并且完全独立于CPU内核而工作。 因此,在内存中生成了一个用于步进电机轴的脉冲序列,然后DMA精确地将其发送出去。
Let’s dig deeper into the code to understand the basics and how to access hardware modules from Python.
让我们深入研究代码以了解基础知识以及如何从Python访问硬件模块。
通用输入输出 (GPIO)
A General Purpose Input Output module controls pin states. Each pin can have low or high state. When we program the micro-controller, we usually use SDK (software development kit) defined variables to write to that pin. For example, to enable a high state for pins 1 and 3:
通用输入输出模块控制引脚状态。 每个引脚可以具有低电平或高电平状态。 对微控制器进行编程时,通常使用SDK(软件开发套件)定义的变量来写入该引脚。 例如,要使引脚1和3处于高电平状态:
PORTA = (1 <<