LinuxCNCKnowledgeBase | RecentChanges | PageIndex | Preferences | LinuxCNC.org

24 sep 2018 tjtr33
( add 84uS latency record, I think its _mostly harmless_ as i've not gotten lcnc rt errs )
( finishing TB6560 wiring to increase speed 10x with 0.0075mm stepsize ~= .0003" )
( a final rerun of all on a new card before marking these not final )>br> ( even so, use at your own risk, "be careful out there" )

How to build a Raspberry Pi CNC control

with Raspbian Debian Jessie RP3B RT Preempt LinuxCNC hal_gpio

running on workbench 16 September 2018

jump to Outline of Steps [1]

The result of these instructions:

The result will have:

The hardware used:


possible sources [2]

The good:

The bad:

Implementation notes:

 All software compilation done on the Pi

This is a long boring recipe,
with LOTS of time to get distracted and you may go watching videos or surfing the net
beware , and pay attention.
one missed step will yield a wasted day!

Outline of steps:

  1. Get Base Linux operating system [3]
    1. I used a pre-built desktop image of Raspbian
  2. Build Realtime kernel [4]
    1. I built RT-Preempt on top of the Raspbian
  3. Build LinuxCNC [5]
    1. I built a Run In Place installation on top of the above
  4. Build hal_gpio software module [6]
    1. I built the module with a revised Makefile, 2 header files , and one C source file
  5. Create .ini and .hal files [7]
    1. Get to know your hardware and tell LinuxCNC about using it
back to top [8]

1) Get Base Linux operating system:

back to outline [1]

I used a Raspbian Debian Jessie Full Desktop image,

This avoids building Xwindows et al.
some web rumors say stretch has mem leaks.

I did not try stretch or 'pure' debian for arm.
This is a "maze of twisty little passages"
I do not know all the routes to success.
I offer you this one route :-)

create a directory for this project.
cd into it

Issue this command on the HOST system ( you dont have a useable RPI yet )
wget https://downloads.raspberrypi.org/raspbian/images/raspbian-2017-07-05/2017-07-05-raspbian-jessie.zip

check the downloaded file
sha256sum 2017-07-05-raspbian-jessie.zip

you should see
455cb85fb54c73bad07bd3a44db67755912579cd84ecbd42e2ab49565206b65e 2017-07-05-raspbian-jessie.zip

it _has_ to match, else repeat download and checking until you succeed

decompress the file
7z e 2017-07-05-raspbian-jessie.zip
   you want to see  
	 Everything is Ok

	 Size:       4661483821
	 Compressed: 1656816607

now use dd to copy the extracted .img file to an sd card

The command used next is very powerful and can wipe your main drive if miss-used it WILL wipe everything off the SD card as it writes the new information

    the _exact_ command I use is _NOT_ the command you need to use.

    you need to know how to use dd or you can wipe your main harddrive.

If you do not understand, then DO NOT PROCEED

    my command was

    dd if=2017-07-05-raspbian-jessie.img of=/dev/sdc bs=1M  

    This command takes a _long_ time and has _NO_ visual feedback
    It wil be nerve racking for the newbies, it will be coffee time for the wizards.


Next, do a initial booting of the SD card and do some minor configurations.

Boot the Pi by turning on the power,
Turn on the monitor likewise.
Log in _on_ the PI.
( using the connected keyboard (usb) mouse (usb) and monitor (hdmi) )
It takes almost a full minute ( seems very long )

I do the initial bootup right on the Pi,

 I think it is simple and it works.

You will be automatically logged in as user 'pi'

 you will be able to use sudo if neccesary
 your password ( unneeded ) is 'raspberry'


Run the raspi-config program.

This program is an easy way to configure your Pi.
Please use your own informations and preferences

Using raspi-config, I change these informations
user password

      use whatever you like, but remember it, you wont get back in without it
      the username will remain 'pi'

boot options | desktop/clI | text console require login

      this removes the automatic entry to the desktop guI , I wont be using it later

