-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathPController6.sol
56 lines (49 loc) · 2.25 KB
/
PController6.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.13;
import "../../number/types/Fixed6.sol";
/// @dev PController6 type
struct PController6 {
UFixed6 k;
UFixed6 max;
}
using PController6Lib for PController6 global;
/// @title PController6Lib
/// @notice Configuration for a the fixed 6-decimal PID controller.
/// @dev Each second, the PID controller's value is incremented by `skew / k`, with `max` as the maximum value.
library PController6Lib {
/// @notice compute the new value and intercept timestamp based on the prior controller state
/// @dev `interceptTimestamp` will never exceed `toTimestamp`
/// @param self the controller configuration
/// @param value the prior value
/// @param skew The prior skew
/// @param fromTimestamp The prior timestamp
/// @param toTimestamp The current timestamp
/// @return newValue the new value
/// @return interceptTimestamp the timestamp at which the value will be at the max
function compute(
PController6 memory self,
Fixed6 value,
Fixed6 skew,
uint256 fromTimestamp,
uint256 toTimestamp
) internal pure returns (Fixed6 newValue, UFixed6 interceptTimestamp) {
// compute the new value without considering the max
Fixed6 newValueUncapped = value.add(
Fixed6Lib.from(int256(toTimestamp - fromTimestamp))
.mul(skew)
.div(Fixed6Lib.from(self.k))
);
// cap the new value at the max
newValue = Fixed6Lib.from(newValueUncapped.sign(), self.max.min(newValueUncapped.abs()));
// compute distance and range to the resultant value
(UFixed6 distance, Fixed6 range) = (UFixed6Lib.from(toTimestamp - fromTimestamp), newValueUncapped.sub(value));
// compute the amount of buffer into the value is outside the max
UFixed6 buffer = value.abs().gt(self.max) ?
UFixed6Lib.ZERO :
Fixed6Lib.from(range.sign(), self.max).sub(value).abs();
// compute the timestamp at which the value will be at the max
interceptTimestamp = range.isZero() ?
UFixed6Lib.from(toTimestamp) :
UFixed6Lib.from(fromTimestamp).add(distance.muldiv(buffer, range.abs())).min(UFixed6Lib.from(toTimestamp));
}
}