gazebo_motor_model.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 
22 #ifndef ROTORS_GAZEBO_PLUGINS_MOTOR_MODELS_H
23 #define ROTORS_GAZEBO_PLUGINS_MOTOR_MODELS_H
24 
25 // SYSTEM
26 #include <stdio.h>
27 
28 // 3RD PARTY
29 #include <boost/bind.hpp>
30 #include <Eigen/Eigen>
31 #include <Eigen/Core>
32 #include <gazebo/common/common.hh>
33 #include <gazebo/common/Plugin.hh>
34 #include <gazebo/gazebo.hh>
35 #include <gazebo/physics/physics.hh>
36 #include <mav_msgs/default_topics.h> // This comes from the mav_comm repo
37 
38 // USER
41 #include "Float32.pb.h"
42 #include "CommandMotorSpeed.pb.h"
43 #include "WindSpeed.pb.h"
44 
45 namespace turning_direction {
46 const static int CCW = 1;
47 const static int CW = -1;
48 } // namespace turning_direction
49 
50 enum class MotorType {
51  kVelocity,
52  kPosition,
53  kForce
54 };
55 
56 namespace gazebo {
57 
58 // Changed name from speed to input for more generality. TODO(kajabo): integrate general actuator command.
61 
62 // Set the max_force_ to the max double value. The limitations get handled by the FirstOrderFilter.
63 static constexpr double kDefaultMaxForce = std::numeric_limits<double>::max();
64 static constexpr double kDefaultMotorConstant = 8.54858e-06;
65 static constexpr double kDefaultMomentConstant = 0.016;
66 static constexpr double kDefaultTimeConstantUp = 1.0 / 80.0;
67 static constexpr double kDefaultTimeConstantDown = 1.0 / 40.0;
68 static constexpr double kDefaulMaxRotVelocity = 838.0;
69 static constexpr double kDefaultRotorDragCoefficient = 1.0e-4;
70 static constexpr double kDefaultRollingMomentCoefficient = 1.0e-6;
71 
72 class GazeboMotorModel : public MotorModel, public ModelPlugin {
73 
74  public:
76  : ModelPlugin(),
77  MotorModel(),
78  command_sub_topic_(mav_msgs::default_topics::COMMAND_ACTUATORS),
79  wind_speed_sub_topic_(mav_msgs::default_topics::WIND_SPEED),
80  motor_speed_pub_topic_(mav_msgs::default_topics::MOTOR_MEASUREMENT),
81  motor_position_pub_topic_(mav_msgs::default_topics::MOTOR_POSITION_MEASUREMENT),
82  motor_force_pub_topic_(mav_msgs::default_topics::MOTOR_FORCE_MEASUREMENT),
83  publish_speed_(true),
84  publish_position_(false),
85  publish_force_(false),
86  motor_number_(0),
87  turning_direction_(turning_direction::CW),
88  motor_type_(MotorType::kVelocity),
89  max_force_(kDefaultMaxForce),
90  max_rot_velocity_(kDefaulMaxRotVelocity),
91  moment_constant_(kDefaultMomentConstant),
92  motor_constant_(kDefaultMotorConstant),
93  ref_motor_input_(0.0),
94  rolling_moment_coefficient_(kDefaultRollingMomentCoefficient),
95  rotor_drag_coefficient_(kDefaultRotorDragCoefficient),
96  rotor_velocity_slowdown_sim_(kDefaultRotorVelocitySlowdownSim),
97  time_constant_down_(kDefaultTimeConstantDown),
98  time_constant_up_(kDefaultTimeConstantUp),
99  node_handle_(nullptr),
100  wind_speed_W_(0, 0, 0),
101  pubs_and_subs_created_(false) {}
102 
103  virtual ~GazeboMotorModel();
104 
105  virtual void InitializeParams();
106  virtual void Publish();
107 
108  protected:
109  virtual void UpdateForcesAndMoments();
110  virtual void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf);
111  virtual void OnUpdate(const common::UpdateInfo & /*_info*/);
112 
113  private:
117 
122  void CreatePubsAndSubs();
123 
125  double NormalizeAngle(double input);
126 
127  std::string command_sub_topic_;
129  std::string joint_name_;
130  std::string link_name_;
134  std::string namespace_;
135 
139 
143 
144  double max_force_;
154 
155  common::PID pids_;
156 
157  gazebo::transport::NodePtr node_handle_;
158 
159  gazebo::transport::PublisherPtr motor_velocity_pub_;
160 
161  gazebo::transport::PublisherPtr motor_position_pub_;
162 
163  gazebo::transport::PublisherPtr motor_force_pub_;
164 
165  gazebo::transport::SubscriberPtr command_sub_;
166 
167  gazebo::transport::SubscriberPtr wind_speed_sub_;
168 
169  physics::ModelPtr model_;
170  physics::JointPtr joint_;
171  physics::LinkPtr link_;
172 
174  event::ConnectionPtr updateConnection_;
175 
176  boost::thread callback_queue_thread_;
177 
178  void QueueThread();
179 
180  gz_std_msgs::Float32 turning_velocity_msg_;
181  gz_std_msgs::Float32 position_msg_;
182  gz_std_msgs::Float32 force_msg_;
183 
184  void ControlCommandCallback(GzCommandMotorInputMsgPtr& command_motor_input_msg);
185 
186  void WindSpeedCallback(GzWindSpeedMsgPtr& wind_speed_msg);
187 
188  std::unique_ptr<FirstOrderFilter<double>> rotor_velocity_filter_;
189  ignition::math::Vector3d wind_speed_W_;
190 };
191 
192 } // namespace gazebo {
193 
194 #endif // ROTORS_GAZEBO_PLUGINS_MOTOR_MODELS_H
static const int CCW
const boost::shared_ptr< const gz_mav_msgs::WindSpeed > GzWindSpeedMsgPtr
static constexpr double kDefaultRotorVelocitySlowdownSim
Definition: common.h:49
event::ConnectionPtr updateConnection_
Pointer to the update event connection.
static constexpr double kDefaultTimeConstantDown
gazebo::transport::NodePtr node_handle_
gazebo::transport::PublisherPtr motor_position_pub_
const boost::shared_ptr< const gz_mav_msgs::CommandMotorSpeed > GzCommandMotorInputMsgPtr
gazebo::transport::SubscriberPtr wind_speed_sub_
gazebo::transport::PublisherPtr motor_velocity_pub_
static constexpr double kDefaultRollingMomentCoefficient
ignition::math::Vector3d wind_speed_W_
gz_std_msgs::Float32 position_msg_
gz_std_msgs::Float32 turning_velocity_msg_
static constexpr double kDefaultMotorConstant
std::unique_ptr< FirstOrderFilter< double > > rotor_velocity_filter_
static const int CW
bool pubs_and_subs_created_
Flag that is set to true once CreatePubsAndSubs() is called, used to prevent CreatePubsAndSubs() from...
static constexpr double kDefaultTimeConstantUp
gz_std_msgs::Float32 force_msg_
static constexpr double kDefaultRotorDragCoefficient
gazebo::transport::PublisherPtr motor_force_pub_
static constexpr double kDefaultMaxForce
static constexpr double kDefaulMaxRotVelocity
static constexpr double kDefaultMomentConstant
boost::thread callback_queue_thread_
gazebo::transport::SubscriberPtr command_sub_


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