Skip to content

Commit

Permalink
Added water boiler example
Browse files Browse the repository at this point in the history
  • Loading branch information
m-lundberg committed Jun 3, 2018
1 parent 171432a commit 2a8e1c0
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions examples/water_boiler.py
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()

0 comments on commit 2a8e1c0

Please sign in to comment.