官术网_书友最值得收藏!

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)
主站蜘蛛池模板: 奈曼旗| 盐亭县| 绥芬河市| 公安县| 五台县| 清河县| 太原市| 防城港市| 平山县| 贵州省| 平谷区| 西宁市| 尖扎县| 林甸县| 始兴县| 延安市| 塔河县| 阿拉善左旗| 牡丹江市| 金湖县| 进贤县| 高密市| 临汾市| 卓尼县| 怀柔区| 灌南县| 安岳县| 金华市| 潢川县| 元江| 丽江市| 靖边县| 交口县| 彝良县| 滦南县| 衡山县| 贵溪市| 安丘市| 鹤岗市| 潞城市| 吉林省|