Wednesday, January 19, 2011

Fun with JMRI

Okay, so I've been having some fun with JMRI (Java Model Railroad Interface) and I've made some good progress.  For those that don't know about it, you should follow the link for details, but essentially JMRI is a FOSS tool to help you manage and automate your model railroad layouts.

After getting my prototype layout completed, I settled in to see how the software works.  I knew already that you can use Jython/Python to do scripting.  So I started diving into that first.

I should start by saying that my vision was to use a Python IDE to write and manage the scripts.  After looking around quite a bit, I settled on Aptana Studio which is, among other things, an IDE front end for PyDev.  Pydev is an Eclipse plug in that lets you write Python code.  Aptana Studio is also based on Eclipse, but is a skinny'd down version, which was attractive as Eclipse is HUGE.

I played around with it and did some research, and I found that, as far as I know, I can't really do what I wanted with Aptana Studio.  I was hoping for integrated debugging and stuff like that, but JMRI really needs the Python scripts to be run inside of the JMRI software.

So I started trying to use JMRI as my IDE.  Let me say that JMRI is NOT intended to be used as an IDE!  There is a window you can bring up that lets you type individual scripts (Script Entry), and an output window (Script Output)  to see the results after executing.  Both windows are spartan to say the least.  There are no menu bars, no copy or paste functions... nothing but a multi-line text field and a button at the bottom to execute what you typed.  I did find that you could paste code in by using the keyboard short cuts.  And this works... but it is FAR from an IDE.

At this point, I should recognize 2 things.  First, JMRI isn't meant to give you the functionality of a Python IDE, or even a simple editor.  Second, since JMRI is open source, I shouldn't complain about it.  I should build it instead.  Alas, I'm a noob, so maybe someday, but right now, I'll find another way around it.

There is one other option in JMRI called Run Script.  For reference, all of these items are in the Panels menu item.  Clicking Run Script allows you to select a Python script to run.  I found, by trial and error, that the output of the script is sent to what looks like a DOS box that is opened when JMRI first loads.   Error messages and the output of print statements will show up here.

There is also a nifty menu option under Panels called "Thread Monitor" that lets you see the scripts that are running.  It will tell you which scripts are active, and how many times they've been cycled. 

Once I found these items, I found that I could sort of combine Aptana Studio with JMRI to get some of the benefits of an IDE - or at least my most important one for now - context completion.

Context Completion is basically the IDE popping up a menu of items that you could logically write at that point in the script.  For instance, if I wanted to print something, I could type "pri" and after the third letter, the menu would pop up offering me options, one of which would be "print".  It seems like a small thing, however, if you are typing in a method call that requires 4 parameters, it will pop in the parenthesis, and quotation marks if the parameter is a string, with each parameter stubbed out with commas between them.  At that point, you just fill in the values (which again, may have options pop up) so coding becomes much more of a pick and choose operation than a total recall operation.

So far, the only context completion that works is the base Python stuff, but I think I'll be able to include the JRMI classes as well, so that all those method calls become available for context completion.

So at this point, I was able to write and save scripts, and then test them by switching to JMRI and clicking the Run Script menu option.  This saved a lot of time, and I made good progress.

In the end, I was able to create a script, extending an example to something more complicated.  The example ran a locomotive between two sensors.  I was able to modify it to run it to one sensor over a set of turnouts, have it reverse direction, and when it got back to the first sensor, it again reversed again, threw the switch, and sent the locomotive to a siding with another sensor, which reversed it's direction again.  I wrote the script so it would continue to do this until stopped. 

Bottom line is that I've written my first successful script, and was able to sit back and watch my train essentially run itself.  Very satisfying.  However, now the doors are open... lots more scripting to work on, and object models, and class design, and... well, you get the idea.

TTFN

Tuesday, January 18, 2011

Prototype Layout Complete!

Santa was good to me this year.  I got a Kato RDC car, along with the decoder.  I also got the rr-cirkits LocoBuffer USB.  With these additions, I was able to start work on my prototype layout!

I started by hooking up the DS64 and the Zephyr Command Station, and wired up a couple of Atlas turnouts.  This worked flawlessly, and seem to be a very robust solution.  I was able to wire up 2 turnouts to the same port on the DS64, confirming that it can indeed drive 2 snap switches without any trouble.

I then hooked up the LocoBuffer to the command station, and began wiring up the Boulder Creek Engineering IR Detectors that I had gotten for my birthday.  I had a little trouble here... but the bottom line is that the sensors also work flawlessly.

My troubles started when I used a generic wall wart to power the DS64.  That ran the DS64 fine, and it ran the switches without a problem.  However, when I wired up the IR sensor, I found some troubles.  It seemed that the signaling voltage was far below the 12-14 volts needed for the DS64 to register the sensor.  I think it was probably that the wall wart didn't provide the necessary amperage.

The next thing I did was to wire up the sensor, and the sensor only, to a Radio Shack external 13.7 volt 2.5 amp power supply.  Trouble was that I continued to power the DS64 via the wall wart.  Well, I'm not quite sure what I did, but after breaking out the voltmeter, it looks as if I fried that sensor.

At that point, I did some independent testing, and decided that it must be a really bad idea to use two different power supplies on the sensors and the DS64.  The DS64 can take power off of two inputs from an external power supply.  So after breaking out a new sensor, and wiring things up, everything seemed to work fine.  I tested the 3 IR sensors I had remaining, and all of them checked out fine.  So all in all, that trouble got averted.  I'm also pretty sure that I want to use an external DC power supply to power a DC bus throughout my layout for sensors and DS64's.  I don't know if 2.5 amps will do it, but I have some time to figure that out later. 

The next thing I did was dive into the JMRI software deeper.  I was able to use it to verify that the IR sensor data was making it onto the Loconet bus.  It worked great in this regard.  So there wasn't much left but to start creating my prototype layout.

A trip to Lowes yielded some wire and a 6 foot length of 12 inch particle board.  I put some supports under it from left over 2X4's and I had a relatively stable platform.  I then laid out some sections of flex track, attached to 2 turnouts.  It is essentially two spurs that collapse into one track on the other side of the switches. 

I had been using a pair of wired connectors for power, but found that they didn't supply power throughout the layout.  So I used a small length of the 2 pair wire that got at Lowes, and fired up the soldering iron.  I found that wiring power leads to flex track is FAR easier than I imagined it would be.  I must have had a bad soldering experience that I've blocked out, because it really was as simple as stripping down the wire and heating it up with a little solder.  I think having the right wire is the key.  I'm using 24 gauge, solid wire and I think that makes a big difference.  I have no doubt now that this is the way to go.  Solder every piece of track to your rail bus people!

At this point, I was able to drill some holes and mounted the IR detectors (and when I say mounted, what I mean is that I drilled some holes and stuffed the IR detectors into them).  I popped off some of the ties in the track to make room for them.  Not pretty, but serviceable. 

I was able to get a train to move smoothly over the layout at this point.  I tied it down in one spot (near the power leads). 

Now came the fun part... watching sensors go hi and lo when a train moved over them.  It all worked like a charm.  Next post, I'll be talking at length about JMRI and the "fun" I've had with that so far. 

TTFN