[Home]RPi3BPreemptRT

LinuxCNCKnowledgeBase | RecentChanges | PageIndex | Preferences | LinuxCNC.org

Showing revision 27
14 sep 2018 tjtr33
( preliminary, pasted in document 14 sep and will edit over next few days )
( these notes have been tested on 2 blank SD cards to verify recipe )
( 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



as seen running on my workbench 16 September 2018

jump to Outline of Steps [1]

The result of these instructions:

(This effort is a trial, and speed hopefully comes later )

The result will have:

The hardware used:

 

The good:

The bad:

Implementation notes:

 All software compilation done on the Pi

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


1) Get Base Linux operating system:


back to top [7]

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 until success

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.


milestone NOW YOU HAVE A BOOTABLE SD CARD


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

    Unplug everything connected to the Pi.
    Plug in the usb keyboard and mouse.
    Plug in both HDMI cable ends.
    Plug in ETH cable to local Lan.

boot it and 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 simpler. I use a program found on the PI called 'raspi-config'.

You will be automatically logged in as user 'pi'

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


CONFIGURE SOME BASIC SETTINGS

Run the raspi-config program.
raspi-config

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

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

interface options | SSH enable

     YES

Advanced options | Expand filesystem

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

<FINISH>

you get one more message "reboot now?"
<yes>

  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.

sudo ssh pi@192.168.1.2

if you get

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!

 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 /root/.ssh/known_hosts  file

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

 (a sort of 'virgin found' message)

The authenticity of host '192.168.1.2 (192.168.1.2)' 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 '192.168.1.2' (ECDSA) to the list of known hosts. pi@192.168.1.2'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'


milestone YOU NOW HAVE A HEADLESS PI AND CAN WORK FROM YOUR BIG COMFY COMPUTER AND SCREEN


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


Build Realtime Kernel
back to top [7]

there are images that you can use BUT 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
KERNEL=kernel7
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 <exit> <exit> 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 )


milestone YOU NOW HAVE RASPIAN DEBIAN JESSIE WITH RT-PREEMPT KERNEL



Now Build LinuxCNC
back to top [7]

cd
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 ..

dpkg-checkbuilddeps

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:
w3c-markup-validator
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 )
so,
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 \
/etc/$server/conf-enabled/w3c-markup-validator.conf
fi

( 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
cd linuxcnc-dev/src
./autogen.sh
./configure --with-realtime=uspace
make

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

sudo make setuid
>br> OK, LinuxCNC is built, and set up for user permissiopns, 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
cd linuxcnc-dev/
. scripts/rip-environment



Test what you have built
runtests

during the tests you will see what APPEARS to be a fail
its NOT 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


milestone YOU NOW HAVE A RASPBIAN JESSIE RTPREEMPT LINUXCNC SYSTEM



Improve the latency and Test the latency
back to top [7]

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 test 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
run
  latency-histogram --base 100000 --servo 1000000

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


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, the goodies have been put away already

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


ADD THE MAGIC hal_gpio module TO DRIVE REAL MOTORS, READ LIMITS, AND RULE THE WORLD PINKY
back to top [7]


get these 4 files: please backup your versions before copying
upload:bcm2835.h
upload:cpuinfo.h
upload:hal_gpio.c
upload:Makefile

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/

this Makefile has rules to compile and link hal_gpio.c

execute: make modules

done!


milestone YOU NOW HAVE A RASPBIAN JESSIE RTPREEMPT LINUXCNC SYSTEM WITH hal_gpio



Create your .ini and .hal files '
back to top [7]

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 
 PLUS 
 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.


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


back to top [7]

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

TODO:

 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

back to top [7]

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


LinuxCNCKnowledgeBase | RecentChanges | PageIndex | Preferences | LinuxCNC.org
This page is read-only. Follow the BasicSteps to edit pages. | View other revisions | View current revision
Edited September 16, 2018 7:55 am by Tjtr33 (diff)
Search:
Published under a Creative Commons License