rosflight_sil.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Daniel Koch, James Jackson and Gary Ellingson, BYU MAGICC Lab.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * * Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #pragma GCC diagnostic ignored "-Wwrite-strings"
33 
34 #include <sstream>
35 #include <stdint.h>
36 #include <stdio.h>
37 
38 #include <eigen3/Eigen/Core>
39 
42 
43 
44 namespace rosflight_sim
45 {
46 
48  gazebo::ModelPlugin(),
49  nh_(nullptr),
50  comm_(board_),
51  firmware_(board_, comm_)
52 {}
53 
55 {
57  if (nh_) {
58  nh_->shutdown();
59  delete nh_;
60  }
61 }
62 
63 void ROSflightSIL::Load(gazebo::physics::ModelPtr _model, sdf::ElementPtr _sdf)
64 {
65  if (!ros::isInitialized())
66  {
67  ROS_FATAL("A ROS node for Gazebo has not been initialized, unable to load plugin");
68  return;
69  }
70  ROS_INFO("Loaded the ROSflight SIL plugin");
71 
72  model_ = _model;
73  world_ = model_->GetWorld();
74 
75  namespace_.clear();
76 
77  /*
78  * Connect the Plugin to the Robot and Save pointers to the various elements in the simulation
79  */
80  if (_sdf->HasElement("namespace"))
81  namespace_ = _sdf->GetElement("namespace")->Get<std::string>();
82  else
83  gzerr << "[ROSflight_SIL] Please specify a namespace.\n";
85 
86  gzmsg << "loading parameters from " << namespace_ << " ns\n";
87 
88  if (_sdf->HasElement("linkName"))
89  link_name_ = _sdf->GetElement("linkName")->Get<std::string>();
90  else
91  gzerr << "[ROSflight_SIL] Please specify a linkName of the forces and moments plugin.\n";
92  link_ = model_->GetLink(link_name_);
93  if (link_ == NULL)
94  gzthrow("[ROSflight_SIL] Couldn't find specified link \"" << link_name_ << "\".");
95 
96  /* Load Params from Gazebo Server */
97  if (_sdf->HasElement("mavType")) {
98  mav_type_ = _sdf->GetElement("mavType")->Get<std::string>();
99  }
100  else {
101  mav_type_ = "multirotor";
102  gzerr << "[rosflight_sim] Please specify a value for parameter \"mavType\".\n";
103  }
104 
105  if(mav_type_ == "multirotor")
107  else if(mav_type_ == "fixedwing")
108  mav_dynamics_ = new Fixedwing(nh_);
109  else
110  gzthrow("unknown or unsupported mav type\n");
111 
112  // Initialize the Firmware
114  firmware_.init();
115 
116  // Connect the update function to the simulation
117  updateConnection_ = gazebo::event::Events::ConnectWorldUpdateBegin(boost::bind(&ROSflightSIL::OnUpdate, this, _1));
118 
120 
121  truth_NED_pub_ = nh_->advertise<nav_msgs::Odometry>("truth/NED", 1);
122  truth_NWU_pub_ = nh_->advertise<nav_msgs::Odometry>("truth/NWU", 1);
123 }
124 
125 
126 // This gets called by the world update event.
127 void ROSflightSIL::OnUpdate(const gazebo::common::UpdateInfo& _info)
128 {
129  // We run twice so that that functions that take place when we don't have new IMU data get run
130  firmware_.run();
131  firmware_.run();
132  Eigen::Matrix3d NWU_to_NED;
133  NWU_to_NED << 1, 0, 0, 0, -1, 0, 0, 0, -1;
134 
139 
140  // Convert gazebo types to Eigen and switch to NED frame
141  state.pos = NWU_to_NED * vec3_to_eigen_from_gazebo(GZ_COMPAT_GET_POS(pose)) ;
142  state.rot = NWU_to_NED * rotation_to_eigen_from_gazebo(GZ_COMPAT_GET_ROT(pose));
143  state.vel = NWU_to_NED * vec3_to_eigen_from_gazebo(vel);
144  state.omega = NWU_to_NED * vec3_to_eigen_from_gazebo(omega);
145  state.t = _info.simTime.Double();
146 
148 
149  // apply the forces and torques to the joint (apply in NWU)
150  GazeboVector force = vec3_to_gazebo_from_eigen(NWU_to_NED * forces_.block<3,1>(0,0));
151  GazeboVector torque = vec3_to_gazebo_from_eigen(NWU_to_NED * forces_.block<3,1>(3,0));
152  link_->AddRelativeForce(force);
153  link_->AddRelativeTorque(torque);
154 
155  publishTruth();
156 
157 }
158 
160 {
161  link_->SetWorldPose(initial_pose_);
162  link_->ResetPhysicsStates();
163 // start_time_us_ = (uint64_t)(world_->GetSimTime().Double() * 1e3);
164 // rosflight_init();
165 }
166 
167 void ROSflightSIL::windCallback(const geometry_msgs::Vector3 &msg)
168 {
169  Eigen::Vector3d wind;
170  wind << msg.x, msg.y, msg.z;
171  mav_dynamics_->set_wind(wind);
172 }
173 
175 {
179 
180  // Publish truth
181  nav_msgs::Odometry truth;
182  truth.header.stamp.sec = GZ_COMPAT_GET_SIM_TIME(world_).sec;
183  truth.header.stamp.nsec = GZ_COMPAT_GET_SIM_TIME(world_).nsec;
184  truth.header.frame_id = link_name_ + "_NWU";
185  truth.pose.pose.orientation.w = GZ_COMPAT_GET_W(GZ_COMPAT_GET_ROT(pose));
186  truth.pose.pose.orientation.x = GZ_COMPAT_GET_X(GZ_COMPAT_GET_ROT(pose));
187  truth.pose.pose.orientation.y = GZ_COMPAT_GET_Y(GZ_COMPAT_GET_ROT(pose));
188  truth.pose.pose.orientation.z = GZ_COMPAT_GET_Z(GZ_COMPAT_GET_ROT(pose));
189  truth.pose.pose.position.x = GZ_COMPAT_GET_X(GZ_COMPAT_GET_POS(pose));
190  truth.pose.pose.position.y = GZ_COMPAT_GET_Y(GZ_COMPAT_GET_POS(pose));
191  truth.pose.pose.position.z = GZ_COMPAT_GET_Z(GZ_COMPAT_GET_POS(pose));
192  truth.twist.twist.linear.x = GZ_COMPAT_GET_X(vel);
193  truth.twist.twist.linear.y = GZ_COMPAT_GET_Y(vel);
194  truth.twist.twist.linear.z = GZ_COMPAT_GET_Z(vel);
195  truth.twist.twist.angular.x = GZ_COMPAT_GET_X(omega);
196  truth.twist.twist.angular.y = GZ_COMPAT_GET_Y(omega);
197  truth.twist.twist.angular.z = GZ_COMPAT_GET_Z(omega);
198  truth_NWU_pub_.publish(truth);
199 
200  // Convert to NED
201  truth.header.frame_id = link_name_ + "_NED";
202  truth.pose.pose.orientation.y *= -1.0;
203  truth.pose.pose.orientation.z *= -1.0;
204  truth.pose.pose.position.y *= -1.0;
205  truth.pose.pose.position.z *= -1.0;
206  truth.twist.twist.linear.y *= -1.0;
207  truth.twist.twist.linear.z *= -1.0;
208  truth.twist.twist.angular.y *= -1.0;
209  truth.twist.twist.angular.z *= -1.0;
210  truth_NED_pub_.publish(truth);
211 }
212 
214 {
215  Eigen::Vector3d out;
216  out << GZ_COMPAT_GET_X(vec), GZ_COMPAT_GET_Y(vec), GZ_COMPAT_GET_Z(vec);
217  return out;
218 }
219 
221 {
222  GazeboVector out(vec(0), vec(1), vec(2));
223  return out;
224 }
225 
227 {
228  Eigen::Quaterniond eig_quat(GZ_COMPAT_GET_W(quat), GZ_COMPAT_GET_X(quat), GZ_COMPAT_GET_Y(quat), GZ_COMPAT_GET_Z(quat));
229  return eig_quat.toRotationMatrix();
230 }
231 
233 } // namespace rosflight_sim
GZ_REGISTER_MODEL_PLUGIN(GazeboRosElevator)
uint32_t out
#define ROS_FATAL(...)
ros::Publisher truth_NWU_pub_
Definition: rosflight_sil.h:88
#define GZ_COMPAT_GET_Z(VECTOR)
Definition: gz_compat.h:75
void publish(const boost::shared_ptr< M > &message) const
gazebo::math::Vector3 GazeboVector
Definition: gz_compat.h:69
virtual void set_wind(Eigen::Vector3d wind)=0
ROSCPP_DECL bool isInitialized()
const int * get_outputs() const
Definition: sil_board.h:199
#define GZ_COMPAT_GET_X(VECTOR)
Definition: gz_compat.h:73
Eigen::Matrix3d rotation_to_eigen_from_gazebo(GazeboQuaternion vec)
Eigen::Vector3d vec3_to_eigen_from_gazebo(GazeboVector vec)
#define GZ_COMPAT_GET_RELATIVE_ANGULAR_VEL(LINK_PTR)
Definition: gz_compat.h:86
void gazebo_setup(gazebo::physics::LinkPtr link, gazebo::physics::WorldPtr world, gazebo::physics::ModelPtr model, ros::NodeHandle *nh, std::string mav_type)
Definition: sil_board.cpp:49
Eigen::Matrix< double, 6, 1 > forces_
Definition: rosflight_sil.h:93
#define GZ_COMPAT_GET_WORLD_COG_POSE(LINK_PTR)
Definition: gz_compat.h:87
MAVForcesAndMoments * mav_dynamics_
Definition: rosflight_sil.h:90
GazeboVector vec3_to_gazebo_from_eigen(Eigen::Vector3d vec)
gazebo::math::Pose GazeboPose
Definition: gz_compat.h:70
#define ROS_INFO(...)
#define GZ_COMPAT_GET_W(VECTOR)
Definition: gz_compat.h:76
#define GZ_COMPAT_GET_ROT(POSE)
Definition: gz_compat.h:82
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
rosflight_firmware::ROSflight firmware_
Definition: rosflight_sil.h:73
gazebo::physics::WorldPtr world_
Definition: rosflight_sil.h:79
void Load(gazebo::physics::ModelPtr _model, sdf::ElementPtr _sdf) override
virtual Eigen::Matrix< double, 6, 1 > updateForcesAndTorques(Current_State x, const int act_cmds[])=0
void OnUpdate(const gazebo::common::UpdateInfo &_info)
gazebo::event::ConnectionPtr updateConnection_
Definition: rosflight_sil.h:84
#define GZ_COMPAT_DISCONNECT_WORLD_UPDATE_BEGIN(CONNECTION)
Definition: gz_compat.h:93
#define NULL
#define GZ_COMPAT_GET_POS(POSE)
Definition: gz_compat.h:81
ros::Publisher truth_NED_pub_
Definition: rosflight_sil.h:87
gazebo::physics::LinkPtr link_
Definition: rosflight_sil.h:81
void windCallback(const geometry_msgs::Vector3 &msg)
#define GZ_COMPAT_GET_SIM_TIME(WORLD_PTR)
Definition: gz_compat.h:84
#define GZ_COMPAT_GET_Y(VECTOR)
Definition: gz_compat.h:74
#define GZ_COMPAT_GET_RELATIVE_LINEAR_VEL(LINK_PTR)
Definition: gz_compat.h:85
gazebo::physics::ModelPtr model_
Definition: rosflight_sil.h:80
gazebo::math::Quaternion GazeboQuaternion
Definition: gz_compat.h:71


rosflight_sim
Author(s): James Jackson, Gary Ellingson, Daniel Koch
autogenerated on Wed Jul 3 2019 20:00:29