42 lines
1.5 KiB
C
42 lines
1.5 KiB
C
|
|
|
||
|
|
#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;
|
||
|
|
}
|