gazebo_bag_plugin.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2015 Fadri Furrer, ASL, ETH Zurich, Switzerland
00003  * Copyright 2015 Michael Burri, ASL, ETH Zurich, Switzerland
00004  * Copyright 2015 Mina Kamel, ASL, ETH Zurich, Switzerland
00005  * Copyright 2015 Janosch Nikolic, ASL, ETH Zurich, Switzerland
00006  * Copyright 2015 Markus Achtelik, ASL, ETH Zurich, Switzerland
00007  * Copyright 2016 Geoffrey Hunter <gbmhunter@gmail.com>
00008  *
00009  * Licensed under the Apache License, Version 2.0 (the "License");
00010  * you may not use this file except in compliance with the License.
00011  * You may obtain a copy of the License at
00012  *
00013  *     http://www.apache.org/licenses/LICENSE-2.0
00014 
00015  * Unless required by applicable law or agreed to in writing, software
00016  * distributed under the License is distributed on an "AS IS" BASIS,
00017  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  * See the License for the specific language governing permissions and
00019  * limitations under the License.
00020  */
00021 
00022 
00023 #ifndef ROTORS_GAZEBO_PLUGINS_GAZEBO_BAG_PLUGIN_H
00024 #define ROTORS_GAZEBO_PLUGINS_GAZEBO_BAG_PLUGIN_H
00025 
00026 #include <string>
00027 
00028 #include <gazebo/common/common.hh>
00029 #include <gazebo/common/Plugin.hh>
00030 #include <gazebo/gazebo.hh>
00031 #include <gazebo/physics/physics.hh>
00032 #include <geometry_msgs/Point.h>
00033 #include <geometry_msgs/PoseStamped.h>
00034 #include <geometry_msgs/TwistStamped.h>
00035 #include <geometry_msgs/WrenchStamped.h>
00036 #include <mav_msgs/Actuators.h>
00037 #include <mav_msgs/AttitudeThrust.h>
00038 #include <mav_msgs/default_topics.h>
00039 #include <mav_msgs/RateThrust.h>
00040 #include <ros/ros.h>
00041 #include <rosbag/bag.h>
00042 #include <sensor_msgs/Imu.h>
00043 #include <std_msgs/Float32.h>
00044 #include <trajectory_msgs/MultiDOFJointTrajectory.h>
00045 
00046 #include "rotors_comm/RecordRosbag.h"
00047 #include "rotors_comm/WindSpeed.h"
00048 #include "rotors_gazebo_plugins/common.h"
00049 
00050 
00051 namespace gazebo {
00052 
00053 // Default values, the rest from common.h
00054 static const std::string kDefaultFrameId = "ground_truth_pose";
00055 static const std::string kDefaultLinkName = "base_link";
00056 static const std::string kDefaultBagFilename_ = "simulator.bag";
00057 static const std::string kDefaultRecordingServiceName = "record_rosbag";
00058 static constexpr bool kDefaultWaitToRecord = false;
00059 static constexpr bool kDefaultIsRecording = false;
00060 
00064 class GazeboBagPlugin : public ModelPlugin {
00065   typedef std::map<const unsigned int, const physics::JointPtr> MotorNumberToJointMap;
00066   typedef std::pair<const unsigned int, const physics::JointPtr> MotorNumberToJointPair;
00067  public:
00068   GazeboBagPlugin()
00069       : ModelPlugin(),
00070         namespace_(kDefaultNamespace),
00071         // DEFAULT TOPICS
00072         ground_truth_pose_topic_(mav_msgs::default_topics::GROUND_TRUTH_POSE),
00073         ground_truth_twist_topic_(mav_msgs::default_topics::GROUND_TRUTH_TWIST),
00074         imu_topic_(mav_msgs::default_topics::IMU),
00075         control_attitude_thrust_topic_(mav_msgs::default_topics::COMMAND_ATTITUDE_THRUST),
00076         control_motor_speed_topic_(mav_msgs::default_topics::COMMAND_ACTUATORS),
00077         control_rate_thrust_topic_(mav_msgs::default_topics::COMMAND_RATE_THRUST),
00078         wind_speed_topic_(mav_msgs::default_topics::WIND_SPEED),
00079         motor_topic_(mav_msgs::default_topics::MOTOR_MEASUREMENT),
00080         wrench_topic_(mav_msgs::default_topics::WRENCH),
00081         external_force_topic_(mav_msgs::default_topics::EXTERNAL_FORCE),
00082         waypoint_topic_(mav_msgs::default_topics::COMMAND_TRAJECTORY),
00083         command_pose_topic_(mav_msgs::default_topics::COMMAND_POSE),
00084         //---------------
00085         frame_id_(kDefaultFrameId),
00086         link_name_(kDefaultLinkName),
00087         bag_filename_(kDefaultBagFilename_),
00088         recording_service_name_(kDefaultRecordingServiceName),
00089         rotor_velocity_slowdown_sim_(kDefaultRotorVelocitySlowdownSim),
00090         wait_to_record_(kDefaultWaitToRecord),
00091         is_recording_(kDefaultIsRecording),
00092         node_handle_(nullptr),
00093         contact_mgr_(nullptr) {}
00094 
00095   virtual ~GazeboBagPlugin();
00096 
00097  protected:
00101   void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf);
00102 
00105   void OnUpdate(const common::UpdateInfo& /*_info*/);
00106 
00108   void StartRecording();
00109 
00111   void StopRecording();
00112 
00115   void ImuCallback(const sensor_msgs::ImuConstPtr& imu_msg);
00116 
00119   void ExternalForceCallback(const geometry_msgs::WrenchStampedConstPtr& force_msg);
00120 
00123   void WaypointCallback(const trajectory_msgs::MultiDOFJointTrajectoryConstPtr& trajectory_msg);
00124 
00127   void CommandPoseCallback(const geometry_msgs::PoseStampedConstPtr& pose_msg);
00128 
00131   void AttitudeThrustCallback(const mav_msgs::AttitudeThrustConstPtr& control_msg);
00132 
00135   void ActuatorsCallback(const mav_msgs::ActuatorsConstPtr& control_msg);
00136 
00139   void RateThrustCallback(const mav_msgs::RateThrustConstPtr& control_msg);
00140 
00143   void WindSpeedCallback(const rotors_comm::WindSpeedConstPtr& wind_speed_msg);
00144 
00147   void LogGroundTruth(const common::Time now);
00148 
00151   void LogMotorVelocities(const common::Time now);
00152 
00155   void LogWrenches(const common::Time now);
00156 
00160   bool RecordingServiceCallback(rotors_comm::RecordRosbag::Request& req,
00161                                 rotors_comm::RecordRosbag::Response& res);
00162 
00163  private:
00165   event::ConnectionPtr update_connection_;
00166 
00167   physics::WorldPtr world_;
00168   physics::ModelPtr model_;
00169   physics::LinkPtr link_;
00170 
00171   physics::Link_V child_links_;
00172 
00173   MotorNumberToJointMap motor_joints_;
00174 
00177   physics::ContactManager *contact_mgr_;
00178 
00179   std::string namespace_;
00180   std::string ground_truth_pose_topic_;
00181   std::string ground_truth_twist_topic_;
00182   std::string imu_topic_;
00183   std::string external_force_topic_;
00184   std::string waypoint_topic_;
00185   std::string command_pose_topic_;
00186   std::string control_attitude_thrust_topic_;
00187   std::string control_motor_speed_topic_;
00188   std::string control_rate_thrust_topic_;
00189   std::string wind_speed_topic_;
00190   std::string wrench_topic_;
00191   std::string motor_topic_;
00192   std::string frame_id_;
00193   std::string link_name_;
00194   std::string bag_filename_;
00195   std::string recording_service_name_;
00196   double rotor_velocity_slowdown_sim_;
00197 
00199   boost::mutex mtx_;
00200 
00202   bool wait_to_record_;
00203 
00205   bool is_recording_;
00206 
00207   rosbag::Bag bag_;
00208   ros::NodeHandle *node_handle_;
00209 
00210   // Ros subscribers
00211   ros::Subscriber imu_sub_;
00212   ros::Subscriber external_force_sub_;
00213   ros::Subscriber waypoint_sub_;
00214   ros::Subscriber control_attitude_thrust_sub_;
00215   ros::Subscriber control_motor_speed_sub_;
00216   ros::Subscriber control_rate_thrust_sub_;
00217   ros::Subscriber wind_speed_sub_;
00218   ros::Subscriber command_pose_sub_;
00219 
00220   // Ros service server
00221   ros::ServiceServer recording_service_;
00222 
00223   std::ofstream csvOut;
00224 
00225   template<class T>
00226   void writeBag(const std::string& topic, const ros::Time& time, const T& msg) {
00227     boost::mutex::scoped_lock lock(mtx_);
00228     try {
00229       bag_.write(topic, time, msg);
00230     }
00231     catch (rosbag::BagIOException& e) {
00232       gzerr << "Error while writing to bag " << e.what() << std::endl;
00233     }
00234     catch (rosbag::BagException& e) {
00235       if (time < ros::TIME_MIN) {
00236         gzerr<<"Header stamp not set for msg published on topic: "<< topic << ". " << e.what() << std::endl;
00237       }
00238       else {
00239         gzerr << "Error while writing to bag " << e.what() << std::endl;
00240       }
00241     }
00242   }
00243 
00244   template<class T>
00245   void writeBag(const std::string& topic, const ros::Time& time, boost::shared_ptr<T const> const& msg) {
00246     boost::mutex::scoped_lock lock(mtx_);
00247     try {
00248       bag_.write(topic, time, msg);
00249     }
00250     catch (rosbag::BagIOException& e) {
00251       gzerr << "Error while writing to bag " << e.what() << std::endl;
00252     }
00253     catch (rosbag::BagException& e) {
00254       if (time < ros::TIME_MIN) {
00255         gzerr<<"Header stamp not set for msg published on topic: "<< topic << ". " << e.what() << std::endl;
00256       }
00257       else {
00258         gzerr << "Error while writing to bag " << e.what() << std::endl;
00259       }
00260     }
00261   }
00262 
00263 };
00264 
00265 } // namespace gazebo
00266 
00267 #endif // ROTORS_GAZEBO_PLUGINS_GAZEBO_BAG_PLUGIN_H


rotors_gazebo_plugins
Author(s): Fadri Furrer, Michael Burri, Mina Kamel, Janosch Nikolic, Markus Achtelik
autogenerated on Thu Apr 18 2019 02:43:43