mocap_pose_estimate.cpp
Go to the documentation of this file.
00001 
00010 /*
00011  * Copyright 2014 Vladimir Ermakov.
00012  *
00013  * This program is free software; you can redistribute it and/or modify
00014  * it under the terms of the GNU General Public License as published by
00015  * the Free Software Foundation; either version 3 of the License, or
00016  * (at your option) any later version.
00017  *
00018  * This program is distributed in the hope that it will be useful, but
00019  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00020  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
00021  * for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License along
00024  * with this program; if not, write to the Free Software Foundation, Inc.,
00025  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00026  */
00027 
00028 #include <mavros/mavros_plugin.h>
00029 #include <pluginlib/class_list_macros.h>
00030 
00031 #include <geometry_msgs/Pose.h>
00032 #include <geometry_msgs/TransformStamped.h>
00033 
00034 
00035 namespace mavplugin {
00036 
00037 class MocapPoseEstimatePlugin : public MavRosPlugin
00038 {
00039 public:
00040         MocapPoseEstimatePlugin() :
00041                 uas(nullptr)
00042                 { };
00043 
00044         void initialize(UAS &uas_,
00045                 ros::NodeHandle &nh,
00046                 diagnostic_updater::Updater &diag_updater)
00047         {
00048                 bool use_tf;
00049                 bool use_pose;
00050 
00051                 uas = &uas_;
00052                 mp_nh = ros::NodeHandle(nh, "mocap");
00053 
00054                 mp_nh.param("use_tf", use_tf, false);  // Vicon
00055                 mp_nh.param("use_pose", use_pose, true);  // Optitrack
00056 
00057 
00058                 if (use_tf && !use_pose)
00059                 {
00060                         mocap_tf_sub = mp_nh.subscribe("tf", 1, &MocapPoseEstimatePlugin::mocap_tf_cb, this);
00061                 }
00062                 else if (use_pose && !use_tf)
00063                 {
00064                         mocap_pose_sub = mp_nh.subscribe("pose", 1, &MocapPoseEstimatePlugin::mocap_pose_cb, this);
00065                 }
00066                 else
00067                 {
00068                         ROS_ERROR_NAMED("mocap", "Use one motion capture source.");
00069                 }
00070         }
00071 
00072         const std::string get_name() const
00073         {
00074                 return "MocapPoseEstimate";
00075         }
00076 
00077         const message_map get_rx_handlers()
00078         {
00079                 return { /* Rx disabled */ };
00080         }
00081 
00082 private:
00083         UAS *uas;
00084 
00085         ros::NodeHandle mp_nh;
00086         ros::Subscriber mocap_pose_sub;
00087         ros::Subscriber mocap_tf_sub;
00088 
00089         // mavlink send
00090 
00091         void mocap_pose_send
00092                 (uint64_t usec,
00093                 float x, float y, float z,
00094                 float roll, float pitch, float yaw)
00095         {
00096                 mavlink_message_t msg;
00097                 mavlink_msg_vicon_position_estimate_pack_chan(UAS_PACK_CHAN(uas), &msg,
00098                         usec,
00099                         x,
00100                         y,
00101                         z,
00102                         roll,
00103                         pitch,
00104                         yaw);
00105                 UAS_FCU(uas)->send_message(&msg);
00106         }
00107 
00108 
00109         void mocap_pose_cb(const geometry_msgs::Pose::ConstPtr &pose)
00110         {       
00111                 ros::Time stamp = ros::Time::now();
00112                 tf::Quaternion quat;
00113                 tf::quaternionMsgToTF(pose->orientation, quat);
00114                 double roll, pitch, yaw;
00115                 tf::Matrix3x3(quat).getRPY(roll, pitch, yaw);
00116                 // Convert to mavlink body frame
00117                 mocap_pose_send(stamp.toNSec() / 1000,
00118                         pose->position.x,
00119                         -pose->position.y,
00120                         -pose->position.z,
00121                         roll, -pitch, -yaw); 
00122         }
00123 
00124         void mocap_tf_cb(const geometry_msgs::TransformStamped::ConstPtr &trans)
00125         {
00126                 tf::Quaternion quat;
00127                 tf::quaternionMsgToTF(trans->transform.rotation, quat);
00128                 double roll, pitch, yaw;
00129                 tf::Matrix3x3(quat).getRPY(roll, pitch, yaw);
00130                 // Convert to mavlink body frame
00131                 mocap_pose_send(trans->header.stamp.toNSec() / 1000,
00132                         trans->transform.translation.x,
00133                         -trans->transform.translation.y,
00134                         -trans->transform.translation.z,
00135                         roll, -pitch, -yaw); 
00136         }
00137 };
00138 
00139 }; // namespace mavplugin
00140 
00141 PLUGINLIB_EXPORT_CLASS(mavplugin::MocapPoseEstimatePlugin, mavplugin::MavRosPlugin)


mavros_extras
Author(s): Vladimir Ermakov
autogenerated on Wed Aug 26 2015 12:29:20