Notifications
Clear all

A model railroader trying to use C# to develop code for Android MEGA 0256s


hacketet
(@hacketet)
Active Member
Joined: 2 months ago
Posts: 6
Topic starter  

I'm not a robots geek, but a model railroader.  I'm using Arduino Mego 0256 microprocessors to control the multiple interlockings on my layout.  The complexity of the logic is beyond my ability to explain in a simple email, but a single interlocking with 4 track switches and 4 signal levers took over 3000 lines of c code.  I didn't expect it to be that complicated.  Of the 32 IO lines on the Arduino, I use 16 for input and 16 for output to send and receive data from small custom input or output boards.  The 8 analog lines are used to address these peripheral boards.  

My problem is debugging the code.  Most of the results of the code are 50 or more feet from where I'm programming the Arduino.  Every time an event happens I've have to get up and go see what the device is doing.  It just didn't work.  So I want to create a simulation in Visual Studio using C#.  I can create graphics that show the output of the logic right on the screen.  The critical C# code can be easily converted to C code for the Arduinos.  There are four of them, and possibly a 5th. 

So far I have completely failed at being able to generate ellipses using C# and Visual Studio.  I had moderate success with VB (I could take the VB code are run it through a converter) , but  I'd prefer to write in C# as it is similar to the C code used in an Arduino - I don't want to use any functions in the core logic  that are not supported in C.  

The problem I'm having appears to be in the header files, although  I'm not sure about that.  What I would like to have is a simple set of code that draws a line and an ellipse on a form when the form is loaded.  As a crutch, I used a button to initialize the graphic lines in VB.

If anyone is interested I'd be happy to share the Eagle schematics and layouts of the input and output boards I'm using.


Quote
Will
 Will
(@will)
Prominent Member
Joined: 5 months ago
Posts: 817
 

@hacketet

Welcome to the forum.

Won't trying to reproduce the effects of the code into another language just double the amount of code to write and test and quadruple the complexity of the whole system ?

Could you just try and simulate the effects of your code on various small circuit boards ? I mean, you'd just need a single LED to simulate a signal light; a few LEDs to simulate a switch and so on. You could then test the components by plugging your simulation board into the Arduino where the real component goes and verify the operation right there at the Arduino.

That would allow you to make simple simulation boards for all your railroad gear, saving huge amounts of time and allowing extensions and improvements by adding new circuits and using the same testing boards.

It would NOT solve wiring issues, but neither will another 100,000 lines of code 🙂

Also, not sure if you already know, but you can effectively extend the number of I/O pins on your Arduino(s) by using 8-bit shift registers for both inout and output. Bill has already done a video explaining their use ...

I don't know anything about model railroads, but I have to tell you that I'm fascinated with your statement "a single interlocking with 4 track switches and 4 signal levers took over 3000 lines of c code".


ReplyQuote
Will
 Will
(@will)
Prominent Member
Joined: 5 months ago
Posts: 817
 

@hacketet

If all you need is to view some remote activity as it happens, then you might be able to use just a remote wifi camera positioned to view the area of interest and broadcast to your smart phone or laptop.

For example ...

"https://www.amazon.ca/gp/product/B09D758DND/ref=ewc_pr_img_2?smid=A270HL5P8KTJQI&psc=1"

"https://www.amazon.ca/gp/product/B09CL3GBHT/ref=ewc_pr_img_1?smid=A1Q0AGF3TD2TE3&psc=1"

You could also use them to monitor your overall setup while running. The smaller one might even be  suitable for attaching to a train for a passenger's eye view of transit 🙂


ReplyQuote
robotBuilder
(@robotbuilder)
Noble Member
Joined: 2 years ago
Posts: 1004
 

@hacketet

Why use something as complicated as c#?

My suggestion is the Processing language.  No complex programming code required. Has C++ like syntax. And lots of simple tutorials.  You can also interface it to an Arduino via the usb port.

