Model railroad control system


27 posts   Page 1 of 2   1, 2
by n1ywb » Tue Oct 11, 2011 4:21 pm
My Raspberry Pi dream is to add a computer controller to my vintage Lionel locomotives.

The basic design would be to write a PWM program that uses GPIO pins to drive a power converter to control the motor, and use a tachometer on the motor as feedback to the PWM program, again via a GPIO pin.

For this to work acceptably, really two things need to happen. First, the Linux kernel needs to be built with the PREEMPT_RT patch set so that it will support hard-realtime operation. That should allow us to do reliable PWM in software.

The second thing is the necessity of generating interrupts from a GPIO input pin to be able to monitor the tach. Polling really wouldn't cut the mustard.

Obviously you could do this with a $5 8 bit microcontroller, but heck at $25, why not do it with Linux?

Then add a $10 micro wifi USB adapter and maybe even a USB video camera, and a speaker for train sound FX, and you're competing with train control systems that would cost much more. Building a system like THAT from a uC would probably be much more difficult than using Linux with all of it's useful bundled software, and the additional difficulty in adding the extra hardware would probably make it less competitive price-wise. And haven't you always wanted to SSH to your locomotive? :)
Posts: 26
Joined: Mon Oct 10, 2011 6:54 pm
by jamesh » Tue Oct 11, 2011 4:29 pm
Cool!
Unemployed software engineer currently specialising in camera drivers and frameworks, but can put mind to most embedded tasks. Got a job in N.Cambridge or surroundings? I'm interested!
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 11779
Joined: Sat Jul 30, 2011 7:41 pm
by hippy » Tue Oct 11, 2011 5:42 pm
Why not add a dollar's worth of hardware and put PWM and tacho reading on an external micro, off-load all the tight realtime control to that micro. The R-Pi can likely still control everything as realtime as it really needs to but the workload is far less onerous. You get the best of both worlds then.
Posts: 806
Joined: Fri Sep 09, 2011 10:34 pm
by ahven » Tue Oct 11, 2011 5:43 pm
Out of interest, I checked on hobbyist prices for model trains and tracks, I hoped a smaller pricetag for such tiny pieces :) (electronic) switch tracks can be 80 euros!
Posts: 12
Joined: Tue Aug 30, 2011 2:02 pm
by jmg123 » Tue Oct 11, 2011 5:51 pm
I too would recommend offloading the pwm and tacho counting to a micro, a small pic or avr would be perfect. (or even an arduino for overkill/simplicity)Then you could use a simple transistor based throttle circuit on the same pcb as the micro.

Personally I am going to try and get a Rocrail orJMRI server running, or at least ethernet <-> loco net running on it, so that I can use it to drive my dcc trains. Though this will in part depend on the how well Java works on the raspberry pi.
Posts: 21
Joined: Thu Sep 22, 2011 6:39 pm
by n1ywb » Wed Oct 12, 2011 3:43 am
I want to have "real time linux PWM system" on my resume. Also I am a cheap bastard and I have very little time to do this so I want to keep part count and cost and complexity to the absolute minimum and having two CPUs in the darn thing would not be minimal when one could suffice. Developing for Linux is like 1000 times easier than developing for a microcontroller. Heck I could SSH into the locomotive over wifi, edit the code in vi in a terminal, compile it with GCC, and run the program, all right on the Rpi target. Try doing that with an AVR.

Anyway PWM has been done in Linux in software before, E.g. see http://www.lukecole.name/resea.....wm_driver/

Of course 100hz is pretty slow for PWM, but it might be fine for my motor, I'll have to try and see. Could also add a big cap to smooth it out. And that other guy wasn't even using the the preempt_rt patch set so it might be possible to go faster.

Either way it will be fun to experiment with, and if doing the PWM on the Rpi turns out to be a complete bust, then maybe I'll consider an alternate approach.

Anyway my real concern at this point is the driver circuit... Can anybody recommend an H-bridge MOSFET device that I can drive from 3.3v CMOS that will handle 5A continuous at 18V? I haven't seen many that high power, I might have to run two in parallel or something. Or some other low cost low part count DC motor driver circuit? There's plenty of examples of mosfet motor driver circuits out there but they all seem to use a lot of discrete parts that IMO ought to be consolidated into a single device.
Posts: 26
Joined: Mon Oct 10, 2011 6:54 pm
by n1ywb » Wed Oct 12, 2011 3:46 am
Quote from ahven on October 11, 2011, 18:43
Out of interest, I checked on hobbyist prices for model trains and tracks, I hoped a smaller pricetag for such tiny pieces :) (electronic) switch tracks can be 80 euros!


