diff --git a/ksp/c/atan_test.c b/ksp/c/atan_test.c new file mode 100644 index 00000000..9cdfbef1 --- /dev/null +++ b/ksp/c/atan_test.c @@ -0,0 +1,23 @@ +#include +#include + +#define PI 3.14159265358979323846264 + +int main(int argc, char **argv) { + + double theta; + double x,y,a; + + for(theta=0;theta<360;theta+=1.0) { + x=sin(theta*PI/180.0); + y=cos(theta*PI/180.0); + if (y<0) a=180.0+((atan(x/y))*180.0/PI); + else a=(atan(x/y))*180.0/PI; + printf("%lf x=%lf y=%lf atan(x/y)=%lf\n", + theta,x,y,a); +// printf("%lf %lf\n",x*600.0,y*600.0); + + } + + return 0; +} diff --git a/ksp/c/ksp_launch.c b/ksp/c/ksp_launch.c index d836b84a..987f929c 100644 --- a/ksp/c/ksp_launch.c +++ b/ksp/c/ksp_launch.c @@ -28,13 +28,28 @@ static void htabvtab(int x,int y) { printf("%c[%d;%dH",27,y,x); } - -/* TODO: want 2d-vectors at least */ - #define KERBIN_RADIUS 600000.0 +int autopilot(double fuel_left, double altitude, double *angle) { + + if (fuel_left>25.0) { + *angle=45.0; + return 1; + } + + if (altitude>KERBIN_RADIUS+40000) { + *angle=100.0; /* actually want tanegent to surface */ + return 1; + } + + return 0; + +} + int main(int argc, char **argv) { + FILE *logfile; + double angle=45; double capsule_mass=1.0; @@ -49,6 +64,7 @@ int main(int argc, char **argv) { double tank_mass=(0.5)*tanks; /* tons */ double fuel_mass=(4.0)*tanks; /* tons */ double total_fuel=fuel_mass; + double fuel_left=100.0; double gravity=-9.8; /* m/s^2 */ double gravity_x=0.0; @@ -57,6 +73,7 @@ int main(int argc, char **argv) { /* Kerbin radius = 600km */ + double rocket_velocity=0.0; double rocket_velocity_x=0.0; /* m/s */ double rocket_velocity_y=0.0; /* m/s */ @@ -64,18 +81,27 @@ int main(int argc, char **argv) { double rocket_acceleration_y=0.0; /* m/s^2 */ double rocket_x=0; - double rocket_y=KERBIN_RADIUS+1000; + double rocket_y=KERBIN_RADIUS+10; double rocket_altitude=KERBIN_RADIUS; /* m */ double deltav,twr; double total_mass,empty_mass; + double v0_x,v0_y; + double time=0.0; /* s */ int stage=1; + int log_step=0; + char input; + int thrusting=1; + + + logfile=fopen("log.jgr","w"); + total_mass=engine_mass+tank_mass+fuel_mass+capsule_mass; empty_mass=total_mass-fuel_mass; @@ -94,43 +120,61 @@ int main(int argc, char **argv) { while(1) { - if (fuel_mass<0.1) { - fuel_mass=0.0; - rocket_acceleration_x=0; - rocket_acceleration_y=0; + fuel_left=fuel_mass*100.0/total_fuel; + thrusting=autopilot(fuel_left, rocket_altitude,&angle); + + if (thrusting) { + if (fuel_mass<0.1) { + fuel_mass=0.0; + rocket_acceleration_x=0; + rocket_acceleration_y=0; + + } + else { + rocket_acceleration_x=(engine_thrust/total_mass)*sin_degrees(angle); + rocket_acceleration_y=(engine_thrust/total_mass)*cos_degrees(angle); + + fuel_mass=fuel_mass-fuel_flow_rate; + total_mass=engine_mass+tank_mass+fuel_mass+capsule_mass; + } } else { - rocket_acceleration_x=(engine_thrust/total_mass)*sin_degrees(angle); - rocket_acceleration_y=(engine_thrust/total_mass)*cos_degrees(angle); - - fuel_mass=fuel_mass-fuel_flow_rate; - total_mass=engine_mass+tank_mass+fuel_mass+capsule_mass; - + rocket_acceleration_x=0.0; + rocket_acceleration_y=0.0; } - gravity_angle=atan(rocket_x/rocket_y); + if (rocket_y<0) gravity_angle=PI+atan(rocket_x/rocket_y); + else gravity_angle=atan(rocket_x/rocket_y); + gravity_y=cos(gravity_angle)*gravity; gravity_x=sin(gravity_angle)*gravity; rocket_acceleration_y+=gravity_y; rocket_acceleration_x+=gravity_x; - rocket_velocity_y=rocket_velocity_y+rocket_acceleration_y*1.0; - rocket_velocity_x=rocket_velocity_x+rocket_acceleration_x*1.0; + /* v=v0+at */ + v0_x=rocket_velocity_x; + v0_y=rocket_velocity_y; - rocket_y=rocket_y+ - (rocket_velocity_y*1.0)+ - 0.5*rocket_acceleration_y*1.0*1.0; + rocket_velocity_y=v0_y+rocket_acceleration_y*1.0; + rocket_velocity_x=v0_x+rocket_acceleration_x*1.0; + rocket_velocity=vector_magnitude(rocket_velocity_x,rocket_velocity_y), - rocket_x=rocket_x+ - (rocket_velocity_x*1.0)+ - 0.5*rocket_acceleration_x*1.0*1.0; + /* deltaX=1/2 (v+v0)t */ + /* could also use deltax=v0t+(1/2)*a*t*t */ + rocket_y=rocket_y+0.5*(v0_y+rocket_velocity_y)*1.0; + rocket_x=rocket_x+0.5*(v0_x+rocket_velocity_x)*1.0; rocket_altitude=vector_magnitude(rocket_x,rocket_y); if (rocket_altitude10) { + log_step=0; + } } + if (logfile) fclose(logfile); + return 0; }