lee_position_controller.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015 Fadri Furrer, ASL, ETH Zurich, Switzerland
3  * Copyright 2015 Michael Burri, ASL, ETH Zurich, Switzerland
4  * Copyright 2015 Mina Kamel, ASL, ETH Zurich, Switzerland
5  * Copyright 2015 Janosch Nikolic, ASL, ETH Zurich, Switzerland
6  * Copyright 2015 Markus Achtelik, ASL, ETH Zurich, Switzerland
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
22 
23 namespace rotors_control {
24 
26  : initialized_params_(false),
27  controller_active_(false) {
29 }
30 
32 
35  // To make the tuning independent of the inertia matrix we divide here.
37  * vehicle_parameters_.inertia_.inverse();
38  // To make the tuning independent of the inertia matrix we divide here.
40  * vehicle_parameters_.inertia_.inverse();
41 
42  Eigen::Matrix4d I;
43  I.setZero();
44  I.block<3, 3>(0, 0) = vehicle_parameters_.inertia_;
45  I(3, 3) = 1;
47  // Calculate the pseude-inverse A^{ \dagger} and then multiply by the inertia matrix I.
48  // A^{ \dagger} = A^T*(A*A^T)^{-1}
51  * controller_parameters_.allocation_matrix_.transpose()).inverse() * I;
52  initialized_params_ = true;
53 }
54 
55 void LeePositionController::CalculateRotorVelocities(Eigen::VectorXd* rotor_velocities) const {
56  assert(rotor_velocities);
57  assert(initialized_params_);
58 
59  rotor_velocities->resize(vehicle_parameters_.rotor_configuration_.rotors.size());
60  // Return 0 velocities on all rotors, until the first command is received.
61  if (!controller_active_) {
62  *rotor_velocities = Eigen::VectorXd::Zero(rotor_velocities->rows());
63  return;
64  }
65 
66  Eigen::Vector3d acceleration;
67  ComputeDesiredAcceleration(&acceleration);
68 
69  Eigen::Vector3d angular_acceleration;
70  ComputeDesiredAngularAcc(acceleration, &angular_acceleration);
71 
72  // Project thrust onto body z axis.
73  double thrust = -vehicle_parameters_.mass_ * acceleration.dot(odometry_.orientation.toRotationMatrix().col(2));
74 
75  Eigen::Vector4d angular_acceleration_thrust;
76  angular_acceleration_thrust.block<3, 1>(0, 0) = angular_acceleration;
77  angular_acceleration_thrust(3) = thrust;
78 
79  *rotor_velocities = angular_acc_to_rotor_velocities_ * angular_acceleration_thrust;
80  *rotor_velocities = rotor_velocities->cwiseMax(Eigen::VectorXd::Zero(rotor_velocities->rows()));
81  *rotor_velocities = rotor_velocities->cwiseSqrt();
82 }
83 
85  odometry_ = odometry;
86 }
87 
89  const mav_msgs::EigenTrajectoryPoint& command_trajectory) {
90  command_trajectory_ = command_trajectory;
91  controller_active_ = true;
92 }
93 
94 void LeePositionController::ComputeDesiredAcceleration(Eigen::Vector3d* acceleration) const {
95  assert(acceleration);
96 
97  Eigen::Vector3d position_error;
99 
100  // Transform velocity to world frame.
101  const Eigen::Matrix3d R_W_I = odometry_.orientation.toRotationMatrix();
102  Eigen::Vector3d velocity_W = R_W_I * odometry_.velocity;
103  Eigen::Vector3d velocity_error;
104  velocity_error = velocity_W - command_trajectory_.velocity_W;
105 
106  Eigen::Vector3d e_3(Eigen::Vector3d::UnitZ());
107 
108  *acceleration = (position_error.cwiseProduct(controller_parameters_.position_gain_)
109  + velocity_error.cwiseProduct(controller_parameters_.velocity_gain_)) / vehicle_parameters_.mass_
111 }
112 
113 // Implementation from the T. Lee et al. paper
114 // Control of complex maneuvers for a quadrotor UAV using geometric methods on SE(3)
115 void LeePositionController::ComputeDesiredAngularAcc(const Eigen::Vector3d& acceleration,
116  Eigen::Vector3d* angular_acceleration) const {
117  assert(angular_acceleration);
118 
119  Eigen::Matrix3d R = odometry_.orientation.toRotationMatrix();
120 
121  // Get the desired rotation matrix.
122  Eigen::Vector3d b1_des;
123  double yaw = command_trajectory_.getYaw();
124  b1_des << cos(yaw), sin(yaw), 0;
125 
126  Eigen::Vector3d b3_des;
127  b3_des = -acceleration / acceleration.norm();
128 
129  Eigen::Vector3d b2_des;
130  b2_des = b3_des.cross(b1_des);
131  b2_des.normalize();
132 
133  Eigen::Matrix3d R_des;
134  R_des.col(0) = b2_des.cross(b3_des);
135  R_des.col(1) = b2_des;
136  R_des.col(2) = b3_des;
137 
138  // Angle error according to lee et al.
139  Eigen::Matrix3d angle_error_matrix = 0.5 * (R_des.transpose() * R - R.transpose() * R_des);
140  Eigen::Vector3d angle_error;
141  vectorFromSkewMatrix(angle_error_matrix, &angle_error);
142 
143  // TODO(burrimi) include angular rate references at some point.
144  Eigen::Vector3d angular_rate_des(Eigen::Vector3d::Zero());
145  angular_rate_des[2] = command_trajectory_.getYawRate();
146 
147  Eigen::Vector3d angular_rate_error = odometry_.angular_velocity - R_des.transpose() * R * angular_rate_des;
148 
149  *angular_acceleration = -1 * angle_error.cwiseProduct(normalized_attitude_gain_)
150  - angular_rate_error.cwiseProduct(normalized_angular_rate_gain_)
151  + odometry_.angular_velocity.cross(odometry_.angular_velocity); // we don't need the inertia matrix here
152 }
153 }
mav_msgs::EigenTrajectoryPoint command_trajectory_
void ComputeDesiredAcceleration(Eigen::Vector3d *acceleration) const
void ComputeDesiredAngularAcc(const Eigen::Vector3d &acceleration, Eigen::Vector3d *angular_acceleration) const
Eigen::Vector3d position
Definition: common.h:63
Eigen::Vector3d angular_velocity
Definition: common.h:68
Eigen::Vector3d velocity
Definition: common.h:67
RotorConfiguration rotor_configuration_
Definition: parameters.h:83
void SetTrajectoryPoint(const mav_msgs::EigenTrajectoryPoint &command_trajectory)
void vectorFromSkewMatrix(Eigen::Matrix3d &skew_matrix, Eigen::Vector3d *vector)
Definition: common.h:116
std::vector< Rotor > rotors
Definition: parameters.h:69
void SetOdometry(const EigenOdometry &odometry)
Eigen::Quaterniond orientation
Definition: common.h:66
void CalculateRotorVelocities(Eigen::VectorXd *rotor_velocities) const
void calculateAllocationMatrix(const RotorConfiguration &rotor_configuration, Eigen::Matrix4Xd *allocation_matrix)
Definition: common.h:79
LeePositionControllerParameters controller_parameters_


rotors_control
Author(s): Fadri Furrer, Michael Burri, Mina Kamel, Janosch Nikolic, Markus Achtelik
autogenerated on Mon Feb 28 2022 23:38:55