https://processing.org/

Here is a simple oval and line and the code to produce it.  Simple? Just like the Arduino IDE.

To enlarge an image right mouse click on image and choose open window in new window.

program
display

 

This post was modified 1 month ago 2 times by robotBuilder

ReplyQuote
frogandtoad
(@frogandtoad)
Prominent Member
Joined: 2 years ago
Posts: 830
 

@hacketet

Comments inline:

Posted by: @hacketet

I'm not a robots geek, but a model railroader.  I'm using Arduino Mego 0256 microprocessors to control the multiple interlockings on my layout.  The complexity of the logic is beyond my ability to explain in a simple email, but a single interlocking with 4 track switches and 4 signal levers took over 3000 lines of c code. I didn't expect it to be that complicated.  Of the 32 IO lines on the Arduino, I use 16 for input and 16 for output to send and receive data from small custom input or output boards.  The 8 analog lines are used to address these peripheral boards.  

That sounds like a lot of code.
Did you use classes etc... are you able to provide it?

My problem is debugging the code.  Most of the results of the code are 50 or more feet from where I'm programming the Arduino.  Every time an event happens I've have to get up and go see what the device is doing.  It just didn't work. 

Did you know that Arduino can provide serial events?

So I want to create a simulation in Visual Studio using C#.  I can create graphics that show the output of the logic right on the screen.  The critical C# code can be easily converted to C code for the Arduinos.  There are four of them, and possibly a 5th. 

I'm not sure what you mean by that?

So far I have completely failed at being able to generate ellipses using C# and Visual Studio. 

Please explain what was the issue of doing that?

I had moderate success with VB (I could take the VB code are run it through a converter) , but  I'd prefer to write in C# as it is similar to the C code used in an Arduino - I don't want to use any functions in the core logic  that are not supported in C.  

Why do you need a converter to start with?  I don't understand this requirement?
Can you please explain this requirement in detail?

The problem I'm having appears to be in the header files, although  I'm not sure about that.  What I would like to have is a simple set of code that draws a line and an ellipse on a form when the form is loaded.  As a crutch, I used a button to initialize the graphic lines in VB.

At this point, I'm pretty confused 🙂
How do the header files fit in to this problem?

If anyone is interested I'd be happy to share the Eagle schematics and layouts of the input and output boards I'm using.

Not exactly clear on what your issue is to be honest.
Visual Studio can successfully connect to Arduino directly, or even by virtual serial ports, and control it via each of those interfaces.

If there is something more that you require, please let us know, and we will do our best to help you, if we can.

Cheers.


ReplyQuote
robotBuilder
(@robotbuilder)
Noble Member
Joined: 2 years ago
Posts: 1004
 

@frogandtoad 

My assumption here was that @hacketet wanted to write a simulation of his train setup.  The idea is that the code to control the simulation would be essentially identical to the code to control the real setup.

This is was I did when experimenting with odometry.  It is the same as with the simulator used to teach ROS.

If the controller code is the same or similar syntax for both simulated and real hardware then it will be easy to transfer. You get the simulator working correctly and then just transfer the control code to the real Arduino. The odometry code tested and debugged in my simulated robot base is the same as the odometry code in the physical robot base.

To enlarge an image right mouse click on image and choose open in new window.

simulation1

 

simulation2
This post was modified 1 month ago 4 times by robotBuilder

ReplyQuote
hacketet
(@hacketet)
Active Member
Joined: 2 months ago
Posts: 6
Topic starter  

@robotbuilder 

Thanks, you have been a big help.  You get what I'm trying to do.  I've not heard of that processing language before, but it looks like what I need.  I downloaded it and will give it a good look later today.  It doesn't have buttons and other things, but that isn't a problem.  

