版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、ython3d_PythonCNC3D打印机python 3d打印机by Nikolay Khabarov通过尼古拉哈巴罗夫(Nikolay Khabarov)如何使 Python构建的 CNC控制器和 打印机 (How you can use Python to buildyour 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.本讨论了
2、我来在纯Python上构建第个CNC机床控制器实现的过程。Computer numerical control (CNC) machine controllers are typically implemented using the C or C+ programminglanguage. They run on OS-less or real-time operating systems with simple microcontrollers.计算机数控(CNC)机器控制器通常使C或C +编程语来实现。 它们在操作系统或具有简单微控制器的实时操作系统上运。In this article,
3、 Ill 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 te
4、chnologies, solutions, andinfrastructures. This makes the whole project developer-friendly.这种现代法为与其他前沿技术,解决案和基础架构的集成提供了泛的选择。 这使整个项对开发员友好。 Modern ARM boards typically use Linux as a reference operating system. This gives us access to the entire Linuxinfrastructure with all the Linux software packages
5、. Wecan host a web server on a board, use Bluetooth connectivity, usefor image recognition, and build a cluster of boards, among other things.现代ARM板通常使Linux作为参考操作系统。 这使我们可以使所有Linux软件包访问整个Linux基础结构。 我们可以在板上托管Web服务器,使蓝连接,使进图像识别以及构建板集群等。These are well-known tasks that can be implemented on ARM boards,
6、and they can be really useful for custom CNCmachines. 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 cant generate pulses with the required
7、 timings to control steppermotors directly from the board pins with running software, even as a kernel module. So, how can we use steppers and high-level Linux features? Wecan use two chips one microcontroller with a classic CNC implementation, and an ARM boardconnected to this microcontroller via U
8、ART (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 a
9、xes that arenot implemented in the microcontroller? Any modifications to the existing C/C+ firmware will require plenty ofdevelopment time and efforts. Lets see if we can make it easier and even save money on microcontrollers by simplyremoving them.如果该微控制器没有合适的固件功能怎么办? 如果我们需要控制微控制器中未实现的其他轴怎么办? 对现有C
10、/ C +固件的任何修改都将需要量的开发时间和精。 让我们看看是否可以通过简单地删除微控制器来使它变得更容易甚省钱。 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 anyb
11、oard and use everything that Linux offers. And you can keep the entire G-code runtime on one board without the need fora separate microcontroller for real-time operation.是个免费的开源性能G代码解释器和CNC / 3D打印机控制器。 它可以在各种基于Linux的基于ARM的板上运,例如RaspberryPi,Odroid,Beaglebone等。 这使您可以灵活地选择任何板并使Linux提供的所有功能。 且,您可以将整个G代码
12、运时保持在块板上,需单独的微控制器进实时操作。Choosing Python as the main programming language significantly reduces the code base compared to C/C+ projects. Italso reduces the boilerplate and microcontroller-specific code, and makes the project accessible to a wider audience.与C / C +项相,选择Python作为主要编程语会减少代码库。 它还减少了样板代码和微控
13、制器特定的代码,并使该项可供更泛的受众使。 The project uses DMA (Direct Memory Access) on the chip hardware module. It simply copies the GPIO (General PurposeInput Output) states buffer allocated in RAM to the actual GPIO registers. This copying process is synchronized by thesystem clock and works completely independent
14、ly from the CPU cores. Thus, a sequence of pulses for the steppermotors axis is generated in memory and then the DMA precisely sends them out.该项在芯硬件模块上使DMA(直接内存访问)。 它仅将RAM中分配的GPIO(通输输出)状态缓冲区复制到实际的GPIO寄存器。 该复制过程由系统时钟同步,并且完全独于CPU内核作。 因此,在内存中成了个于步进电机轴的脉冲序列,然后DMA精确地将其发送出去。Lets dig deeper into the code t
15、o understand the basics and how to access hardware modules from Python.让我们深研究代码以了解基础知识以及如何从Python访问硬件模块。 A General Purpose Input Output module controls pin states. Each pin can have low or high state. When we program themicro-controller, we usually use SDK (software development kit) defined variable
16、s to write to that pin. For example, toenable a high state for pins 1 and 3:通输输出模块控制引脚状态。 每个引脚可以具有低电平或电平状态。 对微控制器进编程时,通常使SDK(软件开发套件)定义的变量来写该引脚。 例如,要使引脚1和3处于电平状态:PORTA = (1 PIN1) | (1 PIN3)If you look in the SDK,you will find the declaration of this variable, and it will look similar to:如果查看SDK,则会发现此
17、变量的声明,它的外观类似于:#define PORTA (*(volatile uint8_t *)(0 x12345678)Its just a pointer. It doesnt point to the location in RAM,but to the address of the physical processor. The actual GPIOmodule is located at this address.这只是个指针。 它不是指向RAM中的位置,是指向物理处理器的地址。 实际的GPIO模块位于此地址。To manage pins, we can write and r
18、ead data. Raspberry Pis ARM processor is not an exception, and it has the samemodule. To control pins, we can write/read data. Wecan find the addresses and data structures in the for processorperipherals.要管理引脚,我们可以写和读取数据。 Raspberry Pi的ARM处理器也不例外,它具有相同的模块。 为了控制引脚,我们可以写/读取数据。 我们可以在处理器外围设备的找到地址和数据结构。Wh
19、en we run a process in the users runtime, the process starts in the virtual address space. The actual peripheral isaccessible directly. But we can still access real physical addresses with/dev/mem device.当我们在户的运时中运个进程时,该进程在虚拟地址空间中启动。 实际的外围设备可直接访问。 但是我们仍然可以使设备访问真实的物理地址。Here is some simple code in Pyt
20、hon that controls a pins state using this approach:这是Python中的些简单代码,可以使这种法控制引脚的状态:Lets break it down line by line:让我们逐将其分解:Lines 16:headers, imports.第16 :标头,导。Line 7:open/dev/memdevice access to the physical address.第7 :打开设备访问物理地址。Line 8:we use the system call to map a file (though in our case, this
21、file represents physical memory) into the processsvirtual memory. Wespecify the length and offset of the map area. For the length, we take the page size. And the offset is0 x3F200000.第8 :我们使系统调将件(尽管在本例中,该件代表物理内存)映射到进程的虚拟内存中。 我们指定地图区域的长度和偏移量。对于长度,我们采页。 偏移量为0 x3F200000 。The documentation says that the
22、 bus 0 x7E200000 contains GPIO registers, and we need to specify the physicaladdress. The documentation says (page 6,paragraph 1.2.3) that the0 x7E000000 bus address is mapped to the0 x20000000physical address, but this documentation is for Raspberry 1.Please note that all module bus addresses are t
23、he same forRaspberry Pi 13,but this map was changed 0 x3F000000 for RPi 2 and 3.So, the address here is0 x3F200000. ForRaspberry Pi 1,change it to0 x20200000.该档说总线地址0 x7E200000包含GPIO寄存器,我们需要指定物理地址。 档说(第6页,第1.2.3节),0 x7E000000总线地址已映射到0 x20000000物理地址,但是本档适于Raspberry1。请注意,Raspberry Pi 1-3的所有模块总线地址均相同,但
24、是对于RPi2和3,此映射已更改为0 x3F000000 。因此,此处的地址为0 x3F200000 。 对于Raspberry Pi 1,将其更改为0 x20200000 。After this, we can write to our processs virtual memory, but it actually writes to the GPIO module.此后,我们可以写进程的虚拟内存,但实际上是写GPIO模块。Line 9:close the file handle, since we dont need to store it.第9 :关闭件句柄,因为我们不需要存储它。Li
25、nes 1114:we read and write to our map with the0 x08 offset. According to the documentation, it is the GPFSEL2 GPIOFunction Select 2 register. And this register controls pin functions.第1114 :我们以0 x08偏移量读写地图。 根据档,它是GPFSEL2 GPIO功能选择2寄存器。 该寄存器控制引脚功能。Weset (clear all, then set with the OR operator) 3 bit
26、s with the 3rd bit set to001. This value means that the pin works as anoutput. There are many pins and possible modes for them. This is why the register for modes is divided into severalregisters, where each contains the modes for 10 pins.我们设置(清除所有,然后OR运算符设置)3位,并将第3位设置为001 。 该值表该引脚作输出。 有许多引脚和可能的模式。
27、这就是为什么模式寄存器分为个寄存器,每个寄存器包含10个引脚的模式的原因。Lines 16 and 22:set up the Ctrl+C interruption handler.第16和22 :设置“ Ctrl + C”中断处理程序。Line 17:infinite loop.第17 :限循环。Line 18:set the pin to the high state by writing to the GPSET0 register.第18 :通过写GPSET0寄存器将引脚设置为电平状态。Please note, Raspberry Pi doesnt have registers l
28、ike PORTA (AVR microcontrollers) have. Wecant write the whole GPIOstate of all pins. There are just set and clear registers which are used to set and clear specified with bitwise mask pins.请注意,Raspberry Pi没有像PORTA(AVR微控制器)那样的寄存器。 我们不能写所有引脚的整个GPIO状态。 仅有设置和清除寄存器,于设置和清除按位屏蔽引脚指定的寄存器。Lines 19 and 21:dela
29、y第19和21 :延迟Line 20:set pin to low state with the GPCLR0 register.第20 :使GPCLR0寄存器将引脚设置为低电平状态。Lines 25 and 26:switch pin to default, input state. Close the memory map.第25和26 :将引脚切换为默认输状态。 关闭内存映射。This code should be run with superuser privileges. Name the filegpio.py and run it withsudo python gpio.py.
30、 If you have aLED connected to pin 21,it will blink.该代码应以超级户权限运。 将件命名为gpio.py并使sudo python gpio.py运它。 如果您有个LED连接到引脚21,它将闪烁。Direct Memory Access is a special module that is designed to copy memory blocks from one area to another. Wewill copydata from the memory buffer to the GPIO module. First of all
31、, we need a solid area in physical RAM that will be copied.直接内存访问是个特殊的模块,旨在将内存块从个区域复制到另个区域。 我们将数据从内存缓冲区复制到GPIO模块。 先,我们需要将要复制的物理RAM中的固定区域。There are few possible solutions:可能的解决案很少:1. Wecan create a simple kernel driver that will allocate, lock, and report to us the address of this memory.我们可以创建个简单的内核
32、驱动程序,它将分配,锁定并向我们报告该内存的地址。2. In some implementations, virtual memory is allocated and uses/proc/self/pagemap to convert the address to thephysical one. I wouldnt recommend this approach, especially when we need to allocate big area. Any virtually allocatedmemory (even locked, see the ) can be moved t
33、o the physical area.在某些实现中,虚拟内存被分配并使/proc/self/pagemap将地址转换为物理地址。 我不推荐这种法,特别是当我们需要分配积的时候。 任何虚拟分配的内存(即使已锁定,也请参阅 )都可以移物理区域。3. All Raspberry Pi have a/dev/vcio device, which is a part of the graphic driver and can allocate physical memory for us.An official shows how to do it. And we can use it instead
34、 of creating our own.所有的Raspberry /dev/vcio设备,该设备是图形驱动程序的部分,可以为我们分配物理内存。 个官显了如何执此操作。 且我们可以使它不是创建我们的。The DMA module itself is just a set of registers that are located somewhere at a physical address. Wecan control thismodule via these registers. Basically, there are source, destination, and control r
35、egisters. Lets check some simple codethat shows how to use the DMA modules to manage the GPIO.DMA模块本就是组位于物理地址某处的寄存器。 我们可以通过这些寄存器控制此模块。 基本上,有源,标和控制寄存器。 让我们检查些简单的代码,这些代码显了如何使DMA模块来管理GPIO。Since additional code is required to allocate physical memory with/dev/vcio, we will use a with an existing CMAPhys
36、icalMemory class implementation. Wewill also use the PhysicalMemory class, which performs the trick with memapfrom the previous sample.由于额外的代码需要分配物理内存/dev/vcio ,我们将使个与现有的CMA PhysicalMemory类实现。 我们还将使PhysicalMemory类,该类使上个例中的memap执技巧。Lets break it down line by line:让我们逐将其分解:Lines 13:headers, imports.第1
37、3 :标头,导。Lines 56:constants with the channelDMA number and GPIO pin that we will use.第5-6 :常量,我们将使通道DMA编号和GPIO引脚。Lines 815:initialize the specified GPIO pin as an output, and light it up for a half second for visual control. In fact, itsthe same thing we did in the previous example, written in a more
38、 pythonic way.第8-15 :将指定的GPIO引脚初始化为输出,并点亮半秒以进可视控制。 实际上,这与我们在前的例中所做的相同,只是使了更多的Python语。Line 17:allocates64 bytes in physical memory.第17 :在物理内存中分配64个字节。Line 18:creates special structures control blocks for the DMA module. The following lines break the structure of thisblock. Each field has a length of3
39、2 bit.第18 :创建特殊结构-DMA模块的控制块。 以下中断了此块的结构。 每个字段的长度为32位。Line 19:transfers information flags. You can find a full description of each flag on page 50 of the official documentation.第19 :传输信息标志。 您可以在官档的第50页上找到每个标志的完整说明。Line 20:source address. This address must be a bus address, so we callget_bus_address()
40、. The DMA control block must bealigned by 32 bytes, but the size of this block 24 bytes. So we have 8 bytes, which we use as storage.第20 :源地址。 该地址必须是总线地址,因此我们调get_bus_address() 。 DMA控制块必须对齐32个字节,但是此块的为24个字节。 因此,我们有8个字节,作存储空间。Line 21:destination address. In our case, its the address of the SET regist
41、er of the GPIO module.第21 :标地址。 在我们的例中,它是GPIO模块的SET寄存器的地址。Line 22:transmission length 4 bytes.第22 :传输长度4个字节。Line 23:stride. Wedo not use this feature, set0.第23 :步向前。 我们不使此功能,设置为0。Line 24:address of the next control block, in our case, next 32 bytes.第24 :下个控制块的地址,在本例中为下个32字节。Line 25:padding. But sinc
42、e we used this address as a data source, put a bit, which should trigger GPIO.第25 :填充。 但是,由于我们将此地址作数据源,因此请放置点,这将触发GPIO。Line 26:padding.第26 :填充。Lines 2837:fill in the second DMA control block. The difference is that we write to CLEAR GPIO register and set our firstblock as a next control block to loo
43、p the transmission.第2837 :填写第个DMA控制块。 区别在于我们写CLEAR GPIO寄存器,并将我们的第个块设置为循环传输的下个控制块。Lines 3839:write control blocks to physical memory.第3839 :将控制块写物理内存。Line 41:get the DMA module object with the selected channel.第41 :获取具有所选通道的DMA模块对象。Lines 4243:reset the DMA module.第4243 :重置DMA模块。Line 44:specify the ad
44、dress of the first block.第44 :指定第个块的地址。Line 45:run the DMA module.第45 :运DMA模块。Lines 4952:clean up. Stop the DMA module and switch the GPIO pin to the default state.第4952 :清理。 停DMA模块,并将GPIO引脚切换为默认状态。Lets connect the oscilloscope to the specified pin and run this application (do not forget about sudo
45、privileges). Wewillobserve 1.5 MHzsquare pulses:让我们将波器连接到指定的引脚并运该应程序(不要忘记sudo特权)。 我们将观察到约1.5 MHz的波: There are several things that you should take into consideration before building a real CNC machine.在构建真正的CNC机床之前,您需要考虑件事。First, the size of the DMA buffer can be hundreds of megabytes.先,DMA缓冲区的可以为数百兆
46、字节。Second, the DMA module is designed for a fast data copying. If several DMA channels are working, we can go beyond thememory bandwidth, and buffer will be copied with delays that can cause jitters in the output pulses. So, its better to havesome synchronization mechanism.其次,DMA模块设计于快速数据复制。 如果个DMA通
47、道正在作,我们将超出内存带宽,缓冲区将被复制,延迟会导致输出脉冲抖动。 因此,最好具有些同步机制。To overcome this, I created a special design for control blocks:为了克服这个问题,我为控制块创建了个特殊的设计:The oscillogram at the top of the image shows the desired GPIO states. The blocks below represent the DMA control blocksthat generate this waveform. “Delay 1” spec
48、ifies the pulse length, and “Delay 2” is the pause length between pulses.With this approach, the buffer size depends only on the number of pulses.图像顶部的波形图显了所需的GPIO状态。 下的块表产此波形的DMA控制块。 “延迟1”指定脉冲长度,“延迟2”是脉冲之间的暂停长度。 使这种法,缓冲区仅取决于脉冲数。For example, for a machine with 200mm travel length and 400 pulses per
49、mm,each pulse would take 128 bytes (4 controlblocks per 32 bytes), and the total size will be 9.8MB. Wewould have more than one axis, but most of the pulses wouldoccur at the same time. And it would be dozens of megabytes, not hundreds.例如,对于程长度为200mm的机器和每毫400个脉冲的机器,每个脉冲将占128字节(每32字节4个控制块),总约为9.8MB。我
50、们将有多个轴,但多数脉冲将同时发。 它将是兆字节,不是数百兆字节。I solved the second challenge, related to synchronization, by introducing temporary delays through the control blocks. TheDMA module has a special feature: it can wait for a special ready signal from the module where it writes data. The mostsuitable module for us is
51、the PWM (pulse width modulation) module, which will also help us with synchronization.我通过在控制块中引了临时延迟来解决了与同步有关的第个挑战。 DMA模块具有个特殊功能:它可以等待来其写数据的模块的特殊就绪信号。 最适合我们的模块是PWM(脉冲宽度调制)模块,它也将帮助我们进同步。The PWM module can serialize the data and send it with fixed speed. In this mode, it generates a ready signal for t
52、he FIFO(first in, first out) buffer of the PWM module. So, lets write data to the PWM module and use it only for synchronization.PWM模块可以序列化数据并以固定速度发送。 在这种模式下,它将为PWM模块的FIFO(先进先出)缓冲器成就绪信号。 因此,让我们将数据写PWM模块,并将其仅于同步。Basically, we would need to enable a special flag in the perceptual mapping of the transf
53、er information flag, and then run thePWM module with the desired frequency. The implementation is quite long you can it yourself.基本上,我们需要在传输信息标志的感知映射中启特殊标志,然后以所需的频率运PWM模块。 实现时间很长-您可以 。Instead, lets create some simple code that can use the existing module to generate precise pulses.相反,让我们创建些简单的代码,可以使
54、现有模块成精确的脉冲。import rpgpioPIN=21PINMASK = 1 PINPULSE_LENGTH_US = 1000PULSE_DELAY_US = 1000DELAY_US = 2000 g = rpgpio.GPIO()g.init(PIN, rpgpio.GPIO.MODE_The code is pretty simple, and there is no need to break it down. If you run this code and connect an oscilloscope, you willsee:代码很简单,不需要分解。 如果运此代码并连接
55、波器,则会看到:And now we can create real G-code interpreter and control stepper motors. But wait! It is already implemented . You can usethis project, as its distributed under the MIT license.现在,我们可以创建真正的G代码解释器并控制步进电机。 可是等等! 它已经实现。 您可以使此项,因为它是根据MIT许可证分发的。 The Python project can be adopted for your purpose
56、s. But in order to inspire you, I will describe the original hardwareimplementation of this project a 3D printer. It basically contains the following components:可以根据需要使Python项。 但是为了激发您的灵感,我将介绍该项的原始硬件实现-3D打印机。 它基本上包含以下组件:1. Raspberry Pi 3树莓派32.3. 4 A4988 or DRV8825 module4个A4988或DRV8825模块4. RepRap Pr
57、usa i3 frame with equipment (end-stops, motors, heaters, and sensors)RepRap Prusa i3框架,带设备(终端挡块,电机,加热器和传感器)5. 12V 15A power supply unit12V 15A电源单元6. LM2596S DC-DC step down converter moduleLM2596S DC-DC降压转换器模块7. MAX4420 chipMAX4420芯8. ADS1115 analog to digital converter moduleADS1115模数转换器模块9. UDMA13
58、3 IDE ribbon cableUDMA133 IDE带状电缆10. Acrylic glass亚克玻璃11. PCB standsPCB架12. Set of connectors with 2.54mm step组2.54mm台阶的连接器The 40-pin IDE ribbon cable is suitable for the Raspberry Pi 40 pins connector, but the opposite end requires some work.Cut off the existing connector from the opposite end and
59、crimp connectors to the cable wires.40针IDE带状电缆适于Raspberry Pi 40针连接器,但另端需要做些作。 从相反的端切断现有的连接器,然后将连接器压接到电缆上。The RAMPSv1.4 board was originally designed for connection to the Mega connector, so there is no easy way to connectthis board to the Raspberry Pi. The following method allows you to simplify the
60、 boards connection. You will need to connectless than 40 wires.RAMPSv1.4开发板最初是为连接 Mega连接器设计的,因此没有简便的法将该开发板连接Raspberry Pi。 以下法可以简化电路板的连接。 您将需要连接少于40根电线。I hope this connection diagram is fairly simple and easily duplicated. Its better to connect some pins (2nd extruder,servos) for future use, even if
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 土地流转合同范本及操作指南
- 新版职工劳动合同签订流程
- 企业捐赠合同模板及注意事项
- 零售行业客服话术与客户维护技巧
- 电力公司宣教活动策划方案
- 社区环保活动策划方案(3篇)
- dems弱电施工方案(3篇)
- 国外春节活动策划方案(3篇)
- 沥青铺垫施工方案(3篇)
- 2024年企业市场营销年度计划
- 《合理利用网络》(优质课件)
- 中深度镇静红外线全身热疗方法课件
- 第四单元地理信息技术的应用课件 【高效课堂+精研精讲】高中地理鲁教版(2019)必修第一册
- 鲁科版高中化学必修一教案全册
- 管理养老机构 养老机构的服务提供与管理
- 提高隧道初支平整度合格率
- 2022年环保标记试题库(含答案)
- 2023年版测量结果的计量溯源性要求
- 建筑能耗与碳排放研究报告
- GB 29415-2013耐火电缆槽盒
- 中国古代经济试题
评论
0/150
提交评论