00001
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #pragma once
00028
00029 #include <mavros/utils.h>
00030 #include <mavros/mavros_plugin.h>
00031
00032 #include <tf/transform_listener.h>
00033
00034 namespace mavplugin {
00035
00041 template <class D>
00042 class SetPositionTargetLocalNEDMixin {
00043 public:
00044 void set_position_target_local_ned(uint32_t time_boot_ms, uint8_t coordinate_frame,
00045 uint16_t type_mask,
00046 float x, float y, float z,
00047 float vx, float vy, float vz,
00048 float afx, float afy, float afz,
00049 float yaw, float yaw_rate) {
00050 UAS *_uas = static_cast<D *>(this)->uas;
00051 mavlink_message_t msg;
00052 mavlink_msg_set_position_target_local_ned_pack_chan(UAS_PACK_CHAN(_uas), &msg,
00053 time_boot_ms,
00054 UAS_PACK_TGT(_uas),
00055 coordinate_frame,
00056 type_mask,
00057 x, y, z,
00058 vz, vy, vz,
00059 afx, afy, afz,
00060 yaw, yaw_rate);
00061 UAS_FCU(_uas)->send_message(&msg);
00062 }
00063 };
00064
00071 template <class D>
00072 class TFListenerMixin {
00073 public:
00074 std::thread tf_thread;
00075 std::string thd_name;
00076 boost::function<void (const tf::Transform&, const ros::Time&)> tf_transform_cb;
00077
00084 void tf_start(const char *_thd_name, void (D::*cbp)(const tf::Transform&, const ros::Time&) ) {
00085 thd_name = _thd_name;
00086 tf_transform_cb = boost::bind(cbp, static_cast<D *>(this), _1, _2);
00087
00088 std::thread t(boost::bind(&TFListenerMixin::tf_listener, this));
00089 mavutils::set_thread_name(t, thd_name);
00090 tf_thread.swap(t);
00091 }
00092
00093 void tf_listener(void) {
00094 ros::NodeHandle &_sp_nh = static_cast<D *>(this)->sp_nh;
00095 std::string &_frame_id = static_cast<D *>(this)->frame_id;
00096 std::string &_child_frame_id = static_cast<D *>(this)->child_frame_id;
00097
00098 tf::TransformListener listener(_sp_nh);
00099 tf::StampedTransform transform;
00100 ros::Rate rate(static_cast<D *>(this)->tf_rate);
00101 while (_sp_nh.ok()) {
00102
00103 listener.waitForTransform(_frame_id, _child_frame_id, ros::Time(0), ros::Duration(3.0));
00104 try {
00105 listener.lookupTransform(_frame_id, _child_frame_id, ros::Time(0), transform);
00106 tf_transform_cb(static_cast<tf::Transform>(transform), transform.stamp_);
00107 }
00108 catch (tf::TransformException ex) {
00109 ROS_ERROR_NAMED("setpoint", "%s: %s", thd_name.c_str(), ex.what());
00110 }
00111 rate.sleep();
00112 }
00113 }
00114 };
00115
00116 };