• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

temporalio / sdk-java / #272

21 Jun 2024 08:17PM UTC coverage: 77.548% (+0.04%) from 77.506%
#272

push

github

web-flow
Resource based tuner (#2110)

275 of 338 new or added lines in 11 files covered. (81.36%)

12 existing lines in 5 files now uncovered.

19522 of 25174 relevant lines covered (77.55%)

0.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.1
/temporal-sdk/src/main/java/io/temporal/worker/tuning/PIDController.java
1
/*
2
 * Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3
 *
4
 * Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5
 *
6
 * Modifications copyright (C) 2017 Uber Technologies, Inc.
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this material except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *   http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20

21
package io.temporal.worker.tuning;
22

23
/**
24
 * A simple PID closed control loop. <br>
25
 * <br>
26
 * License : MIT
27
 *
28
 * @author Charles Grassin
29
 */
30
class PIDController {
31
  // PID coefficients
32
  private double setPoint;
33
  private double kP, kI, kD;
34

35
  /** Limit bound of the output. */
36
  private double minLimit = Double.NaN, maxLimit = Double.NaN;
1✔
37

38
  // Dynamic variables
39
  private double previousTime = Double.NaN;
1✔
40
  private double lastError = 0;
1✔
41
  private double integralError = 0;
1✔
42

43
  /**
44
   * Constructs a new PID with set coefficients.
45
   *
46
   * @param setPoint The initial target value.
47
   * @param kP The proportional gain coefficient.
48
   * @param kI The integral gain coefficient.
49
   * @param kD The derivative gain coefficient.
50
   */
51
  PIDController(final double setPoint, final double kP, final double kI, final double kD) {
1✔
52
    this.setSetpoint(setPoint);
1✔
53
    this.kP = kP;
1✔
54
    this.kI = kI;
1✔
55
    this.kD = kD;
1✔
56
  }
1✔
57

58
  /**
59
   * Updates the controller with the current time and value and outputs the PID controller output.
60
   *
61
   * @param currentTime The current time (in arbitrary time unit, such as seconds). If the PID is
62
   *     assumed to run at a constant frequency, you can simply put '1'.
63
   * @param currentValue The current, measured value.
64
   * @return The PID controller output.
65
   */
66
  double getOutput(final double currentTime, final double currentValue) {
67
    final double error = setPoint - currentValue;
1✔
68
    final double dt = (!Double.isNaN(previousTime)) ? (currentTime - previousTime) : 0;
1✔
69

70
    // Compute Integral & Derivative error
71
    final double derivativeError = (dt != 0) ? ((error - lastError) / dt) : 0;
1✔
72
    integralError += error * dt;
1✔
73

74
    // Save history
75
    previousTime = currentTime;
1✔
76
    lastError = error;
1✔
77

78
    return checkLimits((kP * error) + (kI * integralError) + (kD * derivativeError));
1✔
79
  }
80

81
  /** Resets the integral and derivative errors. */
82
  void reset() {
83
    previousTime = 0;
1✔
84
    lastError = 0;
1✔
85
    integralError = 0;
1✔
86
  }
1✔
87

88
  /**
89
   * Bounds the PID output between the lower limit and the upper limit.
90
   *
91
   * @param output The target output value.
92
   * @return The output value, bounded to the limits.
93
   */
94
  private double checkLimits(final double output) {
95
    if (!Double.isNaN(minLimit) && output < minLimit) return minLimit;
1✔
96
    else if (!Double.isNaN(maxLimit) && output > maxLimit) return maxLimit;
1✔
97
    else return output;
1✔
98
  }
99

100
  // Getters & Setters
101

102
  /**
103
   * Sets the output limits of the PID controller. If the minLimit is superior to the maxLimit, it
104
   * will use the smallest as the minLimit.
105
   *
106
   * @param minLimit The lower limit of the PID output.
107
   * @param maxLimit The upper limit of the PID output.
108
   */
109
  void setOuputLimits(final double minLimit, final double maxLimit) {
NEW
110
    if (minLimit < maxLimit) {
×
NEW
111
      this.minLimit = minLimit;
×
NEW
112
      this.maxLimit = maxLimit;
×
113
    } else {
NEW
114
      this.minLimit = maxLimit;
×
NEW
115
      this.maxLimit = minLimit;
×
116
    }
NEW
117
  }
×
118

119
  /** Removes the output limits of the PID controller */
120
  void removeOuputLimits() {
NEW
121
    this.minLimit = Double.NaN;
×
NEW
122
    this.maxLimit = Double.NaN;
×
NEW
123
  }
×
124

125
  /**
126
   * @return the kP parameter
127
   */
128
  public double getkP() {
NEW
129
    return kP;
×
130
  }
131

132
  /**
133
   * @param kP the kP parameter to set
134
   */
135
  void setkP(double kP) {
NEW
136
    this.kP = kP;
×
NEW
137
    reset();
×
NEW
138
  }
×
139

140
  /**
141
   * @return the kI parameter
142
   */
143
  double getkI() {
NEW
144
    return kI;
×
145
  }
146

147
  /**
148
   * @param kI the kI parameter to set
149
   */
150
  void setkI(double kI) {
NEW
151
    this.kI = kI;
×
NEW
152
    reset();
×
NEW
153
  }
×
154

155
  /**
156
   * @return the kD parameter
157
   */
158
  double getkD() {
NEW
159
    return kD;
×
160
  }
161

162
  /**
163
   * @param kD the kD parameter to set
164
   */
165
  void setkD(double kD) {
NEW
166
    this.kD = kD;
×
NEW
167
    reset();
×
NEW
168
  }
×
169

170
  /**
171
   * @return the setPoint
172
   */
173
  double getSetPoint() {
NEW
174
    return setPoint;
×
175
  }
176

177
  /**
178
   * Establishes a new set point for the PID controller.
179
   *
180
   * @param setPoint The new target point.
181
   */
182
  void setSetpoint(final double setPoint) {
183
    reset();
1✔
184
    this.setPoint = setPoint;
1✔
185
  }
1✔
186
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc