My first stab at a programmable factory

This board is to show, discuss and archive useful combinator- and logic-creations.
Smart triggering, counters and sensors, useful circuitry, switching as an art :), computers.
Please provide if possible always a blueprint of your creation.
Post Reply
User avatar
MadOverlord
Burner Inserter
Burner Inserter
Posts: 8
Joined: Tue Dec 13, 2016 6:37 pm
Contact:

My first stab at a programmable factory

Post by MadOverlord »

This was inspired by the many tutorials and examples out there that I stumbled across when I first started playing a couple of weeks ago. I apologize for not keeping track of the links so I can give proper credit to those whose cleverness was so helpful -- and I would greatly appreciate any suggestions for improvement from more experienced builders (ie: everyone reading this post)

So, for my first logic project, I decided to build a programmable factory that would maintain stock levels for multiple items. The factory is controlled by a single constant combinator that contains the minimum stock levels for the items; as soon as more items are needed, they (and their subcomponents) are automatically constructed and placed in chests. If a red signal is present, the factory pauses.

The end result looks like this:

Image

The inner loop contains all the feedSTOCK; these are items produced outside the factory -- things like plates, and also items that require liquids to manufacture.

The outer loop moves all the assembled components and subcomponents around.

The core computational logic figures out what needs to be done:

Image

In the hazard concrete is the ORDER chest, which contains the stock levels you want to maintain.

Coming up from the south is the INVENTORY, the sum of the contents of all holding chests.

A number of additional intermediate signals are computed; all of them are in the form MIN(A-B), so negative values don't propagate. EXTRA is the number of extra items in the boxes above our stocking levels. BUILD is how many items we need to build in order to meet our stocking requirements. NEED (coming up from the south) is what items we need in order to BUILD the items; it is computed by a recipe lattice (see below). Some of the NEEDed items may be in EXTRA inventory, so we may need to MAKE less than we NEED. If so, then we compute what items to EMIT from the boxes. Finally, we keep track of what items are on the outer "manufactured items" belt (more below) and use it to adjust our MAKE downwards and figure out how many items we need to ASSEMble; otherwise we would overproduce a bit because we wouldn't get updated on newly made items until they went around the loop and ended up back in the boxes.

ASSEM is sent off to control the inserters feeding the assemblers; EMIT controls inserters removing items from the chests.

The recipe lattice:

Image

Note: this and some of the other images are from a prior version that didn't track the contents of the belt.

The lattice is pretty simple; for every item it has one or more combinators that compute the materials needed. In the case of items like copper wire where a single bit of raw material generates multiple items, 1 is added to the quantity needed before dividing, so it rounds up. The lattice loops back on itself, so that requirements from higher-level recipes propagate downwards until everything settles on a build order.

The belt counter:

Image

Every inserter that places items on the belt is connected to a latch that maintains a running count of every placement. Similarly, every inserter that removes items is connected to a latch that keeps track of them. They report pulses. The removal circuit has the extra complexity that it also has to hold the commands to the inserters, and these need to be removed before going into the latch. There is an extra null comparator on the ASSEM input in order to balance the gate delays on the two legs of the circuit that end in the latch. The two latches are subtracted to result in a final BELT inventory.

The latches end up counting all the operations, so they could eventually overflow. It's my Factorio Y2K bug. :)

The inventory chests have filter inserters that grab everything they can. If a chest is commanded by EMIT to dump items back onto the belt, it puts them on a sub-belt and then another inserter moves them to the main belt; it is this inserter that does the counting. This is needed to keep the circuits separated.

The STOCK market:

Image

This area maintains the supply of STOCK materials in the inner loop. The two rail wagons are used as sorters, each for 4 STOCK materials; they are dumped into it by multiple inserters, but extracted by a filter inserter associated with each stock chest. If the quantity in a chest goes below 200, the inserters at the far south dump the appropriate materials into the serpentine belt that puts them into the appropriate wagon.

All unused materials coming around the inner loop are put back into the wagons for restocking.

At the top of the STOCK market is the order computation logic. It gets the MAKE signals, and filters out just the STOCK items. If it turns out the resulting signal is empty, it replaces it with an "All items" signal.

This is then sent to the rate-limiter, which dumps out the required items, but waits after each cycle for the deposit area to clear; this prevents things from clogging up.

The end result is that if nothing is being built, the inner belt contains an even mix of all the STOCK items, but as soon as the factory starts building, the mix will change to just the items needed at any point in the build; this means that the assemblers on average will spend less time waiting for their inputs.

I look forward to your feedback; I am sure there are many simpler ways to implement these functions!
Blueprint

User avatar
Gertibrumm
Fast Inserter
Fast Inserter
Posts: 162
Joined: Fri Jun 03, 2016 6:54 pm
Contact:

Re: My first stab at a programmable factory

Post by Gertibrumm »

This is nice, I consider the factory done.
It has high combinator count but that could be necessary in your case.
I guess your second stab will be on a facory which that sets the recipes of assemblers?

User avatar
MadOverlord
Burner Inserter
Burner Inserter
Posts: 8
Joined: Tue Dec 13, 2016 6:37 pm
Contact:

Re: My first stab at a programmable factory

Post by MadOverlord »

Gertibrumm wrote:This is nice, I consider the factory done.
It has high combinator count but that could be necessary in your case.
I guess your second stab will be on a facory which that sets the recipes of assemblers?
I prioritized clarity over combinator count. I am sure there are ways to reduce it considerably but I was more concerned with getting each part working correctly. I'm still not happy with what I had to do in order to get the belt counting working, it's overly complex and I want to revisit it at some point.

As far as I know, currently you can't change the assembler recipe programmatically. If this becomes possible, then I'd certainly want to do that. If I am laboring under a misconception, please let me know.

Yoyobuae
Filter Inserter
Filter Inserter
Posts: 499
Joined: Fri Nov 04, 2016 11:04 pm
Contact:

Re: My first stab at a programmable factory

Post by Yoyobuae »

MadOverlord wrote:As far as I know, currently you can't change the assembler recipe programmatically. If this becomes possible, then I'd certainly want to do that. If I am laboring under a misconception, please let me know.
Probably referring to mods like:
https://mods.factorio.com/mods/theRusty ... combinator

I made this using it:
https://youtu.be/cGfAFxHplnE

The issue with changing recipe programmatically is that an inserter may pickup an item, recipe changes, and now inserter cannot put the item into the assembly machine (because new recipe does not use that particular item). So the whole thing can jam.

The (rather big) circuit that I made specifically works around those issues by implementing sufficient delays between turning off input inserters, recipe switching, and reenabling input inserters.

You may have run into a similar issue with furnaces which try to make steel (maybe because output inserter is backward) but inserters are stuck trying to put iron ore in. That happens because furnaces CAN change recipe automatically.

User avatar
Gertibrumm
Fast Inserter
Fast Inserter
Posts: 162
Joined: Fri Jun 03, 2016 6:54 pm
Contact:

Re: My first stab at a programmable factory

Post by Gertibrumm »

the ultimate solution to inserter jamming is count accuracy!
its is the most elegant solution and almost a must for any demand factory/programmable factory

once your count accurate products are finished you can switch recipe. That is where it gets complicated with combinators, but CPUs can help (repetitive system with endless variation = perfect for a CPU)

Yoyobuae
Filter Inserter
Filter Inserter
Posts: 499
Joined: Fri Nov 04, 2016 11:04 pm
Contact:

Re: My first stab at a programmable factory

Post by Yoyobuae »

Gertibrumm wrote:the ultimate solution to inserter jamming is count accuracy!
its is the most elegant solution and almost a must for any demand factory/programmable factory
You know those videos about real life factories, about how things happen so quickly, accurately, reliably. Sometimes I stop to think what it would take to accomplish such things. My conclusion: massive amounts of engineering!

Real life is anything but deterministic/simple. So every variable needs to be control, every variation accounted for. It's work, a lot of it.

Achieving count + time accuracy in Factorio is possible (and a bit easier than on real life) but it still a lot of work.
Gertibrumm wrote:once your count accurate products are finished you can switch recipe. That is where it gets complicated with combinators, but CPUs can help (repetitive system with endless variation = perfect for a CPU)
I personally have my doubts about using CPUs for these kind of things. Interfacing with a live factory is going to be as much, if not more work than making the CPU in the first place. Also I'm not sure any program that can run will be fast enough to deal with potentially tight timing requirements.

I prefer to use (relatively) simpler stuff. In the above video I used a state machine basically. Now I'm working on a furnace which is based off a shift register (easy configurable delays up to 34.67 seconds).

I personally feel like Factorio combinators are more like the early era of digital circuits, when logical gates, multiplexers, latches and such were used.

If someone makes an actual CPU controlled factory then that will prove me wrong, though. ;)

User avatar
hansinator
Fast Inserter
Fast Inserter
Posts: 160
Joined: Sat Sep 10, 2016 10:42 pm
Contact:

Re: My first stab at a programmable factory

Post by hansinator »

I have a belt-based smart furnace running without problems. What I'm doing is accurate accounting of input material and perfect timing, as gertibrumm suggested. It is possible but I've been working on it for weeks. I have developed a couple of separate "functions" to accomplish the tasks - these could be re-used for assembly machines and that would reduce the amount of work needed. I'll post the stuff in XKnights smart furnace thread when I find time for it.