boot options | splash screen | NO

      else messy screen from graphic getting text written over it

localisation | locale | remove mark on en_GB add en_US UTF-8 UTF-8

      I chose en_US.UTF-8, you choose what you need

localisation | time zone | asia bangkok

     I doubt this is your choice

localisation | keyboard layout | generic 105key intl

     English (US)
     Englishv(US)	yes twice
     No ALtGR? key
     No compose key
     use ^AltBS? to kill X?  No

interface options | SSH enable


Advanced options | Expand filesystem

     you get a message "Root partition has been resized, will be enlarged on next reboot'


you get one more message "reboot now?"

  The system will reboot,
  The screen will be all text
  The change for 'no splash' seems to fail for me, 
    so I just repeat that step and it works

login: pi
Password: whatyousaid
pi@raspberrypi: $

Get used to doing clean shutdowns to protect the Pi and SD card

  OK, that was just a test boot, now get a clean shutdown before try using ssh

sudo halt -n

  NB: __ALWAYS__ shutdown clean, dont pull the power cord or kill the outlet

now wait, this is practice on how to shutdown the PI and not harm it or the SD card.

  the screen will blank.
  wait longer for no green led flashes.
  wait for another green led hiccup
  then wait some more, at least 30 seconds after last green led flash.
  the red led will stay on, that just means the power supply is on and plugged in.
  NOW turn off the power supply and wait another 30 seconds after the red led is off.

First remote login after clean shutdown, Pi is powered off and is already prepared for remote use

Plug in an ethernet cable from your router to the pi

Power the PI back on ( it still has monitor keyboard and mouse ) and wait When you get login on the terminal screen, just let it sit there

SSH into the Pi from another computer (the HOST) connected to your lan.

Find the IP address of the HOST (not Pi) computer ( I use 'sudo ifconfig')
Find the IP addess of the PI ( I use 'ping' )

Now SSH into the Pi from the HOST.

example sudo ssh pi@

if you get


 this means that the pi is at an address that _was_ known to belong to a different computer
 and we need to tell the system that its ok.
 (these are not the droids you seek)
 so, just delete the file   /root/.ssh/known_hosts

 then you should get the following message
 you had no problem and already got the following message.

 (a sort of 'virgin found' message)

The authenticity of host ' (' can't be established. ... Are you sure you want to continue connecting (yes/no)?

say yes

the the system will add the new address to its allowed list after challenging you for the password of user 'pi'

... Warning: Permanently added '' (ECDSA) to the list of known hosts. pi@'s password:

enter PI's password here

pi@raspberrypi:~ $ <pi's_password>

ok you're now ssh'd into the pi

  lets check what the PI is running,
uname -a
Linux raspberrypI 4.9.35-v7+ #1014 SMP FrI Jun 30 14:47:43 BST 2017 armv7l GNU/Linux
  later, after we add the RT PREEMPT kernel, you'll see it change  
  so its good to see a before & after report
  later, you'll also see these words 'PREEMPT RT'


After a proper shutdown,
you can remove from the Pi:

You can remov ethat stuff because we will SSH into the Pi from now on.
If you wish, leave them attached ( I needed the desk space, and am comfortable runnning the Pi 'headless' )

2 ) Build Realtime Kernel

back to outline [1]

there are images that you can use
unless you BUILD it you dont have the tools or the sources for other , later purposes
so get dirty, build it

sudo apt-get update
sudo apt-get install git bc mc libncurses5-dev
git clone --depth=1 https://github.com/raspberrypi/linux -b rpi-4.14.y-rt

here i'm getting a specific branch, the ONLY realtime branch available on Raspbian (so far)

cd linux
make bcm2709_defconfig
make menuconfig

during the 'make menuconfig' you'll get an old school text gui ( a tui )
navigate to
Kernel features | Preemption Model | mark FULLY PREEMPTABLE KERNEL RT
this step makes sure Fully Preemptble Kernel(RT) is chosen
(mine was ALREADY set, but its good to check )

