Counters are not boring!

Don't know how to use a machine? Looking for efficient setups? Stuck in a mission?
piriform
Fast Inserter
Fast Inserter
Posts: 117
Joined: Mon Jan 25, 2016 10:02 pm
Contact:

Counters are not boring!

Post by piriform »

Counters are Boring?

Most people who have taken the time to read the wonderful Combinators 101 have realized that combinator counters are handy, useful tools that have a place in their bag of er, tools. Timers, delays, clocks, gates ,samplers, etc are easy to construct and make many types of circuits viable.
But let's face it, the counters themselves are not really all that exciting. Same old: 1,2,3.....,1,2,3. Even a picture of a counter (1st) is rather bland.

Oh really?
hmm.jpg
hmm.jpg (34.98 KiB) Viewed 3563 times
Did you know you can get two sets of numbers from a single counter at the SAME time?
Look at the first picture closely. There I am, staring at a simple counter, scratching my head because both lamps are set to Blue = 0. One is lit, but the other? What the h... is going on? BTW that is not a bug, but a FEATURE! One can get two sequences (0...N-1, and 1..N) at the SAME time (but different wires).


I'm almost certain that many (OK, maybe some) have wandered on how to go about generating an exciting sequence of numbers like: 1, 4 ,9,10,16,27,43,67, 103,137....
Or ever had a thought: "How can I get my (insert resource) under control? This bang,bang controller is too slow and results in overshoots.
Or contemplated writing a nasty post to the designers, complaining that pumps are way too fast and there is no way to slow them down.

Well these are all valid questions and what follows are, if not the answers, at least hints on how to answer them.
po2counter.jpg
po2counter.jpg (40.72 KiB) Viewed 3563 times
If you look at the second picture you may recognize a counter. The difference is that a wire from the output to the
input was replaced with two combinators. One multiplies the output by 2 and the second subtracts 1. Why complicate a simple counter with additional combinators?
You'd do it if you want/need the following sequence: 1,2,4,8,16,32,64,128....

You could also generate seemingly weird sequences (1,4,9,10..), by placing two arithmetic combinators in parallel and feeding the output back to the input.
Admittedly, this is a rather niche application, but now that you know it's possible, think of it as another tool.(hmm... binary sequence generator connected to a binary decoder,would give me a bit-wave and..)
register.jpg
register.jpg (40.41 KiB) Viewed 3563 times
Well that was all very exciting and all, but now let's look at something (that looks) simple (picture 3).
Yes, it's a register. It's also a counter. If you don't believe me, just give it a signal that lasts more than one tick.
It'll count all right, and make your life miserable while you are chasing down the stupid bug.

The only difference between this and a standard counter is that a standard counter is usually set to Signal < N, while this one has Pistol = 0. Whereas standard counter resets whenever the count reaches N, this one must be reset by sending the Pistol (either by separate green wire or inline-just shoot the damn thang!).
In the meantime, this register/counter will happily count whatever is in its input.
An intriguing possibility arises if we intentionally let this register/counter count at set intervals without resets. The counter will then accumulate input values which when multiplied by the intervals should generate .....
Sigma X delta t = Oh!

But enough of this abstract stuff. Factorio is all about controlling one's factory. After all that's what combinators are intended for.
bangbang.jpg
bangbang.jpg (61.69 KiB) Viewed 3563 times
Picture 4 shows a simplified but hopefully valid scenario.
-Two liquid storage tanks (A,B) connected by two pumps.
-The upper pump transfers liquid from B to A and the lower from A to B.The upper pump may represent consumption by other parts of the factory and is activated at random intervals by a PRNG.

-The lower pump tries to keep the two tanks balanced (i.e. same fluid level in each). Lower pump is controlled by
three deciders. Two of them simply generate signals A & B (representing fluid levels in A & B tanks), and the third
(in the middle) generates error signal E = A-B. Whenever E > 0, the lower pump is activated. This bang-bang type of control works reasonably well in many cases, but has the tendency to overshoot.

A Proportional type of control (P-type) may be better in some cases. Whereas Bang-Bang is always fully on or fully off, P-type is more nuanced. When the error (i.e. E) is large, P-type will be fully on. When E is small, P-type will reduce it's output accordingly.
p_type.jpg
p_type.jpg (70.22 KiB) Viewed 3563 times
This picture shows such a controller.
The upper yellow rectangle has two combinators. A counter (on the left) counts 1..12. Decider to the right compares the count with E (error) signal. Whenever E is greater than the current count,the pump is activated. In other words, depending on E's value, the pump will be activated for a portion of a 12 tick cycle. Some of you may recognize this as a crude form of Pulse Width Modulation or PWM. (This technique can be used by itself to slow down a devices)

The lower yellow rectangle has three arithmetic combinators that scale E (error) and optionally add an offset. E can be multiplied by C , divided by D and added to or subtracted by F. All of those are contained in the constant combinator to the right. Why do this? By scaling up the original E, controller will be made more "aggressive" or faster reacting ( until it start oscillating of course). By decreasing E the controller's response will be slowed down.
Final word. All p-type controllers must be "tuned" for an optimal performance. The "tuning" is the process of
setting the scaling factor to an appropriate value which is unique to every situation. But that's another topic (calling all Control Engineers).

I hope that this post has been of some help. If you noticed any errors or omissions, have any questions or would
like to see more , please let me know.
Post Reply

Return to “Gameplay Help”