Lok24

Control your trains without smart device - with Pybricks

Recommended Posts

Thanks Lok24!

I was able to install the Motor Control on my blue cargo train (60052) with 2 motors and everything works great.

Patrick

Share this post


Link to post
Share on other sites

@CCSG Question, does your City Hub have issues turning off after installing PyBricks (Takes multiple attempts, blue light keeps blinking)?  I think I've narrowed it down to having a train motor on port B.  I'm using a workaround but it involves a custom firmware and Linux.  I have 5 hubs and I've tested several, so I'm fairly certain I'm not crazy :pir_laugh2:

Also while I'm here I'm working on trying to figure out the best way to use this program to control a single motor, but allow for the other output to be used for lights, decoupler etc.  This program is a bit over my head to reverse engineer and I keep getting distracted by other projects :pir-grin:

Share this post


Link to post
Share on other sites
Just now, Modeltrainman said:

How is this different than the standard remote?

It allows for smooth controlled acceleration (two profiles with adjustable settings for each), reversing one or both motors in the programming and using any motor type.  This should be possible with the Lego app, but you'd have to use a phone as an intermediary, which I try to avoid at all costs.  I plan to make a video on it eventually but I'm kind of stuck with the poweroff bug in PyBricks.

Share this post


Link to post
Share on other sites
2 minutes ago, BatteryPoweredBricks said:

It allows for smooth controlled acceleration (two profiles with adjustable settings for each), reversing one or both motors in the programming and using any motor type.  This should be possible with the Lego app, but you'd have to use a phone as an intermediary, which I try to avoid at all costs.  I plan to make a video on it eventually but I'm kind of stuck with the poweroff bug in PyBricks.

Thank you! It sounds appealing, but I can't read German. Wouldn't know how to install. 

Share this post


Link to post
Share on other sites

@Modeltrainman

I don't know any German either I was able to use the built in translator on Safari to understand Lok24's different code. The Pybricks website and instructions are in English. 

20 minutes ago, BatteryPoweredBricks said:

@CCSG Question, does your City Hub have issues turning off after installing PyBricks (Takes multiple attempts, blue light keeps blinking)?  I think I've narrowed it down to having a train motor on port B.  I'm using a workaround but it involves a custom firmware and Linux.  I have 5 hubs and I've tested several, so I'm fairly certain I'm not crazy :pir_laugh2:

Also while I'm here I'm working on trying to figure out the best way to use this program to control a single motor, but allow for the other output to be used for lights, decoupler etc.  This program is a bit over my head to reverse engineer and I keep getting distracted by other projects :pir-grin:

@BatteryPoweredBricks not sure that I had that problem. I seemed to have a problem uploading the firmware, my hub kept timing out or reporting a problem. Once I had the firmware installed, I was able to install the Motorcontrol program and It worked. I like the Power Up system as I'm really not a fan of IR control but am a little surprised that LEGO doesn't off much in the way of instructions on how to use it. 

Share this post


Link to post
Share on other sites
On 8/6/2022 at 9:29 AM, Lok24 said:

Thanks @BatteryPoweredBricks, I changed initial post.

It's all here:

progams, installation and instructions (all in german)

There are three programs:

AutoBox1.12.zip

ShuttleTrain 1.4.zip

MotorControl 2.7.zip

 

Hello good sir, a viewer let me know the download links are not working again.  In the mean time I've shared a google drive link to the motor control program but I know I'm not the only person linking there.

Also I've mentioned in this thread that PyBricks had a bug that wouldn't allow a city hub to turn off when a train motor was connected on port B but it has been fixed in later versions :classic:

Share this post


Link to post
Share on other sites

Hello,

Sorry, no, the links don't work any more, as it took too much effort to me to adapt the programs and description to the changes pybricks made.

 

Share this post


Link to post
Share on other sites
On 3/29/2023 at 10:53 AM, Lok24 said:

