BPreemptRT - rmu75/linuxcnc-wiki GitHub Wiki


date: '2018-09-14T19:18:23' title: BPreemptRT

14 sep 2018 tjtr33


How to build a Raspberry Pi CNC control

with raspbian debian jessie rpi3b rtpreempt linuxcnc hal_gpio


( preliminary, pasted in document 14 sep and will edit over next few days )


upload:bcm2835.h
upload:cpuinfo.h
upload:hal_gpio.c
upload:Makefile

The result of these instructions: is a very small , cheap cnc controller. is a stabile, error free , low f-error, high uptime ( >2 weeks as of 14 sep 2018) is a pretty slow maxVelocity of 225mm/min. (This effort is a trial, and speed hopefully comes later )

The result will have: Raspbian Debian Jessie OS you can use debian repositories RT-Preempt kernel it's deterministic, you get responses on time LinuxcCNC you get a world of control system and a community of users and most importantly, developers hal_gpio module to control hardware using RaspberryPi gpio lines you get lots of i/o, individually programmable direction, with very low cost interfacing

The hardware used: Raspberry PI 3B with official power supply SD card ( 10x rated ) Host computer capable of 'ssh -x' into the RPi3B ( I run 'headless') Ethernet cable connection to working LAN router. external 5V power Supply for I/O 3.3V to 5V bidirectional i/o modules

 for initial bootups only:
   HDMI monitor and cable
   usb keyboard and mouse ( I used logitech wireless dongle in single usb port )

My own system added these components: Gecko G540 4 axis stepper driver Sable 2015 minI mill Palm router for spindle SSR for Spindle power

The good: It's cheap ( in context of cnc controls ) It's realtime It's extensible It's tiny , like a wallet or cell phone or pack of cigarretes ( pick your analogy )

The bad: It has a low step rate, My particular system has a teeny stepsize of 0.00075mm due to several contstarints. The PI, as configured here, has a maximum 10000 changes per second One _change _ is used to turn a 'step command on, and another change to turn it off since a step needs one on and one off, I got a step rate of 5000 steps per second

   5000 stepspersecond * 0.00075mmPerStep = 3.75mm per second 
   3.75 * 60seconds = 225mm/minute
   SLOW!  but this effort is a practice run before trying speedups ( dbl step and PICnc )
   
 one more bad thing:
   I run headless, and if the 'main' computer turns off, I dont know how to reconnect to the still running RPi
   (happened twice in 3 weeks, the Pi kept running though! (big cap?) )

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 I used a pre-built image of Raspian ( begat from Debian Jessie )
  2. Build Realtime kernel I built RT-Preempt on top of the Raspbian
  3. Build LinuxCNC I built this on top of the rest, it will be a RIP install with all the innards easily findable
  4. ** Add hal_gpio software module** I use a prebuilt module , copied into ????

1) Base Linux operating system:


  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 )
  
  <tt> ** wget <https://downloads.raspberrypi.org/raspbian/images/raspbian-2017-07-05/2017-07-05-raspbian-jessie.zip> ** </tt>

    check the downloaded file
	
    <tt> ** sha256sum 2017-07-05-raspbian-jessie.zip ** </tt>

    compare the result with the sum given on the same web page where you got the .zip
	it _has_ to match, else repeat until success
	
    decompress the file
  
  <tt> ** 7z e 2017-07-05-raspbian-jessie.zip ** </tt>

    you want to see  
	"""
	 Everything is Ok

	 Size:       4661483821
	 Compressed: 1656816607
    """
	
    now 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.

** Understand? if not DO NOT PROCEED **


    my command was
    
  <tt> ** dd if=2017-07-05-raspbian-jessie.img of=/dev/sdc bs=1M ** </tt>

    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.

 ** NOW YOUHAVE A BOOTABLE SD CARD ** 

    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' so, just FYI you will be able to use sudo if neccesary your password ( unneeded ) is 'raspberry'

open a terminal by clicking on the icon run ** raspi-config**

This program will let you configure your pi. I make some changes and expect them for the rest of this recipe.

your country and personal preferences apply

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 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 whatever is apropo

** 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 another 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

<tt> ''' login: pi Password: whatyousaid pi@raspberrypi: $
''' </tt>

  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.

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

on another computer (the HOST) , with a connection to your lan, ssh into the Pi

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

now ssh into the pI from the external system

