-
Notifications
You must be signed in to change notification settings - Fork 14k
Spacecraft Attitude Control #24721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Pedro-Roque
wants to merge
8
commits into
main
Choose a base branch
from
dev-sc_att_control
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Spacecraft Attitude Control #24721
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
95123b8
rft: initial merging of controllers for spacecraft vehicles
Pedro-Roque de9755b
feat: rate controller nominal
Pedro-Roque 4eb3a23
feat: spacecraft tooling for commander and VehicleStatus
Pedro-Roque 4593471
fix: format
Pedro-Roque a6643d8
fix: remove iostream
Pedro-Roque c1d8ad4
feat: spacecraft attitude control and minor refactoring of params
Pedro-Roque abf6e44
rft: explicit naming sc to spacecraft
Pedro-Roque 8219eef
fix: remove extra line
Pedro-Roque File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/modules/spacecraft/SpacecraftAttitudeControl/AttitudeControl/AttitudeControl.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/**************************************************************************** | ||
* | ||
* Copyright (c) 2019 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 AttitudeControl.cpp | ||
*/ | ||
|
||
#include <AttitudeControl.hpp> | ||
|
||
#include <mathlib/math/Functions.hpp> | ||
|
||
using namespace matrix; | ||
|
||
void ScAttitudeControl::setProportionalGain(const matrix::Vector3f &proportional_gain) | ||
{ | ||
_proportional_gain = proportional_gain; | ||
} | ||
|
||
matrix::Vector3f ScAttitudeControl::update(const Quatf &q) const | ||
{ | ||
Quatf qd = _attitude_setpoint_q; | ||
|
||
// calculate reduced desired attitude neglecting vehicle's yaw to prioritize roll and pitch | ||
const Vector3f e_z = q.dcm_z(); | ||
const Vector3f e_z_d = qd.dcm_z(); | ||
Quatf qd_red(e_z, e_z_d); | ||
|
||
if (fabsf(qd_red(1)) > (1.f - 1e-5f) || fabsf(qd_red(2)) > (1.f - 1e-5f)) { | ||
// In the infinitesimal corner case where the vehicle and thrust have the completely opposite direction, | ||
// full attitude control anyways generates no yaw input and directly takes the combination of | ||
// roll and pitch leading to the correct desired yaw. Ignoring this case would still be totally safe and stable. | ||
qd_red = qd; | ||
|
||
} else { | ||
// transform rotation from current to desired thrust vector into a world frame reduced desired attitude | ||
qd_red *= q; | ||
} | ||
|
||
// mix full and reduced desired attitude | ||
Quatf q_mix = qd_red.inversed() * qd; | ||
q_mix.canonicalize(); | ||
|
||
// catch numerical problems with the domain of acosf and asinf | ||
q_mix(0) = math::constrain(q_mix(0), -1.f, 1.f); | ||
q_mix(3) = math::constrain(q_mix(3), -1.f, 1.f); | ||
qd = qd_red * Quatf(q_mix(0), 0, 0, q_mix(3)); | ||
|
||
// quaternion attitude control law, qe is rotation from q to qd | ||
const Quatf qe = q.inversed() * qd; | ||
|
||
// using sin(alpha/2) scaled rotation axis as attitude error (see quaternion definition by axis angle) | ||
// also taking care of the antipodal unit quaternion ambiguity | ||
const Vector3f eq = 2.f * qe.canonical().imag(); | ||
|
||
// calculate angular rates setpoint | ||
Vector3f rate_setpoint = eq.emult(_proportional_gain); | ||
|
||
// limit rates | ||
for (int i = 0; i < 3; i++) { | ||
rate_setpoint(i) = math::constrain(rate_setpoint(i), -_rate_limit(i), _rate_limit(i)); | ||
} | ||
|
||
return rate_setpoint; | ||
} |
105 changes: 105 additions & 0 deletions
105
src/modules/spacecraft/SpacecraftAttitudeControl/AttitudeControl/AttitudeControl.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/**************************************************************************** | ||
* | ||
* Copyright (c) 2019 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 AttitudeControl.hpp | ||
* | ||
* A quaternion based attitude controller. | ||
* | ||
* @author Matthias Grob <maetugr@gmail.com> | ||
* | ||
* Publication documenting the implemented Quaternion Attitude Control: | ||
* Nonlinear Quadrocopter Attitude Control (2013) | ||
* by Dario Brescianini, Markus Hehn and Raffaello D'Andrea | ||
* Institute for Dynamic Systems and Control (IDSC), ETH Zurich | ||
* | ||
* https://www.research-collection.ethz.ch/bitstream/handle/20.500.11850/154099/eth-7387-01.pdf | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <matrix/matrix/math.hpp> | ||
#include <mathlib/math/Limits.hpp> | ||
|
||
class ScAttitudeControl | ||
{ | ||
public: | ||
ScAttitudeControl() = default; | ||
~ScAttitudeControl() = default; | ||
|
||
/** | ||
* Set proportional attitude control gain | ||
* @param proportional_gain 3D vector containing gains for roll, pitch, yaw | ||
*/ | ||
void setProportionalGain(const matrix::Vector3f &proportional_gain); | ||
|
||
/** | ||
* Set hard limit for output rate setpoints | ||
* @param rate_limit [rad/s] 3D vector containing limits for roll, pitch, yaw | ||
*/ | ||
void setRateLimit(const matrix::Vector3f &rate_limit) { _rate_limit = rate_limit; } | ||
|
||
/** | ||
* Set a new attitude setpoint replacing the one tracked before | ||
* @param qd desired vehicle attitude setpoint | ||
*/ | ||
void setAttitudeSetpoint(const matrix::Quatf &qd) | ||
{ | ||
_attitude_setpoint_q = qd; | ||
_attitude_setpoint_q.normalize(); | ||
} | ||
|
||
/** | ||
* Adjust last known attitude setpoint by a delta rotation | ||
* Optional use to avoid glitches when attitude estimate reference e.g. heading changes. | ||
* @param q_delta delta rotation to apply | ||
*/ | ||
void adaptAttitudeSetpoint(const matrix::Quatf &q_delta) | ||
{ | ||
_attitude_setpoint_q = q_delta * _attitude_setpoint_q; | ||
_attitude_setpoint_q.normalize(); | ||
} | ||
|
||
/** | ||
* Run one control loop cycle calculation | ||
* @param q estimation of the current vehicle attitude unit quaternion | ||
* @return [rad/s] body frame 3D angular rate setpoint vector to be executed by the rate controller | ||
*/ | ||
matrix::Vector3f update(const matrix::Quatf &q) const; | ||
|
||
private: | ||
matrix::Vector3f _proportional_gain; | ||
matrix::Vector3f _rate_limit; | ||
|
||
matrix::Quatf _attitude_setpoint_q; ///< latest known attitude setpoint e.g. from position control | ||
}; |
70 changes: 70 additions & 0 deletions
70
src/modules/spacecraft/SpacecraftAttitudeControl/AttitudeControl/AttitudeControlMath.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/**************************************************************************** | ||
* | ||
* 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. | ||
* | ||
****************************************************************************/ | ||
|
||
/** | ||
* @file AttitudeControlMath.hpp | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <matrix/matrix/math.hpp> | ||
|
||
namespace AttitudeControlMath | ||
{ | ||
/** | ||
* Rotate a tilt quaternion (without yaw rotation) such that when rotated by a yaw setpoint | ||
* the resulting tilt is the same as if it was rotated by the current yaw of the vehicle | ||
* @param q_sp_tilt pure tilt quaternion (yaw = 0) that needs to be corrected | ||
* @param q_att current attitude of the vehicle | ||
* @param q_sp_yaw pure yaw quaternion of the desired yaw setpoint | ||
*/ | ||
void inline correctTiltSetpointForYawError(matrix::Quatf &q_sp_tilt, const matrix::Quatf &q_att, | ||
const matrix::Quatf &q_sp_yaw) | ||
{ | ||
const matrix::Vector3f z_unit(0.f, 0.f, 1.f); | ||
|
||
// Extract yaw from the current attitude | ||
const matrix::Vector3f att_z = q_att.dcm_z(); | ||
const matrix::Quatf q_tilt(z_unit, att_z); | ||
const matrix::Quatf q_yaw = q_tilt.inversed() * q_att; // This is not euler yaw | ||
|
||
// Find the quaternion that creates a tilt aligned with the body frame | ||
// when rotated by the yaw setpoint | ||
// To do so, solve q_yaw * q_tilt_ne = q_sp_yaw * q_sp_rp_compensated | ||
const matrix::Quatf q_sp_rp_compensated = q_sp_yaw.inversed() * q_yaw * q_sp_tilt; | ||
|
||
// Extract the corrected tilt | ||
const matrix::Vector3f att_sp_z = q_sp_rp_compensated.dcm_z(); | ||
q_sp_tilt = matrix::Quatf(z_unit, att_sp_z); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.