-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpidcontroller.py
executable file
·302 lines (236 loc) · 11 KB
/
pidcontroller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/python3
from os import remove
from epics import caget, caput
from simple_pid import PID
import argparse
from pathlib import Path
from time import sleep, time
import matplotlib.pyplot as plt
ver = "1.1.0"
author = "Valentin Reichenbach"
description = """
TODO: Insert description
"""
epilog = """
Author: Valentin Reichenbach
Version: 1.1.0
License: GPLv3+
"""
def getCurrentVal(args, debugFile: str) -> float:
if args.mode == 'debug':
# Check if the debug file exists
try:
f = open(debugFile, 'r')
val = f.read()
f.close()
except Exception as e:
print('The debug file ' + str(debugFile) + ' was not found')
print('Exception: ', e)
print('Exiting...')
exit()
# BUG: sometimes nothing is read when not in v mode
try:
val = float(val)
except Exception as e:
if args.verbose >= 1:
print('couldn\'t convert val: "' + str(val) + '" to float. Returned 0 instead')
if args.verbose >= 2:
print('Exception: ', e)
val = 0
return val
elif args.mode == 'normal':
# unused
return caget(args.pv)
else:
print('Something went wrong while parsing the arguments\nExiting...')
exit()
def debugMode(args):
debugFile = args.file
args.pv = 'debug'
getCurrentVal(args=args, debugFile=debugFile)
pid = PID(Kp=args.proportional, Kd=args.derivative, Ki=args.integral, setpoint=args.niveau)
x = []
y1 = []
y2 = []
y3 = []
plt.ion() # plot all graphs in the same window
startTime = time()
graphLen = 80
# log options
if args.log == True:
logFile = args.log_file
header = 'PID-Controller Log File\nPV: ' + str(args.pv) + '\nKp: ' + str(args.proportional) + '\nKi: ' + str(args.integral) + '\nKd: ' + str(args.derivative) + '\nNiveau: ' + str(args.niveau) + '\nDelay: ' + str(args.delay) + '\n\n'
with open(logFile, 'w') as l:
l.write(header)
l.write('time, corrected Value, current Value\n')
l.close()
try:
while True:
currentVal = getCurrentVal(args=args, debugFile=debugFile)
fileVal = currentVal
# get new value
correctVal = float(pid(currentVal)) + currentVal
# wrtie new value to debug file
f = open(debugFile, 'w')
f.write(str(correctVal))
f.close()
if args.verbose >= 2:
print('time: ' + str(time()-startTime) + ', corrected Value: ' + str(correctVal) + ', current Value: ' + str(currentVal) + '')
if args.log == True:
logFile = args.log_file
l = open(logFile, 'a')
l.write(str(time() - startTime) + ', ' + str(correctVal) + ', ' + str(currentVal) + '\n')
l.close()
# plotting
if args.visualize == True:
x.append(time() - startTime)
y1.append(correctVal)
y2.append(args.niveau)
y3.append(fileVal)
# makes sure that the graph doesnt grow infinitly
if len(x) >= graphLen:
x.pop(0)
if len(y1) >= graphLen:
y1.pop(0)
if len(y2) >= graphLen:
y2.pop(0)
if len(y3) >= graphLen:
y3.pop(0)
plt.cla() # clear axes
plt.title('correct: ' + str(correctVal))
plt.plot(x, y1, label='Value')
plt.plot(x, y2, label='Niveau')
plt.plot(x, y3, label='Current Value')
plt.legend(loc='upper left')
plt.pause(0.1)
sleep(args.delay)
except KeyboardInterrupt:
print('\nKeyboard interrupt detected\nExiting...')
exit()
def deltaI(deltaX):
return -0.021836 * deltaX
def normalMode(args):
currentVal = caget(args.pv + ':outCur')
pid = PID(Kp=args.proportional, Kd=args.derivative, Ki=args.integral, setpoint=args.niveau)
x = []
y1 = []
y2 = []
y3 = []
plt.ion() # plot all graphs in the same window
startTime = time()
graphLen = 80
# log options
if args.log == True:
logFile = args.log_file
header = 'PID-Controller Log File\nPV: ' + str(args.pv) + '\nKp: ' + str(args.proportional) + '\nKi: ' + str(args.integral) + '\nKd: ' + str(args.derivative) + '\nNiveau: ' + str(args.niveau) + '\nDelay: ' + str(args.delay) + '\n\n'
with open(logFile, 'w') as l:
l.write(header)
l.write('time, current pos, corrected pos, current shift, current current, new current')
l.close()
try:
while True:
f = open('position.txt', 'r')
current_pos = float(f.read())
f.close()
# get new value
corrected_pos = float(pid(current_pos)) + current_pos
pos_shift = corrected_pos - current_pos
current_shift = deltaI(pos_shift)
# write new value to pv
current_current = caget(args.pv + ':outCur')
new_current = current_current - current_shift
caput(args.pv + ':outCur', new_current)
if args.verbose >= 2:
print('time: ' + str(time()-startTime) + ', current_pos: ' + str(current_pos) + ', corrected_pos: ' + str(corrected_pos) + ', current_shift: ' + str(current_shift) + ', current_current: ' + str(current_current) + ', new_current: ' + str(new_current) + '')
if args.log == True:
logFile = args.log_file
l = open(logFile, 'a')
l.write(str(time() - startTime) + ', ' + str(current_pos) + ', ' + str(corrected_pos) + ', ' + str(current_shift) + ', ' + str(current_current) + ', ' + str(new_current) + '\n')
l.close()
# plotting
if args.visualize == True:
x.append(time() - startTime)
y1.append(current_pos)
y2.append(args.niveau)
# makes sure that the graph doesnt grow infinitly
if len(x) >= graphLen:
x.pop(0)
if len(y1) >= graphLen:
y1.pop(0)
if len(y2) >= graphLen:
y2.pop(0)
if len(y3) >= graphLen:
y3.pop(0)
plt.cla() # clear axes
plt.title('PID-Controller')
plt.plot(x, y1, label='Current Position')
plt.plot(x, y2, label='Niveau')
plt.legend(loc='upper left')
plt.pause(0.1)
sleep(args.delay)
except KeyboardInterrupt:
print('\nKeyboard interrupt detected\nExiting...')
exit()
def main():
parser = argparse.ArgumentParser(
description=description, epilog=epilog, formatter_class=argparse.RawDescriptionHelpFormatter)
parentParser = argparse.ArgumentParser('The Parrent parser', add_help=False)
# general options
parentParser.add_argument('--visualize', action='store_true', default=False, help='visualize the changed values live')
parentParser.add_argument('--version', action='version', version=ver)
parentParser.add_argument('-v', '--verbose', action='count', default=0, help='verbose output')
parentParser.add_argument('--force', action='store_true', default=False,
help='forces all checks like the pv connect chec to pass. Additionally every file used (like "log.txt") will be overwritten This is should only be used for development and testing purposes')
# PID controller options
pidControllerOptions = parentParser.add_argument_group('PID-Controller options')
pidControllerOptions.add_argument('-p', '--proportional', type=float, default=0.5,
help='specifys the coefficient for the proportional term')
pidControllerOptions.add_argument('-i', '--integral', type=float, default=0.3,
help='specifys the coefficient for the integral term')
pidControllerOptions.add_argument('-d', '--derivative', type=float, default=0,
help='specifys the coefficient for the derivative term')
pidControllerOptions.add_argument('-n', '--niveau', type=float, default=1,
help='specifys the niveau that the PID controler should aim for')
pidControllerOptions.add_argument('--min', type=float, default=-3,
help='specifys the minimum value for the PID controller')
pidControllerOptions.add_argument('--max', type=float, default=3,
help='specifys the maximum value for the PID controller')
pidControllerOptions.add_argument('-D', '--delay', type=float, default=0.0, help='specifys the delay between the PID controller reading the current value and outputting the new value. This is 0 by default')
# logging
loggingOptions = parentParser.add_argument_group('logging options')
loggingOptions.add_argument('--log', action='store_true', default=False, help='the program will log the used values if this flag is used. the default file is "log.txt"')
loggingOptions.add_argument('--log-file', type=Path, default=Path('log.txt'), help='the file used for logging')
# Subcommands
subparsers = parser.add_subparsers(dest='mode', help='the program can use an epics interface or a debug enviroment controlled by another script')
subparsers.required = True
# normal mode
pvParser = subparsers.add_parser('normal', help='uses the epics interface', parents=[parentParser])
pvParser.add_argument('--pv', type=str, default='I1SV02' ,help='the process variable that should be controlled. The default is I1SV02')
# debug mode
debugParser = subparsers.add_parser('debug', help='uses a test enviroment instead of the epics interface', parents=[parentParser])
debugParser.add_argument('-f', '--file', type=str, default='debugEnv.txt', help='the text file used for simulating the debug enviroment. The default name is "debugEnv.txt"')
args = parser.parse_args()
if args.log == True:
if args.log_file.exists():
if args.force:
print('Log file: ' + str(args.log_file) + ' already exists\nOverwriting...')
remove(args.log_file)
else:
print('Log file: ' + str(args.log_file) + ' already exists')
print('Use the --force flag to overwrite the file')
print('Exiting...')
exit()
args.log_file = str(args.log_file)
try:
args.file = str(args.file)
except:
pass
if args.mode == 'debug':
debugMode(args)
elif args.mode == 'normal':
normalMode(args)
else:
print('Something went wrong while parsing the arguments\nExiting...')
exit()
if __name__ == '__main__':
main()