c_projects/cixdof/test.c

121 lines
4.2 KiB
C
Raw Permalink Normal View History

2026-11-01 23:38:25 -05:00
#include "drone.h"
#include "forces.h"
2026-11-03 22:38:36 -05:00
#include "integration.h"
2026-10-31 23:12:53 -04:00
#include <math.h>
#include <stdio.h>
#ifdef run_test
#undef run_test
#endif
#define STR(x) #x
#define run_test(test) \
tests_total += 1; \
if (test()) { \
tests_passed += 1; \
printf("[OK] %s\n", STR(test)); \
} else { \
printf("\033[91m[FAILED] %s\n\033[0m", STR(test)); \
}
int test_body_position_to_inertial(void) {
struct Vec3 x_o2cg_i = {1.0, 1.0, 1.0};
struct Vec3 x_cg2p_b = {1.0, 0.0, 0.0};
struct Quat identity = yaw_pitch_roll_to_quat(0, 0, 0);
struct Vec3 expected_x_noop = {2.0, 1.0, 1.0};
struct Vec3 x_o2p_i_noop =
2026-11-01 23:38:25 -05:00
body_position_to_inertial(x_o2cg_i, identity, x_cg2p_b);
2026-10-31 23:12:53 -04:00
if (!vec3_eql_tol(expected_x_noop, x_o2p_i_noop, 1e-12)) {
return 0;
}
return 1;
}
2026-11-01 23:38:25 -05:00
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);
}
2026-11-03 22:38:36 -05:00
int test_timestep(void) {
struct QuadCopter quad = {
.mass_kg = 0.5,
.drag_coeff = 0.0,
.moment_of_inertia = {{2.0, 0.0, 0.0}, {0.0, 0.5, 0.0}, {0.0, 0.0, 2.0}}};
struct State s = {
.x_o2cg_inertial = {0.0, 0.0, 0.0},
.v_cg_inertial = {0.0, 0.0, 0.0},
.q_inertial2body = {0.0, 0.0, 0.0, 1.0},
.omega = {0.0, 0.0, 0.0},
};
struct Propeller prop = {.angular_velocity = 100.0,
.x_cg2p_b = {1.0, 0.0, 0.0},
.moment_coefficient = 0.1,
.thrust_coefficient = 0.5,
.direction = CW};
timestep(0.005, &quad, 1, &prop, &s);
struct Vec3 expected_v = {0.0, 0.0, -49.95096675};
struct Vec3 expected_x = {0.0, 0.0, -0.24975483375};
struct Vec3 expected_omega = {0.0, 50.0, 2.5};
struct Quat expected_q = {0.0, 0.12403234937465502, 0.006201617468732751,
0.9922587949972401};
int first_step_correct = vec3_eql_tol(s.v_cg_inertial, expected_v, 1e-8) &&
vec3_eql_tol(s.x_o2cg_inertial, expected_x, 1e-8) &&
vec3_eql_tol(s.omega, expected_omega, 1e-12) &&
quat_eql_tol(s.q_inertial2body, expected_q, 1e-12);
timestep(0.005, &quad, 1, &prop, &s);
expected_v = (struct Vec3){-1.23072190e+01, -7.69201185e-02, -9.83635311e+01};
expected_x = (struct Vec3){-6.15360950e-02, -3.84600592e-04, -7.41572489e-01};
expected_omega = (struct Vec3){-0.46875, 100.0, 5.0};
expected_q = (struct Quat){-0.0011280012096230429, 0.36096743708693396,
0.01790701920276581, 0.9324057998744074};
int second_step_correct = vec3_eql_tol(s.v_cg_inertial, expected_v, 1e-7) &&
vec3_eql_tol(s.x_o2cg_inertial, expected_x, 1e-7) &&
vec3_eql_tol(s.omega, expected_omega, 1e-12) &&
quat_eql_tol(s.q_inertial2body, expected_q, 1e-12);
return first_step_correct && second_step_correct;
}
2026-10-31 23:12:53 -04:00
int main(void) {
int tests_total = 0;
int tests_passed = 0;
run_test(test_body_position_to_inertial);
2026-11-01 23:38:25 -05:00
run_test(test_gravity);
run_test(test_thrust);
2026-11-03 22:38:36 -05:00
run_test(test_timestep);
2026-10-31 23:12:53 -04:00
if (tests_total == tests_passed) {
printf("\033[92mAll tests passed!\033[0m");
} else {
printf("\033[91m%d of %d tests failed\033[0m", tests_total - tests_passed,
tests_total);
}
}