上QQ阅读APP看书,第一时间看更新
How it works
First, we define our PID parameters:
P = 1.4
I = 1
D = 0.001
pid = PID.PID(P, I, D)
pid.SetPoint = 0.0
pid.setSampleTime(0.01)
total_sampling = 100
feedback = 0
feedback_list = []
time_list = []
setpoint_list = []
After that, we compute the PID value during sampling. In this case, we set the desired output values as follows:
- Output 1 for sampling from 20 to 60
- Output 0.5 for sampling from 60 to 80
- Output 1.3 for sampling more than 80
for i in range(1, total_sampling):
pid.update(feedback)
output = pid.output
if pid.SetPoint > 0:
feedback += (output - (1 / i))
if 20 < i < 60:
pid.SetPoint = 1
if 60 <= i < 80:
pid.SetPoint = 0.5
if i >= 80:
pid.SetPoint = 1.3
time.sleep(0.02)
feedback_list.append(feedback)
setpoint_list.append(pid.SetPoint)
time_list.append(i)
The last step is to generate a report and save it into a file called result.png:
time_sm = np.array(time_list)
time_smooth = np.linspace(time_sm.min(), time_sm.max(), 300)
feedback_smooth = spline(time_list, feedback_list, time_smooth)
fig1 = plt.gcf()
fig1.subplots_adjust(bottom=0.15)
plt.plot(time_smooth, feedback_smooth, color='red')
plt.plot(time_list, setpoint_list, color='blue')
plt.xlim((0, total_sampling))
plt.ylim((min(feedback_list) - 0.5, max(feedback_list) + 0.5))
plt.xlabel('time (s)')
plt.ylabel('PID (PV)')
plt.title('TEST PID')
plt.grid(True)
print("saving...")
fig1.savefig('result.png', dpi=100)