Skip to content

Commit e7e9f20

Browse files
committed
remove globals from mayuresh
1 parent abdc0ea commit e7e9f20

2 files changed

Lines changed: 63 additions & 11 deletions

File tree

tools/pidmayuresh.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
import math
33
import concore
44
dT = 0.1
5-
global freq
6-
Prev_Error = 0
7-
I = 0
85

96
sp = concore.tryparam('sp', 67.5)
107
Kp = concore.tryparam('Kp', 0.075)
@@ -14,8 +11,9 @@
1411
sigout = concore.tryparam('sigout',True)
1512
cin = concore.tryparam('cin', 'hr')
1613

17-
def pid_controller(Prev_Error, I, ym):
18-
global freq
14+
def pid_controller(state, ym, sp, Kp, Ki, Kd, sigout, cin, low, up):
15+
Prev_Error = state[0]
16+
I = state[1]
1917
if cin == 'hr':
2018
Error = sp - ym[1]
2119
elif cin == 'map':
@@ -29,24 +27,26 @@ def pid_controller(Prev_Error, I, ym):
2927
amp = Kp*P + Ki*I + Kd*D
3028
Prev_Error = Error
3129
if sigout:
32-
amp = 3.0/(1.0 + math.exp(amp))
33-
ustar = np.array([amp,freq])
34-
return (Prev_Error, I, ustar)
30+
amp = (up-low)/(1.0 + math.exp(amp)) + low
31+
state = [Prev_Error, I]
32+
return (state, amp)
3533

3634

3735
concore.default_maxtime(150)
3836
concore.delay = 0.02
3937
init_simtime_ym = "[0.0, 70.0,91]"
4038
ym = np.array(concore.initval(init_simtime_ym))
39+
state = [0.0, 0.0]
4140
print("Mayuresh's PID controller: sp is "+str(sp))
4241
print(concore.params)
4342
while(concore.simtime<concore.maxtime):
4443
while concore.unchanged():
4544
ym = concore.read(1,"ym",init_simtime_ym)
4645
ym = np.array(ym)
47-
(Prev_Error, I, ustar) = pid_controller(Prev_Error, I, ym)
48-
print(str(concore.simtime) + " u="+str(ustar) + "ym="+str(ym))
49-
concore.write(1,"u",list(ustar),delta=0)
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)
5050

5151

5252

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

0 commit comments

Comments
 (0)