Move StickYaw class into a globally available library

This commit is contained in:
Matthias Grob
2025-04-22 15:35:26 +02:00
parent bae6328c7b
commit 174147208e
12 changed files with 46 additions and 10 deletions
@@ -35,5 +35,5 @@ px4_add_library(FlightTaskAuto
FlightTaskAuto.cpp
)
target_link_libraries(FlightTaskAuto PUBLIC FlightTask FlightTaskUtility WeatherVane)
target_link_libraries(FlightTaskAuto PUBLIC FlightTask FlightTaskUtility StickYaw WeatherVane)
target_include_directories(FlightTaskAuto PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
@@ -46,12 +46,12 @@
#include <uORB/topics/manual_control_setpoint.h>
#include <uORB/topics/vehicle_status.h>
#include <lib/geo/geo.h>
#include <lib/weather_vane/WeatherVane.hpp>
#include <lib/mathlib/math/filter/AlphaFilter.hpp>
#include <lib/motion_planning/PositionSmoothing.hpp>
#include <lib/stick_yaw/StickYaw.hpp>
#include <lib/weather_vane/WeatherVane.hpp>
#include "Sticks.hpp"
#include "StickAccelerationXY.hpp"
#include "StickYaw.hpp"
/**
* This enum has to agree with position_setpoint_s type definition
@@ -35,5 +35,5 @@ px4_add_library(FlightTaskDescend
FlightTaskDescend.cpp
)
target_link_libraries(FlightTaskDescend PUBLIC FlightTask FlightTaskUtility)
target_link_libraries(FlightTaskDescend PUBLIC FlightTask FlightTaskUtility StickYaw)
target_include_directories(FlightTaskDescend PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
@@ -37,10 +37,10 @@
#pragma once
#include <lib/stick_yaw/StickYaw.hpp>
#include "FlightTask.hpp"
#include "Sticks.hpp"
#include "StickTiltXY.hpp"
#include "StickYaw.hpp"
class FlightTaskDescend : public FlightTask
{
@@ -33,10 +33,9 @@
#pragma once
#include <lib/weather_vane/WeatherVane.hpp>
#include "FlightTaskManualAltitudeSmoothVel.hpp"
#include "StickAccelerationXY.hpp"
#include "StickYaw.hpp"
#include <lib/weather_vane/WeatherVane.hpp>
class FlightTaskManualAcceleration : public FlightTaskManualAltitudeSmoothVel
{
@@ -35,5 +35,5 @@ px4_add_library(FlightTaskManualAltitude
FlightTaskManualAltitude.cpp
)
target_link_libraries(FlightTaskManualAltitude PUBLIC FlightTask FlightTaskUtility)
target_link_libraries(FlightTaskManualAltitude PUBLIC FlightTask FlightTaskUtility StickYaw)
target_include_directories(FlightTaskManualAltitude PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
@@ -39,10 +39,10 @@
#pragma once
#include <lib/stick_yaw/StickYaw.hpp>
#include "FlightTask.hpp"
#include "Sticks.hpp"
#include "StickTiltXY.hpp"
#include "StickYaw.hpp"
#include <uORB/Subscription.hpp>
class FlightTaskManualAltitude : public FlightTask
@@ -35,7 +35,6 @@ px4_add_library(FlightTaskUtility
Sticks.cpp
StickAccelerationXY.cpp
StickTiltXY.cpp
StickYaw.cpp
Gimbal.cpp
)
@@ -1,124 +0,0 @@
/****************************************************************************
*
* Copyright (c) 2020-2023 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#include "StickYaw.hpp"
#include <px4_platform_common/defines.h>
using matrix::wrap_pi;
StickYaw::StickYaw(ModuleParams *parent) :
ModuleParams(parent)
{}
void StickYaw::reset(const float yaw, const float unaided_yaw)
{
if (PX4_ISFINITE(unaided_yaw)) {
_yaw_error_lpf.reset(wrap_pi(yaw - unaided_yaw));
}
}
void StickYaw::ekfResetHandler(const float delta_yaw)
{
_yaw_error_lpf.reset(wrap_pi(_yaw_error_lpf.getState() + delta_yaw));
_yaw_error_ref = wrap_pi(_yaw_error_ref + delta_yaw);
}
void StickYaw::generateYawSetpoint(float &yawspeed_setpoint, float &yaw_setpoint, const float stick_yaw,
const float yaw, const float deltatime, const float unaided_yaw)
{
const float yaw_correction_prev = _yaw_correction;
const bool reset_setpoint = updateYawCorrection(yaw, unaided_yaw, deltatime);
if (reset_setpoint) {
yaw_setpoint = NAN;
}
_yawspeed_filter.setParameters(deltatime, _param_mpc_man_y_tau.get());
const float yawspeed_scale = math::min(math::radians(_param_mpc_man_y_max.get()), _yawspeed_constraint);
yawspeed_setpoint = _yawspeed_filter.update(stick_yaw * yawspeed_scale);
yaw_setpoint = updateYawLock(yaw, yawspeed_setpoint, yaw_setpoint, yaw_correction_prev);
}
bool StickYaw::updateYawCorrection(const float yaw, const float unaided_yaw, const float deltatime)
{
if (!PX4_ISFINITE(unaided_yaw)) {
_yaw_correction = 0.f;
return false;
}
// Detect the convergence phase of the yaw estimate by monitoring its relative
// distance from an unaided yaw source.
const float yaw_error = wrap_pi(yaw - unaided_yaw);
// Run it through a high-pass filter to detect transients
const float yaw_error_hpf = wrap_pi(yaw_error - _yaw_error_lpf.getState());
_yaw_error_lpf.update(yaw_error, deltatime);
const bool was_converging = _yaw_estimate_converging;
_yaw_estimate_converging = fabsf(yaw_error_hpf) > _kYawErrorChangeThreshold;
bool reset_setpoint = false;
if (!_yaw_estimate_converging) {
_yaw_error_ref = yaw_error;
if (was_converging) {
// Force a reset of the locking mechanism
reset_setpoint = true;
}
}
_yaw_correction = wrap_pi(yaw_error - _yaw_error_ref);
return reset_setpoint;
}
float StickYaw::updateYawLock(const float yaw, const float yawspeed_setpoint, const float yaw_setpoint,
const float yaw_correction_prev) const
{
// Yaw-lock depends on desired yawspeed input. If not locked, yaw_sp is set to NAN.
if (fabsf(yawspeed_setpoint) > FLT_EPSILON) {
// no fixed heading when rotating around yaw by stick
return NAN;
} else {
// break down and hold the current heading when no more rotation commanded
if (!PX4_ISFINITE(yaw_setpoint)) {
return yaw;
} else {
return wrap_pi(yaw_setpoint - yaw_correction_prev + _yaw_correction);
}
}
}
@@ -1,88 +0,0 @@
/****************************************************************************
*
* Copyright (c) 2020-2023 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* @file StickYaw.hpp
* @brief Generate yaw and angular yawspeed setpoints from stick input
* @author Matthias Grob <maetugr@gmail.com>
*/
#pragma once
#include <lib/mathlib/math/filter/AlphaFilter.hpp>
#include <px4_platform_common/module_params.h>
class StickYaw : public ModuleParams
{
public:
StickYaw(ModuleParams *parent);
~StickYaw() = default;
void reset(float yaw, float unaided_yaw = NAN);
void ekfResetHandler(float delta_yaw);
void generateYawSetpoint(float &yawspeed_setpoint, float &yaw_setpoint, float stick_yaw, float yaw, float deltatime,
float unaided_yaw = NAN);
void setYawspeedConstraint(float yawspeed) { _yawspeed_constraint = yawspeed; };
private:
AlphaFilter<float> _yawspeed_filter;
float _yaw_error_ref{0.f};
float _yaw_correction{0.f};
bool _yaw_estimate_converging{false};
AlphaFilter<float> _yaw_error_lpf{_kYawErrorTimeConstant}; ///< used to create a high-pass filter
static constexpr float _kYawErrorTimeConstant{1.f}; ///< time constant of the high-pass filter used to detect yaw convergence
static constexpr float _kYawErrorChangeThreshold{radians(1.f)}; ///< we consider the yaw estimate as "converging" when above this threshold
bool updateYawCorrection(float yaw, float unaided_yaw, float deltatime);
/**
* Lock yaw when not currently turning
* When applying a yawspeed the vehicle is turning, when the speed is
* set to zero the vehicle needs to slow down and then lock at the yaw
* it stops at to not drift over time.
* @param yawspeed current yaw rotational rate state
* @param yaw current yaw rotational rate state
* @param yawspeed_setpoint rotation rate at which to turn around yaw axis
* @param yaw current yaw setpoint which then will be overwritten by the return value
* @return yaw setpoint to execute to have a yaw lock at the correct moment in time
*/
float updateYawLock(float yaw, float yawspeed_setpoint, float yaw_setpoint, float yaw_correction_prev) const;
float _yawspeed_constraint{INFINITY};
DEFINE_PARAMETERS(
(ParamFloat<px4::params::MPC_MAN_Y_MAX>) _param_mpc_man_y_max, ///< Maximum yaw speed with full stick deflection
(ParamFloat<px4::params::MPC_MAN_Y_TAU>) _param_mpc_man_y_tau ///< time constant for yaw speed filtering
)
};