Global type declarations, Beckhoff TwinCAT

S

Thread Starter

sulfur8

Yup, the last company where I worked I did a lot of OOP especially using thinks like Java, Ruby, C++, etc., and now I am programming in Structured Text. At least it's not LD.

Anyway, the short story is that I/d like to declare a global type so that I don't have to redeclare it in each function block. Something like...

TYPE CONTROL
STRUCT
A: INT;
B: BOOL;
END_STRUCT
END_TYPE

and in Main, declare...

VAR X: CONTROL; END_VAR

while in a function block,

VAR_IN_OUT Z: CONTROL ; END_VAR
...
Z.A := 4;
...

------------------

The idea is to create an object with attached methods. The problem, as I see it with function blocks is that each one represents a single method. But upon aggregation one could create a class with objects from it by instantiating the structure; I can live without inheritance because these codes are quite tame compared to a GUI implementation, for example.

However, this becomes quite difficult without the ability to create a global type declaration. I <i>know</i> it's possible, there are control structures defined in the AB libraries...

Perhaps I'm getting this whole idea quite wrong and should approach it from another direction.

-----------------
 
OK, I see this is done using "actions."

You declare everything within the function block, and then instantiate it, and can call actions. This makes sense but it's a bit awkward, because typically object oriented stuff is written in the sense...
football.kick;
where football is the noun- the object, and kick is the verb. Their technique leaves one to create a default action for the function block, in other words:
football;
does something... but anyway, this works.

What still confuses me about this language is that I'd like to declare a global type. I go into MAIN and put

TYPE ERROR_CODES:
(NO_ERROR, ERROR_1, ERROR_2);
END_TYPE

but I get this crazy compiler error:
ERROR 3780: MAIN(4): 'VAR', 'VAR_INPUT', 'VAR_OUTPUT' or 'VAR_IN_OUT' expected

I mean, it just makes sense to declare types before declaring variables, n'est-ce pas?
 
R

Roger Christopher

Click on the Data Types tab in the left pane of the editor, then define your structures, enums and aliases by right clicking and selecting Create Object

Actions are a bit like methods, but (unfortunately) they do not return a value, so you have to use an output variable of the fb. Bit clumsy, but it works. Roll on TwinCAT 3!

Roger
 
Top