GAP
Posts: 98
Joined: Thu Apr 12, 2018 9:18 am
Location: New South Wales Australia

Model train project help

Fri May 11, 2018 8:41 am

I am looking for some guidance on how to draw a line drawing consisting of segments that represents the track layout of a model train line on a screen using a PI running a Python program. This will hopefully give me a visual representation of where a train/s are on the line.

What I am trying to achieve is to have my PI receive inputs from sensors placed around the line that will change the colour of the segments from a default red colour to green.

Inputs will be digital signals ie true or false from micro controller chips (PICAXE) and applied via the GPIO to the PI.

As I have said am planning on writing the program in Python, which I have some books on but have never used, in fact I have no computer programming experience at all only some very limited experience in coding with basic for PICAXE.
I am virtually computer illiterate having only been a windows user.

I will greatly appreciate any help or guidance that I receive.

B.Goode
Posts: 6196
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Model train project help

Fri May 11, 2018 8:57 am

I will greatly appreciate any help or guidance that I receive.
First. Break the problem into smaller manageable chunks.

Proving that you can detect and respond to the digital inputs can be done independent of the GUI display. Initially, just report the state of the inputs as text on the screen. For help with this aspect of Physical Computing see https://projects.raspberrypi.org/en/pro ... -computing

For the dynamic GUI, a python library such as pygame or guizero will probably help.

You could very easily make a prototype/mockup with the Scratch programming language: Scratch works with sprites that have 'costumes', so you could design track segments that have 'empty' and 'occupied' versions, and switch between them on demand.

Maybe the same sort of idea could be implemented with pygame or guizero in python?

scotty101
Posts: 2987
Joined: Fri Jun 08, 2012 6:03 pm

Re: Model train project help

Fri May 11, 2018 9:00 am

Options that I would consider are;
PyGame https://www.pygame.org/news A python library for creating multimedia applications. It has functions to draw lines, or more complex widgets on screen.
Tkinter - Pre-installed with most python distributions. A slightly old fashioned but powerful GUI framework. It has a Canvas widget which allows drawings to be created.
There is also guizero which is a binding for tkinter intended to make GUI development easier for kids but at the moment the Canvas widget isn't supported.

Both will involve some work from you to learn the basics but there are plenty of tutorials out there for both of them.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

GAP
Posts: 98
Joined: Thu Apr 12, 2018 9:18 am
Location: New South Wales Australia

Re: Model train project help

Fri May 11, 2018 9:29 am

scotty101 wrote:
Fri May 11, 2018 9:00 am
Options that I would consider are;
PyGame https://www.pygame.org/news A python library for creating multimedia applications. It has functions to draw lines, or more complex widgets on screen.
Tkinter - Pre-installed with most python distributions. A slightly old fashioned but powerful GUI framework. It has a Canvas widget which allows drawings to be created.
There is also guizero which is a binding for tkinter intended to make GUI development easier for kids but at the moment the Canvas widget isn't supported.

Both will involve some work from you to learn the basics but there are plenty of tutorials out there for both of them.
Help, I have loaded pygame to my PI but cannot for the life of me find it so I can open it.

PiGraham
Posts: 3322
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Model train project help

Fri May 11, 2018 10:29 am

GAP wrote:
Fri May 11, 2018 9:29 am
scotty101 wrote:
Fri May 11, 2018 9:00 am
Options that I would consider are;
PyGame https://www.pygame.org/news A python library for creating multimedia applications. It has functions to draw lines, or more complex widgets on screen.
Tkinter - Pre-installed with most python distributions. A slightly old fashioned but powerful GUI framework. It has a Canvas widget which allows drawings to be created.
There is also guizero which is a binding for tkinter intended to make GUI development easier for kids but at the moment the Canvas widget isn't supported.

Both will involve some work from you to learn the basics but there are plenty of tutorials out there for both of them.
Help, I have loaded pygame to my PI but cannot for the life of me find it so I can open it.
It's a programming library, not a program. There are various ways to write Python code. Try IDLE which you will find on the 'start menu' under 'Programming'. You can write code in any text editor but using a program that does syntax highlighting and code completion is nice.

Raspbian comes with IDLE3 which is OK with code completion and syntax highlighting.
You will also find Thonny IDE aimed at beginners

There are a lot of options
https://wiki.python.org/moin/PythonEditors


This free ebook should help you get started.
https://www.raspberrypi.org/blog/make-g ... als-range/
It kicks off with drawing things on screen.

hippy
Posts: 3409
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Model train project help

Fri May 11, 2018 10:45 am