make -j4 zImage modules dtbs this takes under 2 hrs (it all being done ON the Pi)

sudo make modules_install
I wasted DAYS here because it failed with my typo 'sudo make modules install' I had no underscore!
when you get HERE you have built the new RT Prempt Kernel, YAY!

Install the RT PREEMPT Kernel
sudo cp arch/arm/boot/dts/*.dtb /boot/
sudo cp arch/arm/boot/dts/overlays/*.dtb* /boot/overlays/
sudo cp arch/arm/boot/dts/overlays/README /boot/overlays/
sudo cp arch/arm/boot/zImage /boot/$KERNEL.img

sudo reboot -n

ssh in then check that youhave an RT kernel running with
$ uname -a
Linux raspberrypI 4.14.66-rt40-v7+ #1 SMP PREEMPT RT Sat Sep 1 16:17:01 +07 2018 armv7l GNU/Linux

    YAY It says PREEMPT RT (your version numbers may vary )


3) Build LinuxCNC

back to outline [1]

git clone git://github.com/linuxcnc/linuxcnc.git linuxcnc-dev
sudo apt-get install dpkg-dev

now find the dependancies ( most of them, )
cd linuxcnc-dev/debian
./configure uspace
cd ..


the next cmd will report MOST ofthe missing dependancies for linuxcnc, we'll get the remaining few later
You need to build a shopping list from the result of the command,

the command returns something like this:
pi@raspberrypi:~/linuxcnc-dev $ dpkg-checkbuilddeps
dpkg-checkbuilddeps: Unmet build dependencies: debhelper (>= 6) tcl8.6-dev tk8.6-dev libreadline-gplv2-dev asciidoc (>= 8.5) dblatex (>= 0.2.12) \
docbook-xsl dvipng ghostscript graphviz groff imagemagick inkscape python-lxml source-highlight texlive-extra-utils texlive-font-utils texlive-fonts-recommended \
texlive-lang-cyrillic texlive-lang-french texlive-lang-german texlive-lang-polish texlive-lang-spanish texlive-latex-recommended w3c-linkchecker xsltproc python-dev \
libxmu-dev libglu1-mesa-dev libgl1-mesa-dev | libgl1-mesa-swx11-dev libgtk2.0-dev gettext intltool libboost-python-dev yapps2

then get these deps ( from the report, remove the versions, and pick one mesa )
I copied the output of dpkg-checkbuilddeps and turned it into an apt-get command _like_ this:

sudo apt-get install debhelper tcl8.6-dev tk8.6-dev libreadline-gplv2-dev asciidoc dblatex docbook-xsl dvipng ghostscript graphviz groff \
imagemagick inkscape python-lxml source-highlight texlive-extra-utils texlive-font-utils texlive-fonts-recommended texlive-lang-cyrillic \
texlive-lang-french texlive-lang-german texlive-lang-polish texlive-lang-spanish texlive-latex-recommended w3c-linkchecker xsltproc \
python-dev libxmu-dev libglu1-mesa-dev libgl1-mesa-dev libgtk2.0-dev gettext intltool libboost-python-dev yapps2

some sites may fail, its a LONG list and the interweb can be flaky
If your attempt fails, just doggedly keep trying till it succeeds,
use the same command, so its in your history, available using the UP arrow key

I had to run it up to 3 times to succeed

these files may take a long time to get ( depends on interweb )
and will take a VERY long time to install ( maybe 1 1/2 hours )

if its quick, then its wrong !

after a VERY long time you will get an annoying error

Errors were encountered while processing:
E: Sub-process /usr/bin/dpkg returned an error code (1)

I dont know how to avoid this error,
I do know how to fix it.

The problem is that there is a file in the apr-get download, that refers to a bad path
That file needs editing and you dont have the file until midway thru the install!
( bite bullet here )
Here's how to edit it
the offending file is /var/lib/dpkg/info/w3c-markup-validator.postinst
change the stanza that MOSTLY looks like this to EXACTLY THIS
if [ ! -s /etc/$server/conf.d/w3c-markup-validator.conf ]; then
ln -s /etc/w3c/httpd.conf \

( you are changing the 3rd line of this stanza FROM "conf.d" TO "conf-enabled" )

run the apt-get command AGAIN...

now you will get ANOTHER message that requires some attention, its simple though...
Enabling module include.
To activate the new configuration, you need to run:
service apache2 restart
Enabling module rewrite.
To activate the new configuration, you need to run:
service apache2 restart

ok, you type:
sudo service apache2 restart

next, you need a few more missed deps ( yes STILL missing some )
sudo apt-get install bwidget libtk-img tclx autoconf

NOW you finally have all the deps (afaict)

Build LinuxCNC

cd linuxcnc-dev/src
./configure --with-realtime=uspace

for me, the 'make' took just over 1/2 hour

sudo make setuid

OK, LinuxCNC is built, and set up for user permissions, now we can test it.

NOTE: We built this as a RIP ( run-in-place ),
Get used to this litany of commands.
Open a new terminal and say the following everytime you use LinuxCNC or Hal etc

cd linuxcnc-dev/
. scripts/rip-environment

Test what you have built

during the tests you will see what APPEARS to be a fail
its a test that makes sure something fails and you get a message saying that fail happened
( oh the terrors of negative logic )

by golly I think we have it ollie


Improve the latency and Test the latency
back to outline [1]

do a clean shutdown
then bring it all back up and from the HOST
sudo ssh -X pi@192.168.1.whatever
This will enable X forwarding and we can see the program window from the PI on the HOST screen.
You will still see just a text terminal output UNTIL we open an xwindows app on the PI.

Now to improve latency before testing latency:
Add to cmdline.txt
It _was_ this:
dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=0358fc90-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
I added to end of the line :
quiet splash lapic noxsave acpi_osi= isolcpus=2,3 idle=poll acpi_irq_nobalance noirqbalance vmalloc=32MB clocksource=acpi_pm
( this 'improve latency' is from notes at https://groups.google.com/forum/#!msg/machinekit/t2eCs09EvrU/r4Ta5GtGDgAJ a discussion of rpI rtpreempt and machinekit)

For testing the realtimey-ness of your new system, you need more.
sudo apt-get install mesa-utils
This is for glxgears , used in latency testing to load the cpu.
latency-histogram --base 100000 --servo 1000000

A base thread is important for parport type stepping systems using 'double-step'.
We may NOT need it but I plan on implementing double step for hal_gpio .

Once it is running, you can open 4 glxgears ( theres a button )
Let this run overnight or a day or so, I did :-)
I expect you will see the largest reported latency to be near 70 uSec.
typical screenshot

update 24sep2018 I saw one latency of 83uS during a 36hr test
I have not seen any realtime errors in use of linuxcnc,
so one occurred when it didnt get detected( linuxcnc was not running )

Clean up , its a mess in here!
take out the garbage, put out the cat ( yakety yak, dont talk back)!
sudo apt-get clean
This gets rid of .deb files in your apt cache, they are un-needed now.

You can clean the 'linux' dir also,
it was used to build the RT PREEMP kernel,
but the goodies have been put away already.

DONT clear the linuxcnc-dev dir.
You need that to run linuxcnc ( and remember the litany! )


back to outline [1]

get these 4 files: please backup your versions before copying

copy bcm2835.h , cpuinfo.h and hal_gpio.c files to linuxcnc-dev/src/hal/drivers set owner pi:pi set permissions rwxr-wr-w

copy new Makefile to linuxcnc-dev/src/ The new Makefile has rules to compile and link hal_gpio.c.

execute: make modules

done building stuff, begin using stuff :-) !


5) Create your .ini and .hal files

back to outline [1]

You can edit your .inI file similar to what I did to get similar results

[EMC_MOT] ... BASE_PERIOD = 100000 SERVO_PERIOD = 1000000

Your .inI file BASE_PERIOD value should be slightly larger than...

 the largest number seen 
 your .hal file step timing values for  stepgen.<chan>.steplen + stepgen.<chan>.stepspace

Be a little generous here, a few additional microseconds is good ( unscientific fudge factor, but good practice )

You will NOT be able to use any step wizardy tools here, you will need to hand edit a file from the wiz or some supplied example. Because the wizards wont work with latencies as large as yours ( if you can get less than 50uS, go for it )

My BASE_PERIOD is 100000 and my RPI runs for weeks now with no Unexpected Reatime Delays. My resultant step rate is 5000

You can ease up the load that AXIS places on the system by increasing the CYCLE_TIME [TASK] ... CYCLE_TIME = 0.010

After this step, and the RPi's strength, Axis is a bit choppy, but there are no 'latency excursions' ( non-rt, TASK related delays ).

Axis is very useable, I can rotate 3D, zoom and pan without pain, it aint fast but it dont crash neither.

Just as examples, here are my .ini and .hal files.

I Almost forgot, but making the masks up for hal_gpio is tough!
I need to write up how to do that or get permissions to use a spreadsheet I saw.
There are some notes in the above .hal file.
You can have uo to 26 i/o pins, or 22 with SPI or less with serial ports or ( theres a lot of ways to use pins on the Pi )

milestone All done, you have a realtime cnc control, you are now officially dangerous

back to outline [1]

here is some more info https://groups.google.com/forum/#!msg/machinekit/t2eCs09EvrU/r4Ta5GtGDgAJ


 Hardware Step Generator:
   see:  https://github.com/kinsamanka/PICnc-V2/wiki
   Kinsa Manka's PICnc is a hardware step generator that is suited to the RPi
   Its hardware and a software module, allowing up to 40kHz pukse rates

 Double Step for hal_gpio:
   see:  https://emergent.unpythonic.net/01188134101
   There's a trick that Jepler created back in the day, so that a single servo cycle
   could produce a high AND a low ( the stepgen module has a kind of soft one-shot timer )
   Without this, your step rate is halfed! so it is a _very_ good thing to have

 Test TB6560 with FullStep? to get 10x faster than G540 ( and 10x coarser ).

 Get rid of breadboard and make circuit board. Breadboards have terrible connections!

back to outline [1]

  Sable 2015 
fairly stiff comparitively cheap
1.5mm pitch screw
tiny workvolume fun for me
G540, AC entry 48V Kelling

parts suppliers ( not an endorsement )

Raspberry PI 3B with official power supply https://www.raspberrypi.org/products/raspberry-pi-3-model-b/
SD card ( 10x rated ) similar to https://www.sparkfun.com/products/14832
3.3V to 5V bidirectional i/o modules similar to https://www.sparkfun.com/products/12009
Gecko G540 4 axis stepper driver https://www.geckodrive.com/g540.html

 great drive, but the 10x microstepping is problematic in that it requires 10x higher Step rates
Sable 2015 minI mill http://www.cnc-sable.nl/sable-2015-tafelfrees-p-21.html?language=en
 I bought mine direct from mfctr when I was working in Taiwan, great little desktop machine
The 10mm dia 1.5mm pitych screws and round linear rails are ok
The stroke is short but the machine is pretty stiff.
Palm router for spindle similar to Makita cheap line http://makitathailand.com/mt370trimmer
 As cheap as you can get, i got one for about 14USD on sale
The single oddball collet gives me .0053" tir,
I will replace as soon as possible with an older German Overarm router with better collets and runout.
hockey puck SSR for Spindle power similar to http://www.crydom.com/en/products/panel-mount/
back to top [8]

back to outline [1]

LinuxCNCKnowledgeBase | RecentChanges | PageIndex | Preferences | LinuxCNC.org
This page is read-only. Follow the BasicSteps to edit pages. | View other revisions
Last edited September 24, 2018 12:10 am by Tjtr33 (diff)
Published under a Creative Commons License