I'll give you a very brief description of railroad signaling and I think you will readily see the complexity.  Railroads are divided into electrically isolated sections called blocks, each block being the distance required for a trail travelling at track speed to come to a complete halt using normal break pressure.  Each block is protected by a signal at each end.  On prototype (real) railroads most signals are block signals that provide information about the presence of trains in the blocks ahead.  Where multiple track switches can provide several routes through a section of track things get complicated very quickly.  Back around 1880 mechanical interlocking was invented in the UK which used sliding bars with dogs that would slip into notches on bars connected to the levers controlling signals and switches, locking them in position.  Thus the term interlocking.  For any combination of signal and switch, all levers that could present a hazardous situation (allowing another train to move or throw a switch under the moving train, etc.) would be locked to prevent the operator from causing an accident.  Search 'interlocking tower interior' for images and you will see how complicated this can get.

In 1950, the C&O had two head signals, each head showing red, yellow or green.  Green was displayed on the top head ONLY when the track ahead was clear.  There were 6 aspects, each providing the engineer with information regarding the safe speed for the track ahead; Clear (G/R), Approach - stop at next signal (Y/R), Medium Clear - max speed 30 mph (R/G), Approach Medium (Y/G), Restricting - 15 mph and don't hit anything (R/Y), and Stop (R/R).  I use solenoids to lock the levers rather than mechanical dogs.  Add time locking to this and the logic gets messy real quick.

Enough - I have to take a look at that programming language.


ReplyQuote
robotBuilder
(@robotbuilder)
Noble Member
Joined: 2 years ago
Posts: 1004
 

@hacketet 

To be honest I am very new to Processing and it doesn't come with a GUI or GUI designer like Visual Basic so it may not be suitable for you.

With a search I see there are two GUI libraries written by third parties that have GUI controls for Processing.  All too complicated for my needs.

I am a low level programmer (self taught hobby level) and just write my own buttons and other GUI controls using FreeBASIC graphic commands.  A button is a rectangular area and pressing a button amounts to pressing the mouse button over that rectangle.

This is a Processing example I found on the internet of the basic idea of a button.

https://forum.processing.org/one/topic/simple-button-tutorial

float x = 100;
float y = 50;
float w = 150;
float h = 80;

void setup(){
 size(500,500);
 background(255);
 stroke(0);
 noFill();
}

void draw(){
 background(255);
 rect(x,y,w,h);
 fill(255);
 if(mousePressed){
  if(mouseX>x && mouseX <x+w && mouseY>y && mouseY <y+h){
   println("The mouse is pressed and over the button");
   fill(0);
   //do stuff 
  }
 } 
}

 

 

This post was modified 1 month ago 3 times by robotBuilder

ReplyQuote
robotBuilder
(@robotbuilder)
Noble Member
Joined: 2 years ago
Posts: 1004
 

@hacketet 

Railroads are divided into electrically isolated sections called blocks, each block being the distance required for a train travelling at track speed to come to a complete halt using normal break pressure.

This is a nice visual explanation.
http://www.railsystem.net/balise/

I imagine with a model of a modern railway system you could monitor and control it all from another room or location via the internet on a PC.

Out of curiosity I did some searching. I assume this is the kind of visual representation you would use in a simulation.
https://link.springer.com/article/10.1007/s40534-016-0119-1

trackLayout

 

This post was modified 1 month ago 2 times by robotBuilder

ReplyQuote
hacketet
(@hacketet)
Active Member
Joined: 2 months ago
Posts: 6
Topic starter  

@robotbuilder 

Exactly.  This is a more modern interlocking with three signal heads on many of the masts.  That allows many more aspects or combinations of colors.

I can't thank you enough for  connecting me to Processing.org.  That is a sweet compiler.  I don't know why I've never heard of it.  Unlike many on this site, I go back way before DOS.  Ever hear of a Bendix G10 or an IBM 1620?  How about Hollerith cards?  When DOS came along I did a lot of programming in Assembler to get the speed I needed.  I'm glad those days are long gone...


