5. Object block

Object block is a C++ class, it is another option to write program in RDE. An OB is the equivalent of a Funtion Block (FB) in the IEC 61131-3, this mean that an OB have a static memory that is conserved between different calls of the OB. It is dieerent from the concept of a Function (FC).

An OB is composed from a header file (.h) and a source file (.cpp) like like any C++ class, in addition to these classic files, RDE use the obs file to describe the interface of the Object block. In the obs file, public fields and methods are defined.

5.1. OB

5.1.1. Create a new OB

The following animation, Object block creation, show step by step how to create and deploy a new OB. The main steps are shown and explained also in the static images below.

../../_images/newOB.gif

Object block creation

Create new Object block and an instance of it

In rte project, right click and add new Object block. A folder have to be selected for the compiled file, usually /ob. If the folder ob dosen’t exist add it in the flash memory before creating the Oject Block, see section files and folders.

../../_images/ob-new.png

new Object block

Create new Object block. right click in the tab program of an RTE project

Insert the name of th OB class and the description. The description will be shown in the description colounm in the RTE project. Usually this field is brief. Select the Flash folder, usually /ob, where the compiled OB (.obb) will be saved. The check box Automatic generation should checked, otherwise not all files will be generated.

../../_images/ob-new2.png

Write the OB name, select the folder of destination and check at least the first option

In the following image the result of the creation of an OB is shown:

../../_images/ob-new3.png

Object block structure files

After the creation of a new object block we will obtain 4 files:

  • obs : object block interface file
  • h : C++ header
  • cpp : C++ source
  • obb : Object block binary file (compiled file), that can found in the /ob folder in the Flash files.

Fig. Obs, Header and Source show the auto generated files. As we can see the header and the source files have the structure of a classic C++ class with class name, class constructor and destructor.

../../_images/obobs.png

Obs

Auto-generated OBS file
../../_images/obheader.png

Header

Auto-generated C++ header
../../_images/obcpp.png

Source

Auto-generated C++ source

5.1.2. Deploy an OB

As any object oreinted language, a class have to be instantiated before using it. In the configuration tab of an RTE project, right click Object block and add OB Class or OB Instance. A class could have more than one instance. An OB is similar to an FB (Function block) in PLC programming.

../../_images/ob-class.png

OB Class or OB Instance

Add a class than add an instance. In the figure we can see 2 classes : rc_mgv and rc_motorwheel, and one instance of the first class and two instances of the second one
../../_images/ob-instancepar.png

Object block instance parameters.

In the column Value we can initialize the variables. To keep the program easy to read, it is better to initialize OB properties in R3. Note that properties declared as ro (read-only) are not shown here.

5.1.3. OB basics

As any class of an object oriented language, an Object block have methods (functions) and fields (variables). Public methods and fields that can be accessed from an R3 program should be written in the obs file respectively in the methods and properties blocks.

Properties could be only of simple C++ types: BOOL, I8, I16, I32, U8, U16, U32, INT, FLOAT, REAL, CHAR, could not be of struct type.

Note

Properties name should be lower case, capital letters generates compilation errors.

The source file where the code is implemented is written in the block implementation. An OB can be implemented in more than on source file.

../../_images/obs-example.png

Obs example file

OB example that use another OB from the standard library. The code in implemented in 2 source files. Example taken from the predefined examples of RDE.

When an OB inherit from another OB, and we want to ovveride a property or a method the keyword virtual is used in the declaration.

5.1.4. Using an OB in R3

Suppose we have the class obCylinder and its instance cylinder_right. Let’s suppose the OB have the methods opencyl() and closecyl(), and 2 readonly properties cyl_opened and cyl_closed and 2 not readonly properties cmd_open and cmd_close. We can call in R3 the methods as we call them in C++ using the dot operator: cylinder_right.opencyl(). We can access properties using also the dot operator for reading or writing: bool cyl_closed = cylinder_right.cyl_closed or if(cylinder_right.cyl_opened) or cylinder_right.cmd_open= TRUE and cylinder_right.cmd_close = FALSE.

If we defined a structure in the obs file we can use it to define a variable of that type (stucture type) in R3.

5.2. OB Predefined example

In menu file, workspace, specials, predefined examples, we can find the example OB: Use and OB implementation. This example provide the source code an OB, rc_belt, that handle a belt, a rule and task1 implementation.

The Class rc_belt is an OB that can be find in the Object Block library, this OB inherit from the class rc_belt_base. The example use another OB from the standard library, rc_axis, without providing its source code.

Refer to the official Object Block documentation for more informations about OB classes.

../../_images/ob-belt-example.png

OB interface

OB: Use and OB implementation, predefined example

In the obs file of rc_belt, Obs example file, we can see the interface of the Class, how to use another class by importing it, define inputs and outputs and some methods.

Note

Input and outputs deffer only with the keyword ro. When an property is declared as read only behave like an output only like the output of a Function block, otherwise behave like an input-output like an inputoutput of a Function block.

The OB is implemented in two C++ source files. In this OB, 2 classes were defined. The class rc_belt, that inherit from rc_belt_base, and the class RCBelt. The OB main class is the one written in the OB_FACTORY block

OB_FACTORY(rc_belt)
       OB_INSTANCE(rc_belt)
OB_ENDFACTORY