-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Kai Kreuzer <kai@openhab.org>
- Loading branch information
1 parent
734c3b0
commit 6f9c605
Showing
8,143 changed files
with
12,932 additions
and
8,236 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
.../main/java/org/openhab/automation/groovyscripting/internal/GroovyScriptEngineFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...pting/src/main/java/org/openhab/automation/jythonscripting/JythonScriptEngineFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...controller/src/main/java/org/openhab/automation/pidcontroller/internal/LowpassFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Copyright (c) 2010-2021 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.automation.pidcontroller.internal; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
|
||
/** | ||
* Realizes an first-order FIR low pass filter. To keep code complexity low, it is implemented as moving average (all | ||
* FIR coefficients are set to normalized ones). | ||
* | ||
* The exponential decaying function is used for the calculation (see https://en.wikipedia.org/wiki/Time_constant). That | ||
* means the output value is approx. 63% of the input value after one time constant and approx. 99% after 5 time | ||
* constants. | ||
* | ||
* @author Fabian Wolter - Initial contribution | ||
* | ||
*/ | ||
@NonNullByDefault | ||
public class LowpassFilter { | ||
/** | ||
* Executes one low pass filter step. | ||
* | ||
* @param lastOutput the current filter value (result of the last invocation) | ||
* @param newValue the just sampled value | ||
* @param timeQuotient quotient of the current time and the time constant | ||
* @return the new filter value | ||
*/ | ||
public static double calculate(double lastOutput, double newValue, double timeQuotient) { | ||
double output = lastOutput * Math.exp(-timeQuotient); | ||
output += newValue * (1 - Math.exp(-timeQuotient)); | ||
|
||
return output; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...r/src/main/java/org/openhab/automation/pidcontroller/internal/PIDControllerConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Copyright (c) 2010-2021 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.automation.pidcontroller.internal; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
|
||
/** | ||
* | ||
* Constants for PID controller. | ||
* | ||
* @author Fabian Wolter - Initial contribution | ||
* | ||
*/ | ||
@NonNullByDefault | ||
public class PIDControllerConstants { | ||
public static final String AUTOMATION_NAME = "pidcontroller"; | ||
public static final String CONFIG_INPUT_ITEM = "input"; | ||
public static final String CONFIG_SETPOINT_ITEM = "setpoint"; | ||
public static final String CONFIG_OUTPUT_LOWER_LIMIT = "outputLowerLimit"; | ||
public static final String CONFIG_OUTPUT_UPPER_LIMIT = "outputUpperLimit"; | ||
public static final String CONFIG_LOOP_TIME = "loopTime"; | ||
public static final String CONFIG_KP_GAIN = "kp"; | ||
public static final String CONFIG_KI_GAIN = "ki"; | ||
public static final String CONFIG_KD_GAIN = "kd"; | ||
public static final String CONFIG_KD_TIMECONSTANT = "kdTimeConstant"; | ||
public static final String P_INSPECTOR = "pInspector"; | ||
public static final String I_INSPECTOR = "iInspector"; | ||
public static final String D_INSPECTOR = "dInspector"; | ||
public static final String E_INSPECTOR = "eInspector"; | ||
public static final String OUTPUT = "output"; | ||
} |
31 changes: 31 additions & 0 deletions
31
...dcontroller/src/main/java/org/openhab/automation/pidcontroller/internal/PIDException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Copyright (c) 2010-2021 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.automation.pidcontroller.internal; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
|
||
/** | ||
* | ||
* Common Exception for PID controller. | ||
* | ||
* @author Fabian Wolter - Initial contribution | ||
* | ||
*/ | ||
@NonNullByDefault | ||
public class PIDException extends Exception { | ||
private static final long serialVersionUID = -3029834022610530982L; | ||
|
||
public PIDException(String message) { | ||
super(message); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
.../openhab/automation/pidcontroller/internal/factory/PIDControllerModuleHandlerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* Copyright (c) 2010-2021 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.automation.pidcontroller.internal.factory; | ||
|
||
import java.util.Collection; | ||
import java.util.Set; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.automation.pidcontroller.internal.handler.PIDControllerActionHandler; | ||
import org.openhab.automation.pidcontroller.internal.handler.PIDControllerTriggerHandler; | ||
import org.openhab.core.automation.Action; | ||
import org.openhab.core.automation.Module; | ||
import org.openhab.core.automation.Trigger; | ||
import org.openhab.core.automation.handler.BaseModuleHandlerFactory; | ||
import org.openhab.core.automation.handler.ModuleHandler; | ||
import org.openhab.core.automation.handler.ModuleHandlerFactory; | ||
import org.openhab.core.events.EventPublisher; | ||
import org.openhab.core.items.ItemRegistry; | ||
import org.osgi.framework.BundleContext; | ||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
|
||
/** | ||
* | ||
* @author Hilbrand Bouwkamp - Initial Contribution | ||
*/ | ||
@Component(service = ModuleHandlerFactory.class, configurationPid = "action.pidcontroller") | ||
@NonNullByDefault | ||
public class PIDControllerModuleHandlerFactory extends BaseModuleHandlerFactory { | ||
private static final Collection<String> TYPES = Set.of(PIDControllerTriggerHandler.MODULE_TYPE_ID, | ||
PIDControllerActionHandler.MODULE_TYPE_ID); | ||
private ItemRegistry itemRegistry; | ||
private EventPublisher eventPublisher; | ||
private BundleContext bundleContext; | ||
|
||
@Activate | ||
public PIDControllerModuleHandlerFactory(@Reference ItemRegistry itemRegistry, | ||
@Reference EventPublisher eventPublisher, BundleContext bundleContext) { | ||
this.itemRegistry = itemRegistry; | ||
this.eventPublisher = eventPublisher; | ||
this.bundleContext = bundleContext; | ||
} | ||
|
||
@Override | ||
public Collection<String> getTypes() { | ||
return TYPES; | ||
} | ||
|
||
@Override | ||
protected @Nullable ModuleHandler internalCreate(Module module, String ruleUID) { | ||
switch (module.getTypeUID()) { | ||
case PIDControllerTriggerHandler.MODULE_TYPE_ID: | ||
return new PIDControllerTriggerHandler((Trigger) module, itemRegistry, eventPublisher, bundleContext); | ||
case PIDControllerActionHandler.MODULE_TYPE_ID: | ||
return new PIDControllerActionHandler((Action) module, itemRegistry, eventPublisher); | ||
} | ||
|
||
return null; | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...er/src/main/java/org/openhab/automation/pidcontroller/internal/handler/PIDController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* Copyright (c) 2010-2021 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.automation.pidcontroller.internal.handler; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.openhab.automation.pidcontroller.internal.LowpassFilter; | ||
|
||
/** | ||
* The {@link PIDController} provides the necessary methods for retrieving part(s) of the PID calculations | ||
* and it provides the method for the overall PID calculations. It also resets the PID controller | ||
* | ||
* @author George Erhan - Initial contribution | ||
* @author Hilbrand Bouwkamp - Adapted for new rule engine | ||
* @author Fabian Wolter - Add T1 to D part, add debugging ability for PID values | ||
*/ | ||
@NonNullByDefault | ||
class PIDController { | ||
private final double outputLowerLimit; | ||
private final double outputUpperLimit; | ||
|
||
private double integralResult; | ||
private double derivativeResult; | ||
private double previousError; | ||
private double output; | ||
|
||
private double kp; | ||
private double ki; | ||
private double kd; | ||
private double derivativeTimeConstantSec; | ||
|
||
public PIDController(double outputLowerLimit, double outputUpperLimit, double kpAdjuster, double kiAdjuster, | ||
double kdAdjuster, double derivativeTimeConstantSec) { | ||
this.outputLowerLimit = outputLowerLimit; | ||
this.outputUpperLimit = outputUpperLimit; | ||
this.kp = kpAdjuster; | ||
this.ki = kiAdjuster; | ||
this.kd = kdAdjuster; | ||
this.derivativeTimeConstantSec = derivativeTimeConstantSec; | ||
} | ||
|
||
public PIDOutputDTO calculate(double input, double setpoint, long lastInvocationMs) { | ||
final double lastInvocationSec = lastInvocationMs / 1000d; | ||
final double error = setpoint - input; | ||
|
||
// derivative T1 calculation | ||
final double timeQuotient = lastInvocationSec / derivativeTimeConstantSec; | ||
if (derivativeTimeConstantSec != 0) { | ||
derivativeResult = LowpassFilter.calculate(derivativeResult, error - previousError, timeQuotient); | ||
previousError = error; | ||
} | ||
|
||
// integral calculation | ||
integralResult += error * lastInvocationSec; | ||
// limit to output limits | ||
if (ki != 0) { | ||
final double maxIntegral = outputUpperLimit / ki; | ||
final double minIntegral = outputLowerLimit / ki; | ||
integralResult = Math.min(maxIntegral, Math.max(minIntegral, integralResult)); | ||
} | ||
|
||
// calculate parts | ||
final double proportionalPart = kp * error; | ||
final double integralPart = ki * integralResult; | ||
final double derivativePart = kd * derivativeResult; | ||
output = proportionalPart + integralPart + derivativePart; | ||
|
||
// limit output value | ||
output = Math.min(outputUpperLimit, Math.max(outputLowerLimit, output)); | ||
|
||
return new PIDOutputDTO(output, proportionalPart, integralPart, derivativePart, error); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...ava/org/openhab/automation/pidcontroller/internal/handler/PIDControllerActionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Copyright (c) 2010-2021 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.automation.pidcontroller.internal.handler; | ||
|
||
import static org.openhab.automation.pidcontroller.internal.PIDControllerConstants.*; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.core.automation.Action; | ||
import org.openhab.core.automation.handler.ActionHandler; | ||
import org.openhab.core.automation.handler.BaseModuleHandler; | ||
import org.openhab.core.events.EventPublisher; | ||
import org.openhab.core.items.ItemRegistry; | ||
import org.openhab.core.items.events.ItemCommandEvent; | ||
import org.openhab.core.items.events.ItemEventFactory; | ||
import org.openhab.core.library.types.DecimalType; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* | ||
* @author Hilbrand Bouwkamp - Initial Contribution | ||
* @author Fabian Wolter - Add PID debugging items | ||
*/ | ||
@NonNullByDefault | ||
public class PIDControllerActionHandler extends BaseModuleHandler<Action> implements ActionHandler { | ||
public static final String MODULE_TYPE_ID = AUTOMATION_NAME + ".action"; | ||
|
||
private final Logger logger = LoggerFactory.getLogger(PIDControllerActionHandler.class); | ||
|
||
private ItemRegistry itemRegistry; | ||
private EventPublisher eventPublisher; | ||
|
||
public PIDControllerActionHandler(Action module, ItemRegistry itemRegistry, EventPublisher eventPublisher) { | ||
super(module); | ||
this.itemRegistry = itemRegistry; | ||
this.eventPublisher = eventPublisher; | ||
} | ||
|
||
@Override | ||
public @Nullable Map<String, Object> execute(Map<String, Object> context) { | ||
Stream.of(OUTPUT, P_INSPECTOR, I_INSPECTOR, D_INSPECTOR, E_INSPECTOR).forEach(arg -> { | ||
final String itemName = (String) module.getConfiguration().get(arg); | ||
|
||
if (itemName == null || itemName.isBlank()) { | ||
return; | ||
} | ||
|
||
final BigDecimal command = (BigDecimal) context.get("1." + arg); | ||
|
||
if (command != null) { | ||
final DecimalType outputValue = new DecimalType(command); | ||
final ItemCommandEvent itemCommandEvent = ItemEventFactory.createCommandEvent(itemName, outputValue); | ||
|
||
eventPublisher.post(itemCommandEvent); | ||
} else { | ||
logger.warn( | ||
"Command was not posted because either the configuration was not correct or a service was missing: ItemName: {}, Command: {}, eventPublisher: {}, ItemRegistry: {}", | ||
itemName, command, eventPublisher, itemRegistry); | ||
} | ||
}); | ||
return null; | ||
} | ||
} |
Oops, something went wrong.