我正在开发一个月球着陆器程序,该程序将计算出像月球一样的火箭。该程序有手动和自动驾驶两种模式。自动驾驶功能不起作用,因为它不知道地面是什么,或者根本不知道应该停在哪里才能成功。
问题是我不知道在哪里解决这个问题,也不知道从哪里开始。我所知道的是,当选择自动驾驶仪并运行垃圾数据时。
代码如下:
#include <stdio.h>
#include <math.h>
#include <string>
#include <ctype.h>
#pragma warning(disable:4996)
struct _State //different to shorten variable names
{
double mass;
double thrust;
double accel;
double velo;
double distance;
double fuel;
char gameMode; // variable for keeping track of selected mode (A - 65 - Autopilot / M - 77 - Manual)
};
typedef struct _State State;
double getThrust(State* s) // Accepts user input for thrust and validates it
{
double APstate = 0;
if (s->gameMode == 77)
{
printf("Input Thrust bellow. \n--> ");
scanf("%lf", &s->thrust);
while (s->fuel > 0)
{
if (s->thrust < 0)
{
return 0;
}
else if (s->thrust > 45)
{
return 45000;
}
else
return (s->thrust * 1000);
}
if (s->fuel < 0)
{
return 0;
printf("You have run out of fuel, so you can no longer use thrust./n");
}
}
if (s->gameMode == 65)
{
if (s->distance > 5)
{
APstate = ((s->velo * s->velo) - .81) / (2 * (s->distance - 5));
s->thrust = s->mass * (APstate - (-1.6));
}
else if (s->distance < 5)
{
s->thrust = -(s->mass) * (-1.6) * ((s->velo) / 0.9);
}
s->fuel = s->fuel - (s->thrust / 3000);
if (s->fuel <= 0)
{
return 0;
}
if (s->distance <= 0)
{
printf("The lander has landed successfully at a velocity of %.2f.\n", s->velo);
return 0;
}
}
}
void Update(State* s, double t, double stepSize)
{
const double gMoon = -1.6;
//double mass = 9000;
//double fuel = 1800;
s->thrust = getThrust(s);
s->mass = s->mass - (s->thrust / 3000);
s->accel = (s->thrust / s->mass) + gMoon;
s->velo += s->accel * stepSize;
s->distance += s->velo * stepSize;
s->fuel = s->fuel - (s->thrust / 3000);
}
void manualControl(State* s) //manual controll function
{
printf("\nManual mode: on\n");
printf("-----------------\n\n");
if (s->distance == 0) // Sets height value to the initial height value for calculations
{
s->distance = 15000;
}
if (s->velo == 0) // Sets velocity value to the initial velocity value for calculations
{
s->velo = -325;
}
}
void autopilotControl(State* s) // Autopilot controll function
{
printf("\nAutopilot: on\n");
printf("-----------------\n\n");
if (s->distance == 0)
{
s->distance = 15000;
}
if (s->velo == 0)
{
s->velo = -325;
}
}
void modeHandler(State* s) // mode controller. Calls appropriate function depending on the mode selected
{
if (s->gameMode == 65)
{
autopilotControl(s);
}
else if (s->gameMode == 77)
{
manualControl(s);
}
}
void modeSelect(State* s) // modeSelect allows user to pick the game mode and validates their input.
{
printf("--> ");
scanf("%c", &s->gameMode);
s->gameMode = toupper(s->gameMode);
if (s->gameMode == 65 || s->gameMode == 77)
{
modeHandler(s);
}
else
{
printf("\nInvalid input. Please try again!\n");
scanf("%c", &s->gameMode);
s->gameMode = toupper(s->gameMode);
modeSelect(s);
}
}
int main(void)
{
State s = { 9000,0,0,-325,15000,1800 };
printf("Welcome to the Lunar Lander simulation!\n\n");
printf("Your goal is to land the vehicle safely on the moon.\n");
printf("You will be provided with information about the lander\nbased on which you will have to make a decision about\nhow much force should be applied to slow the vehicle down.\n");
printf("-----------------------------------------------------------\n\n");
printf("Choose your game mode.\n(Type \"A\" for autopilot) or (\"M\" for manual mode)\n");
modeSelect(&s);
double t = 0;
double tmax = 150;
const double stepSize = 1;
FILE* f = fopen("lander.csv", "wt");
if (!f)
{
printf("Unable to open file.\n");
system("pause");
return 1;
}
while ((t < tmax) && (s.distance >= 0))
{
fprintf(f, "%.2f,%.2f,%.2f,%.2f%.2f,\n", t, s.accel, s.velo, s.distance, s.fuel);
Update(&s, t, stepSize);
printf("Current height: %.2f m\n", s.distance);
printf("Current velocity: %.2f m/s\n", s.velo);
printf("Fuel left : %.2f kg\n", s.fuel);
printf("---------------------------\n\n");
if (s.distance <= 0)
{
s.distance == 0;
if ((s.velo >= -1) && (s.velo <= 0))
{
printf("You have successfully landed! Good job!\n");
}
else
{
printf("You crashed. Game over!\n\n\n");
}
}
t++;
}
return 0;
system("pause");
}
这是自动驾驶仪运行时的输出
Welcome to the Lunar Lander simulation!
Your goal is to land the vehicle safely on the moon.
You will be provided with information about the lander
based on which you will have to make a decision about
how much force should be applied to slow the vehicle down.
-----------------------------------------------------------
Choose your game mode.
(Type "A" for autopilot) or ("M" for manual mode)
--> A
Autopilot: on
-----------------
Current height: 1.#R m
Current velocity: 1.#R m/s
Fuel left : 1.#R kg
---------------------------