H
H
hexelder2014-09-19 14:44:32
Programming
hexelder, 2014-09-19 14:44:32

Are there any extra conditions?

Good afternoon. Help a beginner process control system.
The essence of the question will be clearer on an abstract problem.
Let's say we have an engine that needs to be controlled: switching on - when 3 valves are opened and the temp. indoor 10.0'C; shutdown - if at least one valve closes or the air temperature drops below 5.0'C.
Below are 2 implementation options. Both of them are working, but in someone else's code, the 1st one is more common for me. Why and why are extra conditions needed? Is there an explanation for this and any recommendations?
Option 1

LoopFunc()
{
    if (Valve1.IsOpen && Valve2.IsOpen && Valve3.IsOpen && Sensor.Value >= 10.0)
        Motor.OnCmd = true;

    if (!Valve1.IsOpen || !Valve2.IsOpen || !Valve3.IsOpen || Sensor.Value <= 5.0)
        Motor.OnCmd = false;
}

Option 2
LoopFunc()
{
    if (Sensor.Value >= 10.0)
        Motor.OnCmd = true;

    if (!Valve1.IsOpen || !Valve2.IsOpen || !Valve3.IsOpen || Sensor.Value <= 5.0)
        Motor.OnCmd = false;
}

I suspect that the 2nd option will have problems with multithreading (if it appears of course), but even in this case it seems to me that it is better to modify the code like this (especially if there are a lot of conditions):
// Global variables
bool TmpOnCmd = false;
TSensor Sensor;
TValve Valve1;
TValve Valve2;
TValve Valve3;
TMotor Motor;

LoopFunc()
{
    if (Sensor.Value >= 10.0)
        TmpOnCmd = true;

    if (!Valve1.IsOpen || !Valve2.IsOpen || !Valve3.IsOpen || Sensor.Value <= 5.0)
        TmpOnCmd = false;

    Motor.OnCmd = TmpOnCmd;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xandox, 2014-09-19
@hexelder

From the first option, it is simple to immediately see under what conditions we turn on the motor, under which we turn it off. The only thing is that I would take out the check of klpons in a separate variable, so that I would not deal with copy-paste.

bool IsValvesOpen = Valve1.IsOpen && Valve2.IsOpen && Valve3.IsOpen;
if (IsValvesOpen && Sensor.Value > 10.0) {
     Motor.Cmd = true;
} 
 
if (!IsVelvesOpen || Sensor.Value < 5.0) {
     Motor.Cmd = false;
}

It is also possible in hardcore without if-s (but they can also swear for this (and if the states are saved))
Motor.Cmd = (Valve1.IsOpen && Valve2.IsOpen && Valve3.IsOpen) && (Sensor.Value > 10 || (Motor.Cmd && Sensor.Value >= 5))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question