Skip to content

Commit 718cb16

Browse files
committed
Create pidmayuresh3.py
1 parent e7e9f20 commit 718cb16

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

tools/pidmayuresh3.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import numpy as np
2+
import math
3+
import concore
4+
dT = 0.1
5+
6+
sp = concore.tryparam('sp', 67.5)
7+
Kp = concore.tryparam('Kp', 0.075)
8+
Ki = concore.tryparam('Ki', 0.02)
9+
Kd = concore.tryparam('Kd', 0.005)
10+
freq = concore.tryparam('freq',30)
11+
sigout = concore.tryparam('sigout',True)
12+
cin = concore.tryparam('cin', 'hr')
13+
14+
def pid_controller(state, ym, sp, Kp, Ki, Kd, sigout, cin, low, up):
15+
Prev_Error = state[0]
16+
I = state[1]
17+
if cin == 'hr':
18+
Error = sp - ym[1]
19+
elif cin == 'map':
20+
Error = sp - ym[0]
21+
else:
22+
print('invalid control input '+cin)
23+
quit()
24+
P = Error
25+
I = I + Error*dT
26+
D = (Error - Prev_Error )/dT
27+
amp = Kp*P + Ki*I + Kd*D
28+
Prev_Error = Error
29+
if sigout:
30+
amp = (up-low)/(1.0 + math.exp(amp)) + low
31+
state = [Prev_Error, I]
32+
return (state, amp)
33+
34+
35+
concore.default_maxtime(150)
36+
concore.delay = 0.02
37+
init_simtime_ym = "[0.0, 70.0,91]"
38+
ym = np.array(concore.initval(init_simtime_ym))
39+
state = [0.0, 0.0]
40+
print("Mayuresh's PID controller: sp is "+str(sp))
41+
print(concore.params)
42+
while(concore.simtime<concore.maxtime):
43+
while concore.unchanged():
44+
ym = concore.read(1,"ym",init_simtime_ym)
45+
ym = np.array(ym)
46+
(state,amp) = pid_controller(state,ym,sp,Kp,Ki,Kd,sigout,cin,0,3)
47+
u = np.array([amp,freq])
48+
print(str(concore.simtime) + " u="+str(u) + "ym="+str(ym))
49+
concore.write(1,"u",list(u),delta=0)
50+
51+
52+

0 commit comments

Comments
 (0)