GAP wrote:
Fri May 11, 2018 8:41 am
I am looking for some guidance on how to draw a line drawing consisting of segments that represents the track layout of a model train line on a screen using a PI running a Python program. This will hopefully give me a visual representation of where a train/s are on the line.
In the Good Old Days (TM) this was done for real train signalling systems using VDU monitors with a special character set to allow a representation of the track to be drawn. There were symbols which could be put together to be tracks, curves, points, and everything else on an 80x25 text display. That was pretty much how it was for all process control information displays back then because there was little else.

It's all fairly easy because, once the track is drawn, it's simply a matter of where and how to update the screen.

You could emulate that even doing it graphically with tiles or drawing it one self. There may even be programs which do all you need already and you only have to get the data to the display program.

Ultibo may be a good choice for this project as it can handle serial input from a PICAXE using the AXE027 download cable and has easy access to a frame buffer, text or graphics. Almost instant boot-up time without all the overhead of Linux.

It might also be worth asking back over at the PICAXE forum as there are a number of members who have been involved in railway modelling.

I'm guessing this is similar to what you want ...

http://www.bwwmrc.co.uk/mrccc/screenshot.gif

B.Goode
Posts: 6196
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Model train project help

Fri May 11, 2018 10:48 am

I have loaded pygame to my PI but cannot for the life of me find it so I can open it.
As @PiGraham said, you can't "open" it because it is a library, not a program or game in its own right. (Not my area, but maybe the picaxe editor has libraries to support different target chips - same sort of idea.)

In addition to the RPF-backed ebook you have been referred to, going direct to the pygame website will also give you some tutorials that should be fairly generic - that's the point of libraries like pygame, to 'hide' the details of particular hardware and operating systems and make code portable.

See https://www.pygame.org/wiki/tutorials

GAP
Posts: 98
Joined: Thu Apr 12, 2018 9:18 am
Location: New South Wales Australia

Re: Model train project help

Fri May 11, 2018 9:30 pm

hippy wrote:
Fri May 11, 2018 10:45 am
GAP wrote:
Fri May 11, 2018 8:41 am
I am looking for some guidance on how to draw a line drawing consisting of segments that represents the track layout of a model train line on a screen using a PI running a Python program. This will hopefully give me a visual representation of where a train/s are on the line.
In the Good Old Days (TM) this was done for real train signalling systems using VDU monitors with a special character set to allow a representation of the track to be drawn. There were symbols which could be put together to be tracks, curves, points, and everything else on an 80x25 text display. That was pretty much how it was for all process control information displays back then because there was little else.

It's all fairly easy because, once the track is drawn, it's simply a matter of where and how to update the screen.

You could emulate that even doing it graphically with tiles or drawing it one self. There may even be programs which do all you need already and you only have to get the data to the display program.

Ultibo may be a good choice for this project as it can handle serial input from a PICAXE using the AXE027 download cable and has easy access to a frame buffer, text or graphics. Almost instant boot-up time without all the overhead of Linux.

It might also be worth asking back over at the PICAXE forum as there are a number of members who have been involved in railway modelling.

I'm guessing this is similar to what you want ...

http://www.bwwmrc.co.uk/mrccc/screenshot.gif
That is very similar to what I want to do really close in fact

EULERPI
Posts: 51
Joined: Sun May 15, 2016 2:44 pm

Re: Model train project help

Sat May 12, 2018 6:10 am

If all you want is the functionality rather than the challenge of writing code yourself then there is JMRI.

I think it stands for Java Model Railway Interface. The Java is what the program suite is written in. There is the potential to extend the suite if needs be using Jython scripts - which I've found quite similar to Python.

GAP
Posts: 98
Joined: Thu Apr 12, 2018 9:18 am
Location: New South Wales Australia

Re: Model train project help

Sat May 19, 2018 4:27 am

EULERPI wrote:
Sat May 12, 2018 6:10 am
If all you want is the functionality rather than the challenge of writing code yourself then there is JMRI.

I think it stands for Java Model Railway Interface. The Java is what the program suite is written in. There is the potential to extend the suite if needs be using Jython scripts - which I've found quite similar to Python.
I am researching JMRI and on the surface it is looking promising for drawing the screen.
The Pi4J project and the GPIO State Listener may let me look at the sensors.
If that works then all I have to do is get the Pi to display the train tracking info.
Maybe I won't have to learn python after all but then again it might come in handy for something else.

GAP
Posts: 98
Joined: Thu Apr 12, 2018 9:18 am
Location: New South Wales Australia

Re: Model train project help

Sat May 26, 2018 11:31 pm

GAP wrote:
Sat May 19, 2018 4:27 am
EULERPI wrote:
Sat May 12, 2018 6:10 am
If all you want is the functionality rather than the challenge of writing code yourself then there is JMRI.

