common.h
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 
21 #ifndef INCLUDE_ROTORS_CONTROL_COMMON_H_
22 #define INCLUDE_ROTORS_CONTROL_COMMON_H_
23 
24 #include <assert.h>
25 
26 #include <mav_msgs/conversions.h>
28 #include <nav_msgs/Odometry.h>
29 
31 
32 namespace rotors_control {
33 
34 // Default values.
35 static const std::string kDefaultNamespace = "";
36 static const std::string kDefaultCommandMotorSpeedTopic =
37  mav_msgs::default_topics::COMMAND_ACTUATORS; // "command/motor_speed";
39  mav_msgs::default_topics::COMMAND_TRAJECTORY; // "command/trajectory"
42  // "command/roll_pitch_yawrate_thrust"
43 static const std::string kDefaultImuTopic =
45 static const std::string kDefaultOdometryTopic =
47 
48 struct EigenOdometry {
50  : position(0.0, 0.0, 0.0),
51  orientation(Eigen::Quaterniond::Identity()),
52  velocity(0.0, 0.0, 0.0),
53  angular_velocity(0.0, 0.0, 0.0) {};
54 
55  EigenOdometry(const Eigen::Vector3d& _position,
56  const Eigen::Quaterniond& _orientation,
57  const Eigen::Vector3d& _velocity,
58  const Eigen::Vector3d& _angular_velocity) {
59  position = _position;
60  orientation = _orientation;
61  velocity = _velocity;
62  angular_velocity = _angular_velocity;
63  };
64 
65  Eigen::Vector3d position;
66  Eigen::Quaterniond orientation;
67  Eigen::Vector3d velocity; // Velocity is expressed in the Body frame!
68  Eigen::Vector3d angular_velocity;
69 };
70 
71 inline void eigenOdometryFromMsg(const nav_msgs::OdometryConstPtr& msg,
72  EigenOdometry* odometry) {
73  odometry->position = mav_msgs::vector3FromPointMsg(msg->pose.pose.position);
74  odometry->orientation = mav_msgs::quaternionFromMsg(msg->pose.pose.orientation);
75  odometry->velocity = mav_msgs::vector3FromMsg(msg->twist.twist.linear);
76  odometry->angular_velocity = mav_msgs::vector3FromMsg(msg->twist.twist.angular);
77 }
78 
79 inline void calculateAllocationMatrix(const RotorConfiguration& rotor_configuration,
80  Eigen::Matrix4Xd* allocation_matrix) {
81  assert(allocation_matrix != nullptr);
82  allocation_matrix->resize(4, rotor_configuration.rotors.size());
83  unsigned int i = 0;
84  for (const Rotor& rotor : rotor_configuration.rotors) {
85  // Set first row of allocation matrix.
86  (*allocation_matrix)(0, i) = sin(rotor.angle) * rotor.arm_length
87  * rotor.rotor_force_constant;
88  // Set second row of allocation matrix.
89  (*allocation_matrix)(1, i) = -cos(rotor.angle) * rotor.arm_length
90  * rotor.rotor_force_constant;
91  // Set third row of allocation matrix.
92  (*allocation_matrix)(2, i) = -rotor.direction * rotor.rotor_force_constant
93  * rotor.rotor_moment_constant;
94  // Set forth row of allocation matrix.
95  (*allocation_matrix)(3, i) = rotor.rotor_force_constant;
96  ++i;
97  }
98  Eigen::FullPivLU<Eigen::Matrix4Xd> lu(*allocation_matrix);
99  // Setting the threshold for when pivots of the rank calculation should be considered nonzero.
100  lu.setThreshold(1e-9);
101  int rank = lu.rank();
102  if (rank < 4) {
103  std::cout << "The rank of the allocation matrix is " << lu.rank()
104  << ", it should have rank 4, to have a fully controllable system,"
105  << " check your configuration." << std::endl;
106  }
107 
108 }
109 
110 inline void skewMatrixFromVector(Eigen::Vector3d& vector, Eigen::Matrix3d* skew_matrix) {
111  *skew_matrix << 0, -vector.z(), vector.y(),
112  vector.z(), 0, -vector.x(),
113  -vector.y(), vector.x(), 0;
114 }
115 
116 inline void vectorFromSkewMatrix(Eigen::Matrix3d& skew_matrix, Eigen::Vector3d* vector) {
117  *vector << skew_matrix(2, 1), skew_matrix(0,2), skew_matrix(1, 0);
118 }
119 }
120 
121 #endif /* INCLUDE_ROTORS_CONTROL_COMMON_H_ */
static const std::string kDefaultOdometryTopic
Definition: common.h:45
static const std::string kDefaultCommandMotorSpeedTopic
Definition: common.h:36
Eigen::Vector3d vector3FromMsg(const geometry_msgs::Vector3 &msg)
static constexpr char COMMAND_ROLL_PITCH_YAWRATE_THRUST[]
Eigen::Vector3d position
Definition: common.h:63
static const std::string kDefaultCommandMultiDofJointTrajectoryTopic
Definition: common.h:38
Eigen::Vector3d angular_velocity
Definition: common.h:68
Eigen::Vector3d velocity
Definition: common.h:67
static constexpr char COMMAND_ACTUATORS[]
double rotor_moment_constant
Definition: parameters.h:43
static constexpr char ODOMETRY[]
static const std::string kDefaultImuTopic
Definition: common.h:43
void vectorFromSkewMatrix(Eigen::Matrix3d &skew_matrix, Eigen::Vector3d *vector)
Definition: common.h:116
EigenOdometry(const Eigen::Vector3d &_position, const Eigen::Quaterniond &_orientation, const Eigen::Vector3d &_velocity, const Eigen::Vector3d &_angular_velocity)
Definition: common.h:55
std::vector< Rotor > rotors
Definition: parameters.h:69
static constexpr char IMU[]
Eigen::Quaterniond orientation
Definition: common.h:66
static const std::string kDefaultNamespace
Definition: common.h:35
Eigen::Quaterniond quaternionFromMsg(const geometry_msgs::Quaternion &msg)
Eigen::Vector3d vector3FromPointMsg(const geometry_msgs::Point &msg)
double rotor_force_constant
Definition: parameters.h:42
static constexpr char COMMAND_TRAJECTORY[]
void eigenOdometryFromMsg(const nav_msgs::OdometryConstPtr &msg, EigenOdometry *odometry)
Definition: common.h:71
void skewMatrixFromVector(Eigen::Vector3d &vector, Eigen::Matrix3d *skew_matrix)
Definition: common.h:110
void calculateAllocationMatrix(const RotorConfiguration &rotor_configuration, Eigen::Matrix4Xd *allocation_matrix)
Definition: common.h:79
static const std::string kDefaultCommandRollPitchYawrateThrustTopic
Definition: common.h:40


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