What is the best way to connect physical IOs (from sensors or whatever), to a twincat 3 program that uses OOP?

99 views Asked by At

I am trying to create a program that controls a fish tank. Oxygen, tempaerature, water level, and pump controls. But I am unsure of How to connec the physical inputs from the sensors to the variables of a function block, as it cant be done directly because the FB is used as a base by different objects. For example, FB_Sensor if I have 3 sensors then I cant link a physical input to a variable in FB_Sensor. So, what is the best way to do it?

I thought about creating a GVL that contains a list of variables that are lnked to the physical connections. Then use a method "Update_Values" which takes in the values from the GVL as inputs and updates the value of the varibales within the object i.e. Oxygen_Sensor.

1

There are 1 answers

2
Fred On BEST ANSWER

In your function blocks, declare addresses this way :

input AT %I*: BIT;
output AT %Q*: BIT;

The type can be anything (including structures), I used BIT just as an example.

This tells the compiler "each instance of the function block will have its own mapped hardware addresses, but these addresses are not defined here". Of course, if you were to type in real addresses, all instances of the FB would refer to the same addresses, which is not what you want.

You can then declare several instances of your function block, and the I/O mapping UI of your IDE will show the variables of each instance as distinct mapping sources/targets.

I would strongly recommend doing this rather than having a global variables list along with code to copy values.