1

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?

  • 1
    Have you considered putting the "AT %I*" and "AT %Q*" in your function block? That would avoid having to declare variables in your program, and creating your instances would not require FB_Init arguments. These addresses can be mapped to the hardware too. It think it would be simpler without any downside. – Fred Aug 06 '23 at 20:33
  • FUNCTION_BLOCK PumpControl VAR sensor AT %I* : BOOL; motor AT %Q*: BOOL; END_VAR Are you saying to modify PumpControl this way? Didn't know I can assign different IO to different instances thi way... If so It'a good idea. Thank you. Do my original code will work also? – Paolo Saija Aug 06 '23 at 21:35
  • @PaoloSaija You can test it on your PC, since you only have digital inputs you should have no problems simulating it. Just activate twincat 3 on your development PC or VM, go to online mode and you can set the inputs as you think would happen in real application. – ziga Aug 08 '23 at 18:18

1 Answers1

0

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:

PROGRAM MAIN
VAR
    fbTank          : FB_TankControl;
END_VAR

Then in FB_TankControl I would make one instance of FB_PumpControl for each Pump:

FUNCTION_BLOCK FB_TankControl
VAR
    fbPump_H20      : FB_PumpControl;
    fbPump_S1       : FB_PumpControl;
    fbPump_S2       : FB_PumpControl;
END_VAR

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:

FUNCTION_BLOCK FB_PumpControl
VAR
    bSensor     AT %I* : BOOL;
    bMotor      AT %I* : BOOL;
END_VAR

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...

Foxtrot97
  • 1
  • 2
  • 1
    pretty sure REFERENCE just returns a 0 if not valid, pointers are the dangerous part that would crash the controller. – ziga Aug 16 '23 at 07:48