From 3cb3e52790253858c3d718e67995ac10977ad98c Mon Sep 17 00:00:00 2001 From: Eggert Jung Date: Sat, 5 Sep 2020 04:35:20 +0200 Subject: [PATCH] pid controller and temperature plot in python --- plot.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 plot.py diff --git a/plot.py b/plot.py new file mode 100644 index 0000000..79ae001 --- /dev/null +++ b/plot.py @@ -0,0 +1,61 @@ +# python_live_plot.py + +import time +from simple_pid import PID +from itertools import count +import matplotlib.pyplot as plt +from matplotlib.animation import FuncAnimation + +import minimalmodbus + +tempSens = minimalmodbus.Instrument('/dev/ttyUSB1', 1) +tempSens.serial.baudrate = 38400 + +OutputDriver = minimalmodbus.Instrument('/dev/ttyUSB1', 2) +OutputDriver.serial.baudrate = 38400 + +pid = PID(1, 0.01, 0, setpoint=60) + +plt.style.use('fivethirtyeight') + +x_values = [] +y_values = [] + +index = count() + + +def animate(i): + try: + temp = tempSens.read_float(1) + y_values.append(temp) + x_values.append(next(index)) + + print(temp, end='\t') + control = pid(temp) + if(control>100): + control = 100; + control = (control/1000.0) + print(control, end='\t') + print(0.1-control) + + if(control > 0): + OutputDriver.write_bit(0, 1, functioncode=15) + + time.sleep(control) + + if(control<0.1): + OutputDriver.write_bit(0, 0, functioncode=15) + + time.sleep(0.1-control) + + plt.cla() + plt.scatter(x_values, y_values) + except: + pass + + +ani = FuncAnimation(plt.gcf(), animate, 2000) + + +plt.tight_layout() +plt.show()