|
|
|
@ -35,28 +35,36 @@
@@ -35,28 +35,36 @@
|
|
|
|
|
* @file ControlAllocationSequentialDesaturation.cpp |
|
|
|
|
* |
|
|
|
|
* @author Roman Bapst <bapstroman@gmail.com> |
|
|
|
|
* @author Beat Küng <beat-kueng@gmx.net> |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
#include "ControlAllocationSequentialDesaturation.hpp" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
ControlAllocationSequentialDesaturation::allocate() |
|
|
|
|
{ |
|
|
|
|
//Compute new gains if needed
|
|
|
|
|
updatePseudoInverse(); |
|
|
|
|
|
|
|
|
|
// Allocate
|
|
|
|
|
_actuator_sp = _actuator_trim + _mix * (_control_sp - _control_trim); |
|
|
|
|
switch (_param_mc_airmode.get()) { |
|
|
|
|
case 1: |
|
|
|
|
mixAirmodeRP(); |
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
case 2: |
|
|
|
|
mixAirmodeRPY(); |
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
// go through control axes from lowest to highest priority and unsaturate the actuators
|
|
|
|
|
for (unsigned i = 0; i < NUM_AXES; i++) { |
|
|
|
|
desaturateActuators(_actuator_sp, _axis_prio_increasing[i]); |
|
|
|
|
default: |
|
|
|
|
mixAirmodeDisabled(); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// TODO: thrust model (THR_MDL_FAC)
|
|
|
|
|
|
|
|
|
|
// Clip
|
|
|
|
|
_actuator_sp = clipActuatorSetpoint(_actuator_sp); |
|
|
|
|
clipActuatorSetpoint(_actuator_sp); |
|
|
|
|
|
|
|
|
|
// Compute achieved control
|
|
|
|
|
_control_allocated = _effectiveness * _actuator_sp; |
|
|
|
@ -64,39 +72,32 @@ ControlAllocationSequentialDesaturation::allocate()
@@ -64,39 +72,32 @@ ControlAllocationSequentialDesaturation::allocate()
|
|
|
|
|
|
|
|
|
|
void ControlAllocationSequentialDesaturation::desaturateActuators( |
|
|
|
|
ActuatorVector &actuator_sp, |
|
|
|
|
const ControlAxis &axis) |
|
|
|
|
const ActuatorVector &desaturation_vector, bool increase_only) |
|
|
|
|
{ |
|
|
|
|
ActuatorVector desaturation_vector = getDesaturationVector(axis); |
|
|
|
|
|
|
|
|
|
float gain = computeDesaturationGain(desaturation_vector, actuator_sp); |
|
|
|
|
|
|
|
|
|
actuator_sp = actuator_sp + gain * desaturation_vector; |
|
|
|
|
|
|
|
|
|
gain = computeDesaturationGain(desaturation_vector, actuator_sp); |
|
|
|
|
if (increase_only && gain < 0.f) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
actuator_sp = actuator_sp + 0.5f * gain * desaturation_vector; |
|
|
|
|
} |
|
|
|
|
for (int i = 0; i < _num_actuators; i++) { |
|
|
|
|
actuator_sp(i) += gain * desaturation_vector(i); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ControlAllocation::ActuatorVector ControlAllocationSequentialDesaturation::getDesaturationVector( |
|
|
|
|
const ControlAxis &axis) |
|
|
|
|
{ |
|
|
|
|
ActuatorVector ret; |
|
|
|
|
gain = 0.5f * computeDesaturationGain(desaturation_vector, actuator_sp); |
|
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < NUM_ACTUATORS; i++) { |
|
|
|
|
ret(i) = _mix(i, axis); |
|
|
|
|
for (int i = 0; i < _num_actuators; i++) { |
|
|
|
|
actuator_sp(i) += gain * desaturation_vector(i); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float ControlAllocationSequentialDesaturation::computeDesaturationGain(const ActuatorVector &desaturation_vector, |
|
|
|
|
const ActuatorVector &actuator_sp) |
|
|
|
|
{ |
|
|
|
|
float k_min = 0.f; |
|
|
|
|
float k_max = 0.f; |
|
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < NUM_ACTUATORS; i++) { |
|
|
|
|
for (int i = 0; i < _num_actuators; i++) { |
|
|
|
|
// Avoid division by zero. If desaturation_vector(i) is zero, there's nothing we can do to unsaturate anyway
|
|
|
|
|
if (fabsf(desaturation_vector(i)) < FLT_EPSILON) { |
|
|
|
|
continue; |
|
|
|
@ -122,3 +123,118 @@ float ControlAllocationSequentialDesaturation::computeDesaturationGain(const Act
@@ -122,3 +123,118 @@ float ControlAllocationSequentialDesaturation::computeDesaturationGain(const Act
|
|
|
|
|
// Reduce the saturation as much as possible
|
|
|
|
|
return k_min + k_max; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
ControlAllocationSequentialDesaturation::mixAirmodeRP() |
|
|
|
|
{ |
|
|
|
|
// Airmode for roll and pitch, but not yaw
|
|
|
|
|
|
|
|
|
|
// Mix without yaw
|
|
|
|
|
ActuatorVector thrust_z; |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < _num_actuators; i++) { |
|
|
|
|
_actuator_sp(i) = _actuator_trim(i) + |
|
|
|
|
_mix(i, ControlAxis::ROLL) * (_control_sp(ControlAxis::ROLL) - _control_trim(ControlAxis::ROLL)) + |
|
|
|
|
_mix(i, ControlAxis::PITCH) * (_control_sp(ControlAxis::PITCH) - _control_trim(ControlAxis::PITCH)) + |
|
|
|
|
_mix(i, ControlAxis::THRUST_X) * (_control_sp(ControlAxis::THRUST_X) - _control_trim(ControlAxis::THRUST_X)) + |
|
|
|
|
_mix(i, ControlAxis::THRUST_Y) * (_control_sp(ControlAxis::THRUST_Y) - _control_trim(ControlAxis::THRUST_Y)) + |
|
|
|
|
_mix(i, ControlAxis::THRUST_Z) * (_control_sp(ControlAxis::THRUST_Z) - _control_trim(ControlAxis::THRUST_Z)); |
|
|
|
|
thrust_z(i) = _mix(i, ControlAxis::THRUST_Z); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
desaturateActuators(_actuator_sp, thrust_z); |
|
|
|
|
|
|
|
|
|
// Mix yaw independently
|
|
|
|
|
mixYaw(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
ControlAllocationSequentialDesaturation::mixAirmodeRPY() |
|
|
|
|
{ |
|
|
|
|
// Airmode for roll, pitch and yaw
|
|
|
|
|
|
|
|
|
|
// Do full mixing
|
|
|
|
|
ActuatorVector thrust_z; |
|
|
|
|
ActuatorVector yaw; |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < _num_actuators; i++) { |
|
|
|
|
_actuator_sp(i) = _actuator_trim(i) + |
|
|
|
|
_mix(i, ControlAxis::ROLL) * (_control_sp(ControlAxis::ROLL) - _control_trim(ControlAxis::ROLL)) + |
|
|
|
|
_mix(i, ControlAxis::PITCH) * (_control_sp(ControlAxis::PITCH) - _control_trim(ControlAxis::PITCH)) + |
|
|
|
|
_mix(i, ControlAxis::YAW) * (_control_sp(ControlAxis::YAW) - _control_trim(ControlAxis::YAW)) + |
|
|
|
|
_mix(i, ControlAxis::THRUST_X) * (_control_sp(ControlAxis::THRUST_X) - _control_trim(ControlAxis::THRUST_X)) + |
|
|
|
|
_mix(i, ControlAxis::THRUST_Y) * (_control_sp(ControlAxis::THRUST_Y) - _control_trim(ControlAxis::THRUST_Y)) + |
|
|
|
|
_mix(i, ControlAxis::THRUST_Z) * (_control_sp(ControlAxis::THRUST_Z) - _control_trim(ControlAxis::THRUST_Z)); |
|
|
|
|
thrust_z(i) = _mix(i, ControlAxis::THRUST_Z); |
|
|
|
|
yaw(i) = _mix(i, ControlAxis::YAW); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
desaturateActuators(_actuator_sp, thrust_z); |
|
|
|
|
|
|
|
|
|
// Unsaturate yaw (in case upper and lower bounds are exceeded)
|
|
|
|
|
// to prioritize roll/pitch over yaw.
|
|
|
|
|
desaturateActuators(_actuator_sp, yaw); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
ControlAllocationSequentialDesaturation::mixAirmodeDisabled() |
|
|
|
|
{ |
|
|
|
|
// Airmode disabled: never allow to increase the thrust to unsaturate a motor
|
|
|
|
|
|
|
|
|
|
// Mix without yaw
|
|
|
|
|
ActuatorVector thrust_z; |
|
|
|
|
ActuatorVector roll; |
|
|
|
|
ActuatorVector pitch; |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < _num_actuators; i++) { |
|
|
|
|
_actuator_sp(i) = _actuator_trim(i) + |
|
|
|
|
_mix(i, ControlAxis::ROLL) * (_control_sp(ControlAxis::ROLL) - _control_trim(ControlAxis::ROLL)) + |
|
|
|
|
_mix(i, ControlAxis::PITCH) * (_control_sp(ControlAxis::PITCH) - _control_trim(ControlAxis::PITCH)) + |
|
|
|
|
_mix(i, ControlAxis::THRUST_X) * (_control_sp(ControlAxis::THRUST_X) - _control_trim(ControlAxis::THRUST_X)) + |
|
|
|
|
_mix(i, ControlAxis::THRUST_Y) * (_control_sp(ControlAxis::THRUST_Y) - _control_trim(ControlAxis::THRUST_Y)) + |
|
|
|
|
_mix(i, ControlAxis::THRUST_Z) * (_control_sp(ControlAxis::THRUST_Z) - _control_trim(ControlAxis::THRUST_Z)); |
|
|
|
|
thrust_z(i) = _mix(i, ControlAxis::THRUST_Z); |
|
|
|
|
roll(i) = _mix(i, ControlAxis::ROLL); |
|
|
|
|
pitch(i) = _mix(i, ControlAxis::PITCH); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// only reduce thrust
|
|
|
|
|
desaturateActuators(_actuator_sp, thrust_z, true); |
|
|
|
|
|
|
|
|
|
// Reduce roll/pitch acceleration if needed to unsaturate
|
|
|
|
|
desaturateActuators(_actuator_sp, roll); |
|
|
|
|
desaturateActuators(_actuator_sp, pitch); |
|
|
|
|
|
|
|
|
|
// Mix yaw independently
|
|
|
|
|
mixYaw(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
ControlAllocationSequentialDesaturation::mixYaw() |
|
|
|
|
{ |
|
|
|
|
// Add yaw to outputs
|
|
|
|
|
ActuatorVector yaw; |
|
|
|
|
ActuatorVector thrust_z; |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < _num_actuators; i++) { |
|
|
|
|
_actuator_sp(i) += _mix(i, ControlAxis::YAW) * (_control_sp(ControlAxis::YAW) - _control_trim(ControlAxis::YAW)); |
|
|
|
|
yaw(i) = _mix(i, ControlAxis::YAW); |
|
|
|
|
thrust_z(i) = _mix(i, ControlAxis::THRUST_Z); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Change yaw acceleration to unsaturate the outputs if needed (do not change roll/pitch),
|
|
|
|
|
// and allow some yaw response at maximum thrust
|
|
|
|
|
ActuatorVector max_prev = _actuator_max; |
|
|
|
|
_actuator_max += (_actuator_max - _actuator_min) * 0.15f; |
|
|
|
|
desaturateActuators(_actuator_sp, yaw); |
|
|
|
|
_actuator_max = max_prev; |
|
|
|
|
|
|
|
|
|
// reduce thrust only
|
|
|
|
|
desaturateActuators(_actuator_sp, thrust_z, true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
ControlAllocationSequentialDesaturation::updateParameters() |
|
|
|
|
{ |
|
|
|
|
updateParams(); |
|
|
|
|
} |
|
|
|
|