Are you looking at HO scale? That's usually the cheapest. I run O scale because I inherited a large collection. It's generally more expensive; I couldn't afford to start from scratch in this hobby. You can get some good deals on used stuff though, folks sell off their entire layout sometimes.
Posts: 26
Joined: Mon Oct 10, 2011 6:54 pm
by n1ywb » Wed Oct 12, 2011 4:07 am
Here's an interesting paper on Linux real time performance with the PREEMPT_RT patches: http://versalogic.com/download.....chmark.pdf
Posts: 26
Joined: Mon Oct 10, 2011 6:54 pm
by Gert van Loo » Wed Oct 12, 2011 8:46 am
I have a project using L6203 driven straight from a 3V3 PIC using the PIC PWM. Works like a charm.
But that does only 4A. You sure you need 5A? I have a 12V motor using 2A lifting 120K (albeit slow).
User avatar
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2059
Joined: Tue Aug 02, 2011 7:27 am
by n1ywb » Wed Oct 12, 2011 1:36 pm
Quote from Gert van Loo on October 12, 2011, 09:46
I have a project using L6203 driven straight from a 3V3 PIC using the PIC PWM. Works like a charm.
But that does only 4A. You sure you need 5A? I have a 12V motor using 2A lifting 120K (albeit slow).


Well, I guess I really just need to measure what the loco actually draws at peak.

Remember these old Lionel motors are 1950's tech, they are notoriously inefficient.
Posts: 26
Joined: Mon Oct 10, 2011 6:54 pm
by netomx » Wed Oct 12, 2011 2:46 pm
Developing in Linux, easier than Microcontroller? Heck, I want to see that! By the way, if you use MikroC (for PICs) you'll see that making a PIC is so easy. How about using a PIC to control PWM and use the UART to comunicate with R-Pi? You dont need realtime ;)

If you use MikroC, I'll teach you how difficult will be to adopt PWM:

Pwm1_Init(5000);
Pwm1_Start();
Pwm_Change_Duty(127);

BAM! you have PWM working now ;)
Posts: 80
Joined: Tue Oct 11, 2011 4:06 am
by n1ywb » Wed Oct 12, 2011 4:59 pm
Everybody please stop suggesting I use an external micro for the PWM. I heard you, thanks for the suggestions, but one of my specific goals for this project is to do the PWM in software on Linux.

I'm a professional software engineer. I've programmed PIC assembly professionally and I've used 8 bit AVRs in hobby projects so I'm not new to this stuff. I'm not scared to write code in C or assembly. But it's time consuming and time is money so I do want to minimize it.

If I add a micro to this thing then I have to
1. Write code for the micro
2. Program the micro
3. Interface them somehow (SPI probably)
4. Develop an interface application protocol

This is not rocket science. But it is a bunch more work. If it CAN be done without it, then it SHOULD be done without it. If the Linux software PWM totally sucks then I will consider an external PWM.

If I were to go with a micro I would certainly use AVR since I can run the whole toolchain on Linux and I already own the Dragon JTAG debugger
Posts: 26
Joined: Mon Oct 10, 2011 6:54 pm
by netomx » Wed Oct 12, 2011 5:22 pm
Sorry to say this to you, but it will be more time-consuming to implement real-time on Linux.

Anyway, your goal is to control everything with the Raspberry. Good luck with that, we will sure want to know about this project!

You wanted a good H bridge? either build your own or TLE 5206-2
Posts: 80
Joined: Tue Oct 11, 2011 4:06 am
by n1ywb » Wed Oct 12, 2011 6:12 pm
Quote from netomx on October 12, 2011, 18:22
You wanted a good H bridge? either build your own or TLE 5206-2


Thanks for the tip on that part, it looks to be perfect for my application, and cheap too ($6 at digikey).
Posts: 26
Joined: Mon Oct 10, 2011 6:54 pm
by n1ywb » Thu Oct 13, 2011 2:15 pm
I measured peak current draw on my loco's motor this morning at 12 amps. Granted most train transformers would probably experience severe voltage drop preventing the current from getting that high in practice. So not only is the TLE 5206-2 too small, but it's also discontinued and getting pretty hard to find.

Now I'm looking at this part. It has all the nice features of the TLE 5206-2 but it's 44 amps. Unfortunately it's only a half bridge so I'll need two, for a total cost of $13.

http://search.digikey.com/us/e.....ND/2080753

