Add timestep function
This commit is contained in:
parent
8904c223cb
commit
c0f37a5166
5 changed files with 118 additions and 1 deletions
|
|
@ -4,9 +4,12 @@
|
||||||
#include "../mathprimitives/mathprimitives.h"
|
#include "../mathprimitives/mathprimitives.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Properties of the quadcopter
|
||||||
|
*/
|
||||||
struct QuadCopter {
|
struct QuadCopter {
|
||||||
double mass_kg;
|
double mass_kg;
|
||||||
struct Mat3 moment_of_intertia;
|
struct Mat3 moment_of_inertia;
|
||||||
double drag_coeff;
|
double drag_coeff;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,4 +17,12 @@ struct ThrustAndMoment {
|
||||||
|
|
||||||
struct ThrustAndMoment thrust(struct Quat attitude, struct Propeller *p);
|
struct ThrustAndMoment thrust(struct Quat attitude, struct Propeller *p);
|
||||||
|
|
||||||
|
static inline struct Vec3 drag(double drag_coefficient, struct Vec3 velocity) {
|
||||||
|
return (struct Vec3){
|
||||||
|
-velocity.x * drag_coefficient,
|
||||||
|
-velocity.y * drag_coefficient,
|
||||||
|
-velocity.z * drag_coefficient,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
41
cixdof/integration.c
Normal file
41
cixdof/integration.c
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
|
||||||
|
#include "integration.h"
|
||||||
|
#include "forces.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Euler integration timestep
|
||||||
|
*/
|
||||||
|
void timestep(double dt, struct QuadCopter *const quad, int n_prop,
|
||||||
|
struct Propeller *const p, struct State *s) {
|
||||||
|
|
||||||
|
struct Vec3 f_inertial = vec3_add(gravity(quad->mass_kg),
|
||||||
|
drag(quad->drag_coeff, s->v_cg_inertial));
|
||||||
|
struct Vec3 moment_body = {0.0, 0.0, 0.0};
|
||||||
|
|
||||||
|
for (int i = 0; i < n_prop; i++) {
|
||||||
|
struct ThrustAndMoment tm = thrust(s->q_inertial2body, &p[i]);
|
||||||
|
f_inertial = vec3_add(f_inertial, tm.thrust_inertial);
|
||||||
|
moment_body = vec3_add(moment_body, tm.moment_body);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update positions/velocity
|
||||||
|
struct Vec3 a = vec3_scale(f_inertial, 1.0 / quad->mass_kg);
|
||||||
|
s->v_cg_inertial = vec3_add(s->v_cg_inertial, vec3_scale(a, dt));
|
||||||
|
s->x_o2cg_inertial =
|
||||||
|
vec3_add(s->x_o2cg_inertial, vec3_scale(s->v_cg_inertial, dt));
|
||||||
|
|
||||||
|
// Update attitude/omega
|
||||||
|
struct Vec3 I_omega = mat3_mul_vec(quad->moment_of_inertia, s->omega);
|
||||||
|
struct Vec3 omega_dot =
|
||||||
|
mat3_mul_vec(mat3_inv(quad->moment_of_inertia),
|
||||||
|
vec3_sub(moment_body, vec3_cross(s->omega, I_omega)));
|
||||||
|
s->omega = vec3_add(s->omega, vec3_scale(omega_dot, dt));
|
||||||
|
|
||||||
|
struct Quat half_omega_q = {s->omega.x * 0.5, s->omega.y * 0.5,
|
||||||
|
s->omega.z * 0.5, 0.0};
|
||||||
|
|
||||||
|
struct Quat qdot = quat_mul(half_omega_q, s->q_inertial2body);
|
||||||
|
s->q_inertial2body =
|
||||||
|
quat_normalized(quat_add(s->q_inertial2body, quat_scale(qdot, dt)));
|
||||||
|
return;
|
||||||
|
}
|
||||||
16
cixdof/integration.h
Normal file
16
cixdof/integration.h
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
#ifndef INTEGRATION_H
|
||||||
|
#define INTEGRATION_H
|
||||||
|
|
||||||
|
#include "drone.h"
|
||||||
|
|
||||||
|
struct State {
|
||||||
|
struct Vec3 x_o2cg_inertial;
|
||||||
|
struct Vec3 v_cg_inertial;
|
||||||
|
struct Quat q_inertial2body;
|
||||||
|
struct Vec3 omega;
|
||||||
|
};
|
||||||
|
|
||||||
|
void timestep(double dt, struct QuadCopter *const quad, int nprop,
|
||||||
|
struct Propeller *const p, struct State *s);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#include "drone.h"
|
#include "drone.h"
|
||||||
#include "forces.h"
|
#include "forces.h"
|
||||||
|
#include "integration.h"
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
@ -54,6 +55,53 @@ int test_thrust(void) {
|
||||||
vec3_eql_tol(expected_moment, tm.moment_body, 1e-11);
|
vec3_eql_tol(expected_moment, tm.moment_body, 1e-11);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
int tests_total = 0;
|
int tests_total = 0;
|
||||||
int tests_passed = 0;
|
int tests_passed = 0;
|
||||||
|
|
@ -61,6 +109,7 @@ int main(void) {
|
||||||
run_test(test_body_position_to_inertial);
|
run_test(test_body_position_to_inertial);
|
||||||
run_test(test_gravity);
|
run_test(test_gravity);
|
||||||
run_test(test_thrust);
|
run_test(test_thrust);
|
||||||
|
run_test(test_timestep);
|
||||||
|
|
||||||
if (tests_total == tests_passed) {
|
if (tests_total == tests_passed) {
|
||||||
printf("\033[92mAll tests passed!\033[0m");
|
printf("\033[92mAll tests passed!\033[0m");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue