-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoilgun_utils.py
109 lines (82 loc) · 2.9 KB
/
coilgun_utils.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
import math
import json
def lerp(a, b, f):
return a + f * (b - a)
def get_data_raw(data, index):
return data[index]
def get_data_interpolated(data, desc, current, distance):
# Sign affects if the projectile should be accelerated or decelerated (if the force should become positive or negative)
sign = 1
if distance < 0 or current < 0:
sign = -1
distance = abs(distance)
current = abs(current)
lower_index = math.floor(distance)
upper_index = math.ceil(distance)
if lower_index >= len(data) or upper_index >= len(data):
return (
distance * sign,
data[len(data) - 1][1], # Inductance
0 # Force TODO: Extrapolate force
)
distance_weigth = distance
distance_weigth -= math.ceil(distance_weigth)
currents = desc['Currents']
#assert current <= currents[len(currents) - 1]
data_a = data[lower_index]
data_b = data[upper_index]
# Deal with current interpolation
# TODO: Improve these two if's (select two nearest forces, and extrapolate)
if current < currents[0]:
t = max(current / currents[0], 0)
inductance = lerp(data_a[1], data_b[1], distance_weigth)
force = lerp(0, data_b[2], t) * sign
return (distance * sign, inductance, force)
lower_current_id = 0
for i in range(len(currents)):
if currents[i] < current:
lower_current_id = i
upper_current_id = 0
for i in range(len(currents)):
if currents[i] > current:
upper_current_id = i
break
current_weigth = (current - currents[lower_current_id]) / (currents[upper_current_id] - currents[lower_current_id])
a = data_a[2 + lower_current_id]
b = data_b[2 + upper_current_id]
# Use current, to interpolate between datas
return (
distance * sign, # Distance
lerp(data_a[1], data_b[1], current_weigth), # Inductance is not signed
lerp(a * sign, b * sign, current_weigth) # Force
)
def load_cgdata(coiltype):
coil_data = []
coil_desc = {}
coilfile_base = "./Data/%s" % coiltype
descFile = "%s.%s" % (coilfile_base, 'json')
dataFile = "%s.%s" % (coilfile_base, 'csv')
# Read JSON file
f = open(descFile)
coil_desc = json.load(f)
f.close()
# Read DATA file
f = open(dataFile)
csv_data = f.readlines()
csv_data.remove(csv_data[0])
f.close()
# Parse CSV data
for i in range(len(csv_data)):
line = csv_data[i].replace(" ", "").split(",")
distance = float(line[0])
inductance = float(line[1])
data_entry = []
data_entry.append(distance)
data_entry.append(inductance)
for j in range(len(coil_desc['Currents'])):
force = float(line[2 + j])
data_entry.append(force)
pass
coil_data.append(data_entry)
pass
return [coil_data, coil_desc]