Add timestep function
This commit is contained in:
parent
8904c223cb
commit
c0f37a5166
5 changed files with 118 additions and 1 deletions
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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue