100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
added simple PID test
Michal Rutka318e962dffe0
diff --git a/.gitignore b/.gitignoreindex 2d5de10e9..e472ac1dc 100644--- a/.gitignore+++ b/.gitignore@@ -16,3 +16,5 @@ bin/*~manager/.factorypathmanager/src/++out/\ No newline at end of filediff --git a/agent/src/test/groovy/org/openremote/test/rules/PIDTest.groovy b/agent/src/test/groovy/org/openremote/test/rules/PIDTest.groovynew file mode 100644index 000000000..e75a9c683--- /dev/null+++ b/agent/src/test/groovy/org/openremote/test/rules/PIDTest.groovy@@ -0,0 +1,75 @@+package org.openremote.test.rules++import org.kie.api.KieServices+import org.kie.api.io.Resource+import org.openremote.agent.AgentService+import org.openremote.agent.rules.RulesProvider+import org.openremote.agent.sensor.CustomSensorState+import org.openremote.test.ContainerTrait+import spock.lang.Specification++import java.util.stream.Stream++class PIDTest extends Specification implements ContainerTrait {++ def "PID controller basic test"() {++ given: "a deployment with commands and sensors"+ def deploymentXml = getClass().getResourceAsStream(+ "/org/openremote/test/rules/pid/agent.xml"+ )++ and: "some rules"+ def rulesProvider = new RulesProvider() {+ @Override+ Stream<Resource> getResources(KieServices kieServices) {+ Stream.of(+ kieServices.getResources().newClassPathResource(+ "org/openremote/test/rules/pid/PID.drl"+ )+ )+ }+ }++ and: "the started server"+ def testCommandBuilder = new TestCommandBuilder();+ def agentService = new AgentService(+ deploymentXml,+ testCommandBuilder,+ rulesProvider+ )+ def serverPort = findEphemeralPort()+ def container = startContainer(defaultConfig(serverPort), [agentService])++ when: "we increase Sp"+ // can we run PID.sp.inc.ON command instead?+ def customStateEvent = new CustomSensorState(358537, "PID.sp.inc", "ON");+ agentService.getContext().update(customStateEvent);++ and: "we wait"+ Thread.sleep(1000);++ then: "set point should be 1.5"+ agentService.getContext().queryValue(358531) == "1.5" // can we check sensors by name?+ agentService.getContext().queryValue(358532) == "1.5000" // PID output++ when: "we decrease Sp"+ customStateEvent = new CustomSensorState(358525, "PID.sp.dec", "ON");+ agentService.getContext().update(customStateEvent);++ and: "we wait"+ Thread.sleep(1000);++ then: "set point should be 1.0"+ agentService.getContext().queryValue(358531) == "1.0" // set point+ agentService.getContext().queryValue(358532) == "1.0000" // PID output++ // how can I test if there is no overshot?+ // how can I test if there are no oscillations?++ // Problem: sometimes it passes, sometimes fails...++ cleanup: "the server should be stopped"+ stopContainer(container)+ }+}diff --git a/agent/src/test/resources/org/openremote/test/rules/pid/PID.drl b/agent/src/test/resources/org/openremote/test/rules/pid/PID.drlnew file mode 100644index 000000000..38496b0a8--- /dev/null+++ b/agent/src/test/resources/org/openremote/test/rules/pid/PID.drl@@ -0,0 +1,199 @@+package org.openremote.agent.test.rules;++import java.util.concurrent.*;+import java.util.Date;+import java.text.SimpleDateFormat;+import java.io.*;+import org.openremote.agent.sensor.*;++global org.openremote.agent.command.Commands commands;+global org.openremote.agent.rules.RulePersistence persistence;+global org.openremote.agent.rules.RuleUtil util;+global com.fasterxml.jackson.databind.ObjectMapper JSON;+global java.util.logging.Logger LOG;++declare SensorState+ @role(event)+end++// PID controller start++rule "-PID: init"+then+ commands.execute("GV.PID.Kp",persistence.readData("GV.PID.Kp", "0.6"));+ commands.execute("GV.PID.Ki",persistence.readData("GV.PID.Ki", "0.3"));+ commands.execute("GV.PID.Kd",persistence.readData("GV.PID.Kd", "0.0"));+ commands.execute("GV.PID.Db",persistence.readData("GV.PID.Db", "0.0"));+ commands.execute("GV.PID.Sp",persistence.readData("GV.PID.Sp", "1.0"));+end++// PID UI++rule "-PID: increase Kp"+ timer(int:300ms)+when+ SensorState(sensorName=="GV.PID.Kp", $v: value)+ SensorState($s:sensorName=="PID.kp.inc", value=="ON")+then+ commands.execute("GV.PID.Kp", String.format("%.1f", Double.parseDouble($v.toString())+0.1) );+ commands.execute($s, "off");+end++rule "-PID: decrease Kp"+ timer(int:300ms)+when+ SensorState(sensorName=="GV.PID.Kp", $v: value)+ SensorState($s:sensorName=="PID.kp.dec", value=="ON")+then+ commands.execute("GV.PID.Kp", String.format("%.1f", Double.parseDouble($v.toString())-0.1) );+ commands.execute($s, "off");+end++rule "-PID: increase Ki"+ timer(int:300ms)+when+ SensorState(sensorName=="GV.PID.Ki", $v: value)+ SensorState($s:sensorName=="PID.ki.inc", value=="ON")+then+ commands.execute("GV.PID.Ki", String.format("%.1f", Double.parseDouble($v.toString())+0.1) );+ commands.execute($s, "off");+end++rule "-PID: decrease Ki"+ timer(int:300ms)+when+ SensorState(sensorName=="GV.PID.Ki", $v: value)+ SensorState($s:sensorName=="PID.ki.dec", value=="ON")+then+ commands.execute("GV.PID.Ki", String.format("%.1f", Double.parseDouble($v.toString())-0.1) );+ commands.execute($s, "off");+end++rule "-PID: increase Kd"+ timer(int:300ms)+when+ SensorState(sensorName=="GV.PID.Kd", $v: value)+ SensorState($s:sensorName=="PID.kd.inc", value=="ON")+then+ commands.execute("GV.PID.Kd", String.format("%.1f", Double.parseDouble($v.toString())+0.1) );+ commands.execute($s, "off");+end++rule "-PID: decrease Kd"+ timer(int:300ms)+when+ SensorState(sensorName=="GV.PID.Kd", $v: value)+ SensorState($s:sensorName=="PID.kd.dec", value=="ON")+then+ commands.execute("GV.PID.Kd", String.format("%.1f", Double.parseDouble($v.toString())-0.1) );+ commands.execute($s, "off");+end++rule "-PID: increase Db"+ timer(int:300ms)+when+ SensorState(sensorName=="GV.PID.Db", $v: value)+ SensorState($s:sensorName=="PID.db.inc", value=="ON")+then+ commands.execute("GV.PID.Db", String.format("%.1f", Double.parseDouble($v.toString())+0.1) );+ commands.execute($s, "off");+end++rule "-PID: decrease Db"+ timer(int:300ms)+when+ SensorState(sensorName=="GV.PID.Db", $v: value)+ SensorState($s:sensorName=="PID.db.dec", value=="ON")+then+ commands.execute("GV.PID.Db", String.format("%.1f", Double.parseDouble($v.toString())-0.1) );+ commands.execute($s, "off");+end++rule "PID: increase Sp"+ timer(int:300ms)+when+ SensorState(sensorName=="GV.PID.Sp", $v: value)+ SensorState($s:sensorName=="PID.sp.inc", value=="ON")+then+ commands.execute("GV.PID.Sp", String.format("%.1f", Double.parseDouble($v.toString())+0.5) );+ commands.execute($s, "off");+end++rule "-PID: decrease Sp"+ timer(int:300ms)+when+ SensorState(sensorName=="GV.PID.Sp", $v: value)+ SensorState($s:sensorName=="PID.sp.dec", value=="ON")+then+ commands.execute("GV.PID.Sp", String.format("%.1f", Double.parseDouble($v.toString())-0.5) );+ commands.execute($s, "off");+end++rule "-PID: reset inc/dec"+ salience -10+ timer(int: 1s)+when+ SensorState($s: sensorName matches "^PID......c$", value!="off")+then+ commands.execute($s, "off");+end++// PID algorithm++declare PIDVars+// Input==Output;+ output: double+ iterm: double+ prevInput: double+ outMin: double+ outMax: double+ active: Boolean+end++rule "-PID: algo init"+then+ PIDVars pv = new PIDVars();+ pv.setActive(true);+ pv.setOutMax(100);+ pv.setOutMin(-100);+ insert(pv);+end++rule "-PID: compute"+ timer(int: 0 10) // 10 times faster than the implemented one+when+ $pv: PIDVars(active==true)+ SensorState(sensorName=="GV.PID.Kp", $kp: value)+ SensorState(sensorName=="GV.PID.Ki", $ki: value)+ SensorState(sensorName=="GV.PID.Kd", $kd: value)+ SensorState(sensorName=="GV.PID.Sp", $sp: value)+ SensorState(sensorName=="GV.PID.Db", $db: value)+then+ double error = Double.parseDouble($sp.toString() ) - $pv.getOutput();+ double kp = Double.parseDouble($kp.toString());+ double ki = Double.parseDouble($ki.toString()); // * sample time in sec+ double kd = Double.parseDouble($kd.toString()); // / sample time in sec+ double sp = Double.parseDouble($sp.toString());+ double db = Double.parseDouble($db.toString());++ double iterm = $pv.getIterm() + (ki * error);+ if(iterm > $pv.getOutMax() ) iterm = $pv.getOutMax();+ else if(iterm < $pv.getOutMin() ) iterm = $pv.getOutMin();+ $pv.setIterm( iterm );++ //double dInput = (Input - lastInput);+ double dInput = ($pv.getOutput() - $pv.getPrevInput() );+ $pv.setPrevInput($pv.getOutput());++ // Compute PID Output+ double output = kp * error + $pv.getIterm()- kd * dInput;+ if(output > $pv.getOutMax()) output = $pv.getOutMax();+ else if(output < $pv.getOutMin()) output = $pv.getOutMin();+ $pv.setOutput(output);++ commands.execute("PID.Se", String.format("%.4f", error) );+ commands.execute("PID.Output", String.format("%.4f", output) );+ LOG.fine($pv.toString() + " error: "+String.format("%f", Double.parseDouble($sp.toString() ) - $pv.getOutput()));+end++// PID enddiff --git a/agent/src/test/resources/org/openremote/test/rules/pid/agent.xml b/agent/src/test/resources/org/openremote/test/rules/pid/agent.xmlnew file mode 100644index 000000000..7cb732fa6--- /dev/null+++ b/agent/src/test/resources/org/openremote/test/rules/pid/agent.xml@@ -0,0 +1,250 @@+<?xml version="1.0" encoding="UTF-8"?>+<openremote xmlns="http://www.openremote.org/schemas/agent/3">+ <sensors>+ <sensor id="358525" name="PID.sp.dec" type="custom">+ <include type="command" ref="2625" />+ </sensor>+ <sensor id="358526" name="PID.kd.inc" type="custom">+ <include type="command" ref="2606" />+ </sensor>+ <sensor id="358527" name="PID.db.inc" type="custom">+ <include type="command" ref="2622" />+ </sensor>+ <sensor id="358528" name="PID.kp.dec" type="custom">+ <include type="command" ref="2608" />+ </sensor>+ <sensor id="358529" name="PID.Se" type="custom">+ <include type="command" ref="2609" />+ </sensor>+ <sensor id="358530" name="PID.ki.dec" type="custom">+ <include type="command" ref="2620" />+ </sensor>+ <sensor id="358531" name="GV.PID.Sp" type="custom">+ <include type="command" ref="2610" />+ </sensor>+ <sensor id="358532" name="PID.Output" type="custom">+ <include type="command" ref="2615" />+ </sensor>+ <sensor id="358533" name="PID.db.dec" type="custom">+ <include type="command" ref="2613" />+ </sensor>+ <sensor id="358534" name="GV.PID.Ki" type="custom">+ <include type="command" ref="2604" />+ </sensor>+ <sensor id="358535" name="PID.kd.dec" type="custom">+ <include type="command" ref="2612" />+ </sensor>+ <sensor id="358536" name="GV.PID.Db" type="custom">+ <include type="command" ref="2624" />+ </sensor>+ <sensor id="358537" name="PID.sp.inc" type="custom">+ <include type="command" ref="2626" />+ </sensor>+ <sensor id="358538" name="GV.PID.Kp" type="custom">+ <include type="command" ref="2603" />+ </sensor>+ <sensor id="358539" name="PID.kp.inc" type="custom">+ <include type="command" ref="2628" />+ </sensor>+ <sensor id="358540" name="PID.ki.inc" type="custom">+ <include type="command" ref="2623" />+ </sensor>+ <sensor id="358541" name="GV.PID.Kd" type="custom">+ <include type="command" ref="2602" />+ </sensor>+ </sensors>+ <commands>+ <command id="2602" protocol="virtual">+ <property name="address" value="pidkd" />+ <property name="command" value="status" />+ <property name="name" value="GV.PID.Kd" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2603" protocol="virtual">+ <property name="address" value="pidkp" />+ <property name="command" value="status" />+ <property name="name" value="GV.PID.Kp" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2604" protocol="virtual">+ <property name="address" value="pidki" />+ <property name="command" value="status" />+ <property name="name" value="GV.PID.Ki" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2605" protocol="virtual">+ <property name="address" value="piddbinc" />+ <property name="command" value="ON" />+ <property name="name" value="PID.db.inc.ON" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2606" protocol="virtual">+ <property name="address" value="pidkdinc" />+ <property name="command" value="status" />+ <property name="name" value="PID.kd.inc" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2607" protocol="virtual">+ <property name="address" value="pidkiinc" />+ <property name="command" value="ON" />+ <property name="name" value="PID.ki.inc.ON" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2608" protocol="virtual">+ <property name="address" value="kpdec" />+ <property name="command" value="status" />+ <property name="name" value="PID.kp.dec" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2609" protocol="virtual">+ <property name="address" value="pidse" />+ <property name="command" value="status" />+ <property name="name" value="PID.Se" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2610" protocol="virtual">+ <property name="address" value="pidsp" />+ <property name="command" value="status" />+ <property name="name" value="GV.PID.Sp" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2611" protocol="virtual">+ <property name="address" value="pidkidec" />+ <property name="command" value="ON" />+ <property name="name" value="PID.ki.dec.ON" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2612" protocol="virtual">+ <property name="address" value="pidkddec" />+ <property name="command" value="status" />+ <property name="name" value="PID.kd.dec" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2613" protocol="virtual">+ <property name="address" value="piddbdec" />+ <property name="command" value="status" />+ <property name="name" value="PID.db.dec" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2614" protocol="virtual">+ <property name="address" value="pidkddec" />+ <property name="command" value="ON" />+ <property name="name" value="PID.kd.dec.ON" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2615" protocol="virtual">+ <property name="address" value="pidoutput" />+ <property name="command" value="status" />+ <property name="name" value="PID.Output" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2616" protocol="virtual">+ <property name="address" value="pidkdinc" />+ <property name="command" value="ON" />+ <property name="name" value="PID.kd.inc.ON" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2617" protocol="virtual">+ <property name="address" value="pidspdec" />+ <property name="command" value="ON" />+ <property name="name" value="PID.sp.dec.ON" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2618" protocol="virtual">+ <property name="address" value="piddbdec" />+ <property name="command" value="ON" />+ <property name="name" value="PID.db.dec.ON" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2619" protocol="virtual">+ <property name="address" value="pidspinc" />+ <property name="command" value="ON" />+ <property name="name" value="PID.sp.inc.ON" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2620" protocol="virtual">+ <property name="address" value="pidkidec" />+ <property name="command" value="status" />+ <property name="name" value="PID.ki.dec" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2621" protocol="virtual">+ <property name="address" value="kpinc" />+ <property name="command" value="ON" />+ <property name="name" value="PID.kp.inc.ON" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2622" protocol="virtual">+ <property name="address" value="piddbinc" />+ <property name="command" value="status" />+ <property name="name" value="PID.db.inc" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2623" protocol="virtual">+ <property name="address" value="pidkiinc" />+ <property name="command" value="status" />+ <property name="name" value="PID.ki.inc" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2624" protocol="virtual">+ <property name="address" value="piddb" />+ <property name="command" value="status" />+ <property name="name" value="GV.PID.Db" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2625" protocol="virtual">+ <property name="address" value="pidspdec" />+ <property name="command" value="status" />+ <property name="name" value="PID.sp.dec" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2626" protocol="virtual">+ <property name="address" value="pidspinc" />+ <property name="command" value="status" />+ <property name="name" value="PID.sp.inc" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2627" protocol="virtual">+ <property name="address" value="kpdec" />+ <property name="command" value="ON" />+ <property name="name" value="PID.kp.dec.ON" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ <command id="2628" protocol="virtual">+ <property name="address" value="kpinc" />+ <property name="command" value="status" />+ <property name="name" value="PID.kp.inc" />+ <property name="urn:openremote:device-command:device-name" value="PID" />+ <property name="urn:openremote:device-command:device-id" value="35487" />+ </command>+ </commands>+ <config>+ </config>+</openremote>+