2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.automation.pidcontroller.internal.handler;
15 import static org.openhab.automation.pidcontroller.internal.PIDControllerConstants.*;
17 import java.math.BigDecimal;
18 import java.util.HashMap;
20 import java.util.Objects;
22 import java.util.concurrent.Executors;
23 import java.util.concurrent.ScheduledExecutorService;
24 import java.util.concurrent.ScheduledFuture;
25 import java.util.concurrent.TimeUnit;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.openhab.automation.pidcontroller.internal.PIDException;
30 import org.openhab.core.automation.ModuleHandlerCallback;
31 import org.openhab.core.automation.Trigger;
32 import org.openhab.core.automation.handler.BaseTriggerModuleHandler;
33 import org.openhab.core.automation.handler.TriggerHandlerCallback;
34 import org.openhab.core.common.NamedThreadFactory;
35 import org.openhab.core.config.core.Configuration;
36 import org.openhab.core.events.Event;
37 import org.openhab.core.events.EventFilter;
38 import org.openhab.core.events.EventPublisher;
39 import org.openhab.core.events.EventSubscriber;
40 import org.openhab.core.items.Item;
41 import org.openhab.core.items.ItemNotFoundException;
42 import org.openhab.core.items.ItemRegistry;
43 import org.openhab.core.items.events.ItemEventFactory;
44 import org.openhab.core.items.events.ItemStateChangedEvent;
45 import org.openhab.core.items.events.ItemStateEvent;
46 import org.openhab.core.library.types.StringType;
47 import org.openhab.core.types.RefreshType;
48 import org.openhab.core.types.State;
49 import org.osgi.framework.BundleContext;
50 import org.osgi.framework.ServiceRegistration;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
56 * @author Hilbrand Bouwkamp - Initial Contribution
57 * @author Fabian Wolter - Add PID debug output values
60 public class PIDControllerTriggerHandler extends BaseTriggerModuleHandler implements EventSubscriber {
61 public static final String MODULE_TYPE_ID = AUTOMATION_NAME + ".trigger";
62 private static final Set<String> SUBSCRIBED_EVENT_TYPES = Set.of(ItemStateEvent.TYPE, ItemStateChangedEvent.TYPE);
63 private final Logger logger = LoggerFactory.getLogger(PIDControllerTriggerHandler.class);
64 private final ScheduledExecutorService scheduler = Executors
65 .newSingleThreadScheduledExecutor(new NamedThreadFactory("OH-automation-" + AUTOMATION_NAME, true));
66 private final ServiceRegistration<?> eventSubscriberRegistration;
67 private final PIDController controller;
68 private final int loopTimeMs;
69 private @Nullable ScheduledFuture<?> controllerjob;
70 private long previousTimeMs = System.currentTimeMillis();
71 private Item inputItem;
72 private Item setpointItem;
73 private EventFilter eventFilter;
75 public PIDControllerTriggerHandler(Trigger module, ItemRegistry itemRegistry, EventPublisher eventPublisher,
76 BundleContext bundleContext) {
79 Configuration config = module.getConfiguration();
81 String inputItemName = (String) requireNonNull(config.get(CONFIG_INPUT_ITEM), "Input item is not set");
82 String setpointItemName = (String) requireNonNull(config.get(CONFIG_SETPOINT_ITEM), "Setpoint item is not set");
85 inputItem = itemRegistry.getItem(inputItemName);
86 } catch (ItemNotFoundException e) {
87 throw new IllegalArgumentException("Configured input item not found: " + inputItemName, e);
91 setpointItem = itemRegistry.getItem(setpointItemName);
92 } catch (ItemNotFoundException e) {
93 throw new IllegalArgumentException("Configured setpoint item not found: " + setpointItemName, e);
96 double outputLowerLimit = getDoubleFromConfig(config, CONFIG_OUTPUT_LOWER_LIMIT);
97 double outputUpperLimit = getDoubleFromConfig(config, CONFIG_OUTPUT_UPPER_LIMIT);
98 double kpAdjuster = getDoubleFromConfig(config, CONFIG_KP_GAIN);
99 double kiAdjuster = getDoubleFromConfig(config, CONFIG_KI_GAIN);
100 double kdAdjuster = getDoubleFromConfig(config, CONFIG_KD_GAIN);
101 double kdTimeConstant = getDoubleFromConfig(config, CONFIG_KD_TIMECONSTANT);
103 loopTimeMs = ((BigDecimal) requireNonNull(config.get(CONFIG_LOOP_TIME), CONFIG_LOOP_TIME + " is not set"))
106 controller = new PIDController(outputLowerLimit, outputUpperLimit, kpAdjuster, kiAdjuster, kdAdjuster,
109 eventFilter = event -> {
110 String topic = event.getTopic();
112 return topic.equals("openhab/items/" + inputItemName + "/state")
113 || topic.equals("openhab/items/" + inputItemName + "/statechanged")
114 || topic.equals("openhab/items/" + setpointItemName + "/statechanged");
117 eventSubscriberRegistration = bundleContext.registerService(EventSubscriber.class.getName(), this, null);
119 eventPublisher.post(ItemEventFactory.createCommandEvent(inputItemName, RefreshType.REFRESH));
121 controllerjob = scheduler.scheduleWithFixedDelay(this::calculate, 0, loopTimeMs, TimeUnit.MILLISECONDS);
124 private <T> T requireNonNull(T obj, String message) {
126 throw new IllegalArgumentException(message);
131 private double getDoubleFromConfig(Configuration config, String key) {
132 return ((BigDecimal) Objects.requireNonNull(config.get(key), key + " is not set")).doubleValue();
135 private void calculate() {
140 input = getItemValueAsNumber(inputItem);
141 } catch (PIDException e) {
142 logger.warn("Input item: {}", e.getMessage());
147 setpoint = getItemValueAsNumber(setpointItem);
148 } catch (PIDException e) {
149 logger.warn("Setpoint item: {}", e.getMessage());
153 long now = System.currentTimeMillis();
155 PIDOutputDTO output = controller.calculate(input, setpoint, now - previousTimeMs);
156 previousTimeMs = now;
158 Map<String, BigDecimal> outputs = new HashMap<>();
160 putBigDecimal(outputs, OUTPUT, output.getOutput());
161 putBigDecimal(outputs, P_INSPECTOR, output.getProportionalPart());
162 putBigDecimal(outputs, I_INSPECTOR, output.getIntegralPart());
163 putBigDecimal(outputs, D_INSPECTOR, output.getDerivativePart());
164 putBigDecimal(outputs, E_INSPECTOR, output.getError());
166 ModuleHandlerCallback localCallback = callback;
167 if (localCallback != null && localCallback instanceof TriggerHandlerCallback) {
168 ((TriggerHandlerCallback) localCallback).triggered(module, outputs);
170 logger.warn("No callback set");
174 private void putBigDecimal(Map<String, BigDecimal> map, String key, double value) {
175 map.put(key, BigDecimal.valueOf(value));
178 private double getItemValueAsNumber(Item item) throws PIDException {
179 State setpointState = item.getState();
181 if (setpointState instanceof Number) {
182 double doubleValue = ((Number) setpointState).doubleValue();
184 if (Double.isFinite(doubleValue)) {
187 } else if (setpointState instanceof StringType) {
189 return Double.parseDouble(setpointState.toString());
190 } catch (NumberFormatException e) {
194 throw new PIDException(
195 "Item type is not a number: " + setpointState.getClass().getSimpleName() + ": " + setpointState);
199 public void receive(Event event) {
200 if (event instanceof ItemStateChangedEvent) {
206 public Set<String> getSubscribedEventTypes() {
207 return SUBSCRIBED_EVENT_TYPES;
211 public @Nullable EventFilter getEventFilter() {
216 public void dispose() {
217 eventSubscriberRegistration.unregister();
219 ScheduledFuture<?> localControllerjob = controllerjob;
220 if (localControllerjob != null) {
221 localControllerjob.cancel(true);