Machinekit running on CHIP with RT PREEMPT kernel


I finally found time to hack a GPIO driver for Machinekit. Machinekit is a general purpose machine control software for Linux. I have described the steps on how to build Machinekit and an RT PREEMPT kernel for the CHIP in this blog post. Does anyone if it is possible to create custom flasher images?

Your Machine Koder

Getting RTlinux, RTAI, or RT preempt on Chip

Nothing more to say.




I bought a BBB a year ago to make MK run on it in order to control my 3D printer.

Awesome to see this running on CHIP. Sadly I have a pocketCHIP and still the BBB around without having anything done with it.
For the BBB I would need a cape to connect the printer, which I don´t have.

Could you suggest a way to connect a printer to the Chip? 4 Steppers, 3 Endstops, 2x Temp
… the usual 3d Printer config…


You are my hero!

Any idea on the latency numbers?


Hi @machinekoder

I have got the implemented CHIP example (square/siggen) running.
Is pwgen implemented as well?
In best case, I have a permission denied.
But probably I don’t understand the syntax.
Can’t find a python based example on Google…
Thanks again.



Hi @marc,

You can use a pwmgen. The Python HAL API is not documented very well at the moment. However, the command you are looking for is hal.loadrt('pwmgen', names='pwm-1, pwm-2'). I will create some usage detailed examples in a later blog post.


Thank you @gnomic. I have not done any latency tests yet.



For running a 3D printer, the BBB is more suitable because it has an additional microcontroller (PRU) for generating steps, PWM, etc… I can recommend you the CRAMPS board. Here you can find an example Machinekit config: MendelMax-CRAMPS.

If you want to control a 3D printer using the CHIP you have to use the software stepgen component. It should work, but I’m not sure about the performance. Furthermore, you will require a hardware interface to the 3D printers steppers.

So far I have only developed a GPIO driver for the CHIP. However, for the low latency stuff, such as measuring temperature and controlling the PWMs one can quickly build an HAL Python component (see this blog post for an example.

RFC for an idea: The MachineCHIP

Hi Gnomic,

Just started in a new wonderful world. So don’t ask too much. :smirk:

By the way I am running the not recommended:
CONFIG_PREEMPT_RT_FULL: Kernel Features > Preemption Model > Fully Preemptible Kernel (RT).

And my WIFI is “freaking out”:

I am mostly interested in the PWM generator.



Hi Machinekoder,

I can’t find a way to get your hint working.

This is what I have at the moment…

from machinekit import hal
from machinekit import rtapi as rt

# we need a thread to execute the component functions
rt.newthread('main-thread', 1000000, fp=True)

# load GPIO driver
rt.loadrt('hal_chip_gpio', output_pins='0,', input_pins='1')

gpio = hal.Component('chip_gpio')

# Is this the way to bind to  pin0 ???'out-00').link('pulse')

# load pwmgen
rt.loadrt('pwmgen', 'output_type=0')

pwmgen = hal.Component('pwmgen.0')'enable').set(1)

# 25 Hz?????'pwm-freq').set(25)'pwm').link('pulse')

# setup update functions
hal.addf('', 'main-thread')
hal.addf('pwmgen.update', 'main-thread')
hal.addf('chip_gpio.write', 'main-thread')

# ready to start the threads

This is a screenshot from halcmd:

By the way I am using a LED on pin0



@marc Your configuration looks good. However, in the halcmd output the pwm-freq pin has a wrong value. Can you please try'pwm-freq').set(25.0). The frequency is in Hz as can be read in the documentatin for the pwmgen component.

Machine Koder


Hi @machinekoder

No matter what I try for the pwm-freq.
Nothing changes.

The same with using halcmd (halcmd setp pwmgen.0.pwm-freq 10.0)

But, when I use 0.1, 0.01, 0.001 the value of the freq becomes always 0.5
When I played random around with pwm-freq , the problem flips at 0.49 and 0.5

pwm-freq 0.5 gives a value of -5e+08
pwm-freq 0.49 gives a value of 0.5

When I use, your example, with siggen there is no problem with “feeding” a new “siggen.0.frequency”.

Thanks for your quick answer.



@marc The pwmgen component also has a make-pulses function:

pwmgen.make-pulses (no floating-point)
Generates the actual PWM waveforms, using information computed by update.
Must be called as frequently as possible, to maximize the attainable PWM frequency and resolution, and minimize jitter.
Operates on all channels at once.

Can you please try to add hal.addf('pwmgen.make-pulses', 'main-thread') to your config.

Machine Koder



Yes! This works.

Sorry, but I forgot a lot…, so I am a noob…
Many moons ago I “played around” with linuxcnc
It’s complex and difficult to learn in 2 days.
I thought “pwmgen.make-pulses.time”, while staring at the “hal output” did this job.
Will continue my fight.

Playing with “setp pwmgen.0.value” activates my LED.

It’s alive :sunglasses:

Step by step. Pulse after pulse…




@marc Good to hear that it works now. Btw. the .time pin of each function show the execution time of the function in CPU ticks.

Machine Koder



Dude, I think you are brilliant. I don’t have my CHIPs yet, so I’m a spectator with ideas. I appreciate the share and the info.


Thanks, but…
Credits where it belongs… -> @Machinekoder



Hi @machinekoder,

I know it’s madness to write a loop in python reading a value from a file in RAM and use halcmd
to set values of pwmgen.0 but it’s fun.

Let’s kick the CHIP to it’s limit…

The left top graph is the raw input of the 6 bit LRADC on the chip.
The right top graph, I filtered the signal
The left bottom graph shows the value (controlled by a slider) which writes the value to the file in a tmpfs and becoms the input for pwmgen.0 and controls the brightness of the LED.
Yep I can fade smooth! Not very fast but that’s fine by me.

I am using pin CSID0
ASAP I will try one LED more and a servo.

Do you think it make sence if have a look at the source of halcmd to reduce CPU load?
Or has halcmd, “by nature” a “big delay”?



It’s only me again.
Probably talking to myself.

@ me

A cyclus of the Python loop crawling to HAL and use halcmd to change a value is around 0.14 sec.
So, as a HAL noob, the only conclusion I can make:
From user space, disturbing the real time process and change settings is a no-go if you need speed.

Or am I wrong?




You are not alone. I find your Python benchmark interesting (alarming). There should be almost no time involved in the actual Python code, but can you post the code? I’ll look it over and provide any suggestion i can.