@Yoyobuae
The problem with factorio is that there are many things where you can not control the variables precisely due to the lame game mechanics. It is very hard and sometimes impossible to control item flow down to a single item. One can barely control which path a single item takes in a splitter. You can not read the contents of a train (which is something I am battling with to build an automated supply train that is better than this one: viewtopic.php?f=193&t=34277). Then you can not exactly know how many items are in your logistics system. The roboport provides a readout but that does not include items that are in transit. You can not control how many items are delivered to a requester chest - it often ends up with extra items on a random basis. Not knowing the state of things exactly is the same as randomness: it will yield unpredictable results. This makes precise control impossible. And for me it destroys the fun. It also degrades the whole circuit logic thing to a toy that is mostly useful for making movie players and other non-factory-related gimmicks.

BTW: I have seen CPU controlled factories before in this forum. As far as I remember gertibrumm did try one (search for a thread named automated sequencer factory). As soon as I got all sub-modules for my mega factory working I'll probably add a CPU to run it. However, given the problems the game mechanics pose I believe it will take me months of work to get to that point :(
Yoyobuae wrote:I personally feel like Factorio combinators are more like the early era of digital circuits, when logical gates, multiplexers, latches and such were used.
These things are still used today and form the basis of all digital circuits. As an FPGA developer I use them almost every day ;)

Yoyobuae
Filter Inserter
Filter Inserter
Posts: 499
Joined: Fri Nov 04, 2016 11:04 pm
Contact:

Re: My first stab at a programmable factory

Post by Yoyobuae »

hansinator wrote:I have a belt-based smart furnace running without problems. What I'm doing is accurate accounting of input material and perfect timing, as gertibrumm suggested. It is possible but I've been working on it for weeks. I have developed a couple of separate "functions" to accomplish the tasks - these could be re-used for assembly machines and that would reduce the amount of work needed. I'll post the stuff in XKnights smart furnace thread when I find time for it.
Definitely looking forward to seeing it. :)
hansinator wrote:
Yoyobuae wrote:I personally feel like Factorio combinators are more like the early era of digital circuits, when logical gates, multiplexers, latches and such were used.
These things are still used today and form the basis of all digital circuits. As an FPGA developer I use them almost every day ;)
Yeah, I did some stuff with VHDL and FPGAs back at university. Used an FPGA to digitally generate a TV signal (NTSC). Real fun stuff! :D

But these days the CPUs get all the attention from the public. :lol:

KingKacke
Burner Inserter
Burner Inserter
Posts: 5
Joined: Thu Nov 03, 2016 6:33 pm
Contact:

Re: My first stab at a programmable factory

Post by KingKacke »

Hey,
i want to build something like this but i am completely new in combinator stuff. Can anybody recommend a good step by step tutorial for this.
I still stuck with latches and such stuff.
Best would be in german but i think english is better here right?

Edit: i tried to work with your blueprint bit i get an error every time i try to put it on a blueprint ingame.

User avatar
MadOverlord
Burner Inserter
Burner Inserter
Posts: 8
Joined: Tue Dec 13, 2016 6:37 pm
Contact:

Re: My first stab at a programmable factory

Post by MadOverlord »

KingKacke wrote:Edit: i tried to work with your blueprint bit i get an error every time i try to put it on a blueprint ingame.
My guess is that you need to install a mod. The most likely one is the "Text Plates" mod, which I used to label the layout.

The other mods I have installed, which I don't use in the factory AFAICT, are Nixie Tubes, Progressive Running, and of course Blueprint String.

KingKacke
Burner Inserter
Burner Inserter
Posts: 5
Joined: Thu Nov 03, 2016 6:33 pm
Contact:

Re: My first stab at a programmable factory

Post by KingKacke »

Thank you ery much! It was Text Plates.
Now i can see what you have done in detail ;)

User avatar
Gertibrumm
Fast Inserter
Fast Inserter
Posts: 162
Joined: Fri Jun 03, 2016 6:54 pm
Contact:

Re: My first stab at a programmable factory

Post by Gertibrumm »

KingKacke wrote:Hey,
i want to build something like this but i am completely new in combinator stuff. Can anybody recommend a good step by step tutorial for this.
I still stuck with latches and such stuff.
I understand you fully but I guess the best thing to do is look through the "combinator creations" in "show my creations" (start with the old stuff, its pretty cool anyway)
Then start a concrete world (world editor) and play with signals (pulse shortener to 1 tick lenght opens up the "digital" world of factorio)
Keep a goal in mind! Mine was the sequencer factory that I am still improving and finishing up to today! (half a year now)

Things to try:
Accurat counting of items (with memory (1 tick signal processing), arithmetic and filter inserters or belts)
demand calculations
biggest item
build your first demand-factory

Post Reply

Return to “Combinator Creations”