-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pid.py
More file actions
191 lines (143 loc) · 6.46 KB
/
test_pid.py
File metadata and controls
191 lines (143 loc) · 6.46 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""
Unit tests for the PID Controller
Run these tests to verify the PID controller implementation works correctly.
"""
import unittest
import time
from pid_controller import PIDController
class TestPIDController(unittest.TestCase):
"""Test cases for the PID Controller class."""
def setUp(self):
"""Set up test fixtures before each test method."""
self.pid = PIDController(kp=1.0, ki=0.1, kd=0.05, setpoint=10.0)
def test_initialization(self):
"""Test PID controller initialization."""
self.assertEqual(self.pid.kp, 1.0)
self.assertEqual(self.pid.ki, 0.1)
self.assertEqual(self.pid.kd, 0.05)
self.assertEqual(self.pid.setpoint, 10.0)
self.assertEqual(self.pid._integral, 0.0)
self.assertEqual(self.pid._last_error, 0.0)
def test_proportional_only(self):
"""Test proportional control only."""
pid = PIDController(kp=2.0, ki=0.0, kd=0.0, setpoint=10.0)
# Test with process variable = 8, error = 2
output = pid.update(8.0, dt=0.1)
expected = 2.0 * 2.0 # kp * error
self.assertAlmostEqual(output, expected, places=5)
def test_integral_accumulation(self):
"""Test integral term accumulation."""
pid = PIDController(kp=0.0, ki=1.0, kd=0.0, setpoint=10.0)
# First update
output1 = pid.update(8.0, dt=0.1) # error = 2
expected1 = 1.0 * 2.0 * 0.1 # ki * error * dt
self.assertAlmostEqual(output1, expected1, places=5)
# Second update (integral should accumulate)
output2 = pid.update(8.0, dt=0.1) # error = 2 again
expected2 = 1.0 * (2.0 * 0.1 + 2.0 * 0.1) # ki * accumulated_integral
self.assertAlmostEqual(output2, expected2, places=5)
def test_derivative_calculation(self):
"""Test derivative term calculation."""
pid = PIDController(kp=0.0, ki=0.0, kd=1.0, setpoint=10.0)
# First update (derivative should be 0)
output1 = pid.update(8.0, dt=0.1)
self.assertAlmostEqual(output1, 0.0, places=5)
# Second update with different error
output2 = pid.update(6.0, dt=0.1) # error changed from 2 to 4
expected2 = 1.0 * (4.0 - 2.0) / 0.1 # kd * (current_error - last_error) / dt
self.assertAlmostEqual(output2, expected2, places=5)
def test_output_limits(self):
"""Test output limiting functionality."""
pid = PIDController(
kp=10.0, ki=0.0, kd=0.0, setpoint=10.0, output_limits=(-5, 5)
)
# Test upper limit
output = pid.update(0.0, dt=0.1) # Large error should be limited
self.assertLessEqual(output, 5.0)
# Test lower limit
pid.set_setpoint(-10.0)
output = pid.update(10.0, dt=0.1) # Large negative error should be limited
self.assertGreaterEqual(output, -5.0)
def test_setpoint_change(self):
"""Test setpoint modification."""
original_setpoint = self.pid.setpoint
new_setpoint = 20.0
self.pid.set_setpoint(new_setpoint)
self.assertEqual(self.pid.setpoint, new_setpoint)
self.assertNotEqual(self.pid.setpoint, original_setpoint)
def test_tuning_update(self):
"""Test PID parameter updates."""
new_kp, new_ki, new_kd = 2.0, 0.5, 0.2
self.pid.set_tunings(new_kp, new_ki, new_kd)
self.assertEqual(self.pid.kp, new_kp)
self.assertEqual(self.pid.ki, new_ki)
self.assertEqual(self.pid.kd, new_kd)
def test_reset_functionality(self):
"""Test controller reset."""
# Run a few updates to build up integral and history
self.pid.update(5.0, dt=0.1)
self.pid.update(6.0, dt=0.1)
self.pid.update(7.0, dt=0.1)
# Verify some state has been built up
self.assertNotEqual(self.pid._integral, 0.0)
self.assertGreater(len(self.pid.error_history), 0)
# Reset and verify clean state
self.pid.reset()
self.assertEqual(self.pid._integral, 0.0)
self.assertEqual(self.pid._last_error, 0.0)
self.assertEqual(len(self.pid.error_history), 0)
self.assertEqual(len(self.pid.output_history), 0)
def test_get_components(self):
"""Test individual component calculation."""
pid = PIDController(kp=2.0, ki=1.0, kd=0.5, setpoint=10.0)
# Build up some integral
pid.update(8.0, dt=0.1)
# Get components
p, i, d = pid.get_components(8.0, dt=0.1)
# Verify proportional component
self.assertAlmostEqual(p, 2.0 * 2.0, places=5) # kp * error
# Verify integral component
self.assertAlmostEqual(i, 1.0 * 0.2, places=5) # ki * accumulated_integral
def test_history_tracking(self):
"""Test that error and output history is tracked correctly."""
initial_history_length = len(self.pid.error_history)
# Perform several updates
for i in range(5):
self.pid.update(8.0 + i, dt=0.1)
# Verify history is being tracked
self.assertEqual(len(self.pid.error_history), initial_history_length + 5)
self.assertEqual(len(self.pid.output_history), initial_history_length + 5)
self.assertEqual(len(self.pid.time_history), initial_history_length + 5)
def run_simple_test():
"""Run a simple functional test of the PID controller."""
print("Running simple PID controller test...")
# Create a PID controller
pid = PIDController(kp=1.0, ki=0.5, kd=0.1, setpoint=100.0)
# Simulate a simple system response
process_variable = 0.0
dt = 0.1
print(f"{'Time':<6} {'Setpoint':<10} {'Process':<10} {'Error':<10} {'Output':<10}")
print("-" * 55)
for i in range(20):
time_step = i * dt
# Get control output
control_output = pid.update(process_variable, dt)
# Simulate simple first-order system response
# This is a very basic simulation for demonstration
process_variable += control_output * 0.1 * dt
error = pid.setpoint - process_variable
print(
f"{time_step:<6.1f} {pid.setpoint:<10.1f} {process_variable:<10.2f} "
f"{error:<10.2f} {control_output:<10.2f}"
)
print(f"\nFinal process variable: {process_variable:.2f}")
print(f"Final error: {abs(error):.2f}")
if __name__ == "__main__":
print("PID Controller Tests")
print("=" * 50)
# Run unit tests
print("\nRunning unit tests...")
unittest.main(verbosity=2, exit=False)
print("\n" + "=" * 50)
# Run simple functional test
run_simple_test()