-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathautotuner_config.py
249 lines (201 loc) · 8.91 KB
/
autotuner_config.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
import json
import os.path
import sys
import shutil
class autotuner_config:
def __init__(self):
self.file_path = '/data/autotuner.json'
def get(self, key, default):
if not os.path.exists(self.file_path): #create dictionary if it doesn't exist
dictionary = {
}
with open(self.file_path, 'w', encoding='utf8') as file:
json.dump(dictionary, file, indent=2, sort_keys=True, separators=(',', ': '), ensure_ascii=False)
with open(self.file_path, 'r') as file:
config_data = json.loads(file.read())
try:
if len(config_data[key]) == 0: #if key is empty, use default from OP
return default
return config_data[key]
except KeyError: # if key doesn't exist, use default
if not os.path.exists('/data/autotuner/queues/'):
os.mkdir('/data/autotuner/queues/')
new_queue_file = "/data/autotuner/queues/" + str(key) + ".queue" #also queue it in a separate file, to be processed via bash
if not os.path.exists(new_queue_file): #make sure it wasn't already created for queue
dictionary = {
}
dictionary[str(key)] = str(default)
with open(new_queue_file, 'w', encoding='utf8') as file:
json.dump(dictionary, file, indent=2, sort_keys=True, separators=(',', ': '), ensure_ascii=False)
return default
def bash_new_queue(self, from_file):
with open(self.file_path, 'r') as file: #read our configuration
config_data = json.loads(file.read())
with open(from_file, 'r') as file: #get the queue to append
new_data = json.loads(file.read())
new_dictionary = dict(list(config_data.items()) + list(new_data.items()))
with open('/data/autotuner.tmp', 'w', encoding='utf8') as file: #write new config to a temporary file
json.dump(new_dictionary, file, indent=2, sort_keys=True, separators=(',', ': '), ensure_ascii=False)
shutil.move('/data/autotuner.tmp', '/data/autotuner.json') #change it as main config file
def resetall_config(self):
dictionary = {
"reset_defaults": "1"
}
with open('/data/autotuner.tmp', 'w', encoding='utf8') as file: #write new config to a temporary file
json.dump(dictionary, file, indent=2, sort_keys=True, separators=(',', ': '), ensure_ascii=False)
file.flush()
shutil.move('/data/autotuner.tmp', '/data/autotuner.json') #change it as main config file
#FROM BASH ARGUMENTS
if len(sys.argv) > 1:
autotuner_config = autotuner_config()
if not os.path.exists('/data/autotuner.json'):
dictionary = {
}
with open('/data/autotuner.json', 'w', encoding='utf8') as file:
json.dump(dictionary, file, indent=2, sort_keys=True, separators=(',', ': '), ensure_ascii=False)
with open('/data/autotuner.json', 'r') as file: #read our configuration
config_data = json.loads(file.read())
for i in range(1, len(sys.argv)):
if sys.argv[i] == "DEFAULT_LIST":
try:
BPV_list = "[[0, 2560, 8000], [0, 2560, 3840]]"
config_data['torqueBPV'] = str(BPV_list)
except KeyError:
continue
if sys.argv[i] == "SET.TONY":
try:
BPV_list = "[[0, 2327, 3525, 4119, 4511, 5131, 10435, 21091, 23755], [0, 512, 768, 1144, 1516, 2048, 2560, 3584, 3840]]"
config_data['torqueBPV'] = str(BPV_list)
except KeyError:
continue
if sys.argv[i] == "RESET":
autotuner_config.resetall_config()
raise SystemExit()
if sys.argv[i] == "BASH_NEW_QUEUE":
autotuner_config.bash_new_queue(sys.argv[2])
raise SystemExit()
if sys.argv[i] == "SET.BP":
try:
BPV_list = eval(config_data['torqueBPV'])
BPV_list[0][-1] = round(float(sys.argv[i+1]))
config_data['torqueBPV'] = str(BPV_list)
except KeyError:
continue
if sys.argv[i] == "SET.V":
try:
BPV_list = eval(config_data['torqueBPV'])
BPV_list[1][-1] = round(float(sys.argv[i+1]))
config_data['torqueBPV'] = str(BPV_list)
except KeyError:
continue
if sys.argv[i] == "SET.MP":
try:
BPV_list = eval(config_data['torqueBPV'])
if ( len(BPV_list[0]) != 3 ):
print ("Only 3 value lists are supported")
continue
if ( len(BPV_list[1]) != 3 ):
print ("Only 3 value lists are supported")
continue
BPV_list[0][1] = round(float(sys.argv[i+1]))
BPV_list[1][1] = round(float(sys.argv[i+1]))
config_data['torqueBPV'] = str(BPV_list)
except KeyError:
continue
if sys.argv[i] == "SET.MP1":
BPV_list = eval(config_data['torqueBPV'])
try:
if ( len(BPV_list[0]) != 3 ):
print ("Only 3 value lists are supported")
continue
BPV_list[0][1] = round(float(sys.argv[i+1]))
config_data['torqueBPV'] = str(BPV_list)
except KeyError:
continue
if sys.argv[i] == "SET.MP2":
BPV_list = eval(config_data['torqueBPV'])
try:
if ( len(BPV_list[1]) != 3 ):
print ("Only 3 value lists are supported")
continue
BPV_list[1][1] = round(float(sys.argv[i+1]))
config_data['torqueBPV'] = str(BPV_list)
except KeyError:
continue
if sys.argv[i] == "INC.MP1":
try:
BPV_list = eval(config_data['torqueBPV'])
if ( len(BPV_list[0]) != 3 ):
print ("Only 3 value lists are supported")
continue
BPV_list[0][1] = BPV_list[0][1] + int(sys.argv[i+1])
config_data['torqueBPV'] = str(BPV_list)
KpKi_list = eval(config_data['pidKpKi'])
print ("\n New tune set!\n " + str(BPV_list) + "\n " + str(KpKi_list) + "\n")
except KeyError:
continue
if sys.argv[i] == "SET.KP":
try:
KpKi_list = eval(config_data['pidKpKi'])
KpKi_list[0][0] = float(sys.argv[i+1])
config_data['pidKpKi'] = str(KpKi_list)
except KeyError:
continue
if sys.argv[i] == "SET.KI":
try:
KpKi_list = eval(config_data['pidKpKi'])
KpKi_list[1][0] = float(sys.argv[i+1])
config_data['pidKpKi'] = str(KpKi_list)
except KeyError:
continue
if sys.argv[i] == "SET.KPDIV":
try:
KpDIV = float(sys.argv[i+1])
except KeyError:
continue
if sys.argv[i] == "TUNEKP":
try:
KpKi_list = eval(config_data['pidKpKi'])
KpKi_list[0][0] = float(sys.argv[i+1])
config_data['pidKpKi'] = str(KpKi_list)
except KeyError:
continue
if sys.argv[i] == "SET.KF":
try:
config_data['kf'] = str(sys.argv[i+1])
except KeyError:
continue
###############################################################################################
try: # CONFIGURE BP/V/Kp/Ki #
if "TUNEKP" in str(sys.argv): # THEN PRINT RESULT #
BPV_list = eval(config_data['torqueBPV']) ########################
KpKi_list = eval(config_data['pidKpKi'])
KP_DIV = float(KpDIV) if "KPDIV" in str(sys.argv) else float(3)
if not "SET.BP" in str(sys.argv):
if ( len(BPV_list[0]) == 3 ):
BPV_list[0][-1] = round(float(BPV_list[1][-1]) * 0.8 / float(KpKi_list[0][0]))
config_data['torqueBPV'] = str(BPV_list)
KpKi_list[1][0] = round(float(KpKi_list[0][0]) / KP_DIV, 5)
config_data['pidKpKi'] = str(KpKi_list)
if "SET.BP" in str(sys.argv) or \
"SET.V" in str(sys.argv) or \
"SET.MP" in str(sys.argv) or \
"SET.KP" in str(sys.argv) or \
"SET.KI" in str(sys.argv) or \
"SET.KPDIV" in str(sys.argv) or \
"TUNEKP" in str(sys.argv):
BPV_list = eval(config_data['torqueBPV'])
KpKi_list = eval(config_data['pidKpKi'])
print ("\n New tune set!\n " + str(BPV_list) + "\n " + str(KpKi_list) + "\n")
###############################################################################################
except KeyError:
print ("\x1b[1;31m\n Missing torqueBP, torqueV, kpV, kiV sets, make sure engine is")
print (" running so keys will be generated\n\x1b[0m")
###############################################################################################
######################################################################################################################
with open('/data/autotuner.tmp', 'w', encoding='utf8') as file: # UPDATE JSON #
json.dump(config_data, file, indent=2, sort_keys=True, separators=(',', ': '), ensure_ascii=False) ###############
file.flush()
shutil.move("/data/autotuner.tmp", "/data/autotuner.json") #change it as main config file
######################################################################################################################
######################################################################################################################