and_ampersand_and

Controlling multiple locomotives on the same train with Pybricks

Recommended Posts

Some of you may be aware of Lok24's excellent script for controlling trains using Pybricks, a third party firmware for official Lego hubs. It's a great script that automatically detects which motors you are using and provides a lot of quality of life features to controlling Lego trains:

 

Recently, I've been working on the script to add some new features. I'm excited to announce that I've added the ability to control multiple locomotives on one train at the same time, allowing you to simulate Multi-unit and Distributed Power setups like real trains use, or just to add more motors to longer trains to give them more power.

This works by utilizing a recent feature added to Pybricks, which allows Hubs to broadcast information for other hubs to read. One locomotive will be the main hub, which connects to the remote and then broadcasts it's speed information. Your other locomotives will be observers, which will listen for the main hub's broadcasts and match speed with it.

This is useful in a number of scenarios:
-If you're running a train with a locomotive at each end (i.e. if you bought 2 copies of any of the recent official passenger trains and run them as one big train with a locomotive at each end)
-You have multiple locomotives double/tripe/etc-heading your train or spread throughout, like how many trains are run today
-You have a single locomotive on a train, but it isn't strong enough. You can add a powered piece of rolling stock to give the train more power

Theoretically, you can connect any number of hubs together this way, although I've only tested it with 3 hubs. You can have multiple sets of hubs running at the same time, each on their own bluetooth channel.

This only really works if all powered units in your train use the same motor/gearing/wheels, as each motor will be running at the same speed. While there may be a slight amount of lag, it's not typically noticeable unless one of the hubs is actively connected to a computer and it has never interfered with running trains for me.

This is all on top of the various other benefits this script adds, such as running dual motors on one Hub and setting their direction correctly, smooth acceleration instead of jerkily switching between speed steps, and access to speed control on Technic motors instead of them only running full speed while the remote buttons are held. The Broadcasting feature is optional, so you can still use the script for these benefits and keep broadcasting off.

You can find the script here: https://github.com/and-ampersand-and/PyBricks-Train-Motor-Control-Script

Here's a video from Youtuber BatteryPoweredBricks going over this new feature: 

 

Additionally BatteryPoweredBricks has a video going over the install process, which is super simple. This was made before the broadcasting feature was added, so it isn't covered here.

 

Special Thanks to Lok24 for writing the original script. It's made running Lego trains so much more enjoyable.
Also special thanks to BatteryPoweredBricks for helping test this feature and making videos covering it!

Please let me know if you have any questions!
 

Edited by and_ampersand_and

Share this post


Link to post
Share on other sites

I watched the Youtube video from @BatteryPoweredBricks yesterday evening, and I was amazed at what you guys have managed to accomplish. I thought the original script from @Lok24 was amazing (It is!), but this addition will really make a difference to me personally.

Presently I have a 10233 Horizon Express double set with Power functions. Battery Box, Receiver, lights and motor at one end, then two really long extension leads through the coaches to the other loco where there is another motor and lights. With this new setup I can remove all the extension leads, and still have lights and motors at both ends! Just incredible. No move having to separate the extension leads to break the train into two halves.

The idea of double or triple heading is also very appealing. I have built @zephyr1934's Conrail version of the 10219 Maersk locomotive and am looking to build a few more in various liveries. This development means I don't need to add more weight to the train with unpowered dummies and can actually get some decent tractive effort at the head end. 

Gosh! I can practically feel my wallet lightening at the thought of it! Yikes!

Well done @and_ampersand_and.

Edited by Toxic43
Spelling error: lock corrected to loco

Share this post


Link to post
Share on other sites

Wait your wallet is lightening? :pir_laugh2:

I've been wanting something like this for a year and a half.  There are probably posts here and elsewhere of me dreaming out loud about such a system back then.  The @Pybricks team has done an excellent job, hub to hub communication is a real game changer.  Of course @Lok24 and @and_ampersand_and are absolute legends for writing and modifying the program respectively.  Programming has never been my strongsuit, it really speaks to the power of the Lego community to see something like this come to fruition!

