-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfinal.py
138 lines (100 loc) · 3.19 KB
/
final.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
import matplotlib.pyplot as plt
import struct
def should_launch_thrust():
sim_with_thrust_1 = struct.unpack('ffff', time_to_reach_ground(1))
sim_with_thrust_05 = struct.unpack('ffff', time_to_reach_ground(0.5))
sim_with_thrust_025 = struct.unpack('ffff', time_to_reach_ground(0.25))
sim_without_thrust = struct.unpack('ffff', time_to_reach_ground(0))
# print("t:", t, "\nwith th:", sim_with_thrust, "\nwithout:", sim_without_thrust)
print(sim_with_thrust_1)
if sim_with_thrust_1[1] - vCurr * (t_step + 1) < 0:
return 1
elif sim_with_thrust_05[1] - vCurr * (t_step + 1) < 0 and hCurr < 1000:
return 0.5
elif sim_with_thrust_025[1] - vCurr * (t_step + 1) < 0 and hCurr < 500:
return 0.25
return 0
def time_to_reach_ground(thrust):
time = -t_step
a_temp = - Tmax * thrust / Ms + g
v_temp = vCurr + a_temp * t_step
h_temp = hCurr - v_temp * t_step
while h_temp >= 0 and v_temp >= 0:
v_temp += a_temp * t_step
h_temp -= v_temp * t_step
time += t_step
# 1 tick more, to be ahead of crush and prefent it. It will couse oscilations.
var = struct.pack('ffff', time, h_temp, v_temp, a_temp)
return var
if __name__ == '__main__':
G = 6.67 * pow(10, -11) # stała grawitacyjna
tSimEnd = 1000
t_step = 0.5 #czas miedzy pomiarami
t = 0
hStart = 6000 # wysokosc poczatkowa
vStart = 10 # predkosc m/s poczatkowa, dodatnia gdy zbliza się
Ms = 1000 # masa łazika
Mp = 6.41 * pow(10, 23) # masa planety
Rp = 3389000 # km promien
g = G * Mp / (Rp * Rp) # grawitacja planety
max_impact_force = 500
hCurr = hStart
vCurr = vStart
aCurr = g
thr = 0
Tmax = 15000 # N maksymalna przepustnica
Fmax = - Tmax / Ms + g
#tablice dla wykresów
dt = []
h = []
v = [] # na plusie, gdy spada
a = []
thrust_applied = []
while t < tSimEnd:
dt.append(t)
a.append(aCurr)
v.append(vCurr)
h.append(hCurr)
thrust_applied.append(thr)
thr = should_launch_thrust()
aCurr = thr * -Tmax / Ms + g
print("t:", t, "h:", hCurr, "applying thrust:", thr)
vCurr += aCurr * t_step
hCurr -= vCurr * t_step
t += t_step
if hCurr <= 0:
impact_force = vCurr * Ms
if impact_force > max_impact_force:
print("Crush!")
else:
print("Landed!")
print("With speed:",vCurr,"and impact force of:",impact_force)
break
plt.subplot(1, 4, 1)
plt.plot(dt, h)
plt.title("Wysokość")
plt.xlabel("Czas [s]")
plt.ylabel("Wysokość [m]")
plt.grid(True)
#
plt.subplot(1, 4, 2)
plt.plot(dt, v)
plt.title("prędkość")
plt.xlabel("Czas [s]")
plt.ylabel("v")
plt.grid(True)
plt.subplot(1, 4, 3)
plt.plot(dt, a)
# plt.legend(["u_pi", "u"])
plt.title("przyspieszenie")
plt.xlabel("Czas [s]")
plt.ylabel("a")
plt.grid(True)
plt.subplot(1, 4, 4)
plt.plot(dt, thrust_applied)
# plt.legend(["u_pi", "u"])
plt.title("ciąg")
plt.xlabel("Czas [s]")
plt.ylabel("thr")
plt.grid(True)
plt.show()