Skip to content

Commit

Permalink
Feed forward guidance
Browse files Browse the repository at this point in the history
  • Loading branch information
dewagter committed Sep 25, 2023
1 parent f4f0cf2 commit d72671a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
12 changes: 11 additions & 1 deletion sw/airborne/firmwares/rotorcraft/guidance/guidance_indi_hybrid.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,16 @@ struct StabilizationSetpoint guidance_indi_run(struct FloatVect3 *accel_sp, floa
omega -= accely_filt.o[0]*FWD_SIDESLIP_GAIN;
#endif

// We can pre-compute the required rates to achieve this turn rate:
// NOTE: there *should* not be any problems possible with Euler singularities here
struct FloatEulers *euler_zyx = stateGetNedToBodyEulers_f();

struct FloatRates ff_rates;

ff_rates.p = -sinf(euler_zyx->theta) * omega;
ff_rates.q = cosf(euler_zyx->theta) * sinf(euler_zyx->phi) * omega;
ff_rates.r = cosf(euler_zyx->theta) * cosf(euler_zyx->phi) * omega;

// For a hybrid it is important to reduce the sideslip, which is done by changing the heading.
// For experiments, it is possible to fix the heading to a different value.
if (take_heading_control) {
Expand Down Expand Up @@ -399,7 +409,7 @@ struct StabilizationSetpoint guidance_indi_run(struct FloatVect3 *accel_sp, floa
float_quat_of_eulers_zxy(&sp_quat, &guidance_euler_cmd);
float_quat_normalize(&sp_quat);

return stab_sp_from_quat_f(&sp_quat);
return stab_sp_from_quat_ff_rates_f(&sp_quat, &ff_rates);
}

// compute accel setpoint from speed setpoint (use global variables ! FIXME)
Expand Down
23 changes: 17 additions & 6 deletions sw/airborne/firmwares/rotorcraft/stabilization.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void stabilization_filter_commands(void)

struct Int32Quat stab_sp_to_quat_i(struct StabilizationSetpoint *sp)
{
if (sp->type == STAB_SP_QUAT) {
if ((sp->type == STAB_SP_QUAT) || (sp->type == STAB_SP_QUAT_FF_RATE)) {
if (sp->format == STAB_SP_INT) {
return sp->sp.quat_i;
} else {
Expand Down Expand Up @@ -164,7 +164,7 @@ struct Int32Quat stab_sp_to_quat_i(struct StabilizationSetpoint *sp)

struct FloatQuat stab_sp_to_quat_f(struct StabilizationSetpoint *sp)
{
if (sp->type == STAB_SP_QUAT) {
if ((sp->type == STAB_SP_QUAT) || (sp->type == STAB_SP_QUAT_FF_RATE)) {
if (sp->format == STAB_SP_FLOAT) {
return sp->sp.quat_f;
} else {
Expand Down Expand Up @@ -214,7 +214,7 @@ struct Int32Eulers stab_sp_to_eulers_i(struct StabilizationSetpoint *sp)
EULERS_BFP_OF_REAL(eulers, sp->sp.eulers_f);
return eulers;
}
} else if (sp->type == STAB_SP_QUAT) {
} else if ((sp->type == STAB_SP_QUAT) || (sp->type == STAB_SP_QUAT_FF_RATE)) {
if (sp->format == STAB_SP_INT) {
struct Int32Eulers eulers;
int32_eulers_of_quat(&eulers, &sp->sp.quat_i);
Expand Down Expand Up @@ -253,7 +253,7 @@ struct FloatEulers stab_sp_to_eulers_f(struct StabilizationSetpoint *sp)
EULERS_FLOAT_OF_BFP(eulers, sp->sp.eulers_i);
return eulers;
}
} else if (sp->type == STAB_SP_QUAT) {
} else if ((sp->type == STAB_SP_QUAT) || (sp->type == STAB_SP_QUAT_FF_RATE)) {
if (sp->format == STAB_SP_FLOAT) {
struct FloatEulers eulers;
float_eulers_of_quat(&eulers, &sp->sp.quat_f);
Expand Down Expand Up @@ -284,7 +284,7 @@ struct FloatEulers stab_sp_to_eulers_f(struct StabilizationSetpoint *sp)

struct Int32Rates stab_sp_to_rates_i(struct StabilizationSetpoint *sp)
{
if (sp->type == STAB_SP_RATES) {
if ((sp->type == STAB_SP_RATES) || (sp->type == STAB_SP_QUAT_FF_RATE)) {
if (sp->format == STAB_SP_INT) {
return sp->sp.rates_i;
} else {
Expand All @@ -301,7 +301,7 @@ struct Int32Rates stab_sp_to_rates_i(struct StabilizationSetpoint *sp)

struct FloatRates stab_sp_to_rates_f(struct StabilizationSetpoint *sp)
{
if (sp->type == STAB_SP_RATES) {
if ((sp->type == STAB_SP_RATES) || (sp->type == STAB_SP_QUAT_FF_RATE)) {
if (sp->format == STAB_SP_FLOAT) {
return sp->sp.rates_f;
} else {
Expand Down Expand Up @@ -336,6 +336,17 @@ struct StabilizationSetpoint stab_sp_from_quat_f(struct FloatQuat *quat)
return sp;
}

struct StabilizationSetpoint stab_sp_from_quat_ff_rates_f(struct FloatQuat *quat, struct FloatRates *rates)
{
struct StabilizationSetpoint sp = {
.type = STAB_SP_QUAT_FF_RATE,
.format = STAB_SP_FLOAT,
.sp.quat_f = *quat,
.sp.rates_f = *rates
};
return sp;
}

struct StabilizationSetpoint stab_sp_from_eulers_i(struct Int32Eulers *eulers)
{
struct StabilizationSetpoint sp = {
Expand Down
4 changes: 3 additions & 1 deletion sw/airborne/firmwares/rotorcraft/stabilization.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ struct StabilizationSetpoint {
STAB_SP_QUAT, ///< LTP to Body orientation in unit quaternion
STAB_SP_EULERS, ///< LTP to Body orientation in euler angles
STAB_SP_LTP, ///< banking and heading in LTP (NED) frame
STAB_SP_RATES ///< body rates
STAB_SP_RATES, ///< body rates
STAB_SP_QUAT_FF_RATE ///< LTP to Body orientation in unit quaternion with precomputed feedforward rates
} type;
enum {
STAB_SP_INT,
Expand Down Expand Up @@ -73,6 +74,7 @@ extern struct FloatRates stab_sp_to_rates_f(struct StabilizationSetpoint *sp);
// helper make functions
extern struct StabilizationSetpoint stab_sp_from_quat_i(struct Int32Quat *quat);
extern struct StabilizationSetpoint stab_sp_from_quat_f(struct FloatQuat *quat);
extern struct StabilizationSetpoint stab_sp_from_quat_ff_rates_f(struct FloatQuat *quat, struct FloatRates *rates);
extern struct StabilizationSetpoint stab_sp_from_eulers_i(struct Int32Eulers *eulers);
extern struct StabilizationSetpoint stab_sp_from_eulers_f(struct FloatEulers *eulers);
extern struct StabilizationSetpoint stab_sp_from_ltp_i(struct Int32Vect2 *vect, int32_t heading);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ int32_t num_thrusters;

struct Int32Eulers stab_att_sp_euler;
struct Int32Quat stab_att_sp_quat;
struct Int32Rates stab_att_ff_rates;

abi_event rpm_ev;
static void rpm_cb(uint8_t sender_id, struct rpm_act_t *rpm_msg, uint8_t num_act);
Expand Down Expand Up @@ -428,6 +429,7 @@ void stabilization_indi_set_stab_sp(struct StabilizationSetpoint *sp)
{
stab_att_sp_euler = stab_sp_to_eulers_i(sp);
stab_att_sp_quat = stab_sp_to_quat_i(sp);
stab_att_ff_rates = stab_sp_to_rates_i(sp);
}

/**
Expand Down Expand Up @@ -646,6 +648,9 @@ void stabilization_indi_attitude_run(struct Int32Quat quat_sp, bool in_flight)
rate_sp.q = indi_gains.att.q * att_fb.y / indi_gains.rate.q;
rate_sp.r = indi_gains.att.r * att_fb.z / indi_gains.rate.r;

// Add feed-forward rates to the attitude feedback part
RATES_ADD(rate_sp, stab_att_ff_rates);

// Store for telemetry
angular_rate_ref.p = rate_sp.p;
angular_rate_ref.q = rate_sp.q;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@

struct Int32Eulers stab_att_sp_euler;
struct Int32Quat stab_att_sp_quat;
struct Int32Rates stab_att_ff_rates;

static struct FirstOrderLowPass rates_filt_fo[3];

Expand Down Expand Up @@ -304,6 +305,7 @@ void stabilization_indi_set_stab_sp(struct StabilizationSetpoint *sp)
{
stab_att_sp_euler = stab_sp_to_eulers_i(sp);
stab_att_sp_quat = stab_sp_to_quat_i(sp);
stab_att_ff_rates = stab_sp_to_rates_i(sp);
}

/**
Expand Down Expand Up @@ -485,6 +487,9 @@ void stabilization_indi_attitude_run(struct Int32Quat quat_sp, bool in_flight __
rate_sp.q = indi.gains.att.q * att_fb.y / indi.gains.rate.q;
rate_sp.r = indi.gains.att.r * att_fb.z / indi.gains.rate.r;

// Add feed-forward rates to the attitude feedback part
RATES_ADD(rate_sp, stab_att_ff_rates);

/* compute the INDI command */
stabilization_indi_rate_run(rate_sp, in_flight);
}
Expand Down

0 comments on commit d72671a

Please sign in to comment.