I think it stands for Java Model Railway Interface. The Java is what the program suite is written in. There is the potential to extend the suite if needs be using Jython scripts - which I've found quite similar to Python.
I am researching JMRI and on the surface it is looking promising for drawing the screen.
The Pi4J project and the GPIO State Listener may let me look at the sensors.
If that works then all I have to do is get the Pi to display the train tracking info.
Maybe I won't have to learn python after all but then again it might come in handy for something else.
After experimenting with JMRI I do not think it is going to work in my situation, so I am now back at square one.

What I want to do is;

Draw a representation of the track layout on a screen as lines.
Would this be a GUI or something else as there would be no interaction between me and the screen?

My logic goes something like this

Draw lines on screen with each line being separate from the other and related to a particular sensor with a default colour as green (GUI?)
Look at a sensor then react to its output ie if o/p is 0 then do nothing else turn line colour red till o/p returns to 0.
This would repeat for each sensor.

From what I have read this should be a really simple program to construct I am just having trouble getting started.

superace
Posts: 13
Joined: Thu Feb 25, 2016 12:02 pm

Re: Model train project help

Sun May 27, 2018 2:43 am

How about this?
1 Use any drawing software to draw your complete track in one color (for example green track white background), let's call this the green picture
2 Make one more picture with the same track but using a different color (red on white background) let's call this the red picture
3 Cut the red picture into as many zones as you have on your track. let's call this zone-picture

Use pygame create a screen with the green picture as background and use the blit function to change the active zone to the correct selected zone-picture depending on the IO signals. After the train has passed restore the background picture and blit the next zone.

It will require a little cut and past but I think the general idea would work.

EULERPI
Posts: 51
Joined: Sun May 15, 2016 2:44 pm

Re: Model train project help

Sun May 27, 2018 5:12 am

GAP wrote:
Sat May 26, 2018 11:31 pm
GAP wrote:
Sat May 19, 2018 4:27 am
EULERPI wrote:
Sat May 12, 2018 6:10 am
If all you want is the functionality rather than the challenge of writing code yourself then there is JMRI.

I think it stands for Java Model Railway Interface. The Java is what the program suite is written in. There is the potential to extend the suite if needs be using Jython scripts - which I've found quite similar to Python.
I am researching JMRI and on the surface it is looking promising for drawing the screen.
The Pi4J project and the GPIO State Listener may let me look at the sensors.
If that works then all I have to do is get the Pi to display the train tracking info.
Maybe I won't have to learn python after all but then again it might come in handy for something else.
After experimenting with JMRI I do not think it is going to work in my situation, so I am now back at square one.

What I want to do is;

Draw a representation of the track layout on a screen as lines.
Would this be a GUI or something else as there would be no interaction between me and the screen?

My logic goes something like this

Draw lines on screen with each line being separate from the other and related to a particular sensor with a default colour as green (GUI?)
Look at a sensor then react to its output ie if o/p is 0 then do nothing else turn line colour red till o/p returns to 0.
This would repeat for each sensor.

From what I have read this should be a really simple program to construct I am just having trouble getting started.
Hi,

I think JMRI Panel Pro can do what you describe regarding coloured sensor lines on screen although it took me a while learning the software suite to work out how to do it.

Make a JMRI compatible interface to your track sensors.
In JMRI Panel Pro, select that interface in Preference/Connections.
In JMRI Panel Pro create a table with all the track sensors in.
In the JMRI Panel Pro layout editor draw lines to represent each track sensor. In the Panel Pro Tables, select Logix and create Logix functions to turn each line to a particular colour when a sensor is in the appropriate state. Save the Layout editor file with the tables. This stage might be possible with a JMRI script but I haven't tried that yet.
In JMRI Panel Pro select Preferences/Startup add the name of the Layout/Tables file so that the Layout file runs when JMRI starts up.
Reboot Panel Pro and the lines on the layout should change colour as the sensors change state.

Regards

Forris
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 242
Joined: Fri Jan 06, 2012 7:46 pm

Re: Model train project help

Sun May 27, 2018 7:38 am

As was suggested further up, you could, fairly simply, create a prototype using Scratch. I think the key is not to think of the graphics in terms of a 'line drawing', but instead as sprites. Each section of track could be represented as a separate sprite, which would be simple enough in Scratch.

If you want to use Python for your end product but find that Pygame is a bit too complex, may I suggest https://pygame-zero.readthedocs.io/en/stable/ which is a simplified version of Pygame but would be ideal for your needs.

GAP
Posts: 98
Joined: Thu Apr 12, 2018 9:18 am
Location: New South Wales Australia

Re: Model train project help

Mon May 28, 2018 9:33 am

Thank you all for the suggestions I will now go off and play with them as time permits, domestic and work commitments demands override this unfortunately, but I am sure I will be back with some questions.
The grand plan is to have 2 Pi Zeros talking via wifi to avoid hard wiring this.

Return to “General discussion”

Who is online

Users browsing this forum: Bing [Bot], droleary and 28 guests