-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
162 lines (110 loc) · 3.19 KB
/
main.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
import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt
from transforms3d import euler
import matplotlib.patches as mpatches
omega = np.array([0,0,-0.0])
#omega = np.array([0,0,0])
F = np.array([9.8,0,0])
M = np.array([0,0,0.0])
Tburn = 10
Tsim = 4*Tburn
pressision = Tsim*10
#Cd = 0.1
#Cl = 0.001
def Cd(alpha):
#print(alpha*180/np.pi)
#return 0.1+0.01*alpha*180/np.pi if alpha < np.pi/4 else 0.1+0.45
return 0.1 + 1*np.sin(2*alpha)
def Cl(alpha):
#return 0.04*alpha*180/np.pi if alpha < np.pi/4 else 0.04*45
return 0.8*np.sin(2*alpha)
rho = 1.29
Sd = 0.001
Sl = 0.0
S = 0.05
I = np.array([
[1, 0, 0],
[0, 100, 0],
[0, 0, 100]
])
Ipv = np.array([1,100,100])
Rf = np.array([-0.3, 0, 0])
Rt = np.array([-0.3, 0, 0])
R0 = 6400000
g0 = -9.81
m = 1
pos0 = euler.euler2mat(0, 0, np.pi/2, axes='sxyz')
def Thurst(y, t):
thurst = np.array([0,0,0])
if (t < Tburn):
thurst = np.array([20, 1, 0])
return thurst
def dynamics(y, t):
X, Y, Z, Vx, Vy, Vz, gamma, psi, thetta, omega_x, omega_y, omega_z = y
R2 = X**2 + Y**2 + Z**2
R = np.sqrt(R2)
V = np.array([Vx, Vy, Vz])
rot2rocket = euler.euler2mat(gamma, psi, thetta, axes='sxyz')
rot = pos0.dot(rot2rocket)
V_scal = np.sqrt(Vx**2 + Vy**2 + Vz**2)
#Xa = Cd*rho*Sd*(Vx**2 + Vy**2 + Vz**2)/2
#Ya = Cl*rho*Sl*(Vx**2 + Vy**2 + Vz**2)/2
cosAlpha = 1
if (V_scal > 0):
cosAlpha = Vx/V_scal
sinAlpha = np.sqrt(1-cosAlpha**2)
Alpha = np.arcsin(sinAlpha)
Ra = np.array([
-(1/2)*(Cd(Alpha)*cosAlpha - Cl(Alpha)*sinAlpha)*rho*S*V_scal**2,
-(1/2)*(Cd(Alpha)*sinAlpha + Cl(Alpha)*cosAlpha)*rho*S*Vy*V_scal/sinAlpha if sinAlpha > 0 else 0,
-(1/2)*(Cd(Alpha)*sinAlpha + Cl(Alpha)*cosAlpha)*rho*S*Vz*V_scal/sinAlpha if sinAlpha > 0 else 0
])
#print(sinAlpha)
a = np.concatenate([
rot.dot(V),
Thurst(y, t)/m - np.cross(np.array([omega_x, omega_y, omega_z]), V)
+ Ra/m
+ g0*(R0**2/R2)*np.array([X/R, Y/R, Z/R]).dot(rot),
np.array([omega_x, omega_y, omega_z]),
M/Ipv - np.array([
(Ipv[2] - Ipv[1])*omega_y*omega_z/Ipv[0],
(Ipv[0] - Ipv[2])*omega_z*omega_x/Ipv[1],
(Ipv[1] - Ipv[0])*omega_x*omega_y/Ipv[2]
])
+ np.cross(Rf, Ra)/Ipv
+ np.cross(Rt, Thurst(y, t))/Ipv
])
return a
#- np.cross(omega, V)
y0 = [0,R0,0,0,0,0,0,0,0, omega[0], omega[1], omega[2]]
t = np.linspace(0,Tsim,pressision)
sol = integrate.odeint(dynamics, y0, t)
#plt.plot(sol[:, 0], sol[:, 1], 'r', label='trajectory')
#plt.plot(sol[:, 1], sol[:, 3], 'r', label='Vx')
#plt.plot(sol[:, 1], sol[:, 4], 'g', label='Vy')
#plt.plot(t, sol[:, 0], 'g', label='X')
#plt.plot(t, sol[:, 1], 'b', label='Y')
#filter(lambda x: x % 2, sol)
Toff = Tburn/(Tsim/pressision)
Toff = int(Toff)
ax = plt.subplot(111, aspect=1)
ax.plot(sol[0:Toff, 0], sol[0:Toff, 1], 'b', label='active')
ax.plot(sol[Toff-1:, 0], sol[Toff-1:, 1], 'r', label='passive')
earth = mpatches.Circle((0, 0), 6400000, fc="g")
atmosphere = mpatches.Circle((0, 0), 6500000, facecolor='none', edgecolor='r', linestyle='--')
#plt.legend()
ax.add_patch(earth)
ax.add_patch(atmosphere)
"""
plt.plot(t, sol[:, 11], 'g', label='omega')
plt.plot(t, sol[:, 8], 'r', label='thetta')
"""
plt.show()
"""
plt.legend(loc='best')
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
plt.show()
"""