Greg Williams GregW66

I have finalized my DCC system. As previously mentioned I have built a DCC++ Base Station per the instructions found on the DCC++ YouTube site and other resources on the web. On my blog I have lots of links and how to’s for that. To run DCC++ you either have to use the software that was developed for it or use JMRI. I chose JMRI. Instead of running it on my lap top I wanted a dedicated computer for it so I invested in a Raspberry Pi. A Pi, is a very small credit card sized board designed to run a version of Linux. No worries, no Linux knowledge is needed. Steve Todd has it all done for you and a visit to his site will get you what you need to set it up.

https://mstevetodd.com/jmri-raspberrypi-access-point

I went another way. I wanted to document the procedure start to finish for my own pleasure and so I started with a brand new installation of the Raspbian Operating System. Below I will detail that procedure out. It got complicated because the latest OS is a little different than previous versions and the procedure to change it to an Access Point is different. I have shown step by step what I have done for anyone wanting to repeat my process.

I began by downloading the latest version of NOOBS (New Out Of the Box Software) for the Raspberry Pi. (12/9/17) this file contains the latest version of the Raspian operating system for the Pi. I am using a Raspberry Pi 3 mounted in a plastic case.

Once downloaded, NOOBS was loaded on the micro SD card and the simple instructions were followed from https://www.raspberrypi.org/learning/software-guide/This provided me with a fresh, clean install of the operating system.

With the operating system installed, I installed JMRI following the instructions as found at: http://jmri.org/install/Raspbian.shtml

  • Setup JMRI to connect to my DCC++ Base Station
  • Copied Roster from PC to JMRI with USB stick.
  • Exported Roster on PC
  • Imported Roster on Pi
  • Renamed from MyJMRIRailroad to West Haven Industrial via Edit/Preferences/Railroad Name

Run DecoderPro at Startup
In a terminal open the session autostart file:
sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart
Add the following line, with the path to the location where you installed DecoderPro:
@/home/pi/JMRI/DecoderPro
Save the file and quit the editor.

  • Set WiThrottle Server to open at startup via Edit/Preferences/Start Up/Start WiThrottle Server
  • Set JMRI to turn on track power at startup via Edit/Preferences/Start Up/Power Button
  • Setup VNC to allow remote access and control

Setup Pi as Access Point
NOTE: Raspbian Stretch works differently than most of the tutorials on the internet… Follow instructions below:
Download and install DNS and AP software packages:
sudo apt-get install dnsmasq hostapd
Edit the dhcpcd.conf file:
sudo nano /etc/dhcpcd.conf
add the following:
interface wlan0
static ip_address=192.168.6.1/24
static routers=192.168.6.1
static domain_name_servers=8.8.8.8denyinterfaces wlan0
Edit dnsmasq.conf file:
sudo nano /etc/dnsmasq.conf
add the following:
interface=wlan0
domain-needed
bogus-priv
dhcp-range=192.168.4.8,192.168.4.250,12h
dhcp-option=252,”\n”
Edit /etc/hostapd/hostapd.conf
sudo nano /etc/hostapd/hostapd.conf
add the following:
interface=wlan0
driver=nl80211
ssid=westhaven
hw_mode=g
channel=6
ieee80211n=1
wmm_enabled=1
macaddr_acl=0
auth_algs=3
ignore_broadcast_ssid=0
wpa=3
wpa_key_mgmt=WPA-PSK
wpa_passphrase=xxxxxxxx
wpa_pairwise=TKIP
rsn_pairwise=CCMP
eap_reauth_period=360000000
Edit /etc/default/hostapd
sudo nano /etc/default/hostapd​
add the following:
DAEMON_CONF=”/etc/hostapd/hostapd.conf”
Start the services:
sudo service hostapd start
sudo service dnsmasq start

Reboot the Pi

You can now access the westhaven network via WiThrottle  and VNC
Again, none of this is necessary as the image from Steve Todd does ALL the configuration for you. This is just for the DIY crowd using the latest version of the Pi OS.

With everything up and running, I wanted to try attaching my MERG Booster to the system. This was an easy thing to do. Track output of the DCC++ to the DCC IN on the booster. Power up the Pi and the DCC++ Base Station, then power up the booster. I was running trains in 5 minutes. See my comments on the MERG Booster for more information.

Now I have a complete 5Amp DCC system and it cost me less than $200 Canadian. The booster with power supply worked out to $62. The DCC++ Base Station was less than $20 and the rest is the Pi, memory card and its associated power supply. I bought a kit but the individual components are available cheaper.

GregW66
https://gregstrainyard.com

Greg Williams
Superintendent - Eastern Canada Division - NMRA
Reply 0
Raphael

RPi shutdown...

One thing to point out is that the RPi needs to be properly shutdown every single time. Randomly turning off power like we would do for an embedded command station will quickly result in sdcard corruption (and it won't be apparent at first). 

There are lots of complicated solutions to make the sdcard read-only but that won't work very well here since you'd want JMRI to keep any updates to things like the roster or panels.

Hardware solution to that problem exist: e.g. the supercacitor approach, or one "simple" workaround is to put a push button on the gpio and have a small program initiating a shutdown when pushed. 

JMRI is also quite verbose in its logging so unless that logging is directed to some kind of ramfs, it will wear the sdcard prematurely (e.g. remount /tmp and /var/log to a ramfs or to dev/null...)

Also if possible you'd want to backup your jmri config once in a while such that when the sdcard fails you can just restore it from an image and then simply copy the JMRI xml config over. (I have a git repo on bitbucket where I just push my jmri config once in a while).

Ralf~
[ web site ]

Reply 0
George Sinos gsinos

I've been running a similar

I've been running a similar setup on my home layout and on our N-trak club layout. We tested the wi-fi performance at a show a few weeks ago and it worked much better than expected.  I quit testing when I got about 100 feet from the layout.  The photo was taken while the test setup was on the workbench.

%20small.JPG 

%20small.JPG 

Reply 0
Greg Williams GregW66

Pi Shutdown

I am very aware that improperly shutting down the Pi will result in corruption. I ended up with a corrupt SD card that ended up in the trash. To solve the problem I have installed VNC on both my laptop and phone so I can control it remotely for shut down. Perhaps a button might be easier and less complicated. 

Greg Williams
Superintendent - Eastern Canada Division - NMRA
Reply 0
Raphael

Super caps

Unfortunately a lot of people don't treat RPis as normal computers that need a proper shutdown sequence so I think it's worth overstating. The Pi Touch approach is a good way to have a button on screen to do that, or have a physical push button monitored via the GPIO. It does require users to actually care and push that button and not brush it away as irrelevant.

Another hardware option I wanted to try on mine is this super capacitor approach:

http://www.sensorsiot.org/safe-shutdown-for-raspberry-pi-with-super-capacitors-133/

Andreas has a video of the same here: 

If you go that route you need to be careful that any addon device on the RPi (usb, pi touch, gpio) might affect the shutdown time.

Ralf~
[ web site ]

Reply 0
Jim at BSME

SD card corruption

Quote:

improperly shutting down the Pi will result in corruption. I ended up with a corrupt SD card that ended up in the trash

Not that I don't believe you, but this is hard to believe that the corruption caused the card to be unusable, possibly it was a bad card to start.

If a card gets corrupted by improper shutdown you should be able to reformat / reload the card. You could reformat the card by putting it in a windows PC and selecting format, or put in your digital camera, or smart phone and have it format the card. That should at least give you a good card you could copy the image onto.

- Jim B.
Baltimore Society of Model Engineers, Estd. 1932
O & HO Scale model railroading
Check out BSME on: FacebookInstagram
Reply 0
Raphael

sdcard formatting

Jim,

The sdcard corruption from an improper shutdown will corrupt the ext4 fs on the sdcard. To recover from that, you need more than just reformat using Windows or a phone. The raspbian partition scheme creates 2 or more partitions on the sdcard and these won't be properly erased (only the small fat boot partition will be visible, not the main data ext4 one). On Windows you can use the SDFormatter utility to properly reformat the card, or use diskpart on the command line (google [ rpi diskpart] for details) to get rid of the custom partitions and then use Windows to format it. There are similar ways to deal with this on Linux or MacOS.

The other issue is sdcard wear, which will inevitably happen due to the incessant logs being written on disk.

The RPi can also be nitpicking about sdcards. Even brand names that work flawlessly as usb or in photo cameras sometimes fail on the RPi.

Ralf~
[ web site ]

Reply 0
crusader27529

I'm not saying that

I'm not saying that corruption can't happen if the RPI is shut down un-gracefully, BUT in conversations with the author(Steve Todd) of the RPI image told me that after years of using his own system, and never gracefully shutting down, he's never experienced any corruption. I specifically raised the issue with him. Unfortunately, I can't seem to find the email........

Granted, it'd be best to shut down correctly, but as long as there's no real activity when shut down, it shouldn't cause an issue.

 

 

Reply 0
Greg Williams GregW66

Bad Card?

All I know is I went to boot the system after the last unsafe shutdown and the card was toast. I could not format it in Windows using ANY method. The message continually said the SD card was write protected and there was no way around that. The card reader did not have any write protect switch, as some do. There was a trick using the registry to get around the write protect as well, but that didn't work. Maybe the improper shut down did something, maybe the card was just a dud. It wasn't a high quality card, something that came with a complete Pi kit I bought. I bought a new card and all is well. 

Greg Williams
Superintendent - Eastern Canada Division - NMRA
Reply 0
Logger01

RPi UPS and DCC++ Hardware

I have several projects that use Raspberry Pis and several other boards in Headless configurations including two JMRI systems where startup(s) and shutdown(s) need to be managed automatically. To manage system power I rely on several available Uninterruptible Power Supplies (UPS) or power controllers. Although there are other UPS's available for the RPI, for my installations I have found the SmartUPS and UPS PIco to be very effective.

It is a little bizarre that this configurations requires two microprocessor boards to function. RPis have enough processing power and speed to perform all of the DCC coding and motor driver functions without the need for the Arduino board. All that is need is a DCC driver for RPi Debian Linux. Actually there are several Linux drivers, but like the driver for the Beagle Board Blacks  with TI PRUs, they are specific to the hardware available on the boards. Any of you RPi Linux coders out there have the time for another little task?

Ken K

gSkidder.GIF 

Reply 0
Logger01

Oops Greg

Greg,

I also wanted to say thanks for a great post and tutorial.

Ken K

gSkidder.GIF 

Reply 0
Greg Williams GregW66

@Ken

Thanks, I am hopeful it would be helpful. I should provide a disclaimer that what I wrote down worked for me and is based on information gleaned from various internet sources. I am by no means proficient in Linux and while I understand the basics of what I did, am no expert in anything. I'd like to thank Google for everything I know... lol

Greg Williams
Superintendent - Eastern Canada Division - NMRA
Reply 0
HVT Dave

Another vote

I, too, would like to have a Linux developer put together an image for the Raspberry Pi that would create the DCC signal directly while running JMRI, then output the DCC signal thru the GPIO to a motor controller board.

Dave

Member of the Four Amigos

 

Reply 0
Raphael

Linux is not an RTOS

Linux is not an RTOS (realtime operating system). As such, it does not offer *strong* guarantees in execution time. Remember that DCC works by emitting bits with a very precise timing. A program that runs on Linux can be paused at any time when the system needs to do something more critical. To simplify, the CPU responds to interrupts from the display, usb, sdcard, whatever, and these pause the main program for tiny fractions of time. At a human scale it seems like several things happen at once when in reality it's more a succession of tasks.

An Arduino or similar such as an ESP will run its code at a guaranteed rate and will make sure its program executes with a very precise timing. Interrupts can be tightly controlled so the author of a program relying on interrupts knows exactly when things happen.

In practice, that means that trying to write a DCC coder/decoder in userland on Linux accessing the GPIO or so would be a bit amateurish. The timings might work, but they might also be off without control, most of the time. I guess a kernel driver would offer more guarantees but not that much. 

It's not impossible. There is a class of operating system called "RTOS" which do offer these guarantees. There are various RTOS that have been ported to the RPi in some capacity or other. Google them for details. They are in general very much less user-friendly than your average Linux distrib.

Keep in mind that generally speaking the RPi isn't the greatest hardware around. It has a catchy name and gained a lot of hype and coverage, but it's not without design flaws. E.g. the usb and ethernet stacks have their quirks, as does the sdcard thing. It's not exactly the hardware I'd rely for anything critical. Example in point: the sdcard / shutdown thing. Yes most of us will be lucky and most of the time a random power off will do nothing, but that's just luck, and not something to rely about. "Hope is not a strategy" as we say in engineering.

Having multiple "CPUs" is not a new thing. It is actually more common than you think. Your mouse, your keyboard... heck even that tiny SDCard contains an ARM microcontroller.

I think the way Greg has it setup is a very good way -- you have 2 pieces of hardware, each dedicated to what they do best. The DCC++ arduino for proper reliable control and then the RPi for its flexibility in presenting a user interface and running non-critical stuff like JMRI.

Ralf~
[ web site ]

Reply 0
HVT Dave

Thanks Ralf.....

........for explaining the differences in the two types of hardware.  That makes total sense so I'll live happily with what I have.  Yes, Gregg is to be commended for putting together DCC++ and making it available for the model railroading public.  I am using it on one small layout and have three different versions of the DCC++ Base Station.

Regards,

Dave

Member of the Four Amigos

 

Reply 0
Logger01

Real Time is Relative and RT Linux is Real and Deterministic

Actually the processing requirements for DCC are well within the operating capabilities of Linux on an RPi, and, through the use of available timer functions, PWM and associated hardware available on Broadcom 28XX Micros, would be fairly easy to develop. However, driver development is best left to the few engineers who specialize in uPC driver software, and I have but hate writing drivers. In addition, if push comes to shove, there are several version of Real Time Linux kernels compatible with Debian distributions that are running very effectively on RPi's controlling substantially more time critical (or more properly deterministic) applications. In many ways the simultaneous handling of WiFi / LAN and GPU is significantly more demanding than DCC, and since RPi's seem to effectively handle multiple channels simultaneously at data rates significantly beyond those of DCC RPi's can handle DCC.

To put it in a different perspective, the first microprocessor based real time operating system (RTOS) I worked on ran at a whopping 1 MHz where RPi's can easily run at over 1 GHz. Those RCA 1802 processors managed to get us to the moon and and beyond our solar system. Although a gross over simplification - The minimum pulse width for a DCC pulse is 58 microseconds (uSec) or minimum pulse period of 116 uSec which, with RPi's Broadcom ARM processors executing an instruction about every picosecond, means that the processor only has to deal with a DCC pulse about every 58,000 instructions or with hardware timer and PWM support every 116,000 instructions. Now Linux operating systems have a lot of other things to going on, but for a dedicated DCC application, where I do not expect to deal with streaming video, I think there is enough timing headroom.

So now we are down to semantics and a little history where you should see the conundrum of when is an operating system a Real Time System? If you only have to deal with a event every few hundred microseconds then a human can be a pretty good RTOS. It's called driving. If a 1 MHz processor was running real time software (actually where ARM processors execute an instruction almost every clock cycle the 1802 required 16 to 24 clocks per instruction for a execution rate under 100 kHz), how can we say that a processor running  more than 10,000 times faster, even if running Linux, is not capable of running real time processes. I love (trashing) semantics.

Ken K

gSkidder.GIF 

Reply 0
YoHo

I was going to say, there are

I was going to say, there are Linux based RTOS and there are things far more time critical running on non RTOS versions of linux. Look at the open networking standard. Openswitch is not a RTOS. 

Reply 0
Reply