/* Copyright (c) 2015, Louis Winter All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ // GCC -lm -lcurses #include #include #include #include // earth data #define PLANET_RADIUS 6371000 #define PLANET_MASS 5.97219e+24 // physics data #define G 6.673e-11 #define DT 1 #define SPIN 360.0 // cumilitive delta time int t = 0; struct coordinate { // vector components float X; float Y; float Z; float pitch; float yaw; float roll; }; struct stage { // building rocket stages float command; // variable polarity float ve; // exhaust velocity int burn; // burnt mass int thrust; // acting thrust int nominal; // nomical thrust int mass; // stage mass int fuel; // fuel mass }; int crt_monitor(struct coordinate p, struct coordinate v) { // display rocket data erase(); printw("time:\t%d:%d\n", (t % 3600) / 60, t % 60); printw("altitude:\t%6.1fkm\n", (sqrt(p.X*p.X + p.Y*p.Y + p.Z*p.Z) - PLANET_RADIUS) / 1000); printw("speed:\t%6.1fkm/s\n", sqrt(v.X*v.X + v.Y*v.Y + v.Z*v.Z) / 1000); printw("pitch:\t%6.0fdegrees\n",fmodf(p.pitch * (180 / M_PI), SPIN)); printw("yaw:\t%6.0fdegrees\n", fmodf(p.yaw * (180 / M_PI), SPIN)); printw("roll:\t%6.0fdegrees\n", fmodf(p.roll * (180 / M_PI), SPIN)); refresh(); return 0; } int main(void) { // main loop // rocket component position struct coordinate p; p.X = 0; p.Y = PLANET_RADIUS; p.Z = 0; p.pitch = 90 * (M_PI / 180); p.yaw = 0 * (M_PI / 180); p.roll = 0 * (M_PI / 180); // rocket component velocity struct coordinate v; v.X = 0; v.Y = 0; v.Z = 0; // rocket component acceleration struct coordinate a; // R7 Soyuz rocket data struct stage stage1; stage1.command = 1; stage1.ve = 310 * 9.81; stage1.nominal = 991000 * 4; stage1.mass = 3784 * 4; stage1.fuel = 39200 * 4; struct stage stage2; stage2.command = 1; stage2.ve = 311 * 9.81; stage2.nominal = 997000; stage2.mass = 6875; stage2.fuel = 95400; struct stage stage3; stage3.command = 0; stage3.ve = 330 * 9.81; stage3.nominal = 298000; stage3.mass = 2355; stage3.fuel = 21400; int payload_mass = 6560; // start ncurses initscr(); printw("LAUNCHING SOYUZ ROCKET\n"); printw("\nCopyright (c) 2015, Louis Winter\nAll rights reserved.\nLICENSE\n"); refresh(); sleep(2); erase(); refresh; int done = 0; while(done == 0) { if(stage1.fuel < 0) { // stage1 depleted stage1.fuel = 0; stage1.mass = 0; stage1.command = 0; } stage1.thrust = stage1.nominal * stage1.command; if(stage2.fuel < 0) { // stage2 depleted stage2.fuel = 0; stage2.mass = 0; stage2.command = 0; stage3.command = 1; } stage2.thrust = stage2.nominal * stage2.command; if(stage3.fuel < 0) { // stage3 depleted stage3.fuel = 0; stage3.mass = 0; stage3.command = 0; } stage3.thrust = stage3.nominal * stage3.command; // calculate mass changes stage1.burn = stage1.thrust / stage1.ve; stage2.burn = stage2.thrust / stage2.ve; stage3.burn = stage3.thrust / stage3.ve; stage1.fuel -= stage1.burn; stage2.fuel -= stage2.burn; stage3.fuel -= stage3.burn; // calculate total acceleration a.X = (stage1.thrust + stage2.thrust + stage3.thrust) * cos(p.pitch) * cos(p.yaw); a.Y = (stage1.thrust + stage2.thrust + stage3.thrust) * sin(p.pitch); a.Z = (stage1.thrust + stage2.thrust + stage3.thrust) * cos(p.pitch) * sin(p.yaw); // calculate gravity int total_mass = payload_mass + stage1.mass + stage1.fuel + stage2.mass + stage2.fuel + stage3.mass + stage3.fuel; float distance = sqrt(p.X*p.X + p.Y*p.Y + p.Z*p.Z); float gravity = G * (PLANET_MASS * total_mass) / (distance * distance); // calculate delta v and change velocity v.X += (a.X - (p.X / distance) * gravity) / total_mass * DT; v.Y += (a.Y - (p.Y / distance) * gravity) / total_mass * DT; v.Z += (a.Z - (p.Z / distance) * gravity) / total_mass * DT; // update soyuz position p.X += v.X; p.Y += v.Y; p.Z += v.Z; // decide to continue if(distance - PLANET_RADIUS < 0) { // end program erase(); refresh(); endwin(); done = 1; }else { // pause for half a DT, display data usleep((DT*1000000)/2); t += DT; crt_monitor(p,v); } } return 0; }