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  * Copyright 2016 Geoffrey Hunter <gbmhunter@gmail.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 #ifndef ROTORS_GAZEBO_PLUGINS_COMMON_H_
23 #define ROTORS_GAZEBO_PLUGINS_COMMON_H_
24 
25 #include <Eigen/Dense>
26 #include <gazebo/gazebo.hh>
27 #include <tinyxml.h>
28 
29 namespace gazebo {
30 
31 //===============================================================================================//
32 //========================================= DEBUGGING ===========================================//
33 //===============================================================================================//
34 
37 
38 // The following boolean constants enable/disable debug printing when certain plugin methods are called.
39 // Suitable for debugging purposes. Left on permanently can swamp std::out and can crash Gazebo.
40 
41 static const bool kPrintOnPluginLoad = false;
42 static const bool kPrintOnUpdates = false;
43 static const bool kPrintOnMsgCallback = false;
44 
46 
47 // Default values
48 static const std::string kDefaultNamespace = "";
49 static constexpr double kDefaultRotorVelocitySlowdownSim = 10.0;
50 
51 //===============================================================================================//
52 //================================== TOPICS FOR ROS INTERFACE ===================================//
53 //===============================================================================================//
54 
55 // These should perhaps be defined in an .sdf/.xacro file instead?
56 static const std::string kConnectGazeboToRosSubtopic = "connect_gazebo_to_ros_subtopic";
57 static const std::string kConnectRosToGazeboSubtopic = "connect_ros_to_gazebo_subtopic";
58 
61 static const std::string kBroadcastTransformSubtopic = "broadcast_transform";
62 
63 
70 template<class T>
71 bool getSdfParam(sdf::ElementPtr sdf, const std::string& name, T& param, const T& default_value, const bool& verbose =
72  false) {
73  if (sdf->HasElement(name)) {
74  param = sdf->GetElement(name)->Get<T>();
75  return true;
76  }
77  else {
78  param = default_value;
79  if (verbose)
80  gzerr << "[rotors_gazebo_plugins] Please specify a value for parameter \"" << name << "\".\n";
81  }
82  return false;
83 }
84 
85 template <typename T>
86 void model_param(const std::string& world_name, const std::string& model_name, const std::string& param, T& param_value)
87 {
88  TiXmlElement* e_param = nullptr;
89  TiXmlElement* e_param_tmp = nullptr;
90  std::string dbg_param;
91 
92  TiXmlDocument doc(world_name + ".xml");
93  if (doc.LoadFile())
94  {
95  TiXmlHandle h_root(doc.RootElement());
96 
97  TiXmlElement* e_model = h_root.FirstChild("model").Element();
98 
99  for( e_model; e_model; e_model=e_model->NextSiblingElement("model") )
100  {
101  const char* attr_name = e_model->Attribute("name");
102  if (attr_name)
103  {
104  //specific
105  if (model_name.compare(attr_name) == 0)
106  {
107  e_param_tmp = e_model->FirstChildElement(param);
108  if (e_param_tmp)
109  {
110  e_param = e_param_tmp;
111  dbg_param = "";
112  }
113  break;
114  }
115  }
116  else
117  {
118  //common
119  e_param = e_model->FirstChildElement(param);
120  dbg_param = "common ";
121  }
122  }
123 
124  if (e_param)
125  {
126  std::istringstream iss(e_param->GetText());
127  iss >> param_value;
128 
129  gzdbg << model_name << " model: " << dbg_param << "parameter " << param << " = " << param_value << " from " << doc.Value() << "\n";
130  }
131  }
132 
133 }
134 
135 }
136 
147 template <typename T>
149 
150  public:
151  FirstOrderFilter(double timeConstantUp, double timeConstantDown, T initialState):
152  timeConstantUp_(timeConstantUp),
153  timeConstantDown_(timeConstantDown),
154  previousState_(initialState) {}
155 
157  T updateFilter(T inputState, double samplingTime) {
158 
159  T outputState;
160  if (inputState > previousState_) {
161  // Calcuate the outputState if accelerating.
162  double alphaUp = exp(-samplingTime / timeConstantUp_);
163  // x(k+1) = Ad*x(k) + Bd*u(k)
164  outputState = alphaUp * previousState_ + (1 - alphaUp) * inputState;
165 
166  }
167  else {
168  // Calculate the outputState if decelerating.
169  double alphaDown = exp(-samplingTime / timeConstantDown_);
170  outputState = alphaDown * previousState_ + (1 - alphaDown) * inputState;
171  }
172  previousState_ = outputState;
173  return outputState;
174 
175  }
176 
178 
179  protected:
183 };
184 
186 template<class Derived>
187 Eigen::Quaternion<typename Derived::Scalar> QuaternionFromSmallAngle(const Eigen::MatrixBase<Derived> & theta) {
188  typedef typename Derived::Scalar Scalar;
189  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived);
190  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived, 3);
191  const Scalar q_squared = theta.squaredNorm() / 4.0;
192 
193  if (q_squared < 1) {
194  return Eigen::Quaternion<Scalar>(sqrt(1 - q_squared), theta[0] * 0.5, theta[1] * 0.5, theta[2] * 0.5);
195  }
196  else {
197  const Scalar w = 1.0 / sqrt(1 + q_squared);
198  const Scalar f = w * 0.5;
199  return Eigen::Quaternion<Scalar>(w, theta[0] * f, theta[1] * f, theta[2] * f);
200  }
201 }
202 
203 template<class In, class Out>
204 void copyPosition(const In& in, Out* out) {
205  out->x = in.x;
206  out->y = in.y;
207  out->z = in.z;
208 }
209 
210 #endif /* ROTORS_GAZEBO_PLUGINS_COMMON_H_ */
static constexpr double kDefaultRotorVelocitySlowdownSim
Definition: common.h:49
double timeConstantDown_
Definition: common.h:181
XmlRpcValue param_value
static const std::string kConnectRosToGazeboSubtopic
Definition: common.h:57
Parameter param
static const std::string kDefaultNamespace
Definition: common.h:48
FirstOrderFilter(double timeConstantUp, double timeConstantDown, T initialState)
Definition: common.h:151
static const bool kPrintOnPluginLoad
Definition: common.h:41
INLINE Rall1d< T, V, S > sqrt(const Rall1d< T, V, S > &arg)
INLINE Rall1d< T, V, S > exp(const Rall1d< T, V, S > &arg)
Eigen::Quaternion< typename Derived::Scalar > QuaternionFromSmallAngle(const Eigen::MatrixBase< Derived > &theta)
Computes a quaternion from the 3-element small angle approximation theta.
Definition: common.h:187
void copyPosition(const In &in, Out *out)
Definition: common.h:204
static const bool kPrintOnUpdates
Definition: common.h:42
T updateFilter(T inputState, double samplingTime)
This method will apply a first order filter on the inputState.
Definition: common.h:157
bool getSdfParam(sdf::ElementPtr sdf, const std::string &name, T &param, const T &default_value, const bool &verbose=false)
Obtains a parameter from sdf.
Definition: common.h:71
static const std::string kConnectGazeboToRosSubtopic
Definition: common.h:56
static const std::string kBroadcastTransformSubtopic
Special-case topic for ROS interface plugin to listen to (if present) and broadcast transforms to the...
Definition: common.h:61
static const bool kPrintOnMsgCallback
Definition: common.h:43
This class can be used to apply a first order filter on a signal. It allows different acceleration an...
Definition: common.h:148
double timeConstantUp_
Definition: common.h:180
void model_param(const std::string &world_name, const std::string &model_name, const std::string &param, T &param_value)
Definition: common.h:86


rotors_gazebo_plugins
Author(s): Fadri Furrer, Michael Burri, Mina Kamel, Janosch Nikolic, Markus Achtelik
autogenerated on Mon Feb 28 2022 23:39:03