Difference between revisions of "Function Object With C Code"

From Proview Wiki
Jump to: navigation, search
m (Code: - Removed path name to pwr_cvoldesclasses.h)
m (Even more content!)
Line 153: Line 153:
 
$gcc -g -c IBL.c -o $pwrp_obj/IBL.o -I$pwrp_inc -I$pwr_inc -DOS_LINUX=1 -DOS=linux -DHW_X86=1 -DHW=x86 -DOS_POSIX <br \>
 
$gcc -g -c IBL.c -o $pwrp_obj/IBL.o -I$pwrp_inc -I$pwr_inc -DOS_LINUX=1 -DOS=linux -DHW_X86=1 -DHW=x86 -DOS_POSIX <br \>
 
<br \>
 
<br \>
 
 
==Linking==
 
==Linking==
 +
The object file needs to be linked to the PLC program.  This requires a link file (.opt) in the $pwrp_exe directory.<br \>
 +
* Create a .opt file in /usr/local/pwrp/cpptest/bld/x86_64_linux/exe/ called plc_'NodeName'_'BusNumber'_'plcName'.opt, where the NodeName is the name of your host, BusNumer is the bus number (most likely 0999 if you're just starting) and plcName is the name of the PLC Program.  The contents of this file are:<br \>
 +
<br \>
 +
<nowiki>$pwr_obj/rt_io_user.o $pwrp_obj/IBL.o</nowiki>
 +
<br \>
 +
 +
 +
--[[User:Sdockter|Sdockter]] ([[User talk:Sdockter|talk]]) 17:39, 22 July 2014 (CDT)

Revision as of 16:39, 22 July 2014

These are the step by step instructions for creating a Function Object With C Code from the Proview "Designer's Guide" Chapter 22.4 - Create Classes.

Contents

Create a Custom Class Volume

The class definition objects reside in a class volume. First, the class volume has to be created and registered. This is done is done by the administrator. To create and register a custom Class Volume:

  • Start the administrator with the command 'pwra'
  • Open the Global Volume from File->Open->GlobalVolumeList in the Project List window
  • Login as administrator
  • Enter edit mode
  • Create a VolumeReg object in the $Hier class named ClassVolumes
  • Open the new VolumeReg object and enter a unique name and volume identity. The volume identity for user created class volumes should be within the interval 0.0.2-249.1-254. It is recommended to use the prefix CVol for the name since it is the preferred method to indicate that it is a class volume. The current project also needs to be defined.
  • Save
  • Logout with the command 'logout'

GlobalVolumeForPLCFunctions.png

Create a Custom Function Object Class

Function objects are used in the PLC editor to program the PLC program. A function object is also described by a class, usually a bit more complex than a data class. It defines the data structure, the graphic layout with inputs and outputs, and the code that is to be executed for the PLC program.

The code can be defined either by C code, or by graphical programming in the PLC editor. This example uses C Code.

The next step is to configure the custom class volume in the directory volume of the project.

  • Open the Project Directory
  • Enter Edit mode
  • Create a ClassVolumeConfig object
  • Name the new object the same name as the Custom Class Volume (in this case CVolDES)
  • Save and leave Edit mode

CustomClassVolume.png

  • Right click on the ClassVolumeConfig object and select 'Open ClassEditor...' from the menu

OpenClassEditor.png

  • Enter Edit mode
  • Create a $ClassHier object. This will automatically be named 'Class'.
  • Create a $ClassDef object in the $ClassHier object
  • Name the $ClassDef IBL
  • Right click on the newly named $ClassDef and in the popup menu select Configure->CCodeFo. This will automatically create the following objects in the new Class Object:
    • a RtBody object.
    • a DevBody object with a PlcNode object that defines a buffer for graphic information in the instances.
    • a GraphPlcNode object that contains information for graphic and code generation for the class.

NewFunctionClass.png

The next step is to define the attributes of the class. The attributes are divided into input, internal, and output.

Inputs

The input attributes define the input pins of the function object, i.e. values that are fetched from output pins of other function objects. The inputs are defined by $Input objects and are placed below the RtBody object.

The datatype of the input, TypeRef, needs to be defined. Valid datatypes for an input are:

  • pwrs:Type-Float32
  • pwrs:Type-Int32
  • pwrs:Type-String80


In GraphName the text at the input pin in the function object is stated. Normally you use 2 - 4 characters, block letters for analog signals, lower-case for digital, and first character upper-case for other signal types.

An input attribute in an instance object, contains both a pointer to the output it is connected to, and a value that can be stated. You choose whether to use the input pin and connect an output, or to set a value, with a checkbox (Used). If you choose not to mark Used, the input pin is not displayed in the function object. In the Template object, you can set default values for the input, that will be used when the input is not connected.

Internal

Internal attributes are attributes that are not inputs or outputs. They can be used for calculated values that need to be stored in the object, or values that are used to configure the object.

All common datatypes are valid for intern attributes.

Outputs

The output attributes define the output pins of the function object, i.e. values that are storedin the object, and can be fetched by inputs of other function objects. The outputs are defined by $Output objects that are placed below the RtBody object.

The datatype for the output is stated in TypeRef. As for $Input, Boolean, Float32, Int32 and String80 can be stated, and in GraphName the text for the output pin in the function object is stated.

Note !
$Input, $Intern, and $Output have to be placed in this order below RtBody: $Input first, then $Intern and then $Output.

  • Add two $Inputs, one $Intern, and one $Output to the RtBody class
  • Change the name of the $Inputs to In1 and In2
  • Change the GraphName attribute for In1 and In2 to IN1 and IN2
  • Change the name of the $Intern to Factor
  • Change the GraphName attribute for Factor to FACTOR
  • Change the name of the $Output to Out
  • Change the GraphName attribute for Out to OUT
  • Change all the TypeRef attributes to pwrs:Type-$Float32
  • Change the order so the $Inputs are first, followed by the $Intern, and finally the $Output

NewFunctionClassConfig.png

  • Save and leave Edit mode


Default Values

Default values of the $Input and $Intern attributes can be set in the Template object.

GraphPlcNode Object

If you want to state which inputs and outputs should be viewed as default, there is a mask in the GraphPlcNode object that controls this called default_mask. Bits in default_mask[0] correspond to input attributes, and bits in default_mask[1] to output attributes. If the bit that corresponds to a specific input or output is set, this will be viewed as default.

Code

  • In the Class Editor window click on Functions->Build Volume in the menu.

When the class volume is built, an h-file with a c structure for the class is generated. The name of the struct is pwr_'CustomClassVolumeName'classes.h where CustomClassVolumeName is the name of the Custom Class Volume. It is located in the $pwrp_inc directory. For this example it's called pwr_cvoldesclasses.h. Below is what was generated for this example:

/*	Proview V5.1.0 pwr_cvoldesclasses.h */

/*	Generated by co_convert. */
/*	Do not edit this file. */

#ifndef pwr_cvoldesclasses_h
#define pwr_cvoldesclasses_h

#ifndef pwr_class_h
#include "pwr_class.h"
#endif



#ifndef pwr_cClass_IBL
#define pwr_cClass_IBL 16842768UL

/*_* Class: IBL
    Body:  RtBody
    @Aref IBL pwr_sClass_IBL
*/

typedef struct {
  pwr_tFloat32                        *In1P pwr_dAlignLW;
  pwr_tFloat32                        In1 pwr_dAlignLW;
  pwr_tFloat32                        *In2P pwr_dAlignLW;
  pwr_tFloat32                        In2 pwr_dAlignLW;
  pwr_tFloat32                        Factor pwr_dAlignW;
  pwr_tFloat32                        Out pwr_dAlignW;
} pwr_sClass_IBL;

/*_* Class: IBL
    Body:  DevBody
    @Aref IBL pwr_sdClass_IBL
*/

typedef struct {
  pwr_sPlcNode                        PlcNode pwr_dAlignLW;
} pwr_sdClass_IBL;

#endif

#endif

Note that each input consist of two elements; a pointer with the suffix 'P', and an element that can be given a value if the input is not connected. If the input is connected, the pointer element will point to the output it is connected to, otherwise it will point to the value element. Therefore, in the C code, you should use the pointer element to fetch the value of the input.

You can name the C code file any name name you want and can put it anywhere. The normal place is in $pwrp_src or any subdirectory. You have to compile the C code to an object file (.o) manually. Normally the object file is in $pwrp_obj or you can insert put it in an archive on $pwrp_lib. The code file for this example is called IBL.c. The code for the class a function which looks like this:

IBL.c

#include "pwr.h"
#include "pwr_cvoldesclasses.h"
#include "rt_c_plcthread.h"

void IBL_exec( plc_sThread *tp,
pwr_sClass_IBL *o) {
o->Out = o->Factor * (*o->In1P + *o->In2P);
}

Compiling

Once you have copied the above code into your C file and saved it, it's time to compile it using:
$gcc -g -c IBL.c -o $pwrp_obj/IBL.o -I$pwrp_inc -I$pwr_inc -DOS_LINUX=1 -DOS=linux -DHW_X86=1 -DHW=x86 -DOS_POSIX

Linking

The object file needs to be linked to the PLC program. This requires a link file (.opt) in the $pwrp_exe directory.

  • Create a .opt file in /usr/local/pwrp/cpptest/bld/x86_64_linux/exe/ called plc_'NodeName'_'BusNumber'_'plcName'.opt, where the NodeName is the name of your host, BusNumer is the bus number (most likely 0999 if you're just starting) and plcName is the name of the PLC Program. The contents of this file are:


$pwr_obj/rt_io_user.o $pwrp_obj/IBL.o



--Sdockter (talk) 17:39, 22 July 2014 (CDT)

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox