Cosmik42

Control all your Powered Up & Power Function (SBrick) devices with a single software

Recommended Posts

I'm not sure that the battery level is being accurately reported by the hub.

I had a train Great Ball Contraption module that needed to cover a section of track at a pretty small window of speeds, between 1100 and 1300 milliseconds.  Instead of trying to correlate battery level with speed, I used two sensors to time the train and adjusted the speed as needed.

The code below has not been tested.  I am using the Stopwatch for another BAP project I'm working on, so I know it works for this purpose.  But to give you an idea of how I used it for the GBC project:

In the Globals section at the top:

public static System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

Then in the Edit Code section, at the top:

int startSpeed = 35;  // will be adjusted by stopwatch timer.
int maximumTime = 1300;  // milliseconds.
int minimumTime = 1100;  // milliseconds.

Start the train at that initial speed:

Hub[0].SetMotorSpeed("A", startSpeed);

As the train passes the first sensor:

sw.Start();

As the train passes the second sensor:

sw.Stop();

Adjust the speed by 2% if needed:

if ( sw.ElapsedMilliseconds > maximumTime )  // train is going too slow.
{
    Hub[0].SetMotorSpeed("A", Hub[0].GetSpeed("A") + 2);
}
if ( sw.ElapsedMilliseconds < minimumTime );  // train is going too fast.
{
    Hub[0].SetMotorSpeed("A", Hub[0].GetSpeed("A") - 2);
}
sw.Reset();

Obviously, this only works if you have sensors available.

Share this post


Link to post
Share on other sites

I have a new layout, and now at some places with switches en sensors, the train stops only at a sensor when the loc has passed and the first train car is halfway passed. This derails the train when the switch gets adjusted. Before, and also now at other places in the track layout, the train already stopped when the loc was in front of a sensor. I search but as far as I know this is not and adjustable setting. What can make the difference?

 

A solution can be putting the sensor further away from the switch ofcourse. But thats hard with the small PU wires. Two tracks is the limit for me. Which brings me to another thing I noticed. I think I have the same switch motors as Cosmik42, namely this one: https://www.flickr.com/photos/skaako/albums/72157629455967364 However, on a picture in his manual thread, his motor is pointing away from the switch whereas in my case the motors are on the other side. In the curve of the switch, to get left and right oke in BAP. The direction of the motor is not adjustable in the software, is it?

 