Hello,

Sorry, no, the links don't work any more, as it took too much effort to me to adapt the programs and description to the changes pybricks made.

 

Are these planned changes?  Is it an issue with non-train motors?  I just tried the latest and beta version of Pybricks and everything worked fine.  Even if future versions broke compatibility I have an older version of the firmware I can still use, downloaded via linux.  This is the best solution I've found to control trains with the remote, I won't let it go away :pir-grin:

Share this post


Link to post
Share on other sites

BatteryPoweredBricks, I'm working on my own replacement for Lok24's script. It won't be as advanced, but it has one key feature for me, the ability to set the target speed while the motor is still accelerating. 

 

Lok24, did you notice any delay in the speed control when using motor.run? I'm getting a kind of pumping action as I run trains around s bends, as if the PID control is wildly over compensating and then under 

Share this post


Link to post
Share on other sites

Hi, don't know what version you are running, but 2.9 should still work?

Older versions:
Search for "Motor(port).control.limits....." and replace with "motor.obj.control.limits...."

@ejayb , no I only have tacho motors and had not any problem, have a look:

https://www.youtube.com/watch?v=UaNMkoPSUSA

or

https://www.youtube.com/watch?v=74n1VGK8jwE

 

Share this post


Link to post
Share on other sites

I have a question regarding the watchdog setting.  A friend of mine has a large train layout and the remote tends to disconnect unless it is placed in a central place.  When I set the watchdog setting to False, the motor keeps running when disconnected.  Great!  The problem is I'd like to be able to reconnect the remote while it is still running without chasing the train down and restarting the program.  Is this possible?  I've played with the settings a bit (the last section of the program after setting the watchdog to False), but can't seem to get the desired result.  Anyone more experienced with PyBricks that could help with this?

Share this post


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

Did you try to reconnect the FB (the green Button, as usual)?

Well I feel like an idiot :pir_laugh2:.  I tried again and it works as expected.  I must have still had the PyBricks web interface connected when I first tried it.  I'm used to the old "include program" method of loading programs.  Thanks :pir-huzzah2:

Share this post


Link to post
Share on other sites

Hi,

after much testing with Lok24's script, we've come to a few conclusions, in regards to the firmware 3.2.0 issues.

- train motors still work .. as before. No problem there.
- technic motors don't work (script fails)
- adding lights also makes the script fail.

You can now upload older firmware in PyBricks using the "advanced" feature. So flashing 3.1.0 is no problem. However, the UI fails to upload the script then, as the .mpy format has changed.

However, I've managed to upload the script using firmware 3.1.0 by replacing the main.py file in the firmware zip file with the motorcontrol.py script (after renaming it to main.py), then flashing the firmware with script to the hub and voila: my L-motor now works. So there is a way of getting it still loaded with the old firmware.

/M

Edited by Marlow

Share this post


Link to post
Share on other sites

Inspired by Marlow's efforts, I dove into the script and figured out why it wasn't working with PyBricks 3.2.x. Was able to modify the script enough to get it working with my train equipped with a technic motor with PyBricks 3.2.3

I don't have any lights, so I don't know if the script fails with them still. My guess is yes.

You can find the file on my github here: https://github.com/andrewdschmidt/PyBricks-Train-Motor-Control-Script/blob/main/motorcontrol_modified.py

Let me know if anyone finds any issues with it!

Share this post


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

Inspired by Marlow's efforts, I dove into the script and figured out why it wasn't working with PyBricks 3.2.x. Was able to modify the script enough to get it working with my train equipped with a technic motor with PyBricks 3.2.3

And I can confirm, that this script now works with Technic Motors again and solves some remote reconnect issues, it had on firmware 3.1.0.

Thank you.

/M

Share this post


