mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-05-31 18:47:21 +08:00
mathlib: get rid of now duplicate sign() function
Instead use the one that was copied to the matrix library in https://github.com/PX4/Matrix/pull/116/
This commit is contained in:
@@ -493,7 +493,7 @@ bool FlightTaskAuto::_compute_heading_from_2D_vector(float &heading, Vector2f v)
|
||||
// and multiply by the sign given of cross product of x and v.
|
||||
// Dot product: (x(0)*v(0)+(x(1)*v(1)) = v(0)
|
||||
// Cross product: x(0)*v(1) - v(0)*x(1) = v(1)
|
||||
heading = math::sign(v(1)) * wrap_pi(acosf(v(0)));
|
||||
heading = sign(v(1)) * wrap_pi(acosf(v(0)));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -175,7 +175,7 @@ float FlightTaskAutoLineSmoothVel::_constrainOneSide(float val, float constraint
|
||||
|
||||
float FlightTaskAutoLineSmoothVel::_constrainAbs(float val, float max)
|
||||
{
|
||||
return math::sign(val) * math::min(fabsf(val), fabsf(max));
|
||||
return sign(val) * math::min(fabsf(val), fabsf(max));
|
||||
}
|
||||
|
||||
float FlightTaskAutoLineSmoothVel::_getMaxXYSpeed() const
|
||||
@@ -301,7 +301,7 @@ void FlightTaskAutoLineSmoothVel::_prepareSetpoints()
|
||||
else if (z_pos_setpoint_valid) {
|
||||
// Use Z position setpoint to generate a Z velocity setpoint
|
||||
|
||||
const float z_dir = math::sign(_position_setpoint(2) - _trajectory[2].getCurrentPosition());
|
||||
const float z_dir = sign(_position_setpoint(2) - _trajectory[2].getCurrentPosition());
|
||||
const float vel_sp_z = z_dir * _getMaxZSpeed();
|
||||
|
||||
// If available, use the existing velocity as a feedforward, otherwise replace it
|
||||
|
||||
@@ -117,7 +117,7 @@ bool FlightTaskOrbit::setRadius(float r)
|
||||
|
||||
// small radius is more important than high velocity for safety
|
||||
if (!checkAcceleration(r, _v, _acceleration_max)) {
|
||||
_v = math::sign(_v) * sqrtf(_acceleration_max * r);
|
||||
_v = sign(_v) * sqrtf(_acceleration_max * r);
|
||||
}
|
||||
|
||||
_r = r;
|
||||
|
||||
@@ -36,6 +36,9 @@
|
||||
#include <cstdio>
|
||||
#include <float.h>
|
||||
#include <mathlib/mathlib.h>
|
||||
#include <matrix/matrix/math.hpp>
|
||||
|
||||
using matrix::sign;
|
||||
|
||||
VelocitySmoothing::VelocitySmoothing(float initial_accel, float initial_vel, float initial_pos)
|
||||
{
|
||||
@@ -183,12 +186,12 @@ int VelocitySmoothing::computeDirection()
|
||||
float vel_zero_acc = computeVelAtZeroAcc();
|
||||
|
||||
/* Depending of the direction, start accelerating positively or negatively */
|
||||
int direction = math::sign(_vel_sp - vel_zero_acc);
|
||||
int direction = sign(_vel_sp - vel_zero_acc);
|
||||
|
||||
if (direction == 0) {
|
||||
// If by braking immediately the velocity is exactly
|
||||
// the require one with zero acceleration, then brake
|
||||
direction = math::sign(_state.a);
|
||||
direction = sign(_state.a);
|
||||
}
|
||||
|
||||
return direction;
|
||||
@@ -199,7 +202,7 @@ float VelocitySmoothing::computeVelAtZeroAcc()
|
||||
float vel_zero_acc = _state.v;
|
||||
|
||||
if (fabsf(_state.a) > FLT_EPSILON) {
|
||||
float j_zero_acc = -math::sign(_state.a) * _max_jerk; // Required jerk to reduce the acceleration
|
||||
float j_zero_acc = -sign(_state.a) * _max_jerk; // Required jerk to reduce the acceleration
|
||||
float t_zero_acc = -_state.a / j_zero_acc; // Required time to cancel the current acceleration
|
||||
vel_zero_acc = _state.v + _state.a * t_zero_acc + 0.5f * j_zero_acc * t_zero_acc * t_zero_acc;
|
||||
}
|
||||
|
||||
@@ -41,16 +41,11 @@
|
||||
|
||||
#include "Limits.hpp"
|
||||
|
||||
#include <matrix/matrix/math.hpp>
|
||||
|
||||
namespace math
|
||||
{
|
||||
|
||||
// Type-safe signum function
|
||||
template<typename T>
|
||||
int sign(T val)
|
||||
{
|
||||
return (T(FLT_EPSILON) < val) - (val < T(FLT_EPSILON));
|
||||
}
|
||||
|
||||
// Type-safe signum function with zero treated as positive
|
||||
template<typename T>
|
||||
int signNoZero(T val)
|
||||
@@ -114,7 +109,7 @@ const T deadzone(const T &value, const T &dz)
|
||||
T x = constrain(value, (T) - 1, (T) 1);
|
||||
T dzc = constrain(dz, (T) 0, (T) 0.99);
|
||||
// Rescale the input such that we get a piecewise linear function that will be continuous with applied deadzone
|
||||
T out = (x - sign(x) * dzc) / (1 - dzc);
|
||||
T out = (x - matrix::sign(x) * dzc) / (1 - dzc);
|
||||
// apply the deadzone (values zero around the middle)
|
||||
return out * (fabsf(x) > dzc);
|
||||
}
|
||||
|
||||
+1
-1
Submodule src/lib/matrix updated: 4873dc1c1e...a32892926c
@@ -39,6 +39,8 @@
|
||||
|
||||
#include "zero_order_hover_thrust_ekf.hpp"
|
||||
|
||||
using matrix::sign;
|
||||
|
||||
void ZeroOrderHoverThrustEkf::predict(const float dt)
|
||||
{
|
||||
// State is constant
|
||||
@@ -69,7 +71,7 @@ void ZeroOrderHoverThrustEkf::fuseAccZ(const float acc_z, const float thrust, st
|
||||
bumpStateVariance();
|
||||
}
|
||||
|
||||
const float signed_innov_test_ratio = math::sign(innov) * innov_test_ratio;
|
||||
const float signed_innov_test_ratio = sign(innov) * innov_test_ratio;
|
||||
updateLpf(residual, signed_innov_test_ratio);
|
||||
updateMeasurementNoise(residual, H);
|
||||
|
||||
|
||||
@@ -219,7 +219,7 @@ void PositionControl::_velocityControl(const float dt)
|
||||
_vel_int += vel_error.emult(_gain_vel_i) * dt;
|
||||
|
||||
// limit thrust integral
|
||||
_vel_int(2) = math::min(fabsf(_vel_int(2)), _lim_thr_max) * math::sign(_vel_int(2));
|
||||
_vel_int(2) = math::min(fabsf(_vel_int(2)), _lim_thr_max) * sign(_vel_int(2));
|
||||
}
|
||||
|
||||
bool PositionControl::_updateSuccessful()
|
||||
|
||||
@@ -47,9 +47,7 @@
|
||||
|
||||
#define ACTUATOR_PUBLISH_PERIOD_MS 4
|
||||
|
||||
using matrix::Eulerf;
|
||||
using matrix::Quatf;
|
||||
using matrix::Vector3f;
|
||||
using namespace matrix;
|
||||
|
||||
/**
|
||||
* L1 control app start / stop handling function
|
||||
@@ -261,7 +259,7 @@ RoverPositionControl::control_position(const matrix::Vector2f ¤t_position,
|
||||
|
||||
float desired_r = ground_speed_2d.norm_squared() / math::abs_t(_gnd_control.nav_lateral_acceleration_demand());
|
||||
float desired_theta = (0.5f * M_PI_F) - atan2f(desired_r, _param_wheel_base.get());
|
||||
float control_effort = (desired_theta / _param_max_turn_angle.get()) * math::sign(
|
||||
float control_effort = (desired_theta / _param_max_turn_angle.get()) * sign(
|
||||
_gnd_control.nav_lateral_acceleration_demand());
|
||||
control_effort = math::constrain(control_effort, -1.0f, 1.0f);
|
||||
_act_controls.control[actuator_controls_s::INDEX_YAW] = control_effort;
|
||||
|
||||
Reference in New Issue
Block a user