forked from m-lundberg/simple-pid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
171432a
commit 2a8e1c0
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/usr/bin/env python | ||
|
||
import time | ||
import matplotlib.pyplot as plt | ||
from simple_pid import PID | ||
|
||
|
||
class WaterBoiler: | ||
""" | ||
Simple simulation of a water boiler which can heat up water | ||
and where the heat dissipates slowly over time | ||
""" | ||
def __init__(self): | ||
self.water_temp = 20 | ||
|
||
def update(self, boiler_power, dt): | ||
if boiler_power > 0: | ||
# boiler can only produce heat, not cold | ||
self.water_temp += 1*boiler_power*dt | ||
|
||
# some heat dissipation | ||
self.water_temp -= 0.02*dt | ||
return self.water_temp | ||
|
||
|
||
if __name__ == '__main__': | ||
boiler = WaterBoiler() | ||
water_temp = boiler.water_temp | ||
|
||
pid = PID(5, 0.01, 0.1, setpoint=water_temp) | ||
pid.output_limits = (0, 100) | ||
|
||
start_time = time.time() | ||
last_time = start_time | ||
|
||
# keep track of values for plotting | ||
setpoint, y, x = [], [], [] | ||
|
||
while time.time() - start_time < 10: | ||
current_time = time.time() | ||
dt = current_time - last_time | ||
|
||
power = pid(water_temp) | ||
water_temp = boiler.update(power, dt) | ||
|
||
x += [current_time-start_time] | ||
y += [water_temp] | ||
setpoint += [pid.setpoint] | ||
|
||
if current_time - start_time > 1: | ||
pid.setpoint = 100 | ||
|
||
last_time = current_time | ||
|
||
plt.plot(x, y, label='measured') | ||
plt.plot(x, setpoint, label='target') | ||
plt.xlabel('time') | ||
plt.ylabel('temperature') | ||
plt.legend() | ||
plt.show() |