refactor(control_allocation): Make type alias for actuator bitmask

This commit is contained in:
Balduin
2026-02-20 08:40:34 +01:00
committed by Balduin
parent af3cfaea25
commit b6164107d1
14 changed files with 39 additions and 31 deletions

View File

@@ -84,10 +84,10 @@ int ActuatorEffectiveness::Configuration::totalNumActuators() const
return total_count;
}
void ActuatorEffectiveness::stopMaskedMotorsWithZeroThrust(uint32_t stoppable_motors_mask, ActuatorVector &actuator_sp)
void ActuatorEffectiveness::stopMaskedMotorsWithZeroThrust(ActuatorBitmask stoppable_motors_mask, ActuatorVector &actuator_sp)
{
for (int actuator_idx = 0; actuator_idx < NUM_ACTUATORS; actuator_idx++) {
const uint32_t motor_mask = (1u << actuator_idx);
const ActuatorBitmask motor_mask = (1u << actuator_idx);
if (stoppable_motors_mask & motor_mask) {

View File

@@ -88,6 +88,10 @@ public:
using EffectivenessMatrix = matrix::Matrix<float, NUM_AXES, NUM_ACTUATORS>;
using ActuatorVector = matrix::Vector<float, NUM_ACTUATORS>;
using ActuatorBitmask = uint32_t;
static_assert(NUM_ACTUATORS <= 8 * sizeof(ActuatorBitmask),
"NUM_ACTUATORS exceeds the number of bits available in the mask type.");
enum class FlightPhase {
HOVER_FLIGHT = 0,
@@ -201,7 +205,7 @@ public:
/**
* Get a bitmask of motors to be stopped
*/
virtual uint32_t getStoppedMotors() const { return _stopped_motors_mask; }
virtual ActuatorBitmask getStoppedMotors() const { return _stopped_motors_mask; }
/**
* Fill in the unallocated torque and thrust, customized by effectiveness type.
@@ -215,9 +219,9 @@ public:
* @param stoppable_motors_mask mask of motors that should be stopped if there's no thrust demand
* @param actuator_sp outcome of the allocation to determine if the motor should be stopped
*/
virtual void stopMaskedMotorsWithZeroThrust(uint32_t stoppable_motors_mask, ActuatorVector &actuator_sp);
virtual void stopMaskedMotorsWithZeroThrust(ActuatorBitmask stoppable_motors_mask, ActuatorVector &actuator_sp);
protected:
FlightPhase _flight_phase{FlightPhase::HOVER_FLIGHT};
uint32_t _stopped_motors_mask{0};
ActuatorBitmask _stopped_motors_mask{0};
};

View File

@@ -83,6 +83,7 @@ public:
static constexpr uint8_t NUM_AXES = ActuatorEffectiveness::NUM_AXES;
using ActuatorVector = matrix::Vector<float, NUM_ACTUATORS>;
using ActuatorBitmask = ActuatorEffectiveness::ActuatorBitmask;
enum ControlAxis {
ROLL = 0,
@@ -231,7 +232,7 @@ public:
* @param nan_actuators_mask Bitmask indicating which actuators to set to NaN.
* If (nan_actuators_mask & (1 << i)), _actuator_sp(i) becomes NaN.
*/
void applyNanToActuators(uint32_t nan_actuators_mask)
void applyNanToActuators(ActuatorBitmask nan_actuators_mask)
{
for (int i = 0; i < _num_actuators; i++) {
if (nan_actuators_mask & (1u << i)) {

View File

@@ -604,11 +604,11 @@ ControlAllocator::update_effectiveness_matrix_if_needed(EffectivenessUpdateReaso
void
ControlAllocator::handle_stopped_motors(const hrt_abstime now)
{
const uint32_t stopped_motors_due_to_effectiveness = _actuator_effectiveness->getStoppedMotors();
const ActuatorBitmask stopped_motors_due_to_effectiveness = _actuator_effectiveness->getStoppedMotors();
const uint32_t stopped_motors = stopped_motors_due_to_effectiveness
| _handled_motor_failure_bitmask
| _motor_stop_mask;
const ActuatorBitmask stopped_motors = stopped_motors_due_to_effectiveness
| _handled_motor_failure_bitmask
| _motor_stop_mask;
// Handle stopped motors by setting NaN
const unsigned int allocation_index = 0;

View File

@@ -96,6 +96,7 @@ public:
using ActuatorVector = ActuatorEffectiveness::ActuatorVector;
using ActuatorBitmask = ActuatorEffectiveness::ActuatorBitmask;
ControlAllocator();

View File

@@ -54,5 +54,5 @@ protected:
ActuatorEffectivenessRotors _motors;
ActuatorEffectivenessControlSurfaces _torque;
uint32_t _motors_mask{};
ActuatorBitmask _motors_mask{};
};

View File

@@ -65,5 +65,5 @@ private:
int _first_control_surface_idx{0}; ///< applies to matrix 1
uint32_t _forwards_motors_mask{};
ActuatorBitmask _forwards_motors_mask{};
};

View File

@@ -224,14 +224,14 @@ ActuatorEffectivenessRotors::computeEffectivenessMatrix(const Geometry &geometry
return num_actuators;
}
uint32_t ActuatorEffectivenessRotors::updateAxisFromTilts(const ActuatorEffectivenessTilts &tilts,
ActuatorBitmask ActuatorEffectivenessRotors::updateAxisFromTilts(const ActuatorEffectivenessTilts &tilts,
float collective_tilt_control)
{
if (!PX4_ISFINITE(collective_tilt_control)) {
collective_tilt_control = -1.f;
}
uint32_t nontilted_motors = 0;
ActuatorBitmask nontilted_motors = 0;
for (int i = 0; i < _geometry.num_rotors; ++i) {
int tilt_index = _geometry.rotors[i].tilt_index;
@@ -256,9 +256,9 @@ Vector3f ActuatorEffectivenessRotors::tiltedAxis(float tilt_angle, float tilt_di
return Dcmf{Eulerf{0.f, -tilt_angle, tilt_direction}} * axis;
}
uint32_t ActuatorEffectivenessRotors::getMotors() const
ActuatorBitmask ActuatorEffectivenessRotors::getMotors() const
{
uint32_t motors = 0;
ActuatorBitmask motors = 0;
for (int i = 0; i < _geometry.num_rotors; ++i) {
motors |= 1u << i;
@@ -267,9 +267,9 @@ uint32_t ActuatorEffectivenessRotors::getMotors() const
return motors;
}
uint32_t ActuatorEffectivenessRotors::getUpwardsMotors() const
ActuatorBitmask ActuatorEffectivenessRotors::getUpwardsMotors() const
{
uint32_t upwards_motors = 0;
ActuatorBitmask upwards_motors = 0;
for (int i = 0; i < _geometry.num_rotors; ++i) {
const Vector3f &axis = _geometry.rotors[i].axis;
@@ -282,9 +282,9 @@ uint32_t ActuatorEffectivenessRotors::getUpwardsMotors() const
return upwards_motors;
}
uint32_t ActuatorEffectivenessRotors::getForwardsMotors() const
ActuatorBitmask ActuatorEffectivenessRotors::getForwardsMotors() const
{
uint32_t forward_motors = 0;
ActuatorBitmask forward_motors = 0;
for (int i = 0; i < _geometry.num_rotors; ++i) {
const Vector3f &axis = _geometry.rotors[i].axis;

View File

@@ -51,6 +51,8 @@ class ActuatorEffectivenessTilts;
using namespace time_literals;
using ActuatorBitmask = ActuatorEffectiveness::ActuatorBitmask;
class ActuatorEffectivenessRotors : public ModuleParams, public ActuatorEffectiveness
{
public:
@@ -108,7 +110,7 @@ public:
* @param tilt_control current tilt control in [-1, 1] (can be NAN)
* @return the motors as bitset which are not tiltable
*/
uint32_t updateAxisFromTilts(const ActuatorEffectivenessTilts &tilts, float tilt_control);
ActuatorBitmask updateAxisFromTilts(const ActuatorEffectivenessTilts &tilts, float tilt_control);
const Geometry &geometry() const { return _geometry; }
@@ -126,9 +128,9 @@ public:
void enableThreeDimensionalThrust(bool enable) { _geometry.three_dimensional_thrust_disabled = !enable; }
uint32_t getMotors() const;
uint32_t getUpwardsMotors() const;
uint32_t getForwardsMotors() const;
ActuatorBitmask getMotors() const;
ActuatorBitmask getUpwardsMotors() const;
ActuatorBitmask getForwardsMotors() const;
private:
void updateParams() override;

View File

@@ -48,5 +48,5 @@ public:
const char *name() const override { return "Rover (Ackermann)"; }
private:
uint32_t _motors_mask{};
ActuatorBitmask _motors_mask{};
};

View File

@@ -84,8 +84,8 @@ private:
ActuatorEffectivenessRotors _rotors;
ActuatorEffectivenessControlSurfaces _control_surfaces;
uint32_t _upwards_motors_mask{};
uint32_t _forwards_motors_mask{};
ActuatorBitmask _upwards_motors_mask{};
ActuatorBitmask _forwards_motors_mask{};
int _first_control_surface_idx{0}; ///< applies to matrix 1

View File

@@ -84,7 +84,7 @@ protected:
ActuatorEffectivenessRotors _mc_rotors;
ActuatorEffectivenessControlSurfaces _control_surfaces;
uint32_t _forwards_motors_mask{};
ActuatorBitmask _forwards_motors_mask{};
int _first_control_surface_idx{0}; ///< applies to matrix 1

View File

@@ -93,8 +93,8 @@ protected:
ActuatorEffectivenessControlSurfaces _control_surfaces;
ActuatorEffectivenessTilts _tilts;
uint32_t _motors{};
uint32_t _untiltable_motors{};
ActuatorBitmask _motors{};
ActuatorBitmask _untiltable_motors{};
int _first_control_surface_idx{0}; ///< applies to matrix 1
int _first_tilt_idx{0}; ///< applies to matrix 0

View File

@@ -62,5 +62,5 @@ public:
protected:
ActuatorEffectivenessRotors _rotors;
uint32_t _motors_mask{};
ActuatorBitmask _motors_mask{};
};