First time user and non english speaker here. Please be kind.
I'm trying to write a FB that control a pump which as a sensor on the motor axis to monitor its movement.
I've written the code so that during init of the FB I can reference to the hardware I/O.
FUNCTION_BLOCK PumpControl
VAR
sensor : REFERENCE TO BOOL;
motor : REFERENCE TO BOOL;
END_VAR
Now I have to implement another FB that control a group of three pump
FUNCTION_BLOCK TankControl
VAR
pump1 : REFERENCE TO PumpControl;
pump2 : REFERENCE TO PumpControl;
pump3 : REFERENCE TO PumpControl;
END_VAR
In my main I create the pumps, the tank and all the variables
PROGRAM MAIN
VAR
input1 AT %I* : BOOL;
output1 AT %Q* : BOOL;
pumpH2O : PumpControl(sensor := input1, motor := output1);
input2 AT %I* : BOOL;
output2 AT %Q* : BOOL;
pumpS1 : PumpControl(sensor := input2, motor := output2);
input3 AT %I* : BOOL;
output3 AT %Q* : BOOL;
pumpS2 : PumpControl(sensor := input2, motor := output2);
mainTank : TankControl(pump1 := pumpH2O, pump2 := pumpS1, pump3 := pumpS2);
END_VAR
If I'm right I have to create properties for all the referenced variables to make it work.
In the MAIN code I want to call mainTank() to perform all the action involved.
At the moment I have no access to the hardware and cannot try it myself.
Is this going to work?
this is an interesting approach. Is there a specific reason for the REFERENCE variables? They can be dangerous when they get called without being valid (you should always use an if statement and the __ISVALIDREF(reference) command to avoid stopping the program)
If you're not dead set on using references I have another suggestion for you:
looks like there is a tank with three pumps for three different types of liquids.
I would make an instance of only the tank in MAIN. This makes the program much easier to read and encapsulates as much as possible:
Then in FB_TankControl I would make one instance of FB_PumpControl for each Pump:
The FB_PumpControl will actually look a lot like yours, except that the hardware references are made here. This way you only program this portion out once. Don't worry, since the pumps are instantiated three times, they will all be visible for linking in the PLC instance:
And dont't forget to call the FBs in the actual code (fbTank in MAIN and the three fbPumps in fbTank). Obviously you will still have to implement the actual control code for your case.
Good luck! hope this helps...