From b6164107d117d370f6df10a61b156c9a8457d74f Mon Sep 17 00:00:00 2001 From: Balduin Date: Fri, 20 Feb 2026 08:40:34 +0100 Subject: [PATCH] refactor(control_allocation): Make type alias for actuator bitmask --- .../ActuatorEffectiveness.cpp | 4 ++-- .../ActuatorEffectiveness.hpp | 10 +++++++--- .../control_allocation/ControlAllocation.hpp | 3 ++- .../control_allocator/ControlAllocator.cpp | 8 ++++---- .../control_allocator/ControlAllocator.hpp | 1 + .../ActuatorEffectivenessCustom.hpp | 2 +- .../ActuatorEffectivenessFixedWing.hpp | 2 +- .../ActuatorEffectivenessRotors.cpp | 16 ++++++++-------- .../ActuatorEffectivenessRotors.hpp | 10 ++++++---- .../ActuatorEffectivenessRoverAckermann.hpp | 2 +- .../ActuatorEffectivenessStandardVTOL.hpp | 4 ++-- .../ActuatorEffectivenessTailsitterVTOL.hpp | 2 +- .../ActuatorEffectivenessTiltrotorVTOL.hpp | 4 ++-- .../ActuatorEffectivenessUUV.hpp | 2 +- 14 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/lib/control_allocation/actuator_effectiveness/ActuatorEffectiveness.cpp b/src/lib/control_allocation/actuator_effectiveness/ActuatorEffectiveness.cpp index b36da8230f..87e72a9977 100644 --- a/src/lib/control_allocation/actuator_effectiveness/ActuatorEffectiveness.cpp +++ b/src/lib/control_allocation/actuator_effectiveness/ActuatorEffectiveness.cpp @@ -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) { diff --git a/src/lib/control_allocation/actuator_effectiveness/ActuatorEffectiveness.hpp b/src/lib/control_allocation/actuator_effectiveness/ActuatorEffectiveness.hpp index ffc2315b72..b901d44a21 100644 --- a/src/lib/control_allocation/actuator_effectiveness/ActuatorEffectiveness.hpp +++ b/src/lib/control_allocation/actuator_effectiveness/ActuatorEffectiveness.hpp @@ -88,6 +88,10 @@ public: using EffectivenessMatrix = matrix::Matrix; using ActuatorVector = matrix::Vector; + 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}; }; diff --git a/src/lib/control_allocation/control_allocation/ControlAllocation.hpp b/src/lib/control_allocation/control_allocation/ControlAllocation.hpp index b8d647f834..8d3c75d6e3 100644 --- a/src/lib/control_allocation/control_allocation/ControlAllocation.hpp +++ b/src/lib/control_allocation/control_allocation/ControlAllocation.hpp @@ -83,6 +83,7 @@ public: static constexpr uint8_t NUM_AXES = ActuatorEffectiveness::NUM_AXES; using ActuatorVector = matrix::Vector; + 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)) { diff --git a/src/modules/control_allocator/ControlAllocator.cpp b/src/modules/control_allocator/ControlAllocator.cpp index 26ad512c56..3a6788c33c 100644 --- a/src/modules/control_allocator/ControlAllocator.cpp +++ b/src/modules/control_allocator/ControlAllocator.cpp @@ -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; diff --git a/src/modules/control_allocator/ControlAllocator.hpp b/src/modules/control_allocator/ControlAllocator.hpp index 4ed6e5f0d0..93d8d36463 100644 --- a/src/modules/control_allocator/ControlAllocator.hpp +++ b/src/modules/control_allocator/ControlAllocator.hpp @@ -96,6 +96,7 @@ public: using ActuatorVector = ActuatorEffectiveness::ActuatorVector; + using ActuatorBitmask = ActuatorEffectiveness::ActuatorBitmask; ControlAllocator(); diff --git a/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessCustom.hpp b/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessCustom.hpp index e2f5d4dfa6..2ae810bf6d 100644 --- a/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessCustom.hpp +++ b/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessCustom.hpp @@ -54,5 +54,5 @@ protected: ActuatorEffectivenessRotors _motors; ActuatorEffectivenessControlSurfaces _torque; - uint32_t _motors_mask{}; + ActuatorBitmask _motors_mask{}; }; diff --git a/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessFixedWing.hpp b/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessFixedWing.hpp index 7e2e7802eb..cc955d1acc 100644 --- a/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessFixedWing.hpp +++ b/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessFixedWing.hpp @@ -65,5 +65,5 @@ private: int _first_control_surface_idx{0}; ///< applies to matrix 1 - uint32_t _forwards_motors_mask{}; + ActuatorBitmask _forwards_motors_mask{}; }; diff --git a/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessRotors.cpp b/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessRotors.cpp index b4edd0f2b0..2d3ed43f08 100644 --- a/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessRotors.cpp +++ b/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessRotors.cpp @@ -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; diff --git a/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessRotors.hpp b/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessRotors.hpp index c6f0425569..60d9e11973 100644 --- a/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessRotors.hpp +++ b/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessRotors.hpp @@ -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; diff --git a/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessRoverAckermann.hpp b/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessRoverAckermann.hpp index 82b07733bf..3e6c14f676 100644 --- a/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessRoverAckermann.hpp +++ b/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessRoverAckermann.hpp @@ -48,5 +48,5 @@ public: const char *name() const override { return "Rover (Ackermann)"; } private: - uint32_t _motors_mask{}; + ActuatorBitmask _motors_mask{}; }; diff --git a/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessStandardVTOL.hpp b/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessStandardVTOL.hpp index 4879c6b022..3c1ba1f0c8 100644 --- a/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessStandardVTOL.hpp +++ b/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessStandardVTOL.hpp @@ -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 diff --git a/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessTailsitterVTOL.hpp b/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessTailsitterVTOL.hpp index c7a588954f..293157d729 100644 --- a/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessTailsitterVTOL.hpp +++ b/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessTailsitterVTOL.hpp @@ -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 diff --git a/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessTiltrotorVTOL.hpp b/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessTiltrotorVTOL.hpp index db7b37304b..4c9a412424 100644 --- a/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessTiltrotorVTOL.hpp +++ b/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessTiltrotorVTOL.hpp @@ -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 diff --git a/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessUUV.hpp b/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessUUV.hpp index 78e52c34aa..e4873f38d2 100644 --- a/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessUUV.hpp +++ b/src/modules/control_allocator/VehicleActuatorEffectiveness/ActuatorEffectivenessUUV.hpp @@ -62,5 +62,5 @@ public: protected: ActuatorEffectivenessRotors _rotors; - uint32_t _motors_mask{}; + ActuatorBitmask _motors_mask{}; };