# Controlling Hobby Locomotive Railroad - Physics, Velocity, Momentum, Mass?

Hello world,

I am working on a large remote control project for a Garden Scale Locomotive for my kids. I have most things working, however I wanted to add a little bit more realism, and not being a physics major, i wanted to hear any ideas you may have.

Currently the locomotive is controlled by a Arduino Nano with a Electronic Speed Controller. The nano receives its instructions wireless via Xbee. I can easily make it go forward or backwards at any speed, but I am confusing myself on how I should incorporate momentum.

Right now, they can have 10+ cars connected, pop the throttle wide open and it will take off. If they back off the throttle or apply brakes, it immediately responds. While that works, its not how real trains operate.

So i was thinking, the code should know how many cars are connected, which ultimately determines how much mass the entire train has. Sometimes there may be no cars, just a locomotive, whereas others may have a whole string. This should affect 3 things. (I think)

• More mass, the longer it takes the train to get up to speed
• More mass, the longer the train will “coast” if throttle is reduced, and no brake are applied
• More mass, the longer the train will take to Stop when brakes are applied.

Note, these are model trains, and do not have enough mass to actually coast, i am wanting to exaggerate the values similar to what a full size locomotive experiences. For example, if we had throttle applied, and then went to no throttle, the loco would “coast” and via programming we would have to issue diminishing power settings to the ESC over a set period of time, which i think hints at a momentum formula.

I hope this somewhat explains what I am attempting to do. I eagerly await your thoughts. Thanks!!

Welcome to the forum.

How big are the engine and wagons? What drives and powers them?

What is your electronics and programming experience?

Weedpharma

I have attempted something similar (but for n-gauge trains). The computer code in the loco was as simple as yours is and the inertia and momentum effect was generated by the controller. In other words if I turned the controller to a complete stop the instructions to the loco would not follow that instantly but would ramp the speed down over a period of time.

Assuming your handheld controller is an Arduino or a PC it should be straightforward to do something similar.

I suggest you keep the maths simple and just relate the acceleration (or deceleration) to the length of the train. Figure out (by trial and error) what looks appropriate for the loco on its own and for the loco with max load and then scale it proportionately in between.

I presume your handheld unit has a convenient way to tell it how long the train is.

If your driving is like mine you also need a BIG RED SWITCH to get an instant stop

...R

I’m not quite clued up with physics; I don’t think that the relationship between mass and acceleration is linear but I might be wrong.

But it should be reasonable easy to implement something. The old (hardware) designs that I have seen are based on the charging / discharching of a capacitor via a resistor.

``````/*
charging a capacitor
Vc = Vi * (1 - e(pow(-t/RC)));

Vc = current speed (current capacitor voltage)
Vi = max speed (power source voltage)
R = power of engine; smaller is more powerful
C = mass of train; greater is heavier

discharging a capacitor
Vc = Vs * e(pow(-t/RC))

Vc = current speed (current capacitor voltage)
Vi = initial speed (initial capacitor voltage)
R = power of engine; smaller is more powerful
C = mass of train; greater is heavier
*/
``````

The below demonstrates the charging (till 90%) and discharching (till 1%).

``````void setup()
{
Serial.begin(115200);

float Vi = 50.0;
float Vc = 0.0;
float R = 1000.0;
float C = 0.1;

unsigned long startTime = millis();

Serial.println("Accelerating");
while (Vc / Vi < 0.9)
{
Vc = Vi * (1.0 - pow(M_E, ((millis() - startTime) * -1.0 / (R * C))));
Serial.print("Time / speed / speed: "); Serial.print(millis() - startTime); Serial.print("/"); Serial.print(Vc); Serial.print("/"); Serial.println((int)Vc);
}
Serial.println("Decelerating; wait 5 seconds before starting");
delay(5000);
// set start speed to current speed
Vi = Vc;
startTime = millis();
while (Vc / Vi > 0.01)
{
Vc = Vi * pow(M_E, ((millis() - startTime) * -1.0 / (R * C)));
Serial.print("Time / speed / speed: "); Serial.print(millis() - startTime); Serial.print("/"); Serial.print(Vc); Serial.print("/"); Serial.println((int)Vc);
}
Serial.print("Time / speed / speed: "); Serial.print(millis() - startTime); Serial.print("/"); Serial.print(Vc); Serial.print("/"); Serial.println((int)Vc);
}
``````

In practice, max speed (voltage) and zero speed (voltage) will only be reached after infinite time.

