Notifications
Clear all

Using ESP32 and ESP-NOW to control HO model trains.

5 Posts
3 Users
1 Likes
1,001 Views
(@tiredoldtron)
Member
Joined: 1 year ago
Posts: 1
Topic starter  

I have been trying to automate my grand-sons HO train set to be controlled with wireless ESP's and ESP-NOW. Using the ESP-NOW examples I have been able to easily control stop/start, direction, and speed. What I have not been able to figure out is how to stop the engine if the transmitter signal is lost. The transmitter sends a 12 for start, a 22 for stop, a 32 for forward, and a 42 for reverse, and 0 to 255 for speed. Right now when Tx is lost the train keeps going at whatever speed and direction it was before the Tx was lost. So far I have not been able to figure out how to detect a lost Tx signal. And trolling the internet has been no help.

Thank you very much,

OldTiredTron


   
Quote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6969
 

Posted by: @tiredoldtron

I have been trying to automate my grand-sons HO train set to be controlled with wireless ESP's and ESP-NOW. Using the ESP-NOW examples I have been able to easily control stop/start, direction, and speed. What I have not been able to figure out is how to stop the engine if the transmitter signal is lost. The transmitter sends a 12 for start, a 22 for stop, a 32 for forward, and a 42 for reverse, and 0 to 255 for speed. Right now when Tx is lost the train keeps going at whatever speed and direction it was before the Tx was lost. So far I have not been able to figure out how to detect a lost Tx signal. And trolling the internet has been no help.

Thank you very much,

OldTiredTron

Welcome to the forum.

What you need to do is add a timer to check the status of the train. This does not need to be a fancy hardware timer, just every time through the loop check to see if you received a signal from the train you can call 'I'm alive' and then set a remembered time to the current time as in myTime = millis(); If you received the 'I'm alive' signal from the train, then send a 'Carry on' to the train. If not, issue all stop.

The train does something similar but reversed as in it sends 'I'm alive' and receives 'Carry on'. Again, if it doesn't receive the signal after a few milliseconds, issue an all stop.

 

I am sure I botched up the explanation but you should get the idea or at least now know what to google.

This is the electronic equivalent of a train's deadman switch.

Also known as a 'Heartbeat'

 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1680
 

Hi @tiredoldtron,

   Welcome to forum! I hope you find something of interest.

I have no experience of automating trains or ESP-Now, but having a strategy to detect when the control system breaks down, followed by a 'What should it do now?' is part of the design and development of most complex systems.

As a starting suggestion:

  • How does the train decide the communications has failed?
    • Once per second, the controller sends a 'How are you?' message. and the train responds with 'Health good' or 'Health Bad' message.
      • If the train has not received 'How are you?' message within the last two seconds, it should take 'Communications lost' action
  • What action should the train do when it detects 'Communications Lost'?
    • Stop!

Of course, the above is only a simple skeleton, with arbitrary 1 second timing. Hopefully, it will prompt some thoughts and ideas for you to create  a more realistic scenario.

Also, note I have assumed that the train has automonous start/stop control ... as if it had it's own driver.

If the control of the motion of the train is actually in the 'base station', as would be the case for a simple electric model railway, with locomotives having just a motor, then the controller would be responnsible for deciding that the train had not responded to its recent 'How are You?' message within two seconds, and to cause the train to stop by disconnecting the power to the track.

------

Hope this is enough for you to design your own protocol and implement it. If you get stuck, then ask another question, having decscribed what you would like to achieve.

Best wishes and good luck, Dave

 


   
Ron reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6969
 

@davee FYI @tiredoldtron I agree totally with one caveat, 1 second is waaaaay too long, Make the alive check every pass thru the loop which will probably be a few milliseconds. How far can a train travel in 1 second? I don't know the answer, but if the train missed the slow down for the curve message then 1 second is likely too long. Basically what I am suggesting is that the first thing you do is a health check followed by any other instruction if any.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1680
 

Hi Ron @zander and @tiredoldtron,

  Once again Ron, we have obviously been typing a reply to the same message at the same time!

  Ron is probably right in saying 1 second is too long ... it depends on the dynamics of the particular system.

  I only used 1 second as an arbitrary starting point as a place to begin drafting a scheme ... if, the designer estimates it needs to be shortened to say 10 millliseconds, to make it respond quickly enough, then that is part of the design process.

Out of curosity, I looked to see if Wikipedia had any useful thoughts on the 'loop check time' of real dead man's handle system, and found the following regarding passenger trains: from https://en.wikipedia.org/wiki/Dead_man%27s_switch

Modern trains overcome this risk with the addition of a vigilance system[21] to the dead man's system. A buzzer or bell sounds every minute or so in order to alert the motorman or engineer. If they do not respond by moving a controller, or releasing and then re-applying the dead man's handle, the system will automatically initiate an emergency brake application.

The same article also discusses Tesla's "autopilot"

When the driver has engaged the semi-autonomous driving system "Autopilot", they must keep their hands on the steering wheel. If the driver takes their hands off the steering wheel for more than 30 seconds, a loud alarm will sound inside the car to wake sleeping drivers, if the driver leaves their hands off for more than a minute, then the car will engage its hazard warning lights, and bring the car to a stop. This is done because the Autopilot system is not capable of full-self driving, and requires that the driver be able to take over operation of the vehicle without warning, should the car encounter a problem it doesn't know how to solve.

Not directly comparable to the model train situation, but (assuming the articles are factually correct, which I haven't verified), maybe thought provoking?

Best wishes, Dave


   
ReplyQuote