What I want to do in the code?
I want to create a function block that is made up out of 4 methods: start, stop, run, calculate. Start method will call a run method that has an while loop that periodically calls calculate method. The while loop inside the run method will end only if the stop method is called.
What I have tried already:
- When I call the
object.start()I want it to start a cycle that will go as long as I don't call thestopmethod.
timer(IN := triggerTimer, PT := T#0.1S);
trigger(CLK := timer.Q);
workingFlag := TRUE;
run();
- The cycle will be in the
runmethod. Cycle will consist of a while loop conditioned viaworkingFlagvariable. WhenworkingFlagisTruethe the while loop will constantly trigger a timer structure which will every 0.1S call thecalculatemethod.
WHILE workingFlag = TRUE DO
triggerTimer := TRUE; //Start timer
IF trigger.Q THEN //If timer expired execute code below ...
calculate();
triggerTimer := FALSE; //Reset the timer
END_IF;
END_WHILE
- Finally the
stopmethod will just set theworkingFlagtoFalseand theoretically it would end the cycle inrunmethod.
workingFlag := FALSE;
What is the problem?
- After I call the
object.start()my whole PC crashes. Therefor ... I think something is horribly wrong with my code (:
What I want to achieve with this?
- The object will be a PID controller. And I want in the main
programjust call thestartmethod when I want it to regulate andstopwhen I need it to shut down. - To this day I was calling manually the
calculatemethod inside my main program with thetimerthat you can find in therunmethod above. - My problem with this approach was that when I had more PID's (or another functions I needed to call periodically) the code got messy really quick.
- Therefor I wanted to create a function block that would have local
timersand would be managing the periodical calling by itself.
So please any suggestions how to approach this problem?
Jouke already told you that
WHILEis the reason. Here is your code example refactored. This is how it should be done if you want 100 milliseconds impulse work.But remember that your main PLC cycle should not be longer than 100ms.
Every PID function block has
Enableinput parameter. You can use that to start and stop PID.Just set you
Startvariable to PID function block input, that is it.