Notes:
1)
A train that is ‘coasting’ will eventually loose its speed; this can be simulated by a high resitor value for the deceleration.
2)
You can use different capacitor values to simulate different masses of the train.
3)
You can use different resistor values for breaking and starting.
4)
In most hardware designs that I have seen, different resistors are used for slow and fast acceleration / deceleration; as they form the same part of the equations, the effect is the same (double resistor value or double capacitor value will both result in the same time constant.

Can somebody please very verify my implementation of the formula !!

sterretje:
I don't think that the relationship between mass and acceleration is linear

It is. Force = mass * acceleration.

...R

So we have a constant force (the power of the engine) and a variable mass resulting in an acceleration in m/s2. Thanks, forgotten that type of stuff.

// Edit
In which case one can simply use

``````// acceleration
speed = currentSpeed + acceleration x time
// deceleration
speed = currentSpeed - deceleration x time
``````

You also can use discrete integration to emulate RC filters or exponential physical behaviour. At any time interval (discrete time step) the current through the resistor is proportional to the voltage difference between input and capacitor. That current increments or decrements the charge of the capacitor, resulting in a new cap voltage. Use the new voltage difference in the next time interval.
In the emulation, the cap and resistor are some variables or constants, and the cap charge or voltage is another variable, determining the integrator output. See the I part in the emulation of a PID regulator for possible implementations.

Then you can integrate the applied power (throttle dependent) to get the train speed, which you transmit to the locomotive ESC. Different integration constants can be used for acceleration and braking. Eventually apply limits to the speed output, so that the engine will definitely stop below a certain computed speed, and will not exceed a certain maximum speed.

Load specific behaviour, i.e. depending on the number of wagons, can be selected by a keyboard or pot, else some feedback from the train is required.

IMO the throttle dependent power is equivalent to the motor current, so that it may be sufficient to control the motor current directly from the throttle setting, instead of controlling the motor speed?

Based on my own experience there is no value in a complex solution for this application. I suspect a few alternative acceleration rates will be more than sufficient to give the appearance of the behaviour of a real train.

...R

One thing that may save a bit of time is the important word omitted from your post title... INERTIA

Many model controllers implement profiles to simulate inertia - using the terms you noted. A few Google searches may give you more helpful results.

Well I certainly appreciate most everyone's feedback, great suggestions and excellent writeups. This gives me a lot to go on. Thank you!

Nope, not at all, It was the guy suggesting I did no research before posting a question.

I found the JMRI interesting.. I ran across it quite some time ago, but couldn't quite figure out how to integrate it, so I decided on multiple Arduinos.

A little more info, I am using a full size locomotive control stand and control panel as my inputs. These all feed into a Mega and that's where most of the logic resides. The nano in the Locomotive controls stuff like Speed, Lights, Smoke, RPM of the Wheels (For the Speedometer) and I am working on reading amps and battery voltage.
There are also many outputs that I will be using another mega to control things such as warning lights, Alerter, Mock Radio, Mock EOT device, Speedometer, 4 PSI gauges, Air Flow gauge, Amp Gauge.

Here are all the switch inputs I am reading from the Control Stand and Panel into the mega.
CAB - Control Panel

Master Knife Switch (Supplies power to the Mega)
Ditch Light Front
Ditch Light Rear
Number Board Front
Number Board Rear
Crosswalk Lights
Class Lights (Although am not sure how these are used or how to implement)
Cab Lights
Engine Control
Start Engine
Stop Engine
Reset
Charging Circuit (Breaker)
Run / Isolate
Fuel Pump (Breaker)
Local Control (Breaker)

CAB - Control Stand
Ind Brake
Automatic Brake
Dynamic Brake
Throttle
Reverser
Horn
Bail Off
Sand
Engine Run
Generator Field
Contol + Fuel Pump
Dynamic Brake
Bell

You have a grand list of data - seems like a fascinating project. I presume the loco is battery powered? My interest is battery powered radio control for OO and N gauge.

Are you aware of the Radio Control sections on the RMWeb Forum or on the Free Rails Forum - you may find some useful stuff there.

...R

Yep, the loco was remote controlled by standard hobby remote, but then I happened across a full size train that was being scrapped, and was able to get stuff like the control panel for nothing.
Your right about the batteries, I use 2 12v SLA alarm batteries in a trailing boxcar to power it. And you are exactly right about the amount of inputs and outputs; it got confusing really fast, ended up using spreadsheets to keep track of which wires go where. Right now everything I spread out on a workbench and I am using small switches until I get the control stand hooked up. I am still in the early stages of this, but maybe I should put up a small blog for people that are interested.

I think I got most of it down, its just the simulating the forces of a real locomotive is what was difficult. Several great suggestions are helping me power thru it.

Now when it comes to reading the Independent Brake, Automatic Brake, Dynamic Brake, BailOff, and how to make the associated Air Gauges react (The Gauges are all converted to Servos) will be a challenge as well.

Thanks!

Nope, not at all, It was the guy suggesting I did no research before posting a question.

After reading the posts, I did not find any post suggesting lack of research.

There was one asking for information on your setup and experience. This information allows us to know what you have and what level of explanation is required.

So who was the “jackball” and why so?

Weedpharma

its just the simulating the forces of a real locomotive is what was difficult.

You can't, really. I agree with Robin2 -- if it looks realistic to you, that is the best you can do.

In fact, the physics of mass, acceleration and force don't simply scale to small objects. Friction takes a completely outsized role in proportion, which is why the model locomotive doesn't coast in proper scale to a real locomotive. Movie makers working with models deal with this all the time.

Dear OP,

My name is Jack Ball.

I was trying to help you identify suitable search terms, but glad that you read my footer signature - as that suggests it wasn't a total waste of time (not directed at you!).

You may have noticed there are a lot of newbie, or self-centered posters that ask the same questions over-and over again - and have obviously not even scanned the forum well enough to know there is a search function.

Ohers (not you), simply ask a question - demanding - a solution, that won't use up any of 'their' valuable time...

Nothing at all against you personally, but please double check what you're replying to !

Cheers
Jack

Hi lastchancename! Well, i consider myself the Jackball! I re-read all the posts and discovered that i misread your tag line as an actual post, and I apologize. I seen everyone's positivism towards helping, then read what i thought was your post, and thought who the hell does this guy think he is :).

I made that discovery a few hrs ago, and see that you too posted. I deleted my comment, again, sorry about the jackball,
I've just seen so many trolls or noobs that put no effort into even googling for an example, i completely understand your tagline.

Thanks, I am putting my lil project online for others to see, and I am sure i'll be reaching out again once i bang my head on the wall too many times.

Cheers

I put a pic online of the control stand I am reading the inputs from, i have the independent brake off working on it as well as the radio and EOT device
http://shagbarkvalley.blogspot.com/

I am not a blogger, so the page looks like a 6th grader made it.

SBVRR: