-
Notifications
You must be signed in to change notification settings - Fork 0
/
pid.py
160 lines (138 loc) · 4.63 KB
/
pid.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python3
# coding=utf-8
# SPDX-FileCopyrightText: Copyright (c) 2021 Stefan Krüger s-light.eu
#
# SPDX-License-Identifier: MIT
"""
simple pid control.
based on
https://github.com/B3AU/micropython/blob/master/PID.py
and
https://www.embeddedrelated.com/showarticle/943.php
and
http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-direction/
"""
import time
class PID:
"""
Simple PID control.
P: proportional
I: integral
D: derivative
"""
def __init__(
self,
input_fun,
output_fun,
*, # force keyword arguments
update_intervall=0.1,
P_gain=0.0,
I_gain=0.0,
D_gain=0.0,
output_min=0.0,
output_max=100.0,
debug_out_print=False,
debug_out_fun=None,
):
self.input_fun = input_fun
self.output_fun = output_fun
self.update_intervall = update_intervall
self.P_gain = P_gain
self.P_value = 0
self.I_gain = I_gain
self.I_value = 0
self.I_state = 0
self.I_max = 100.0
self.I_min = 0
self.D_gain = D_gain
self.D_value = 0
self.D_state = 0
self.output_min = output_min
self.output_max = output_max
self.debug_out_print = debug_out_print
self.debug_out_fun = debug_out_fun
self.set_point = 0.0
self.output = 0
self.last_update_time = time.monotonic()
def _update(self, current_value):
# calculate the proportional term
self.P_value = self.P_gain * self.error
# calculate the integral state with appropriate limiting
self.I_state += self.error
# Limit the integrator state if necessary
if self.I_state > self.I_max:
self.I_state = self.I_max
elif self.I_state < self.I_min:
self.I_state = self.I_min
# calculate the integral term
self.I_value = self.I_gain * self.I_state
# calculate the derivative term
self.D_value = self.D_gain * (current_value - self.D_state)
self.D_state = current_value
# calculate output
self.output = self.P_value + self.I_value - self.D_value
# limit output
if self.output < self.output_min:
self.output = self.output_min
if self.output > self.output_max:
self.output = self.output_max
if self.debug_out_fun or self.debug_out_print:
debug_out = (
# "set_point: {set_point}\n"
"P_value: {P_value: > 7.2f} "
"I_value: {I_value: > 7.2f} "
"D_value: {D_value: > 7.2f} "
"output: {output: > 7.2f} "
"".format(
# set_point=self.set_point,
P_value=self.P_value,
I_value=self.I_value,
D_value=self.D_value,
output=self.output,
)
)
if self.debug_out_fun:
self.debug_out_fun(debug_out)
if self.debug_out_print:
print(debug_out, end="")
return self.output / 100.0
def update(
self,
*, # force keyword arguments
current_value=None,
set_point=None,
error=None,
):
"""Calculate PID output value."""
output = None
elapsed_time = time.monotonic() - self.last_update_time
if elapsed_time > self.update_intervall:
if not current_value:
current_value = self.input_fun()
if set_point:
self.set_point = set_point
if not error:
self.error = self.set_point - current_value
if self.debug_out_fun or self.debug_out_print:
debug_out = (
"current_value: {current_value: > 7.2f} "
"set_point: {set_point: > 7.2f} "
"error: {error: > 7.2f} "
"".format(
current_value=current_value,
set_point=self.set_point,
error=self.error,
)
)
if self.debug_out_fun:
self.debug_out_fun(debug_out)
if self.debug_out_print:
print(debug_out, end="")
else:
self.error = error
output = self._update(current_value)
self.output_fun(output)
self.last_update_time = time.monotonic()
if self.debug_out_print:
print()
return output