forked from s2mLab/ControlOdrive
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomputation_2.py
68 lines (56 loc) · 2.51 KB
/
computation_2.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
import json
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
resisting_current_proportional = []
resisting_current_constant = []
x = np.zeros((2, 12))
h = {
"h": 6,
"5": 5,
"4": 4,
"3": 3,
"2": 2,
# "1": 1,
"l": 0,
}
bike = {"10": 11, "8": 15, "6": 19, "4": 25, "3": 28, "2": 32, "1": 36}
for ht in h.keys():
if ht == "h":
for bk in bike.keys():
with open(f"./calibration_files/resisting_current_load_h{ht}_b{bk}.json", "r") as f:
data = json.load(f)
resisting_current_proportional.append(data["a"])
resisting_current_constant.append(data["b"])
x[0, len(resisting_current_proportional) - 1] = h[ht]
x[1, len(resisting_current_proportional) - 1] = bike[bk]
else:
with open(f"./calibration_files/resisting_current_load_h{ht}_b10.json", "r") as f:
data = json.load(f)
resisting_current_proportional.append(data["a"])
resisting_current_constant.append(data["b"])
x[0, len(resisting_current_proportional) - 1] = h[ht]
x[1, len(resisting_current_proportional) - 1] = bike["10"]
def find_pig(x, coeff_pig, coeff_ht, coeff_cst):
return coeff_pig / x[1] + coeff_ht * x[0] + coeff_cst
popt_constant = curve_fit(find_pig, x, resisting_current_constant, p0=[-2.53, -0.023, 0.0])
popt_proportional = curve_fit(find_pig, x, resisting_current_proportional, p0=[-2.53, -0.023, 0.0])
print(popt_constant[0])
plt.plot(x[0, :], resisting_current_constant, "o")
plt.plot(x[0, :], find_pig(x, *popt_constant[0]), "o")
plt.show()
plt.plot(x[1, :], resisting_current_constant, "o")
plt.plot(x[1, :], find_pig(x, *popt_constant[0]), "o")
plt.show()
with open("./ergocycleS2M/parameters/hardware_and_security.json", "r") as f:
hardware_and_security = json.load(f)
hardware_and_security["resisting_current_cst_gear"] = popt_constant[0][0]
hardware_and_security["resisting_current_cst_hometrainer"] = popt_constant[0][1]
hardware_and_security["resisting_current_cst_constant"] = popt_constant[0][2]
hardware_and_security["resisting_current_prop_gear"] = popt_proportional[0][0]
hardware_and_security["resisting_current_prop_hometrainer"] = popt_proportional[0][1]
hardware_and_security["resisting_current_prop_constant"] = popt_proportional[0][2]
# Writing to .json
json_object = json.dumps(hardware_and_security, indent=4)
with open("./ergocycleS2M/parameters/hardware_and_security.json", "w") as outfile:
outfile.write(json_object)