- Intelligent IoT Projects in 7 Days
- Agus Kurniawan
- 171字
- 2021-07-02 18:25:53
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)
推薦閱讀
- 微信公眾平臺與小程序開發:從零搭建整套系統
- Apache Oozie Essentials
- 摩登創客:與智能手機和平板電腦共舞
- JavaScript語言精髓與編程實踐(第3版)
- Android 9 Development Cookbook(Third Edition)
- PyTorch自然語言處理入門與實戰
- C語言最佳實踐
- PHP+MySQL+Dreamweaver動態網站開發實例教程
- Flux Architecture
- Visual Basic程序設計
- NoSQL數據庫原理
- Test-Driven Machine Learning
- 運維前線:一線運維專家的運維方法、技巧與實踐
- Vue.js光速入門及企業項目開發實戰
- Joomla!Search Engine Optimization