Link to post
Share on other sites

  I am ecstatic to be able to report that this is working for me!  For whatever reason, I could never get the remote to connect to it prior to this version.  I swapped out the .py file from the 3.1.0 zip file with this patched code, and then it just immediately worked as it was originally supposed to.  I used the 3.3.0b8 version of Pybricks UI for the firmware write.

  Now it would be great if there was a little more documentation on how to customize the operating modes/parameters.  I can see some of the things that are easy to edit in the code, but I'm only now at the point where I can actually test any changes due to the connection finally working.  Many of the parameters were hinted at in the original documentation but were never spelled out in detail (doc links broken now anyway).  Nearly everything I want to control will involve technic motors rather than train motors, so the performance characteristics and operating scenarios will be quite variable.  Perhaps those of you that are more versed in the use of the code can post some parameter instructions in this forum.

  Thanks again to the OP for his work on developing the project.  This finally brings the PUP experience much closer to that of the PF system.  I can hand the basic remote to my young nephews and expect them to be able to understand it immediately, as well as not risk them trashing a phone.  It's also super easy for the workbench when just designing new builds.

Share this post


Link to post
Share on other sites
4 hours ago, UltraViolet said:

   Now it would be great if there was a little more documentation on how to customize the operating modes/parameters.  I can see some of the things that are easy to edit in the code, 

 

Hi

First of all: thanks for all your interest.

The program was first realeased at Github, with the intention to improve it and keep it "alive", but found no interest at all.
Then it was distributed in some german communities, but always in it's original form.

The idea of the program and it's unusual structure was:
- sepearting functions into dedicated routines (as in libs)
- make behaviour adjustable by user via paramters
- place them all at the beginning of the code

- present them in a way, that no "python-programming" is needed to change them.

So new programs with other functions (like my battery box or pendular train) could be easily written by only defining "function1" and the paramtes, all other is always the same, including the main loop ;-)

If someone  likes to discuss this here and improof the code (more classes instad of function, handling of remote etc), i'd like to offer same ideas ;-)

 

Find here the Original description:

Parameter und ihre Funktion:

Profile

Das Programm hat zwei Geschwindigkeitsprofile, mit denen in erste Linie der Grad der Beschleunigung eingestellt wird
Profil_A = (5,75,10,100)     #min,max,step,acc
Profil_B = (5,75,5,200)      #min,max,step,acc
Die Motoren starten bei „min“ und laufen nicht schnell als „max“, hier also zwischen 5 und 75. Weniger als 5 ist nicht sinnvoll, 100 ist der maximale Wert.
Der dritte Wert „step“ besagt um wie viele Stufen sich die Geschwindigkeit ändern soll, wenn man die Taste kurz drückt. Im ersten Fall also um 10  von 5 auf 15, 25 usw., im zweiten um 5 von 5 auf 10,15,20 usw. 
Der vierte Wert „acc“ gibt die Zeit an, die zwischen den Werten liegt, im ersten Fall 100 Millisekunden, im zweiten 200 ms.
Das Profil B beschleunigt also in kleineren Schritten, die länger auseinanderliegen, das beschleunigt insgesamt langsamer.

Drehrichtung 

Die Drehrichtung kann für die Motoren an A und B getrennt eingestellt werden, Werte sind 1 und -1
dirMotorA = 1                  # Direction 1 or -1
dirMotorB = -1                 # Direction 1 or -1

Automatische Beschleunigung

autoacc = True beschleunigt automatisch weiter, wenn man die Taste gedrückt hält, bis zum Maximalwert. Mit „False“ wird immer nur bei einem erneuten Tastendruck die nächste Stufe erreicht

Knöpfe

Das Programm hat vier Funktionen, die hier den Knöpfen der Fernbedienung zugeordnet werden.
„UP” -> Schneller
„DOWN” -> Langsamer
„“STOP” -> Stop
„SWITCH” -> Umschalten zwischen Profil A und B
Und die Fernbedienung hat sieben Knöpfe:
A+","A-","A0","B+","B-","B0","CENTER" (der grüne)

