Repository navigation
Expand file tree
/
Copy pathrocket_flight_simulator.cpp
More file actions
148 lines (118 loc) · 3.96 KB
/
Copy pathrocket_flight_simulator.cpp
File metadata and controls
148 lines (118 loc) · 3.96 KB
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
#include <iostream>
#include <cmath>
#include <vector>
#include <chrono> //obsluga czasu
#include <thread> //zatrzymanie petli
//klasa ma pobierac wysokosc, predkosc, mase paliwa i ciag silnika
class Rocket {
private:
double height = 0.0;
double velocity = 0.0;
double fuel_mass = 0.0;
double acceleration = 0.0;
double rocket_mass = 0.0;
double total_mass = 0.0;
double fuel_burn_rate = 0.0;
double exhaust_velocity = 0.0;
double thrust = 0.0;
double rocket_diameter = 0.0;
double drag_coefficient = 0.0;
std::vector<double> position;
public:
void load_Data(){
std::cout<<"Add fuel mass of the rocket: \n";
std::cin>>fuel_mass;
std::cout<<"Add rocket mass: \n";
std::cin>>rocket_mass;
std::cout<<"Add fuel burn rate per second: \n";
std::cin>>fuel_burn_rate;
std::cout<<"Add exhaust velocity: \n";
std::cin>>exhaust_velocity;
std::cout<<"Add rocket diameter: \n";
std::cin>>rocket_diameter;
std::cout<<"Add drag coefficient: \n";
std::cin>>drag_coefficient;
}
double get_fuel_mass() {
return fuel_mass;
}
double get_height(){
return height;
}
//wysokosc rakiety
double altitude(){
height = height + velocity;
if (height < 0) {
height = 0;
}
return height;
}
//calkowita masa
double rocket_total_mass(){
total_mass = rocket_mass + fuel_mass;
return total_mass;
}
//opor atmosfery i powietrza
double air_density(double height) {
double rho0 = 1.225; //gestosc przy powierzchni ziemi
double H = 8500.0; // wysokosc skali dla ziemi
double rho = rho0 * std::exp(-height / H);
return rho;
}
void update_thrust() {
if (fuel_mass > 0) {
thrust = exhaust_velocity * fuel_burn_rate;
}
else {
thrust = 0.0; // no fuel used or left
}
}
// dzielenie thrust-masa zeby obliczyc przyspieszenie
double rocket_acceleration(){
rocket_total_mass(); // musimy update total_mass
double radius = rocket_diameter / 2.0;
double area = rocket_mass * pow(radius,2);
double drag = 0.5 * air_density(height) * (velocity *std::abs(velocity)) * drag_coefficient * area;
double net_force = thrust - drag - (rocket_total_mass() * 9.81);
acceleration = net_force/total_mass;
return acceleration;
}
double update_velocity() { //przy zmianie
velocity = velocity + acceleration;
return velocity;
}
//sila przeciazenia
double g_force(){ // przy zmianie mikrokontroller ktory pobiera g force i wprowadza ja tu
return acceleration/9.81;
}
//jak rakieta staje sie lzejsza przy utracie - aktualna masa
double rocket_weight_loss(){
if (fuel_mass > 0) {
fuel_mass = fuel_mass - fuel_burn_rate; //odejmujemy spalane paliwo od calego paliwa
if (fuel_mass < 0){
fuel_mass = 0; // paliwo nie moze byc ujemne
}
}
total_mass = rocket_mass + fuel_mass; //nowa masa rakiety to masa rakiety plus nowa masa paliwa.
return total_mass;
}
//
};
int main(){
Rocket Debris;
Debris.load_Data();
int second = 1;
while(second == 1 || Debris.get_height() > 0 || Debris.get_fuel_mass() > 0) {
//aktualizacja masy inaczej a bedzie liczone dla starej masy
Debris.rocket_weight_loss();
Debris.update_thrust();
//liczymy parametry lotu
double h = Debris.altitude();
double a = Debris.rocket_acceleration();
double v = Debris.update_velocity();
std::cout<<"Second "<<second<< " | Altitude: "<< h << "m | Velocity: "<< v <<" | Acceleration: "<< a << "m/s^2 | G Force: " << Debris.g_force() << "\n";
//
second++;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}