Adding support for LPC pous library using new Beremiz extension paradigm
authorLaurent Bessard
Thu, 14 Jun 2012 15:16:42 +0200
changeset 4 e8a0af6b89d9
parent 2 0c697b8fc443
child 5 7a09e12267a9
child 6 8863b78144ee
Adding support for LPC pous library using new Beremiz extension paradigm
LPCManager.py
pous.xml
--- a/LPCManager.py	Fri Jun 08 12:40:31 2012 +0200
+++ b/LPCManager.py	Thu Jun 14 15:16:42 2012 +0200
@@ -70,7 +70,7 @@
 import targets
 from LPCtarget import LPC_target 
 targets.targets["LPC"] = {"xsd": os.path.join(_base_path, "LPCtarget", "XSD"),
-                          "class": lambda:LPC_target,
+                          "class": lambda: LPC_target,
                           "code": os.path.join(_base_path,"LPCtarget","plc_LPC_main.c")} 
 targets.toolchains["makefile"] = os.path.join(_base_path, "LPCtarget", "XSD_toolchain_makefile")
 
@@ -79,6 +79,17 @@
 misc.opjimg = lambda imgname: os.path.join(_base_folder, "beremiz", "images", imgname+".png")
 
 from Beremiz import *
+
+import features
+from POULibrary import POULibrary
+
+class PLCLibrary(POULibrary):
+    def GetLibraryPath(self):
+        return os.path.join(_base_path, "pous.xml")
+
+features.libraries.append(
+    ('LPC', lambda: PLCLibrary))
+
 from ProjectController import ProjectController
 from ConfigTreeNode import ConfigTreeNode
 from ProjectNodeEditor import ProjectNodeEditor
@@ -583,12 +594,12 @@
             
             if self.GetChildByName("Master") is None:
                 master = self.CTNAddChild("Master", "CanOpenNode", 0)
-                master.BaseParams.setEnabled(False)
+                #master.BaseParams.setEnabled(False)
                 master.CTNRequestSave()
             
             if self.GetChildByName("Slave") is None:
                 slave = self.CTNAddChild("Slave", "CanOpenSlave", 1)
-                slave.BaseParams.setEnabled(False)
+                #slave.BaseParams.setEnabled(False)
                 slave.CTNRequestSave()
     
 
@@ -603,7 +614,7 @@
     @param dst: destination directory
     """
     for i in os.listdir(src):
-        if not i.startswith('.') and i != "pous.xml":
+        if not i.startswith('.'):
             srcpath = os.path.join(src,i)
             dstpath = os.path.join(dst,i)
             if os.path.isdir(srcpath):
@@ -686,12 +697,6 @@
             infos["values"].append(resources)
         return infos
     
-    def ConfNodeLibraryFilePath(self):
-        if self.OrigBuildPath is not None:
-            return os.path.join(self.OrigBuildPath, "pous.xml")
-        else:
-            return ProjectController.ConfNodeLibraryFilePath(self)
-    
     def GetProjectName(self):
         return self.Project.getname()
 
@@ -854,7 +859,7 @@
         
         if havecanfestival and self.GetChildByName("CanOpen") is None:
             canopen = self.CTNAddChild("CanOpen", "CanOpen", 0)
-            canopen.BaseParams.setEnabled(False)
+            #canopen.BaseParams.setEnabled(False)
             canopen.LoadChildren()
             canopen.CTNRequestSave()
         
@@ -1489,12 +1494,12 @@
         leftwindowsizer = wx.BoxSizer(wx.HORIZONTAL)
         leftwindowvsizer.AddSizer(leftwindowsizer, 0, border=0, flag=0)
         
-        self.GenerateEnableButton(leftwindow, leftwindowsizer, confnode)
+        #self.GenerateEnableButton(leftwindow, leftwindowsizer, confnode)
         
         st = wx.StaticText(leftwindow, -1)
         st.SetFont(wx.Font(faces["size"], wx.DEFAULT, wx.NORMAL, wx.BOLD, faceName = faces["helv"]))
         st.SetLabel(confnode.GetFullIEC_Channel())
-        leftwindowsizer.AddWindow(st, 0, border=5, flag=wx.RIGHT)
+        leftwindowsizer.AddWindow(st, 0, border=5, flag=wx.LEFT|wx.RIGHT)
         
         expandbutton_id = wx.NewId()
         expandbutton = wx.lib.buttons.GenBitmapToggleButton(id=expandbutton_id, bitmap=wx.Bitmap(Bpath( 'images', 'plus.png')),
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pous.xml	Thu Jun 14 15:16:42 2012 +0200
@@ -0,0 +1,551 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xmlns="http://www.plcopen.org/xml/tc6.xsd"
+         xmlns:xhtml="http://www.w3.org/1999/xhtml"
+         xsi:schemaLocation="http://www.plcopen.org/xml/tc6.xsd">
+  <fileHeader companyName="Beremiz"
+              productName="Beremiz"
+              productVersion="0.0"
+              creationDateTime="2008-12-14T16:53:26"/>
+  <contentHeader name="LPC POUs"
+                 modificationDateTime="2012-01-20T13:35:46">
+    <coordinateInfo>
+      <fbd>
+        <scaling x="0" y="0"/>
+      </fbd>
+      <ld>
+        <scaling x="0" y="0"/>
+      </ld>
+      <sfc>
+        <scaling x="0" y="0"/>
+      </sfc>
+    </coordinateInfo>
+  </contentHeader>
+  <types>
+    <dataTypes/>
+    <pous>
+		<pou name="DEW_POINT" pouType="functionBlock">
+		  <interface>
+			<inputVars>
+			  <variable name="RH">
+				<type>
+				  <REAL/>
+				</type>
+			  </variable>
+			  <variable name="TEMP">
+				<type>
+				  <REAL/>
+				</type>
+			  </variable>
+			</inputVars>
+			<outputVars>
+			  <variable name="DEW_POINT">
+				<type>
+				  <REAL/>
+				</type>
+			  </variable>
+			</outputVars>
+		  </interface>
+		  <body>
+			<ST>
+		<![CDATA[if RH < 0.0 then
+		  RH := 0.0;
+		end_if;
+		if RH > 100.0 then
+		  RH := 100.0;
+		end_if;
+		DEW_POINT:=sqrt(sqrt(sqrt(RH/100.0)))*(112.0+(0.9*TEMP))+(0.1*TEMP)-112.0;]]>
+			</ST>
+		  </body>
+		</pou>
+      <pou name="GET_RETAIN_DATA" pouType="functionBlock">
+        <interface>
+          <inputVars>
+            <variable name="INDEX">
+              <type>
+                <UINT/>
+              </type>
+              <initialValue>
+                <simpleValue value="0"/>
+              </initialValue>
+              <documentation>
+<![CDATA[Index (row) of the retain array.]]>
+              </documentation>
+            </variable>
+            <variable name="READ">
+              <type>
+                <BOOL/>
+              </type>
+              <initialValue>
+                <simpleValue value="FALSE"/>
+              </initialValue>
+            </variable>
+          </inputVars>
+          <outputVars>
+            <variable name="WORD1">
+              <type>
+                <UINT/>
+              </type>
+              <documentation>
+<![CDATA[Data - first column.]]>
+              </documentation>
+            </variable>
+            <variable name="WORD2">
+              <type>
+                <UINT/>
+              </type>
+            </variable>
+            <variable name="WORD3">
+              <type>
+                <UINT/>
+              </type>
+            </variable>
+            <variable name="RD_OK">
+              <type>
+                <BOOL/>
+              </type>
+              <initialValue>
+                <simpleValue value="FALSE"/>
+              </initialValue>
+            </variable>
+          </outputVars>
+        </interface>
+        <body>
+          <ST>
+<![CDATA[{
+extern IEC_UINT __GetRetainData(BOOL, IEC_UINT, IEC_UINT);
+extern BOOL __GetReadStatus(BOOL);
+__SET_VAR(data__->,WORD1,__GetRetainData(__GET_VAR(data__->READ),__GET_VAR(data__->INDEX),0));
+__SET_VAR(data__->,WORD2,__GetRetainData(__GET_VAR(data__->READ),__GET_VAR(data__->INDEX),1));
+__SET_VAR(data__->,WORD3,__GetRetainData(__GET_VAR(data__->READ),__GET_VAR(data__->INDEX),2));
+__SET_VAR(data__->,RD_OK,__GetReadStatus(0));
+}]]>
+          </ST>
+        </body>
+      </pou>
+      <pou name="SET_RETAIN_DATA" pouType="functionBlock">
+        <interface>
+          <inputVars>
+            <variable name="INDEX">
+              <type>
+                <UINT/>
+              </type>
+              <initialValue>
+                <simpleValue value="0"/>
+              </initialValue>
+            </variable>
+            <variable name="WRITE">
+              <type>
+                <BOOL/>
+              </type>
+              <initialValue>
+                <simpleValue value="FALSE"/>
+              </initialValue>
+            </variable>
+            <variable name="WORD1">
+              <type>
+                <UINT/>
+              </type>
+            </variable>
+            <variable name="WORD2">
+              <type>
+                <UINT/>
+              </type>
+            </variable>
+            <variable name="WORD3">
+              <type>
+                <UINT/>
+              </type>
+            </variable>
+          </inputVars>
+          <outputVars>
+            <variable name="WR_OK">
+              <type>
+                <BOOL/>
+              </type>
+              <initialValue>
+                <simpleValue value="FALSE"/>
+              </initialValue>
+            </variable>
+          </outputVars>
+        </interface>
+        <body>
+          <ST>
+<![CDATA[{
+extern BOOL __SetRetainData(BOOL, IEC_UINT, IEC_UINT, IEC_UINT, IEC_UINT);
+__SET_VAR(data__->,WR_OK,__SetRetainData(__GET_VAR(data__->WRITE),__GET_VAR(data__->INDEX),__GET_VAR(data__->WORD1),__GET_VAR(data__->WORD2),__GET_VAR(data__->WORD3)));
+}]]>
+          </ST>
+        </body>
+      </pou>
+      <pou name="FIND_RETAIN_DATA" pouType="functionBlock">
+        <interface>
+          <outputVars>
+            <variable name="FOUND">
+              <type>
+                <BOOL/>
+              </type>
+              <initialValue>
+                <simpleValue value="False"/>
+              </initialValue>
+            </variable>
+            <variable name="INDEX">
+              <type>
+                <UINT/>
+              </type>
+            </variable>
+          </outputVars>
+          <inputVars>
+            <variable name="START_IDX">
+              <type>
+                <UINT/>
+              </type>
+              <initialValue>
+                <simpleValue value="0"/>
+              </initialValue>
+            </variable>
+            <variable name="END_IDX">
+              <type>
+                <UINT/>
+              </type>
+              <initialValue>
+                <simpleValue value="0"/>
+              </initialValue>
+            </variable>
+            <variable name="SEARCH">
+              <type>
+                <BOOL/>
+              </type>
+              <initialValue>
+                <simpleValue value="False"/>
+              </initialValue>
+            </variable>
+            <variable name="WORD1">
+              <type>
+                <UINT/>
+              </type>
+            </variable>
+            <variable name="WORD2">
+              <type>
+                <UINT/>
+              </type>
+            </variable>
+            <variable name="WORD3">
+              <type>
+                <UINT/>
+              </type>
+            </variable>
+          </inputVars>
+        </interface>
+        <body>
+          <ST>
+<![CDATA[{
+extern BOOL __FindRetainData(BOOL, IEC_UINT, IEC_UINT, IEC_UINT, IEC_UINT, IEC_UINT);
+extern IEC_UINT __GetFoundIndex(BOOL);
+__SET_VAR(data__->,FOUND,__FindRetainData(__GET_VAR(data__->SEARCH),__GET_VAR(data__->START_IDX),__GET_VAR(data__->END_IDX),__GET_VAR(data__->WORD1),__GET_VAR(data__->WORD2),__GET_VAR(data__->WORD3)));
+__SET_VAR(data__->,INDEX,__GetFoundIndex(0));
+}]]>
+          </ST>
+        </body>
+      </pou>
+      <pou name="PID_A" pouType="functionBlock">
+        <interface>
+          <inputVars>
+            <variable name="SET_POINT">
+              <type>
+                <REAL/>
+              </type>
+            </variable>
+          </inputVars>
+          <localVars>
+            <variable name="ena">
+              <type>
+                <INT/>
+              </type>
+              <initialValue>
+                <simpleValue value="1"/>
+              </initialValue>
+            </variable>
+          </localVars>
+          <inputVars>
+            <variable name="ACTUAL_VALUE">
+              <type>
+                <REAL/>
+              </type>
+            </variable>
+            <variable name="REVERSE">
+              <type>
+                <BOOL/>
+              </type>
+            </variable>
+            <variable name="HOLD">
+              <type>
+                <BOOL/>
+              </type>
+            </variable>
+            <variable name="MANUAL">
+              <type>
+                <BOOL/>
+              </type>
+            </variable>
+            <variable name="MANUAL_REF">
+              <type>
+                <REAL/>
+              </type>
+            </variable>
+            <variable name="Kp_GAIN">
+              <type>
+                <REAL/>
+              </type>
+            </variable>
+            <variable name="Ki_GAIN">
+              <type>
+                <REAL/>
+              </type>
+            </variable>
+            <variable name="Kd_GAIN">
+              <type>
+                <REAL/>
+              </type>
+            </variable>
+            <variable name="Td_FADEOUT">
+              <type>
+                <TIME/>
+              </type>
+              <initialValue>
+                <simpleValue value="t#1s"/>
+              </initialValue>
+            </variable>
+            <variable name="DEAD_BAND">
+              <type>
+                <REAL/>
+              </type>
+            </variable>
+            <variable name="OUT_MAX">
+              <type>
+                <REAL/>
+              </type>
+            </variable>
+            <variable name="OUT_MIN">
+              <type>
+                <REAL/>
+              </type>
+            </variable>
+            <variable name="CYCLE_TIME">
+              <type>
+                <TIME/>
+              </type>
+              <initialValue>
+                <simpleValue value="t#0.1s"/>
+              </initialValue>
+            </variable>
+          </inputVars>
+          <outputVars>
+            <variable name="OUT">
+              <type>
+                <REAL/>
+              </type>
+            </variable>
+          </outputVars>
+          <localVars>
+            <variable name="D_fadeout">
+              <type>
+                <REAL/>
+              </type>
+            </variable>
+            <variable name="Error_n">
+              <type>
+                <REAL/>
+              </type>
+              <initialValue>
+                <simpleValue value="0.0"/>
+              </initialValue>
+            </variable>
+            <variable name="Error_n1">
+              <type>
+                <REAL/>
+              </type>
+              <initialValue>
+                <simpleValue value="0.0"/>
+              </initialValue>
+            </variable>
+            <variable name="Error_n2">
+              <type>
+                <REAL/>
+              </type>
+              <initialValue>
+                <simpleValue value="0.0"/>
+              </initialValue>
+            </variable>
+            <variable name="Dout_n">
+              <type>
+                <REAL/>
+              </type>
+              <initialValue>
+                <simpleValue value="0.0"/>
+              </initialValue>
+            </variable>
+            <variable name="Dout">
+              <type>
+                <REAL/>
+              </type>
+              <initialValue>
+                <simpleValue value="0.0"/>
+              </initialValue>
+            </variable>
+            <variable name="Dout_1">
+              <type>
+                <REAL/>
+              </type>
+              <initialValue>
+                <simpleValue value="0.0"/>
+              </initialValue>
+            </variable>
+            <variable name="TON_1">
+              <type>
+                <derived name="TON"/>
+              </type>
+            </variable>
+            <variable name="TON_1_Q">
+              <type>
+                <BOOL/>
+              </type>
+            </variable>
+            <variable name="ET1">
+              <type>
+                <TIME/>
+              </type>
+            </variable>
+            <variable name="CTr">
+              <type>
+                <REAL/>
+              </type>
+            </variable>
+            <variable name="FTr">
+              <type>
+                <REAL/>
+              </type>
+            </variable>
+            <variable name="power">
+              <type>
+                <REAL/>
+              </type>
+            </variable>
+            <variable name="OUTchk">
+              <type>
+                <REAL/>
+              </type>
+            </variable>
+            <variable name="Dout_int">
+              <type>
+                <REAL/>
+              </type>
+            </variable>
+            <variable name="Reverse_1">
+              <type>
+                <BOOL/>
+              </type>
+            </variable>
+            <variable name="Soft_t">
+              <type>
+                <BOOL/>
+              </type>
+            </variable>
+            <variable name="R_chk">
+              <type>
+                <INT/>
+              </type>
+            </variable>
+          </localVars>
+        </interface>
+        <body>
+          <ST>
+<![CDATA[TON_1(
+     IN := NOT TON_1_Q,
+     PT := CYCLE_TIME,
+     Q => TON_1_Q,
+     ET => ET1
+     );
+if TON_1_Q then
+  if REVERSE then
+    Error_n := SET_POINT - ACTUAL_VALUE;
+  else
+    Error_n := ACTUAL_VALUE - SET_POINT;
+  end_if;
+  if Td_FADEOUT <= T#0.05s then
+  Td_FADEOUT := t#0.05s;
+  end_if;
+     CTr := TIME_TO_REAL (CYCLE_TIME);
+     FTr := TIME_TO_REAL (Td_FADEOUT);
+      power := CTr / FTr;
+       D_Fadeout := EXPT( 0.04979 , power );  (*time to reach 5% = e^(-3) *)
+         Dout := Kd_GAIN * (Error_n - 2.0 * Error_n1 + Error_n2); 
+         Dout_n := Dout + Dout_1 * D_Fadeout;  
+         Dout_int := Dout_int + Dout_n;
+           OUTchk := OUTchk + Kp_GAIN * (Error_n - Error_n1) + Ki_GAIN * Error_n * CTr;
+    R_chk:= bool_to_int(REVERSE) + bool_to_int(Reverse_1);
+        if (Soft_t or R_chk=1) then
+         Dout_int := 0.0;
+          Dout_n := 0.0;
+           Dout_1:= 0.0;
+            Dout:= 0.0;
+            OUTchk:= OUT;
+           Error_n1:=Error_n;
+          Error_n2:=Error_n;
+         Soft_t := 0;
+        end_if;
+      if R_chk =1 then 
+        Error_n:=0.0;
+        Soft_t := True;
+      end_if;
+           
+     if OUTchk > OUT_MAX then
+        OUTchk := OUT_MAX;
+     end_if;
+     
+     if OUTchk < OUT_MIN then
+        OUTchk := OUT_MIN;
+     end_if;
+    
+  if MANUAL then
+    OUT := MANUAL_REF;
+     OUTchk:= OUT;
+      Dout_int := 0.0;
+       Dout_n := 0.0;
+       Dout_1:= 0.0;
+  else 
+    if abs(Error_n) < DEAD_BAND or HOLD  then
+       OUT := OUT;
+        OUTchk:= OUT;
+        Dout_int := 0.0;
+        Dout_n := 0.0;
+       Dout_1:= 0.0;
+    else
+      OUT := OUTchk + Dout_int;
+    end_if;
+  end_if;
+  
+ if OUT > OUT_MAX then
+ OUT := OUT_MAX;
+ end_if;
+  
+ if OUT < OUT_MIN then
+ OUT := OUT_MIN;
+ end_if;
+ 
+    Error_n2 := Error_n1;
+     Error_n1 := Error_n;
+      Dout_1:=Dout_n;
+        Reverse_1 := REVERSE;
+else
+  OUT:=OUT;
+end_if;]]>
+          </ST>
+        </body>
+      </pou>
+    </pous>
+  </types>
+  <instances>
+    <configurations/>
+  </instances>
+</project>