** sudo ssh [email protected]**

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

 just delete the /root/.ssh/known_hosts  file
 
 then you get / or you get 

** sudo ssh [email protected] ** 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** ... Warning: Permanently added '192.168.1.2' (ECDSA) to the list of known hosts. [email protected]'s password:

enter PI's password here

  ...
  pi@raspberrypi:~ $ 

ok you're now ssh'd into the pi

  lets check what the PI is running,
  later, after we add the RT PREEMPT kernel, you'll see it change  
  so its good to see a before &amp; after report

** uname -a** Linux raspberrypI 4.9.35-v7+ #1014 SMP FrI Jun 30 14:47:43 BST 2017 armv7l GNU/Linux

later, you'll also see these words 'PREEMPT RT'


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

	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

<tt> ''' 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 >br> ''' </tt> here i'm getting a specific branch,its the ONLY realtime branch available on Raspbian (so far)

<tt> ''' cd linux
KERNEL=kernel7
make bcm2709_defconfig
make menuconfig
''' </tt> navigate to Kernel features | Preemption Model | mark FULLY PREEMPTABLE KERNEL RT by <SELECT> <exit> <exit> this 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! 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 )

                                                                                                                                                                                                            *

 YOU NOW HAVE A RASPIAN DEBIAN JESSIE WITH RT-PREEMP KERNEL

                                                                                                                                                                                                            *



* Now Build LinuxCNC

                                                                                                                                                                                                            *

  cd
  git clone git://github.com/linuxcnc/linuxcnc.git linuxcnc-dev												DONE	
  sudo apt-get install dpkg-dev																		DONE ( already had it )

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

  dpkg-checkbuilddeps
	this cmd will report MOST ofthe missing dependancies for linuxcnc, we'll get remaining few later
	from the list it returns, build a cmd line to apt-get them

it reports: 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 failes, just keep trying till it succeeds, same command, doggedly. I had to run it up to 3 times to succeed

these files will take a long time to get ( depends on interweb ) and 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 it, I do know how to fix it. There's a file that needs editing and you dont have it until midway thru the install!

so, edit it now and run the apt-get command AGAIN...

Here's how to edit it edit /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're 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 """ well, you need to run 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) so build it,,,

  cd																		
  cd linuxcnc-dev/src

  ./autogen.sh
  
  ./configure --with-realtime=uspace
  
  make
					this took just over 1/2 hour
  sudo make setuid

ok its built, now test it



 we built this as a RIP ( run-in-place ), so get used to this litany of commands. Say the magic everytime you use LinuxCNC or Hal etc ---
  
  cd
  cd linuxcnc-dev/
  . scripts/rip-environment
			HEY there's a DOT there , by itself, at the beginning of this command
			( its geekish shorthand for 'source' )
  --- end of memorized litany 

  --- now test what you 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

                                                                                                                                                                                                            *

 YOU NOW HAVE A RASPBIAN JESSIE RTPREEMPT LINUXCNC SYSTEM 

                                                                                                                                                                                                            *



* Improve the latency and Test the latency

                                                                                                                                                                                                            *

do a clean shutdown then bring it all back up and from the HOST sudo ssh -X [email protected] 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

                                                                                                                                                                                                            *
  • YOU NOW HAVE A RASPBIAN JESSIE RTPREEMPT LINUXCNC SYSTEM WITH GOOD ENOUGH LATECY FOR SOFTWARE STEPPING-

                                                                                                                                                                                                              *
    

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 

                                                                                                                                                                                                            *

add the hal_gpio from Michael Halberer, modified by KL Chin

 NEED A PLACE FOT SOURCE AND MODULE, NEED TO BUILD AS NEWER OTHER FILES WILL MAKE PRECOMPILED USELESS SOON

get these 3 files: bcm2835.h from MK or bcm2835.tgz cpuinfo.h from MK hal_gpio.c from KL Chin or me

copy the 3 files to linuxcnc-dev/src/hal/drivers set owner pi:pi set permissions rwxr-wr-w

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

execute: make modules

done!

                                                                                                                                                                                                            *

 YOU NOW HAVE A RASPBIAN JESSIE RTPREEMPT LINUXCNC SYSTEM WITH hal_gpio 

                                                                                                                                                                                                            *



* Create your .ini and .hal files

                                                                                                                                                                                                            *

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.

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

                                                                                                                                                                                                            *

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
⚠️ **GitHub.com Fallback** ⚠️