Klipper3d / klipper

Klipper is a 3d-printer firmware
GNU General Public License v3.0
9.49k stars 5.32k forks source link

Running Klipper as a Linux real-time process ? #485

Closed ghost closed 6 years ago

ghost commented 6 years ago

I am interested about running Klipper as a Linux real-time process. In other words using CPUs native GPIOs, SPI, I2C ports etc. Noticed there is some support in src/linux/, but no much info how this can be used. Would you be able to provide additional information ?

KevinOConnor commented 6 years ago

The Klipper micro-controller code is capable of running as a Linux process with real-time scheduling. This code (in src/linux/) is used with the Replicape to provide some basic ADC, I2C, and SPI capabilities that that board requires. Other interfaces (such as GPIO) have not been implemented. It would be possible to further extend the code to add additional capabilities. However, I suspect controlling a stepper motor at high speeds would be impractical via a Linux real-time process.

-Kevin

EDIT: As for how to use it - see config/generic-replicape.cfg and scripts/klipper-pru-start.sh. In particular, running out/klipper.bin will create a /tmp/klipper_host_mcu pseudo-tty that the main klippy process can use as a "serial" device.

ghost commented 6 years ago

It would be useful support for GPIO, PWM, UART ..., intend to use TMC2208 etc.

Regarding the control of step motors. Some Internet post state that data transfer over 25 MHz via bitbang on RPi might be feasible. If custom Linux kernel driver is written just for the step motors control ?

Another technique might be bitbang via /dev/mem. On i.MX233 Olimex was able to achieve 5.6 MHz - iMX233-OLinuXino GPIOs FASTER and FASTER :) (https://olimex.wordpress.com/2012/09/11/imx233-olinuxino-gpios-faster-and-faster/).

FHeilmann commented 6 years ago

The post you linked states that no other code was running besides the GPIO memmap stuff. So as soon as you're trying to run a full linux side by side with that code the speed is likely to be diminished significantly. At the very least the regularity of the step rates is going to go down significantly so you might see 5 Mhz sometimes but that number is going to vary all over the place.

The source for the rpi is very likely to consider bare-metal software running on the pi as well.

Fully fledged linux machines are simply not suitable for something beyond stuff that the hardware provides (spi, adc i2c). Thats why solutions like the beaglebone where the linux PC is paired with an arduino like real-time mcu exist.

ghost commented 6 years ago

@FHeilmann, I did some profiling, the Klipper and Repetier-Server processor usage is not much.

i.MX233 offers about 500 DMIPS. BCM2837 (RPi 3) - 4x ~2760 DMIPS, 11040 DMIPS which seem like a total waste just for generating step motor signals. I’ve done some comparison for running Klipper on bare-metal, results may not be accurate, see the image bellow. The expected step rates (red) were calculated based on the info from docs/Features.md.

Beaglebone (AM335x, OSD3358 ..) has worst price/performance ratio probably due to the low PRU clock speed - 200 MHz.

Apparently in Linux a CPU core can be reserved for specific program: How to run program or process on specific CPU cores on Linux.. Probably it requires some additional research and testing ?

comparison_klipper

FHeilmann commented 6 years ago

It's not that simple unfortunately.

Feel free to try but prepare yourself for nasty concurrency effects once you move to speeds which require tight timing guarantees.