Create basic data structs

This commit is contained in:
Alex Selimov 2026-10-31 23:12:53 -04:00
parent c9b25ea88e
commit 8f4d61f820
4 changed files with 137 additions and 0 deletions

49
cixdof/Makefile Normal file
View file

@ -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)

3
cixdof/README.md Normal file
View file

@ -0,0 +1,3 @@
# cixdof
Six degree of freedom solver for quadcopter

38
cixdof/drone.h Normal file
View file

@ -0,0 +1,38 @@
#ifndef DRONE_H
#define DRONE_H
#include "../mathprimitives/mathprimitives.h"
#include <stdint.h>
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

47
cixdof/test.c Normal file
View file

@ -0,0 +1,47 @@
#include "./drone.h"
#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 =
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);
}
}