(Like before, I'm using the self-driving module)

 

Edited by TuxTown

Share this post


Link to post
Share on other sites
On 3/19/2020 at 6:04 PM, Walt White said:

I'm not sure that the battery level is being accurately reported by the hub.

I had a train Great Ball Contraption module that needed to cover a section of track at a pretty small window of speeds, between 1100 and 1300 milliseconds.  Instead of trying to correlate battery level with speed, I used two sensors to time the train and adjusted the speed as needed.

The code below has not been tested.  I am using the Stopwatch for another BAP project I'm working on, so I know it works for this purpose.  But to give you an idea of how I used it for the GBC project:

In the Globals section at the top:


public static System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

Then in the Edit Code section, at the top:


int startSpeed = 35;  // will be adjusted by stopwatch timer.
int maximumTime = 1300;  // milliseconds.
int minimumTime = 1100;  // milliseconds.

Start the train at that initial speed:


Hub[0].SetMotorSpeed("A", startSpeed);

As the train passes the first sensor:


sw.Start();

As the train passes the second sensor:


sw.Stop();

Adjust the speed by 2% if needed:


if ( sw.ElapsedMilliseconds > maximumTime )  // train is going too slow.
{
    Hub[0].SetMotorSpeed("A", Hub[0].GetSpeed("A") + 2);
}
if ( sw.ElapsedMilliseconds < minimumTime );  // train is going too fast.
{
    Hub[0].SetMotorSpeed("A", Hub[0].GetSpeed("A") - 2);
}
sw.Reset();

Obviously, this only works if you have sensors available.

Thank you for feedback...

I did my first C# codes in the meantime. I was able to write the battery level of the different trains on the mainboard and to adapt the motor speed depending on the battery level. I did just simple tests, no self driving mode.

By doing this I realised that the battery level is not stabil, sometimes it´s a little bit higher then a little bit lower and so on. Okay but in any case it´s possible to set a higher train speed in case of a low battery level and in the opposite way.

But if the train is running in the selve driving mode the program itself sets the motor speed and I should not intervene in this. I think it´s the better idea to do it with the speed coeficient!

Is there any possibility to set the speed coeficient like I did with the motor speed? For example like Hub[0].SetSpeedCoeficient(0.8)?

Share this post


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

It's desinged and running for Windows 10.....

I believe @collectormania meant to control an Arduino based server with the BAP (client) software.

When that is the case: The Arduino based server needs BLE hardware and a BLE software stack and some programming but should be possible.

Best
Thorsten

Share this post


Link to post
Share on other sites

Ups, sorry, missunderstood.

What could be the purpose of such a construction?

Share this post


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

Ups, sorry, missunderstood.

What could be the purpose of such a construction?

Sensors are much cheaper and there's a greater variety of them.

Also removes the silly 2 devices per hub limit imposed on us :(

Share this post


Link to post
Share on other sites

Well, I believe some control their trains with Arduino based on-board hardware, e.g., instead of using old school PF. I believe that was mainly driven by the limited address/channel range PF has (8 and when you use your own software 16). But I never tried that, pure speculation here. Also, Arduino hardware is readily prepared to go RF, which could be another motivation.

Best
Thorsten

Share this post


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

Sensors are much cheaper and there's a greater variety of them.

Also removes the silly 2 devices per hub limit imposed on us :(

I see! Nice idea!

But then the arduino must be able to understand the complete LWP 3.0.0 (which might not be that difficult, I hope)

Share this post


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

I see! Nice idea!

But then the arduino must be able to understand the complete LWP 3.0.0 (which might not be that difficult, I hope)

If you can just push commands to attached devices then you're halfway there.

I imagine it will require some work on both ends though.

Share this post


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

understand the complete LWP 3.0.0

That's the thing. However, in pure train operation, many of the LWP 3 protocol things are not necessary.

Best
Thorsten 

Share this post


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

I believe @collectormania meant to control an Arduino based server with the BAP (client) software.

When that is the case: The Arduino based server needs BLE hardware and a BLE software stack and some programming but should be possible.

Best
Thorsten

yes that's what i want ;-)

i think the lego sensors are too expensive, and the limitation of PU hubs are a problem too

the Arduino sensors are really cheap

we can add a Bluetooth module to an Arduino, so the BAP may be able to connect with the Arduino, no?

i think we have the same if we use :

PU Hub with 2 servos for switches

and

Arduino with Bluetooth controlling 2 servos

i begin in my automation project, i try to find the best solution

the big problem is to know here is each train

i will read again all this topic, but do the BAP control possition of each train, does it really work?

i'm speaking with a man who create apps for train, and he developped great things to contro lthe layout

but he say the problem with lego train is that current Don't come from tracks but from battery, so we may control with an Arduino Inside each train (no problem it is very cheap, a nano Arduino cost 3 euros)

he has an app which work for it, but not possible to control where are the trains...so this is the problem

if the BAP can contro lwhere each train is, then the problem may be solve...

this is the link :

https://arduinorailwaycontrol.com/index.html

 

Share this post


Link to post
Share on other sites

Is there any possibility to adapt the speed coeficient by code? For example simikar like Hub[0].SetSpeedCoeficient(0.8)?

Share this post


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

i will read again all this topic, but do the BAP control possition of each train, does it really work?

I believe BAP has means of knowing in what track section (you are defining) a train is. This is what I understood. But there are many folks here you know better. I believe @Lok24 does a lot of automation as well as many others.

Yes, the BLE stuff and the Arduino nanos are really nice.

I am not sure why you want to use a servo for the switch drives. It is elegant but again maybe too expensive? A plan vanilla motor will also do. This is amply discussed in another thread here on EB.

Have fun!

Best
Thorsten

Share this post


Link to post
Share on other sites

@Toastie

i will control my switches with 4dbrix motors, i already have a few ones

and for the future i will buy the motors without their control box but use Arduino to control the switches. it is a lot less expensive than the complete 4dbrix solution

i Don't understand what you wrote

11 minutes ago, Toastie said:

 A plan vanilla motor will also do.

 

Share this post


Link to post
Share on other sites

I am sorry: What I wanted to say is that any LEGO motor, preferentially a PF-M motor (#8883 - about $9) will do, as you can use a very compact switch drive design. The LEGO servos are much more expensive (about $25). 

But as you are using 4DBrix equipment, it is a different story.

Best
Thorsten 

Share this post


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

ho ok i understand now ;-)

@collectormania :classic: - plus the "plan" should read "plain" - my goodness, that was so badly phrased and written. Sorry again for the confusion.

Now, I believe that when you want to go the Arduino route (located on the engines or driving the switches), then you should find someone capable of programming the BLE stack of the Arduino to comply with the LEGO Wireless Protocol. That could be a rather restricted subset of commands, requests, etc. you need to handle; after all you most probably want to send out motor commands and/or receive sensor readings. Once your Arduino mimics the service(s) etc. of e.g. the LEGO BLE device called "Hub", which has 2 I/O ports, signing up etc. should be possible using built in routines - but I surely don't know.

Thing is this way you would not need to alter things at both ends (Arduino and BAP).

Best
Thorsten 

On 3/19/2020 at 6:04 PM, Walt White said:

I'm not sure that the battery level is being accurately reported by the hub.

@Walt White Bit late to the game.

In any case - I believe that some averaging is a good idea to get a more stable reading - the battery level won't change that fast - without load that is.

With changing load and not fully loaded batteries (or rechargeables) things will turn far less "stable" though as the clamp voltage will become a pronounced function of the load. It can be viewed (in principle only) as an increasing internal resistance of the battery with drainage.

Also, different battery types have different clamp voltage/drawn current responses.

I believe your approach is the only viable here - and perfectly suited for the case!

All the best
Thorsten   

Edited by Toastie
Forgot the @thing

Share this post


Link to post
Share on other sites

@collectormania - If you manage to recode the LEGO protocol, then yes, you could attach any custom hardware and BAP will integrate it. However, I am not aware of any projects who have tried to reimplement this protocol. It is, however, well documented and could be a worty endeavour? If you do throw yourself on this, keep us posted!

Otherwise no, BAP cannot communicate with any other custom protocols and do not support natively 4DBrix switches unfortunately. The lack of BT support from 4DBrix made it harder to integrate in the current BAP stack.

20 hours ago, Didicas said:

Is there any possibility to adapt the speed coeficient by code? For example simikar like Hub[0].SetSpeedCoeficient(0.8)?

You can set it like this : Hub[0].SpeedCoefficient = 1.0f;

Share this post


Link to post
Share on other sites

I implemented the follwing in the global code, so I assume that could work with some script as well:

System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation ="soundfile1.wav";
player.Play();

 

Edited by Lok24

Share this post


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

I implemented the follwing in the global code, so I assume that could work with some script as well:

System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation ="soundfile1.wav";
player.Play();

 

I'd check it now, but don't think I can claim this as work related while working from home :laugh:

Share this post


Link to post
Share on other sites

Some time ago, I asked about using C# Threads for a two train bypass project that would have been relatively easy with each train following its own Thread.  Cosmik42 replied that Threads cannot be used like this in BAP.

I figured out a way to do it with one big loop.  The Globals and Program code are close to 500 lines total and it is hard to follow.  The trick is how to manage the waiting that each train has to do while the other train continues to move and process the colored tiles in the track.

I'm sure a more experienced programmer could do it in a more elegant and concise structure, but at least it seems to work.  Video and some notes are available.

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.