|
i.CanDrawIt® is designed to allow users with minimal programming skills to create useful programs. However, creating the graphical function blocks and the code inside them does require programming skills. The following examples show how code is added to the graphical symbols. The first example is a simple one. We will look at the Input function block. The graphical symbol looks like this:
The parameter list looks like this:
Click on the Program Code tab to get to the code. The code is very simple, and looks like this:
There are three key areas for code placement: (1) Library: This is where you put code that will be used one time regardless of how many times the same function block is used. Code in this section should be in the form of type declarations and procedures. Library declarations will be delimited by DECLARE: :END. Code will be delimited by CODE: :END. (2) Declare: This is where you put type and variable declarations that will be repeated one time for each instance of placing the graphical symbol on the drawing. Declarations will be delimited by DECLARE: :END. (3) Execute: This is where you put program code that will be repeated one time for each instance of placing the graphical symbol on the drawing. There may be two code sections in the Execute area. The code delimited by ONCE: :END will be executed one time when the program starts (device powers up, etc). The code delimited by REPEAT: :END will be executed repeatedly, in round robin fashion along with all of the repeated code from other function blocks in the drawing. During code generation, parameter substitution for ?Object and @xxx names will be performed. In addition, any instance of $fb$ will be replaced with the function block number. It is important to include $fb$ in variable names that are going to be repeated one time per instance of using the function block on a drawing. The other substitution that is VERY critical is replacing function block connection pin names with source data. The code generator will attempt to substitute anything found inside brackets {} with the pin named inside the brackets. In the above example, the connection pin was named SRC when the graphical symbol was created. Therefore, any occurrence of {SRC} in the code will be substituted with data found by tracing the connecting wire back to a source of data. The programming language used in the function blocks is Control Solutions' PL/i Programming Language for i.CanDoIt. A line oriented code simulator/debugger is available for debugging function block code line by line before incorporating it into a function block (if needed). The full PL/i programming language reference may be found here. A more complex example having all of the code elements included is shown below. This is the generic PID loop function block. The parameter page is shown here:
The code page for the PID loop looks like this as a screen shot, but you need to scroll through to see all of the code.
The expanded view of all of the code is shown below.
The full PL/i programming language reference may be found here. There is one minor trick worth knowing to make your function block code simulator friendly. The statement if {A} >= {B} then {Y} = 1; else {Y} = 0; endif;
can be correctly represented by a single line of code as shown. However, to allow the simulator to be able to stop on this function block regardless of execution path, it is necessary to break it up as follows: if {A} >= {B} then
{Y} = 1;
else
{Y} = 0;
endif;
Without breaking this statement up as illustrated here, the simulator will execute the code, but might not highlight the function block depending on which execution path was taken. Because the bright green highlighting action is skipped, it will give the appearance of skipping execution even though it was executed. This is related to the fact that the simulator is internally tracking code execution line by line, and tracking a line that is only half executed (first example above) creates an ambiguous condition. |