It's hard to find good bridge drivers that come in a through-hole package, they're almost all surface mount >:(
Posts: 26
Joined: Mon Oct 10, 2011 6:54 pm
by netomx » Thu Oct 13, 2011 2:58 pm
Posts: 80
Joined: Tue Oct 11, 2011 4:06 am
by n1ywb » Sun Oct 16, 2011 3:31 pm
After some soul searching I think I will use an AVR after all. The primary motivation is the externally clocked counter peripheral, which will make an excellent tachometer. I'm just sceptical that the RPi running Linux would be able to respond to the tach pulses fast enough, that motor spins pretty fast. I also have a butterfly and dragon sitting on my desk which I haven't done anything with lately, whereas the RPi still isn't released.

I still think Linux could handle the PWM in software, but since I'll use the AVR anyway, I might was well use it for PWM as well. And while I'm at it I guess I might as well implement the PI speed control loop on the AVR. And I can use the ADC for voltage and current measurement. This will leave the RPi in charge of wifi, web cam, and sound FX.

In the meantime I can interface my desktop linux box to the AVR using rs232 to prototype the serial protocol which will eventually be moved to SPI.
Posts: 26
Joined: Mon Oct 10, 2011 6:54 pm
by jmg123 » Wed Oct 19, 2011 10:06 pm
I know this is a bit off the original topic, but as this is a thread about model railroad control....

It was pretty straightforward to get JMRI running on the Qemu emulated raspberry pi http://farm7.static.flickr.com/6236/6262051068_e061b5cdb6_b.jpg It'll make a nice cheap way to set dcc decoder settings using decoder pro. Can't wait to get my hands on the real board and test it out.
Posts: 21
Joined: Thu Sep 22, 2011 6:39 pm
by n1ywb » Thu Oct 20, 2011 2:37 pm
I was looking at JMRI. Since I'll have the RPi right in the loco I could run JMRI right on it and use WiThrottle or something to drive the loco over wifi. I'd have to write a JMRI plugin to talk to my homebrew controller though.

Actually all I really want is WiThrottle; I wonder if I could emulate the protocol it uses to talk to JMRI and brew up a smaller lighter replacement to run on the loco, which can interface directly with my controller.
Posts: 26
Joined: Mon Oct 10, 2011 6:54 pm
by Gert van Loo » Thu Oct 20, 2011 2:55 pm
Since I'll have the RPi right in the loco

I know nothing about model railway but I thought model railway locomotives where max 2x3 inches.
So you are talking about the you-can-sit-on-it stuff. That explains the 12Amps!
User avatar
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2059
Joined: Tue Aug 02, 2011 7:27 am
by n1ywb » Thu Oct 20, 2011 3:48 pm
2-3 inches is super tiny, you're thinking of N scale or something. I'm using Lionel 0 Gauge, which is 1:48 scale; There's plenty of room for everything inside this loco. This is the one I'm using: http://www.postwarlionel.com/c.....?ITEM=2338

Also it only draws 12 amps at 20V and full stall; it's a 1950's era series wound universal motor, it draws a lot of current and it's not very efficient. In practice it never draws that much because the wheels would always slip at a much lower current, so I probably could actually get away with a much smaller driver IC.
Posts: 26
Joined: Mon Oct 10, 2011 6:54 pm
by jmg123 » Thu Oct 20, 2011 5:33 pm
http://jmri.sourceforge.net/he.....ocol.shtml. This is the protocol that withrottle uses, so you should be able to rig up something yourself.

You mentioning putting the rasperry pi inside the train got me thinking..... It could make an amazing dcc chip. One that would support really complex logic, and hours of cd quality sound sychronised in time to the train.
Posts: 21
Joined: Thu Sep 22, 2011 6:39 pm
by netomx » Thu Oct 20, 2011 5:36 pm
I could recommend you to try to use MikroC for your project.. it is extremely easy to program your PICs.
Posts: 80
Joined: Tue Oct 11, 2011 4:06 am
by n1ywb » Thu Oct 20, 2011 6:26 pm
Quote from jmg123 on October 20, 2011, 18:33
http://jmri.sourceforge.net/he.....ocol.shtml. This is the protocol that withrottle uses, so you should be able to rig up something yourself.

You mentioning putting the rasperry pi inside the train got me thinking..... It could make an amazing dcc chip. One that would support really complex logic, and hours of cd quality sound sychronised in time to the train.


Thanks for the link.

I'm basically going to use it like a DCC system, except I'll be signaling over WiFi. The RPi's audio capabilities were definitely a selling point :) And adding a usb video camera to the train will also hopefully be easy.

I guess I could even just run a tiny web app right on the RPi so any wifi device with a browser could drive the train.
Posts: 26
Joined: Mon Oct 10, 2011 6:54 pm
by n1ywb » Thu Oct 20, 2011 7:21 pm
Quote from netomx on October 20, 2011, 18:36
I could recommend you to try to use MikroC for your project.. it is extremely easy to program your PICs.


No thanks. I'm a professional C programmer and I use AVR chips, not PIC. My IDE of choice is VIM+BASH.
Posts: 26
Joined: Mon Oct 10, 2011 6:54 pm