00001
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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);
00055 mp_nh.param("use_pose", use_pose, true);
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 { };
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
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
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
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 };
00140
00141 PLUGINLIB_EXPORT_CLASS(mavplugin::MocapPoseEstimatePlugin, mavplugin::MavRosPlugin)