The double / triple headed trains are absolute pulling monsters.  I've done some testing with inclines as well and it makes a massive difference.  If you do make any more alternate liveries for the 10219 loco I recently bumped the thread here about that.  I'd love to see yours @Toxic43!

Share this post


Link to post
Share on other sites
13 hours ago, and_ampersand_and said:

Here's a video from Youtuber BatteryPoweredBricks going over this new feature:  

That is amazing! Really giving some serious functionality to PU. Also like the updated Metroliner in the video.

Share this post


Link to post
Share on other sites
15 minutes ago, zephyr1934 said:

That is amazing! Really giving some serious functionality to PU. Also like the updated Metroliner in the video.

The updated Metroliner is a MOC I found on ReBrickable by Oninino.  It looks fantastic!  I don't think I ever posted it here anywhere but I did make a video showing off all of its features like opening doors etc.

Share this post


Link to post
Share on other sites

Oh, then I'll probably have to try PyBricks with several HUBs again. I last did that just under a year ago:

Result was:
yes, works: but....

I had a lot of problems connecting the HUBs, it was not reproducible. Once it worked and my train ran with 2 HUBs. Everything off again, on: doesn't work, the HUBs no longer wanted to work together. Then I gave up for the time being.

Your reports encourage me to try again!
Thank you.

 

Thomas

Share this post


Link to post
Share on other sites

Really nice work! Once I have my trains back I'll certainly try it. Still in storage as our building expansion is still being postponed...

Share this post


Link to post
Share on other sites

I just finished converting my PF Horizon Express double set over to PU with this Pybricks code installed. Main unit with train motor and lights with the main unit code, and the same in the rear running the observer hub code.

It's so great! I have the lights set to start at 20%, which is just perfect, and the smooth acceleration is wonderful! Going to go back through my other locos and redo all their code to use it as well!

But the cherry is really the two hubs/one controller functionality. I just turn on the main unit and controller, then the observer in the rear unit and away I go. It's beautiful! And when it is time to pick it up from the layout, I can just separate the units from the rolling stock and put them on the shelf. No long extension leads to separate! I'm sold on the system completely!

Now, if only Lego had a city sized hub with more than two outputs...

Share this post


Link to post
Share on other sites
1 hour ago, Toxic43 said:

I just finished converting my PF Horizon Express double set over to PU with this Pybricks code installed... ~snip~

Now, if only Lego had a city sized hub with more than two outputs...

That's awesome!  As far as the limited outputs I ordered PU and PF compatible plugs and made my own adapters.  I used two PF connectors so I can have mirrored outputs.  I used it in my Amtrak locomotive to have dual motors and a set of lights.  I have a tutorial recorded, it should release in the next couple weeks.  I will say the PF motors are not quite as smooth with the acceleration as the PU motors, but I think that could be remedied by making adjustments to the PyBricks code.
 

Lego PU to dual PF adapter

 

Share this post


Link to post
Share on other sites

Yes, but you loose the load balancing, which is a major advantage with PU.

Depends on what you want to archive.

Share this post


Link to post
Share on other sites
6 hours ago, Lok24 said:

Yes, but you loose the load balancing, which is a major advantage with PU.

Depends on what you want to archive.

2 motors and 2 sets of lights. I have this set up on my 7939 loco, which is Power Functions. Easy to do, because you can just stack the lights together on a channel and the motors on the other.

Powered up is limited to 2 motors, or a motor and 1 set of lights. Quite the downgrade in terms of connectivity. However, I think the ability to add custom firmware to the hub makes up for it a little.

Share this post


Link to post
Share on other sites
11 hours ago, Toxic43 said:

Quite the downgrade in terms of connectivity.

Yes, but great improvement of functionality an quality.

Share this post


Link to post
Share on other sites
3 hours ago, Lok24 said:

Yes, but great improvement of functionality an quality.

Agreed, I can live with my passenger locos having a single motor at each end if I get to have lights on both ends as well. For my freight locos, I'm not so fussed about lights, but two motors are a must here.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.