Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-gu committed Jul 22, 2024
1 parent 6b615bc commit 3e186c9
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions STM32Cube/Calculations/trajectory_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define ROCKET_BASE_AREA 0.0182412538 // m^2
#define SIM_ALTITUDE 1000 // All drag sims conducted at 1000m above sea level
#define TOL 0.00001
#define TIME_STEP 0.05 // s
#define TIME_STEP 0.05 // s

// Struct with the data iterated in RK4 method
typedef struct RK4StateStruct {
Expand Down Expand Up @@ -42,8 +42,12 @@ typedef struct {
/**
* Evaluates a cubic 2 variable polynomial at the given coordinates.
*/
float evaluate_cubic_2_variable(Cubic2VariablePolynomial* poly, float x, float y) {
return poly->p00 + poly->p10 * x + poly->p01 * y + poly->p20 * x * x + poly->p11 * x * y + poly->p02 * y * y + poly->p30 * x * x * x + poly->p21 * x * x * y + poly->p12 * x * y * y + poly->p03 * y * y * y;
float evaluate_cubic_2_variable(Cubic2VariablePolynomial* poly, float x,
float y) {
return poly->p00 + poly->p10 * x + poly->p01 * y + poly->p20 * x * x +
poly->p11 * x * y + poly->p02 * y * y + poly->p30 * x * x * x +
poly->p21 * x * x * y + poly->p12 * x * y * y +
poly->p03 * y * y * y;
}

float interpolate_drag(float extension, float velocity, float altitude) {
Expand All @@ -57,26 +61,36 @@ float interpolate_drag(float extension, float velocity, float altitude) {
float y = (altitude - 5000.0f) / 3172.0f;

Cubic2VariablePolynomial coeffs[11] = {
{ 232.2951f, 244.7010f, -75.1435f, 64.3402f, -79.5220f, 11.7309f, -0.8306f, -20.4344f, 9.7041f, -0.6148f },
{ 235.8993f, 249.2100f, -76.2767f, 65.8251f, -81.2931f, 12.0289f, -0.8408f, -21.0236f, 9.9787f, -0.7853f },
{ 245.6886f, 260.2967f, -80.2111f, 69.3746f, -85.4361f, 12.5705f, -0.6199f, -22.1676f, 10.4297f, -0.6666f },
{ 253.9691f, 270.2409f, -83.5032f, 72.3919f, -89.3117f, 13.3189f, -0.7371f, -23.2489f, 11.0822f, -0.7471f },
{ 263.5127f, 280.7695f, -86.9338f, 75.8929f, -93.6884f, 14.1591f, -0.3771f, -24.5324f, 11.7445f, -0.9399f },
{ 272.4592f, 290.5670f, -90.1040f, 78.8810f, -97.0343f, 14.5126f, -0.1988f, -25.6374f, 12.0348f, -0.7327f },
{ 284.8368f, 304.7727f, -94.4923f, 82.4469f, -101.4462f, 15.1080f, -0.6357f, -26.5433f, 12.4927f, -0.8323f },
{ 296.2638f, 317.3919f, -98.5746f, 86.2809f, -106.2663f, 15.9556f, -0.5224f, -28.0106f, 13.2557f, -0.8541f },
{ 303.1856f, 325.1022f, -100.9674f, 88.7552f, -109.3743f, 16.5627f, -0.4253f, -28.9892f, 13.8034f, -0.9447f },
{ 316.4963f, 339.6502f, -104.5570f, 92.4088f, -114.1954f, 16.9114f, -0.5681f, -30.4995f, 13.9269f, -1.2933f },
{ 340.9146f, 367.1520f, -114.8622f, 101.0439f, -123.1214f, 18.4047f, -0.1879f, -31.8071f, 15.4422f, -1.1456f }
};

int index = (int) (extension * 10.0f);
{232.2951f, 244.7010f, -75.1435f, 64.3402f, -79.5220f, 11.7309f,
-0.8306f, -20.4344f, 9.7041f, -0.6148f},
{235.8993f, 249.2100f, -76.2767f, 65.8251f, -81.2931f, 12.0289f,
-0.8408f, -21.0236f, 9.9787f, -0.7853f},
{245.6886f, 260.2967f, -80.2111f, 69.3746f, -85.4361f, 12.5705f,
-0.6199f, -22.1676f, 10.4297f, -0.6666f},
{253.9691f, 270.2409f, -83.5032f, 72.3919f, -89.3117f, 13.3189f,
-0.7371f, -23.2489f, 11.0822f, -0.7471f},
{263.5127f, 280.7695f, -86.9338f, 75.8929f, -93.6884f, 14.1591f,
-0.3771f, -24.5324f, 11.7445f, -0.9399f},
{272.4592f, 290.5670f, -90.1040f, 78.8810f, -97.0343f, 14.5126f,
-0.1988f, -25.6374f, 12.0348f, -0.7327f},
{284.8368f, 304.7727f, -94.4923f, 82.4469f, -101.4462f, 15.1080f,
-0.6357f, -26.5433f, 12.4927f, -0.8323f},
{296.2638f, 317.3919f, -98.5746f, 86.2809f, -106.2663f, 15.9556f,
-0.5224f, -28.0106f, 13.2557f, -0.8541f},
{303.1856f, 325.1022f, -100.9674f, 88.7552f, -109.3743f, 16.5627f,
-0.4253f, -28.9892f, 13.8034f, -0.9447f},
{316.4963f, 339.6502f, -104.5570f, 92.4088f, -114.1954f, 16.9114f,
-0.5681f, -30.4995f, 13.9269f, -1.2933f},
{340.9146f, 367.1520f, -114.8622f, 101.0439f, -123.1214f, 18.4047f,
-0.1879f, -31.8071f, 15.4422f, -1.1456f}};

int index = (int)(extension * 10.0f);
if (extension * 10.0f == index) {
return evaluate_cubic_2_variable(&coeffs[index], x, y);
}
float first = evaluate_cubic_2_variable(&coeffs[index], x, y);
float second = evaluate_cubic_2_variable(&coeffs[index + 1], x, y);
float diff = extension - ((float) ((int) extension));
float diff = extension - ((float)((int)extension));
return diff * second + (1.0f - diff) * first;
}

Expand All @@ -98,7 +112,7 @@ static float gravitational_acceleration(float altitude) {
* @return forces acting on rocket in the X and Y directions (N)
*/
static Forces get_forces(float extension, float mass, float velX, float velY,
float alt) {
float alt) {
Forces forces;
float velT = sqrt(velY * velY + velX * velX);
float Fd = -interpolate_drag(extension, velT, alt); // force of drag (N)
Expand Down

0 comments on commit 3e186c9

Please sign in to comment.