From 8904c223cb42dc1295e053db363cadf1c4f88d44 Mon Sep 17 00:00:00 2001 From: Alex Selimov Date: Sun, 1 Nov 2026 23:38:25 -0500 Subject: [PATCH] Add gravity and thrust force --- cixdof/drone.h | 2 +- cixdof/forces.c | 20 ++++++++++++++++++++ cixdof/forces.h | 20 ++++++++++++++++++++ cixdof/test.c | 28 ++++++++++++++++++++++++++-- 4 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 cixdof/forces.c create mode 100644 cixdof/forces.h diff --git a/cixdof/drone.h b/cixdof/drone.h index 9d6e85d..45b684d 100644 --- a/cixdof/drone.h +++ b/cixdof/drone.h @@ -10,7 +10,7 @@ struct QuadCopter { double drag_coeff; }; -enum PropSpinDir { CW, CCW }; +enum PropSpinDir { CW = 1, CCW = -1 }; struct Propeller { struct Vec3 x_cg2p_b; diff --git a/cixdof/forces.c b/cixdof/forces.c new file mode 100644 index 0000000..ccc55bb --- /dev/null +++ b/cixdof/forces.c @@ -0,0 +1,20 @@ +#include "forces.h" + +struct ThrustAndMoment thrust(struct Quat attitude, struct Propeller *p) { + struct Vec3 body_thrust = {0.0, 0.0, + -p->thrust_coefficient * p->angular_velocity * + p->angular_velocity}; + + struct Vec3 inertial_thrust = quat_apply(attitude, body_thrust); + + // Pitching/rolling moment induced by thrust force + struct Vec3 force_moment = vec3_cross(p->x_cg2p_b, body_thrust); + + // Yaw moment induced by rotation of propeller + struct Vec3 drag_moment = {0.0, 0.0, + (double)p->direction * p->moment_coefficient * + p->angular_velocity * p->angular_velocity}; + + return (struct ThrustAndMoment){inertial_thrust, + vec3_add(force_moment, drag_moment)}; +} diff --git a/cixdof/forces.h b/cixdof/forces.h new file mode 100644 index 0000000..6e9a477 --- /dev/null +++ b/cixdof/forces.h @@ -0,0 +1,20 @@ +#ifndef FORCES_H +#define FORCES_H + +#include "../mathprimitives/mathprimitives.h" +#include "drone.h" + +static const double G = 9.80665; + +static inline struct Vec3 gravity(double mass) { + return (struct Vec3){0, 0, mass * G}; +} + +struct ThrustAndMoment { + struct Vec3 thrust_inertial; + struct Vec3 moment_body; +}; + +struct ThrustAndMoment thrust(struct Quat attitude, struct Propeller *p); + +#endif diff --git a/cixdof/test.c b/cixdof/test.c index 613588c..6ba42e0 100644 --- a/cixdof/test.c +++ b/cixdof/test.c @@ -1,4 +1,5 @@ -#include "./drone.h" +#include "drone.h" +#include "forces.h" #include #include @@ -24,7 +25,7 @@ int test_body_position_to_inertial(void) { struct Vec3 expected_x_noop = {2.0, 1.0, 1.0}; struct Vec3 x_o2p_i_noop = - body_position_to_intertial(x_o2cg_i, identity, x_cg2p_b); + body_position_to_inertial(x_o2cg_i, identity, x_cg2p_b); if (!vec3_eql_tol(expected_x_noop, x_o2p_i_noop, 1e-12)) { return 0; @@ -32,11 +33,34 @@ int test_body_position_to_inertial(void) { return 1; } +int test_gravity(void) { + double mass = 10.0; + struct Vec3 expected_fg = {0, 0, 98.0665}; + struct Vec3 fg = gravity(mass); + + return vec3_eql_tol(expected_fg, fg, 1e-12); +} + +int test_thrust(void) { + struct Quat attitude = yaw_pitch_roll_to_quat(0.0, 0.0, -M_PI_2); + struct Propeller p = {(struct Vec3){1.0, 1.0, 0.0}, 100.0, 1.0, 2.0, CW}; + + struct Vec3 expected_thrust = {0.0, -100.0 * 100.0, 0.0}; + struct Vec3 expected_moment = {-100.0 * 100.0, 100.0 * 100, 100 * 100 * 2}; + + struct ThrustAndMoment tm = thrust(attitude, &p); + + return vec3_eql_tol(expected_thrust, tm.thrust_inertial, 1e-11) && + vec3_eql_tol(expected_moment, tm.moment_body, 1e-11); +} + int main(void) { int tests_total = 0; int tests_passed = 0; run_test(test_body_position_to_inertial); + run_test(test_gravity); + run_test(test_thrust); if (tests_total == tests_passed) { printf("\033[92mAll tests passed!\033[0m");