ReplyQuote
robotBuilder
(@robotbuilder)
Noble Member
Joined: 2 years ago
Posts: 1004
 

@hacketet 

Well I go back before DOS starting with the TRS80 on which I learned BASIC and Z80 assembler code. I was a keen assembler programmer (6502, 68000 and 80286 with MSDOS),  back when you could program the hardware directly,  until WindowOS appeared on the scene.  I learned C++ using TurboC++ on a MSDOS machine.  Dabbled with Java for a while but it became too complicated.  I see Processing uses the Java language.  Yes I knew about punched cards.  In the army I was in the Signals where we used punched paper tape to save and send messages. I used to type up the Morse Code messages as they arrived. Electronics and computer programming have been two of my interests but only at a hobby level.

 


ReplyQuote
frogandtoad
(@frogandtoad)
Prominent Member
Joined: 2 years ago
Posts: 830
 

@robotbuilder

Some comments inline:

Posted by: @robotbuilder

@frogandtoad 

My assumption here was that @hacketet wanted to write a simulation of his train setup. 

I got that part.

The idea is that the code to control the simulation would be essentially identical to the code to control the real setup.

Sorry, that expectation was not clear to me, that's why I asked.

This is was I did when experimenting with odometry.  It is the same as with the simulator used to teach ROS.

If the controller code is the same or similar syntax for both simulated and real hardware then it will be easy to transfer.

Sure, but that is highly dependent on the translation software being used.

You get the simulator working correctly and then just transfer the control code to the real Arduino. The odometry code tested and debugged in my simulated robot base is the same as the odometry code in the physical robot base.

To enlarge an image right mouse click on image and choose open in new window.

simulation1

 

simulation2

"You get the simulator working correctly and then just transfer the control code to the real Arduino."

I have no arguments there... I thought the original question was how to get there with converters, how to debug, etc...  It appears that the OP was struggling with those issues, AFAICT.

BTW... VS Studio is quite capable under C#, to produce 2D graphics upon a canvas, and very easy to connect to Arduino via an serial port too.

In fact... C# is very good in this area.

Anyway, looks like all is good.

Cheers. 


ReplyQuote
robotBuilder
(@robotbuilder)
Noble Member
Joined: 2 years ago
Posts: 1004
 

@frogandtoad 

If the controller code is the same or similar syntax for both simulated and real hardware then it will be easy to transfer.

Sure, but that is highly dependent on the translation software being used.

The translation software is in the brain 🙂  Top down programming.  Understanding in mentalese and translate to whatever language you are able to use.

C# is not a language I bothered with. When it first came out I bought two teach yourself books, one was Visual Basic Net and the other C# Net or some such titles. The books were identical word for word except for one using VB and the other C# code with stupid squiggly brackets to make it "professional". Maybe for professional MSWindows programmers it was ideal but for a hobby programmer it was a step in the wrong direction. Also it was too MSWindows centric and with hardware projects I also wanted my code to run with Linux.

 


ReplyQuote
frogandtoad
(@frogandtoad)
Prominent Member
Joined: 2 years ago
Posts: 830
 

@robotbuilder

Posted by: @robotbuilder

@frogandtoad 

If the controller code is the same or similar syntax for both simulated and real hardware then it will be easy to transfer.

Sure, but that is highly dependent on the translation software being used.

The translation software is in the brain 🙂  Top down programming.  Understanding in mentalese and translate to whatever language you are able to use.

C# is not a language I bothered with. When it first came out I bought two teach yourself books, one was Visual Basic Net and the other C# Net or some such titles. The books were identical word for word except for one using VB and the other C# code with stupid squiggly brackets to make it "professional". Maybe for professional MSWindows programmers it was ideal but for a hobby programmer it was a step in the wrong direction. Also it was too MSWindows centric and with hardware projects I also wanted my code to run with Linux.

 

Looks like you got this @robotbuilder!

Cheers.


ReplyQuote