If OpenPLC could open PLCOpen XML format would it not be possible to make functionality to import/export of Function Block.
In other words there is a functionality in main program to load /editor/plcopen/xxx.xml when the application start.
It make me to believe it would also be possible to import/export of custom data and exchange between projects.
If take a look inside the plc.xml file that include Function Block we could also see how its made.
ex.
This PLCOpen XML for the tag begins with <pou name="FUNCTION_BLOCK_NAME" pouType="functionBlock">
It means we only need a method to extract pou tag with value of pouType is functionBlock. and add every line in for loop to the end line with tag is </pou>
Next step would be to implant functionality to export or append a custom Function Block to a custom library. That will be loaded next time we open OpenPLC.
I hope to see this functionality in the new OpenPLC Editor.
This is example of Function Block extracted from PLCOpen XML format
<pou name="FB_AutoTuningPID" pouType="functionBlock">
<interface>
<inputVars>
<variable name="Enable">
<type>
<BOOL/>
</type>
</variable>
<variable name="SP">
<type>
<REAL/>
</type>
</variable>
<variable name="PV">
<type>
<REAL/>
</type>
</variable>
<variable name="Kp">
<type>
<REAL/>
</type>
</variable>
<variable name="Ki">
<type>
<REAL/>
</type>
</variable>
<variable name="Kd">
<type>
<REAL/>
</type>
</variable>
</inputVars>
<outputVars>
<variable name="EnableOut">
<type>
<BOOL/>
</type>
</variable>
<variable name="ControlOutput">
<type>
<REAL/>
</type>
</variable>
<variable name="Tuned">
<type>
<BOOL/>
</type>
</variable>
<variable name="Kp_T">
<type>
<REAL/>
</type>
</variable>
<variable name="Ki_T">
<type>
<REAL/>
</type>
</variable>
<variable name="Kd_T">
<type>
<REAL/>
</type>
</variable>
</outputVars>
<localVars>
<variable name="IntegralTerm">
<type>
<REAL/>
</type>
<initialValue>
<simpleValue value="0.0"/>
</initialValue>
</variable>
<variable name="LastError">
<type>
<REAL/>
</type>
<initialValue>
<simpleValue value="0.0"/>
</initialValue>
</variable>
<variable name="Error">
<type>
<REAL/>
</type>
<initialValue>
<simpleValue value="0.0"/>
</initialValue>
</variable>
<variable name="DerivativeTerm">
<type>
<REAL/>
</type>
<initialValue>
<simpleValue value="0.0"/>
</initialValue>
</variable>
<variable name="LastProcessVar">
<type>
<REAL/>
</type>
<initialValue>
<simpleValue value="0.0"/>
</initialValue>
</variable>
<variable name="DeltaT">
<type>
<REAL/>
</type>
<initialValue>
<simpleValue value="0.1"/>
</initialValue>
</variable>
<variable name="TuningPhase">
<type>
<INT/>
</type>
<initialValue>
<simpleValue value="0"/>
</initialValue>
</variable>
<variable name="MaxError">
<type>
<REAL/>
</type>
<initialValue>
<simpleValue value="0.0"/>
</initialValue>
</variable>
<variable name="MinError">
<type>
<REAL/>
</type>
<initialValue>
<simpleValue value="0.0"/>
</initialValue>
</variable>
<variable name="Ku">
<type>
<REAL/>
</type>
</variable>
<variable name="Tu">
<type>
<REAL/>
</type>
</variable>
<variable name="OscillationStartTime">
<type>
<TIME/>
</type>
</variable>
<variable name="AutoTuneEnabled">
<type>
<BOOL/>
</type>
<initialValue>
<simpleValue value="TRUE"/>
</initialValue>
</variable>
<variable name="CURRENT_TIME">
<type>
<TIME/>
</type>
</variable>
</localVars>
</interface>
<body>
<ST>
<xhtml:p><![CDATA[(* When Tuned is True, AutoTuneEnabled is False *)
{__SET_VAR(data__->,CURRENT_TIME,,__CURRENT_TIME)}
IF Enable THEN
(* PID control logic *)
Error := SP - PV;
IntegralTerm := IntegralTerm + (Error * DeltaT);
DerivativeTerm := (PV - LastProcessVar) / DeltaT;
ControlOutput := (Kp * Error) + (Ki * IntegralTerm) - (Kd * DerivativeTerm);
LastProcessVar := PV;
(* Auto-tuning algorithm *)
IF AutoTuneEnabled THEN
CASE TuningPhase OF
0: (* Initialization phase *)
MaxError := Error;
MinError := Error;
TuningPhase := 1;
OscillationStartTime := CURRENT_TIME;
1: (* Measure oscillations *)
IF Error > MaxError THEN
MaxError := Error;
ELSIF Error < MinError THEN
MinError := Error;
END_IF;
IF (CURRENT_TIME - OscillationStartTime) > T#10s THEN
Ku := 4.0 * (2.0 / (MaxError - MinError));
Tu := TIME_TO_REAL(CURRENT_TIME - OscillationStartTime) / 2.0;
TuningPhase := 2;
END_IF;
2: (* Calculate PID gains *)
Kp := 0.6 * Ku;
Ki := 2.0 * Kp / Tu;
Kd := Kp * Tu / 8.0;
Kp_T := Kp;
Ki_T := Ki;
Kd_T := Kd;
AutoTuneEnabled := FALSE;
Tuned := TRUE;
END_CASE;
END_IF;
EnableOut := TRUE;
ELSE
EnableOut := FALSE;
ControlOutput := 0.0;
END_IF;]]></xhtml:p>
</ST>
</body>
</pou>
And do more hacking we realize that inside the file editor/plcopen/
definations.py only need a extra line of code to import the Function Block after next reopen.