Loaders - rain9441/factorio-tls GitHub Wiki
https://github.com/rain9441/factorio-tls/blob/master/screenshots/06-loader.png
The second major component of TLS are the Loaders. For each type of train that exists in your TLS there needs to be a Loader for that train, which also acts as a home for that train when it is not in use. The Loader is an advanced logistic bot/logic based train loader. While it may seem like filling up a train with some materials is an easy task (Just set up a few filters on the cargo wagon and insert from a requester chest), there are still many efficiencies that can be gained by using a loading system. In this loader I have added some QoL enhancements, buffer minimizations, and configurable payloads.
When a train arrives, the contents of that train is compared to what is configured in the constant combinators. These wires are connected to filter inserters to load the train with whatever is missing. Items that are missing from the train are then requested by the requester chests in exact amounts. Bulk items can be loaded from the southern section of the loader (as per orientation of the screenshot above). These requests are spread across multiple requester chests and loaded into the train with multiple stack inserters. For items that are needed in very large quantities (like rails or logistic bots), dedicated requester chests are appropriate for the task. In the example above, the number of rails that should be on the train are configured in the constant combinator. The total number of rails that are missing from the train is divided by six and sent to each requester chest. An additional 1 unit of rail is also sent to each requester chest to cover for the event in which only 5 rails are missing (5 divided by 6 is 0). The right most stack inserter is configured to set the stack size to the number of missing rails. Inserters left of that are configured to be enabled only at certain thresholds. As less rails are needed, less inserters are active. With all of these pieces in place, we guarantee that the exact number of rails configured on the combinator are placed into the cargo wagon, not a single rail more.
On the norther side of the Loader is a more complex logic based approach to loading. The constant combinator is configured with any number of different types of items to be loaded onto the train. For each item, it is compared to what is currently in the train. If items are still necessary, it will request those items and load them into the train. If the number of items that are still needed is greater than 12, we request that number of items into the left most blue chest. If the number of items that are still needed is less than 12, we request that number of items into the right most blue chest. The left most blue chest inserts 12 items at a time into the cargo wagon, but only if we need more than 12. The right most blue chest has a filter inserter configured to load only 1 item at a time into the cargo wagon, but only if we still need that item. The result of this configuration is that items are loaded in bulk, when possible, by the stack inserter, until there are 12 or less items needed. The filter inserter then takes over and inserts one item at a time into the train until it is filled with the exact number of items configured by the loader.
If there are any excess items in the cargo wagon that shouldn't be there, they are unloaded by a stack filter inserter into an active provider chest.
On the west side of the Loader is the departure logic and status display. Each train is configured to depart when D > 0
(both for loading and unloading). When the contents of the train exactly match the items configured in the constant combinators, the train will be allowed to depart. It will only depart if there is an active train stop summoning it (this is done by the Dispatcher). While the train is loading, the total number of items on the train is compared to the number of items that should be on the train - that percentage is applied to the lights to show you how far the train is from being full. This can be helpful to quickly see if you are low on certain resources.
The train stop is configured to only enable the inserters to insert onto the train if the train is docked in the loading bay. If you park a train in the bay on accident, it won't be filled with random items (It may get loaded with fuel though). To do this, it tricks the train station into thinking that the cargo wagon is full when there is no train. As soon as the train arrives, the new contents of the train are supplied to the logic and it can load it appropriately. Due to the 1 tick delay for each combinator, there was also a need delay some signals by a tick or two to ensure that the inserters did not accidentally load or unload incorrectly.
Also included in the Loader blueprint is a simple rocket fuel / nuclear fuel loader on each locomotive slot. When nuclear fuel is present, the rocket fuel inserters are disabled. This will allow you to seamlessly swap over to nuclear fuel as it becomes available and phase out rocket fuel over time.