-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpm2thrust.py
70 lines (58 loc) · 1.81 KB
/
rpm2thrust.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
import scipy as sp
import numpy as np
import argparse
import matplotlib.pyplot as plt
import cvxpy as cp
from numpy.polynomial import Polynomial as poly
from numpy.polynomial.polynomial import Polynomial
def loadFile(filename):
fileData = np.loadtxt(filename, delimiter=',', skiprows=1, ndmin=2)
return fileData
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("filename", nargs='+')
args = parser.parse_args()
data = None
for f in args.filename:
data_f = loadFile(f)
if data is None:
data = data_f
else:
data = np.vstack((data, data_f))
thrust = data[:,0] / 4 # g, per motor
pwm = data[:,1] # PWM value
vbat = data[:,2] # V, battery voltage,
rpm = np.mean(data[:,3:7],axis=1) # average over all motors
m1 = data[:,3]
m2 = data[:,4]
m3 = data[:,5]
m4 = data[:,6]
vSid = data[:, 7] # Volts at system id deck
amp = data[:, 8] # Amps
pwr = data[:, 9] # Power in watts
fig, ax = plt.subplots(2)
ax[0].plot(m1, label='M1')
ax[0].plot(m2, label='M2')
ax[0].plot(m3, label='M3')
ax[0].plot(m4, label='M4')
ax[0].plot(rpm, label='Mean')
ax[0].set_xlabel('Time')
ax[0].set_ylabel('RPM')
ax[0].legend()
ax[0].grid(True)
ax[1].plot(rpm, thrust, label='data')
ax[1].set_xlabel('rpm')
ax[1].set_ylabel('thrust [g]')
# fit a function (rpm -> thrust)
kw = cp.Variable()
cost = cp.sum_squares(thrust - kw * rpm**2)
prob = cp.Problem(cp.Minimize(cost), [])
prob.solve()
print(kw.value)
fitted = kw.value * rpm**2
ax[1].plot(rpm, fitted, label='fit')
# ax[2].set_xlabel('rpm')
# ax[2].set_ylabel('fitted thrust [g]')
ax[1].legend()
ax[1].grid(True)
plt.show()