Das kann man dann so zuordnen:
UP = "A+"
DOWN = "A-"
STOP = "A0"
SWITCH = "CENTER"
Oder auch
UP = "B+"
DOWN = "B-"
STOP = "A0"
SWITCH = "A-"
Oder beliebig anders

LED der Fernbedienung

Einstellen der Farbe und Helligkeit der LED, jeweils bei Profil A oder B
Dazu jeweils die FARBE ändern, dann kommt der Stern, und dann die Helligkeit zwischen 0 und 1
LED_A = Color.GREEN*0.05       # Remote Profil_A, color * brightness
LED_B = Color.RED*0.05         # Remote Profil_B, color * brightness
Das „= Color.“ muss so stehen bleiben.
Gültige Farben (Großschreibung beachten!)
RED
ORANGE
YELLOW
GREEN
CYAN
BLUE
VIOLET
MAGENTA

Watchdog

Bei „True“ stoppen die Motoren, wenn die Fernbedienung den Kontakt verliert, bei „False“ laufen sie weiter, bis die Fernbedienung erneut Kontakt hat (erneut Knopf an der Fernbedienung drücken). 
watchdog = False    # "True" or "False": Stop motors when loosing remote connection
Timeout

Gibt an, wie viele Sekunden nach dem Starten des Programmes auf eine Verbindung zur Fernbedienung gewartet wird, wird die Zeit überschritten geht das Hub aus.
remoteTimeout =5    # hub waits x seconds for remote connect after starting hub

Nur eine bestimmte Fernbedienung verbinden

Wenn ein Wert angeben wird, kann sich nur eine Fernbedienung mit diesem Namen verbinden (der Name kann z.B. über die Powered Up App geändert werden) 
remoteName = ""     # connect this remote only, "" connects any remote

LED des Hub

LEDconn gilt, wenn die Fernbedienung verbunden ist, LEDnotconn, wenn sie nicht verbunden ist, z.B. auch beim Starten des Programms.

LEDconn = Color.GREEN*0.3       # if Hub connected, color * brightness
LEDnotconn = Color.RED*0.5      # if Hub is not connect, color * brightness



 

Edited by Lok24

Share this post


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

  I am ecstatic to be able to report that this is working for me!  For whatever reason, I could never get the remote to connect to it prior to this version.  I swapped out the .py file from the 3.1.0 zip file with this patched code, and then it just immediately worked as it was originally supposed to.  I used the 3.3.0b8 version of Pybricks UI for the firmware write.

 

You'll find, that with the 3.1.0 firmware, the remote won't reconnect though, once it looses it's connection.

The modified script, you can load just regularly using PyBricks into the current firmware and it works now. Including loosing connection and reconnecting.

/M

Share this post


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

The program was first realeased at Github, with the intention to improve it and keep it "alive", but found no interest at all.

Can you provide a link to the github repository ? I didn't see it linked anywhere.

/M

Share this post


Link to post
Share on other sites

Yes, I removed it some time ago, cause it didn't work any more and there was no feedback.

Share this post


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

Yes, I removed it some time ago, cause it didn't work any more and there was no feedback.

There might be interest though (at least from people including myself). But sometimes people have other priorities. Mine include getting my PhD (part-time next to teaching) and building a new LEGO room. Still very valuable work @Lok24. Last week I was for my work at the inaugural lecture of a colleague at some other university and students were using the Py to control all kinds of LEGO hubs.

Share this post


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

Yes, I removed it some time ago, cause it didn't work any more and there was no feedback.

That sounds counter productive, to remove the script from github again, just because there is no feedback. I would have thought, that that is the best way to distribute it and also to gather all bugs/bugfixes/suggestions in one place, instead of having it across a bunch of forums and downloadable from a random webserver as zip file.

Either way, thanks for developing the script in the first place and luckily it has been preserved within the community, so we still can use it.

/M

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.