Files
PX4-Autopilot/src/modules/spacecraft/SpacecraftAttitudeControl/AttitudeControl/ScAttitudeControlMathTest.cpp
T
Pedro Roque 2f55dff8b9 feat: spacecraft support (#24734)
* rft: initial merging of controllers for spacecraft vehicles

* feat: rate controller nominal

* feat: spacecraft tooling for commander and VehicleStatus

* feat: spacecraft tooling for commander and VehicleStatus

* fix: format

* fix: format

* fix: remove iostream

* fix: remove iostream

* feat: spacecraft attitude control and minor refactoring of params

* feat: add position controller

* fix: format

* fix: moved trajectories to new message, removed derivative filters

* fix: format

* fix: removed extra newline

* fix: spacecraft allocation builds

* feat: add thrusters to effectivenes, add spacecraft build to cmake, clean comments

* feat: required changes for allocation

* feat: thruster simulation interface

* fix: update maximum and minimums

* fix: format

* fix: added newline at the end of spacecraft actuator effectiveness

* feat: configurable board pwm freq from Kconfig

* feat: mavlink compliant spacecraft definition

* feat: add orbiter to define

* boards: Increase TELEM2 rx buffer size for DDS over serial use-case (ARK Jetson)

feat: spacecraft tooling for commander and VehicleStatus

fix: format

fix: remove iostream

feat: mavlink compliant spacecraft definition

* feat: add orbiter to define

* feat: add orbiter to define

* fix: change mav_type to new spacecraft orbiter enum value

* fix: build issue

* feat: update mavlink

* feat: update mavlink to latest master with spacecraft

* feat: update mavlink

* feat: update mavlink to latest

* feat: cleanup and synchronization with new mavlink vehicle definition

* fix: get away without specifying spacecraft vehicle

* fix: removed unnecessary definition

* fix: format

* feat: cmake variant for spacecraft

* feat: proper mav_type and rc init

* fix: removed dart from build system

* add: thrusters to actuator type

* rft: reordering actuator type

* rft: initial merging of controllers for spacecraft vehicles

* feat: rate controller nominal

* fix: format

* feat: spacecraft attitude control and minor refactoring of params

* feat: add position controller

* fix: format

* fix: moved trajectories to new message, removed derivative filters

* fix: format

* fix: removed extra newline

* fix: spacecraft allocation builds

* feat: add thrusters to effectivenes, add spacecraft build to cmake, clean comments

* feat: required changes for allocation

* feat: thruster simulation interface

* fix: update maximum and minimums

* fix: format

* fix: added newline at the end of spacecraft actuator effectiveness

* feat: configurable board pwm freq from Kconfig

* feat: add orbiter to define

* feat: cleanup and synchronization with new mavlink vehicle definition

* fix: get away without specifying spacecraft vehicle

* fix: conflicts

* fix: format

* fix: remove duplicate entry

* rft: remove Kconfig changes

* rft: revert main Kconfig

* rft: revert main kcoonfig on platforms

* rft: remove changes to board PWm (go on another PR)

* rft: revert changes to commander (main is correct)

* fix: extra char on commander_helper

* rft: removed extra spaces

* rft: moved effectiveness to spacecraft

* fix: spacecraft effectiveness

* fix: extra space

* feat: preliminary version, still using thrusters

* rft: initial pipeline on PX4 side with rotors instead of thrusters

* feat: add atmos model

* feat: spacecraft with rotor pipeline tested, working

* feat: update gz

* rft: removed thruster interfaces

* fix: format

* fix: remove control allocation

* fix: thruster normalization

* fix: format

* fix: nuttx version

* fix: clang tidy error

* feat: updated gz to add atmos model

* fix: update gz

* fix: update mavlink

* fix: remove friend class from allocation lib

* fix: remove actuator_outputs/motors

---------

Co-authored-by: Alexander Lerach <alexander@auterion.com>
2025-07-23 08:26:27 -07:00

93 lines
4.0 KiB
C++

/****************************************************************************
*
* Copyright (C) 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 <gtest/gtest.h>
#include "AttitudeControlMath.hpp"
using namespace matrix;
using namespace AttitudeControlMath;
static const Vector3f z_unit(0.f, 0.f, 1.f);
TEST(AttitudeControlMath, tiltCorrectionNoError)
{
// GIVEN: some desired (non yaw-rotated) tilt setpoint
Quatf q_tilt_sp_ne(z_unit, Vector3f(-0.3, 0.1, 0.7));
// AND: a desired yaw setpoint
const Quatf q_sp_yaw = AxisAnglef(z_unit, -1.23f);
// WHEN: the current yaw error is zero (regardless of the tilt)
const Quatf q = q_sp_yaw * Quatf(z_unit, Vector3f(0.1f, -0.2f, 1.f));
const Quatf q_tilt_sp_ne_before = q_tilt_sp_ne;
correctTiltSetpointForYawError(q_tilt_sp_ne, q, q_sp_yaw);
// THEN: the tilt setpoint is unchanged
EXPECT_TRUE(isEqual(q_tilt_sp_ne_before, q_tilt_sp_ne));
}
TEST(AttitudeControlMath, tiltCorrectionYaw180)
{
// GIVEN: some desired (non yaw-rotated) tilt setpoint and a desired yaw setpoint
Quatf q_tilt_sp_ne(z_unit, Vector3f(-0.3, 0.1, 0.7));
const Quatf q_sp_yaw = AxisAnglef(z_unit, -M_PI_F / 2.f);
// WHEN: there is a yaw error of 180 degrees
const Quatf q_yaw = Quatf(AxisAnglef(z_unit, M_PI_F / 2.f));
const Quatf q = q_yaw * Quatf(z_unit, Vector3f(0.1f, -0.2f, 1.f));
const Quatf q_tilt_sp_ne_before = q_tilt_sp_ne;
correctTiltSetpointForYawError(q_tilt_sp_ne, q, q_sp_yaw);
// THEN: the tilt is reversed (the corrected tilt angle is the same but the axis of rotation is opposite)
EXPECT_FLOAT_EQ(AxisAnglef(q_tilt_sp_ne_before).angle(), AxisAnglef(q_tilt_sp_ne).angle());
EXPECT_TRUE(isEqual(AxisAnglef(q_tilt_sp_ne_before).axis(), -AxisAnglef(q_tilt_sp_ne).axis()));
}
TEST(AttitudeControlMath, tiltCorrection)
{
// GIVEN: some desired (non yaw-rotated) tilt setpoint and a desired yaw setpoint
Quatf q_tilt_sp_ne(z_unit, Vector3f(0.5, -0.1, 0.7));
const Quatf q_sp_yaw = AxisAnglef(z_unit, -1.23f);
// WHEN: there is a some yaw error
const Quatf q_yaw = Quatf(AxisAnglef(z_unit, 3.1f));
const Quatf q = q_yaw * Quatf(z_unit, Vector3f(0.1f, -0.2f, 1.f));
const Quatf q_tilt_sp_ne_before = q_tilt_sp_ne;
correctTiltSetpointForYawError(q_tilt_sp_ne, q, q_sp_yaw);
// THEN: the tilt vector obtained by rotating the corrected tilt by the yaw setpoint is the same as
// the one obtained by rotating the initial tilt by the current yaw of the vehicle
EXPECT_TRUE(isEqual((q_sp_yaw * q_tilt_sp_ne).dcm_z(), (q_yaw * q_tilt_sp_ne_before).dcm_z()));
}