Stux Posted November 2, 2020 Posted November 2, 2020 (edited) After playing with the kids trains, and trying to implement some logic, I quickly grew tired of not being able to implement re-usable logic blocks, until I found a way to have one block able to affect another, and have those blocks be re-usable for different trains. The trick was to figure out a way to specify variable references for composite blocks, so that the composite blocks could both read and write to the variables, to allow input/output to a block, and one block to affect another block in the same program This allows implementing re-usable control blocks which can be used to simply implement relatively complicated lego train control and automation. The attached flickr album contains screenshots of the blocks (handset control, motor control, and colour based speed change, as well as an automatic reverser and a station routine loops (stop, reverse if necessary, then pickup passengers and continue). The kids enjoy it, and I enjoyed playing with their trains ;) Here's a video demonstration of the train control program running, with last nights kids layout. The green loco is controlled independently of the yellow one, which is automated, but you can override the automation... with inevitable results. Video of Layout Running Flickr Album: Motor Controller This is the motor control subroutine First parameter is the motor port to control, 2nd and 3rd parameters are the velocity and direction global variables. I use "v" and "d" globals for the first train, "w" and "e" for the second train, and so on. The routine needs to be run in a loop. v is a value between 0..100, where zero results in motor braking, and d is 1 or -1, indicating reverse. This can easily be modified to run a pair of motors, one in reverse, etc. Or you could run two separate loops with the same globals for chaining trains. Handset Controller This is the handset control block. 1st parameter is the handset port to read, 2nd and 3rd parameters are the relevant velocity and direction global references. Implements emergency stop, and will correctly adjust speed up/down as you press +/- (or hold). Will snap to zero when below 30, and will clamp to 100. Needs to be run in a loop, and if a motor control block is also running with the same variables, then the motor will respond to the handset port. In theory you should be able to have multiple handsets controlling the same variables, for multi-person train control. The v > 40 block is used to play a braking sound, but because playing the sound will prevent the block from terminating, in order to correctly implement you need to use another global variable as a flag, which is then picked up in the main program. 0.15s wait at the end controls how fast the speed increments when held down, and seemed to give the right speed response to me. Colour Speed Change This is a colour based speed change block. By putting plates of the desired colour in the track (10 studs long for reliability) you can automatically change the speed of the train, for example, have green before a long straight to speed up, and then white/yellow etc before a curve/station to slow the train down. First parameter is the sensor port to read. The sensor would be integrated into a train. 2nd parameter is the colour to detect. 3rd parameter is the velocity global reference for the train in question, and 4th parameter is the speed to apply when the colour is detected. One loop running this block is needed for each train that you want to obey the "speed limit", and the speed limit can be different per train. The kids love being able to move these plates about. When created, the various blocks look like this in the palette The blank ports are hub/port specifiers, the suitcases represent global variables, you specify a global as letter/symbol, and they *must* be globals, not locals, and the paintbrush is a colour. Negation Operator The negation operator is only present if you are using the various controller layouts, so here is an implementation of it, and also demonstrates how to implement math operations, use the tool icon on the icon to change the block type. The full control program Initialised globals v,d for train 1, and w,e for train 2, sets "s" to be train 1s colour sensor. Starts routines 1,2,3,4,7, which are colour detection routines. Also, starts two handset routines which read the a/b ports of the handset and configure train 2 and 1's speed/direction respectively. The motor control routines are started and apply train 1 and 2s speed/direction respectively. There are 5 colour detection routines setup for sensor "s", white/yellow/green are speed changes, red is a reverser, and blue is a station routine. The station routine will stop at a station, ie a blue plate, and will back up if it overshoots. After a brief period (picking up passengers) the train continues. Brake and arrival sounds are played. If yellow plates are used it will help the train stop without overshooting! I haven't figured out how to put the reverser logic into a composite block successfuly, and haven't tried to put the station routine into a block... yet... the global i/o is a bit finicky in PUp. Gee, I wish I could duplicate composite blocks, and I wish I could label them... and variables... and I wish I could import/export/duplicate blocks and programs! Edited November 2, 2020 by Stux Quote
Lok24 Posted November 2, 2020 Posted November 2, 2020 On 11/2/2020 at 12:45 AM, Stux said: Gee, I wish I could duplicate composite blocks, and I wish I could label them... and variables... and I wish I could import/export/duplicate blocks and programs! Expand With an android device this is possible, you find them in a seperate folder. Quote
Stux Posted November 2, 2020 Author Posted November 2, 2020 On 11/2/2020 at 10:35 AM, Lok24 said: With an android device this is possible, you find them in a seperate folder. Expand Not sure this is possible on an iPad. I was thinking the app could support other actions on the composite blocks, like how the delete button is, but perhaps a “...” button Quote
Lok24 Posted November 3, 2020 Posted November 3, 2020 On 11/2/2020 at 9:41 PM, Stux said: Not sure this is possible on an iPad. Expand No. As I wrote: on Android devices Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.