ControlMath: adding limitTilt() helper function

which takes care of limiting the lilt angle of a "body" vector with
respect to a "world" vector. Both vectors have to be unit length!
This commit is contained in:
Matthias Grob
2020-01-27 10:33:14 +01:00
parent aa53cabaa4
commit 1d2ac41edc
3 changed files with 88 additions and 3 deletions
@@ -50,6 +50,23 @@ void thrustToAttitude(const Vector3f &thr_sp, const float yaw_sp, vehicle_attitu
att_sp.thrust_body[2] = -thr_sp.length();
}
void limitTilt(Vector3f &body_unit, const Vector3f &world_unit, const float max_angle)
{
// determine tilt
const float dot_product_unit = body_unit.dot(world_unit);
float angle = acosf(dot_product_unit);
// limit tilt
angle = math::min(angle, max_angle);
Vector3f rejection = body_unit - (dot_product_unit * world_unit);
// corner case exactly parallel vectors
if (rejection.norm_squared() < FLT_EPSILON) {
rejection(0) = 1.f;
}
body_unit = cosf(angle) * world_unit + sinf(angle) * rejection.unit();
}
void bodyzToAttitude(Vector3f body_z, const float yaw_sp, vehicle_attitude_setpoint_s &att_sp)
{
// zero vector, no direction, set safe level value