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

2 comments:

  1. Moose How are you writing this script, is there a step by step instruction how to ???? I have JMRI and you usb rr cirtkut, familier with them all. Do I need to set up a panel first the write the script. Tri County N-Scale I'm on train board.

    ReplyDelete
  2. I wrote the scripts based on some examples in the JMRI documentation online. There was one that was written to use the more traditional block detection method of taking sensor inputs. But it doesn't really matter what kind of sensor you are using... it is still accessible via the DS64 ports. You just have to wire up the high and low outputs of the sensor appropriately. Then you have the sensor fire like event handlers. Most of the scripts just run over and over again looking for the event. When the state changes, you have the script execute whatever train control you want, like changing direction, etc.

    ReplyDelete