PyCNC - first CNC machine controller on pure Python for RPi


9 posts
by Nikolay-Kha » Sun May 14, 2017 9:21 pm
Hello!
I'd like to present first ever CNC machine controller implementation on pure Python(even without dependencies or any C modules) for ARM based Linux boards. Hardware access layer(HAL) allows to implement support for almost any ARM SoC/CPU with suitable DMA module, but currently it is implemented for Raspberry Pi only. It already works on Raspberry Pi 2 and 3.

Typically, CNC machine controllers are implemented with C or C++ programming language and running on OSless or very real time operation systems. For example, Linux by default is not real time operation system and running LinuxCNC on it require to add special real time features to kernel. I believe that is very old approaches for building such controllers and it's high time to use modern ARM processors for CNC and use very high level programming languages like Python to provide easy development, flexibility and migration between hardware.

I managed to do this! First ever CNC controller implemented on pure Python on Raspberry Pi 2 running Linux, it's better to see, than read: https://youtu.be/vcedo59raS4

How it works? It uses DMA(Direct Memory Access) on chip hardware module, which just copy allocated in RAM buffer of GPIO states to actual GPIO registers. And this copying process is clocked by system clock and works completely independently from CPU cores. So, I was needed just to generated pulses sequence for axis stepper motors in memory and DMA precisely sends it. As for access to processor registers, it's implemented with /dev/mem system device. That can be implemented with different languages, but Python is very simple language which provides better development experiences. Of course, Python performance is not good, but on the other hand, modern ARMv7 processor performance is more than enough to run it.

And the most pleasant part, I open sourced this project!
Project is here - https://github.com/Nikolay-Kha/PyCNC
There is a wiring diagram and running instructions in git repo.
It's just a begging of this project, currently, it supports just simple things - spindle control, linear interpolation and some basic gcode commands. I have plans in near feature to add round interpolation, 4th axis, temperature sensors, heaters support and implement fully functional 3D printer CNC controller.

Thank you for reading, hope it was interesting for you.
Posts: 4
Joined: Sun May 14, 2017 9:01 pm
by Nikolay-Kha » Mon May 22, 2017 1:17 am
Now PyCNC is compatible with HDMI monitors. So, PyCNC can be run directly from Raspberry Pi3 with mouse, keyboard and monitor: 
https://youtu.be/fFp9MaTRyE4
Also, I purchased a new frame — Chinese implementation of RepRap Prusa i3 in aluminum. Now I’m awaiting for delivery. 
As you may see, I’m going to implement full functional 3d printer with PyCNC, and RepRap Prusa i3 and Raspberry Pi3 will be reference devices for that.
Posts: 4
Joined: Sun May 14, 2017 9:01 pm
by yaheath » Tue May 23, 2017 1:51 pm
Wow, that's really neat. I wonder if that DMA technique could be used for generating other types of arbitrary signals from GPIO pins...
Posts: 12
Joined: Tue Mar 28, 2017 9:58 pm
by Gavinmc42 » Wed May 24, 2017 1:42 am
Wow, that's really neat. I wonder if that DMA technique could be used for generating other types of arbitrary signals from GPIO pins...

http://abyz.co.uk/rpi/pigpio/
Might need Joan's confirmation but that is how I understood the pigpio waveform stuff worked.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges
User avatar
Posts: 1286
Joined: Wed Aug 28, 2013 3:31 am
by joan » Wed May 24, 2017 3:06 am
Gavinmc42 wrote:
Wow, that's really neat. I wonder if that DMA technique could be used for generating other types of arbitrary signals from GPIO pins...

http://abyz.co.uk/rpi/pigpio/
Might need Joan's confirmation but that is how I understood the pigpio waveform stuff worked.

Yes, that's right.

Also RPIO.GPIO at https://github.com/metachris/RPIO has a similar feature.
User avatar
Posts: 12688
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK
by Nikolay-Kha » Mon May 29, 2017 9:14 pm
yaheath wrote:Wow, that's really neat. I wonder if that DMA technique could be used for generating other types of arbitrary signals from GPIO pins...

It's totally possible to generate any arbitrary digital waveforms. I believe, it's even possible to generate arbitrary analog waveform with pulse-width modulation and low pass filter after GPIO pin, but bandwidth would be very narrow(few kilohertz I think) and waveform wouldn't be so nice like in real DAC.

{MOD EDIT - references to GPIO FM transmission removed]
Posts: 4
Joined: Sun May 14, 2017 9:01 pm
by RaspISteve » Sun Jun 11, 2017 9:13 pm
Nik,
Just looked again at the circuit showing the X,Y & Y stop wiring. It occurs to suggest that having a single signal the report hitting any/all end stop(s) could confuse things.

If at start up one or more of the STOP switches are made the program will not know which way to drive to get away. Worst case would be to continue driving in the wrong direction.

Remember that micro switches had an amount of hysteresis so having just driven into a switch you will need to drive in the opposite direction for a number of steps before the switch toggles off again.
Share and Enjoy.
Posts: 79
Joined: Wed Oct 24, 2012 9:15 pm
Location: Cheltenham, UK
by pws » Mon Jun 12, 2017 8:04 pm
A single signal for e-stops is commonly used and works well. They are there for when something goes wrong and the machine goes beyond the "soft" limits of each axis. If the system "comes up" with one of the switches made, it needs deliberate, somewhat manual intervention as it should never be at those e-stops. Nothing should be confused as there is no "process" running - nor will one run until the e-stops are "cleared".
Posts: 33
Joined: Mon Apr 11, 2016 4:16 pm
by Nikolay-Kha » Mon Jun 19, 2017 11:11 am
PyCNC endstop implementation has different inputs for different channels. And it uses only for initial moving head to the zero position. After that, signals from endstops are totally ignored, because there is no way to use feedback from machine in realtime with this approach.

joan wrote:
Gavinmc42 wrote:
Wow, that's really neat. I wonder if that DMA technique could be used for generating other types of arbitrary signals from GPIO pins...

http://abyz.co.uk/rpi/pigpio/
Might need Joan's confirmation but that is how I understood the pigpio waveform stuff worked.

Yes, that's right.

Also RPIO.GPIO at https://github.com/metachris/RPIO has a similar feature.

Be careful with these projects, RPIO and pigpio(by default, see below) both of them using DMA buffers allocated in process virtual memory(they use /proc/self/pagemap to find out physical memory location and pass this pointer to DMA).
Virtual memory in Linux, even locked memory(actually, it just protects from moving to swap), still can be moved by kernel memory manager.
See https://www.kernel.org/doc/Documentatio ... le-lru.txt
Linux supports migration of mlocked pages and other unevictable pages. This involves simply moving the PG_mlocked and PG_unevictable states from the old page to the new page.

It means, that there is always the chance that DMA stop working unexpectedly, just because kernel decide to move process virtual memory to another physical memory page(for example to defragment memory). And the longer dma buffer is, the bigger chance that it will happen.
note: pigpio by default uses tricky selector, which can use pagemap for DMA buffers. pigpio has PI_MEM_ALLOC_MAILBOX mode which will prevent you from this situation described above. I would recommend to use this mode always.
Posts: 4
Joined: Sun May 14, 2017 9:01 pm