diff --git a/cixdof/Makefile b/cixdof/Makefile new file mode 100644 index 0000000..391df0a --- /dev/null +++ b/cixdof/Makefile @@ -0,0 +1,49 @@ +CC=clang +DEBUG=yes +EXEC=test + +# all c files in this directory +SRC = $(wildcard *.c) +# otherwise +#SRC = $(wildcard src_dir1/*.c) \ +# $(wildcard src_dir2/*.c) + +# -g for debug +# -Wall to get all the possible warnings +# -ansi -pendantic to get portable code +ifeq ($(DEBUG),yes) + CFLAGS=-Wall -Wpedantic -g3 -Wextra -Wconversion -Wshadow -fsanitize=address,undefined -fno-omit-frame-pointer -fno-optimize-sibling-calls + LDFLAGS= -Wall -lm -fsanitize=address,undefined +else + CFLAGS=-W -Wall -pedantic -std=c23 -O3 + LDFLAGS= -Wall -lm +endif + +############################################### +# end of the configuration +############################################### + +all: $(EXEC) + +obj = $(SRC:.c=.o) +dep = $(obj:.o=.d) + + +$(EXEC): $(obj) + $(CC) -o $@ $^ $(LDFLAGS) + +.PHONY: tests +tests: $(EXEC) + ./test + +-include $(dep) + +%.d: %.c + @$(CC) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@ + +%.o: %.c + $(CC) $(CFLAGS) -o $@ -c $< + +.PHONY: clean +clean: + rm -f $(obj) $(EXEC) $(dep) diff --git a/cixdof/README.md b/cixdof/README.md new file mode 100644 index 0000000..dab8f40 --- /dev/null +++ b/cixdof/README.md @@ -0,0 +1,3 @@ +# cixdof + +Six degree of freedom solver for quadcopter diff --git a/cixdof/drone.h b/cixdof/drone.h new file mode 100644 index 0000000..9d6e85d --- /dev/null +++ b/cixdof/drone.h @@ -0,0 +1,38 @@ +#ifndef DRONE_H +#define DRONE_H + +#include "../mathprimitives/mathprimitives.h" +#include + +struct QuadCopter { + double mass_kg; + struct Mat3 moment_of_intertia; + double drag_coeff; +}; + +enum PropSpinDir { CW, CCW }; + +struct Propeller { + struct Vec3 x_cg2p_b; + double angular_velocity; + double thrust_coefficient; + double moment_coefficient; + enum PropSpinDir direction; +}; + +struct QuadCopterSim { + double timestep; + uint32_t iterations; + struct QuadCopter quad_copter; + struct Propeller props[4]; +}; + +static inline struct Vec3 body_position_to_inertial(struct Vec3 x_o2cg_i, + struct Quat attitude, + struct Vec3 x_cg2point_b) { + + struct Vec3 x_cg2point_i = quat_apply(quat_conjugate(attitude), x_cg2point_b); + return vec3_add(x_o2cg_i, x_cg2point_i); +} + +#endif diff --git a/cixdof/test.c b/cixdof/test.c new file mode 100644 index 0000000..613588c --- /dev/null +++ b/cixdof/test.c @@ -0,0 +1,47 @@ +#include "./drone.h" +#include +#include + +#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 = + body_position_to_intertial(x_o2cg_i, identity, x_cg2p_b); + + if (!vec3_eql_tol(expected_x_noop, x_o2p_i_noop, 1e-12)) { + return 0; + } + return 1; +} + +int main(void) { + int tests_total = 0; + int tests_passed = 0; + + run_test(test_body_position_to_inertial); + + 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); + } +}