- 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)
推薦閱讀
- 計算機網(wǎng)絡
- Android應用程序開發(fā)與典型案例
- Web Scraping with Python
- PHP網(wǎng)絡編程學習筆記
- 人人都懂設(shè)計模式:從生活中領(lǐng)悟設(shè)計模式(Python實現(xiàn))
- 精通網(wǎng)絡視頻核心開發(fā)技術(shù)
- KnockoutJS Starter
- Spring快速入門
- Regression Analysis with Python
- H5+移動營銷設(shè)計寶典
- AutoCAD基礎(chǔ)教程
- Node.js 6.x Blueprints
- 和孩子一起學編程:用Scratch玩Minecraft我的世界
- Pandas 1.x Cookbook
- Instant Pygame for Python Game Development How-to