Sequential Logic 3: State machines
- Nick
- Feb 3
- 2 min read
I do not pretend to be the best with state machines or digital logic. Our electrical engineering and computer science buddies will always beat me there. However, I have picked up a few tricks from them over the years.
State machines are systems have a finite number of steps, which are followed in a sequence. Systems you write should have them and systems you work on only might have them. Imagine you have a filter that you need to backwash. You want to start by (1) closing the influent, (2) draining the level down, (3) running water backwards, (4) waiting until the water is clean, (5), stop running water backwards, (6) waste a little water to clean leftovers out and (7) return to normal. For each step, you need actions to do and conditions to stop at. You would not want to re-open in influent at step 3.
<Quick refresher> The IEC 61131-3 languages are...
Structured Text (ST)
Sequential Function Charts (SFC)
Ladder Logic Diagram (LD)
Function Block Diagram (FBD)
Instruction List (IL)
If you are lost, check out this article from Solis. They have an excellent blog article on this language types and a great website in general. </Quick refresher>
What happens without a state machine sequence? I am removing a PLC that appears to have only structured text and ladder, because the original programmer did not like sequential function charts. This program runs a series of basins, each of which has pumps, valves and augers on timers. They should run in a particular sequence, but have nothing defined in a SFC.
The code looks like this...
If edge (basinMethod == 1024) and condition1 and condition2 and condition3 then
condition3 = true
condition1 = false
turn on pump1
nested IF for pump1 run timer
endif;
if edge (basinMethod ==1024) and edge (condition3) then...
It's 30 years later, and I need to make some major modifications to the code. I go ahead and have to forced-compile download (yes, it's that old). What happens? Well, the basin sequence does not immediately start. Why did that happen?
Well, without an SFC, there is no starting location. There is nothing to set the system up and enter one of the run conditions. We had to trick the PLC into getting into a valid condition for the basins to run.
I literally had to click around with the operators until the system kicked back on. It was horrible. And will remain horrible, until this hideous code integration is completed, or I figure out how to trick it into starting up in the sequence.
Comments