00001
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <mavros/mavros_plugin.h>
00018 #include <mavros/setpoint_mixin.h>
00019 #include <pluginlib/class_list_macros.h>
00020 #include <eigen_conversions/eigen_msg.h>
00021
00022 #include <mavros_msgs/AttitudeTarget.h>
00023 #include <mavros_msgs/PositionTarget.h>
00024 #include <mavros_msgs/GlobalPositionTarget.h>
00025
00026 namespace mavplugin {
00033 class SetpointRawPlugin : public MavRosPlugin,
00034 private SetPositionTargetLocalNEDMixin<SetpointRawPlugin> {
00035 public:
00036 SetpointRawPlugin() :
00037 sp_nh("~setpoint_raw"),
00038 uas(nullptr)
00039 { };
00040
00041 void initialize(UAS &uas_)
00042 {
00043 bool tf_listen;
00044
00045 uas = &uas_;
00046
00047 local_sub = sp_nh.subscribe("local", 10, &SetpointRawPlugin::local_cb, this);
00048 global_sub = sp_nh.subscribe("global", 10, &SetpointRawPlugin::global_cb, this);
00049 attitude_sub = sp_nh.subscribe("attitude", 10, &SetpointRawPlugin::attitude_cb, this);
00050 target_local_pub = sp_nh.advertise<mavros_msgs::PositionTarget>("target_local", 10);
00051 target_global_pub = sp_nh.advertise<mavros_msgs::GlobalPositionTarget>("target_global", 10);
00052 target_attitude_pub = sp_nh.advertise<mavros_msgs::AttitudeTarget>("target_attitude", 10);
00053 }
00054
00055 const message_map get_rx_handlers() {
00056 return {
00057 MESSAGE_HANDLER(MAVLINK_MSG_ID_POSITION_TARGET_LOCAL_NED, &SetpointRawPlugin::handle_position_target_local_ned),
00058 MESSAGE_HANDLER(MAVLINK_MSG_ID_POSITION_TARGET_GLOBAL_INT, &SetpointRawPlugin::handle_position_target_global_int),
00059 MESSAGE_HANDLER(MAVLINK_MSG_ID_ATTITUDE_TARGET, &SetpointRawPlugin::handle_attitude_target),
00060 };
00061 }
00062
00063 private:
00064 friend class SetPositionTargetLocalNEDMixin;
00065 ros::NodeHandle sp_nh;
00066 UAS *uas;
00067
00068 ros::Subscriber local_sub, global_sub, attitude_sub;
00069 ros::Publisher target_local_pub, target_global_pub, target_attitude_pub;
00070
00071
00072 void handle_position_target_local_ned(const mavlink_message_t *msg, uint8_t sysid, uint8_t compid) {
00073 mavlink_position_target_local_ned_t tgt;
00074 mavlink_msg_position_target_local_ned_decode(msg, &tgt);
00075
00076
00077 auto position = UAS::transform_frame_ned_enu(Eigen::Vector3d(tgt.x, tgt.y, tgt.z));
00078 auto velocity = UAS::transform_frame_ned_enu(Eigen::Vector3d(tgt.vx, tgt.vy, tgt.vz));
00079 auto af = UAS::transform_frame_ned_enu(Eigen::Vector3d(tgt.afx, tgt.afy, tgt.afz));
00080 float yaw = UAS::transform_frame_yaw_ned_enu(tgt.yaw);
00081 float yaw_rate = UAS::transform_frame_yaw_ned_enu(tgt.yaw_rate);
00082
00083 auto target = boost::make_shared<mavros_msgs::PositionTarget>();
00084
00085 target->header.stamp = uas->synchronise_stamp(tgt.time_boot_ms);
00086 target->coordinate_frame = tgt.coordinate_frame;
00087 target->type_mask = tgt.type_mask;
00088 tf::pointEigenToMsg(position, target->position);
00089 tf::vectorEigenToMsg(velocity, target->velocity);
00090 tf::vectorEigenToMsg(af, target->acceleration_or_force);
00091 target->yaw = yaw;
00092 target->yaw_rate = yaw_rate;
00093
00094 target_local_pub.publish(target);
00095 }
00096
00097 void handle_position_target_global_int(const mavlink_message_t *msg, uint8_t sysid, uint8_t compid) {
00098 mavlink_position_target_global_int_t tgt;
00099 mavlink_msg_position_target_global_int_decode(msg, &tgt);
00100
00101
00102 auto velocity = UAS::transform_frame_ned_enu(Eigen::Vector3d(tgt.vx, tgt.vy, tgt.vz));
00103 auto af = UAS::transform_frame_ned_enu(Eigen::Vector3d(tgt.afx, tgt.afy, tgt.afz));
00104 float yaw = UAS::transform_frame_yaw_ned_enu(tgt.yaw);
00105 float yaw_rate = UAS::transform_frame_yaw_ned_enu(tgt.yaw_rate);
00106
00107 auto target = boost::make_shared<mavros_msgs::GlobalPositionTarget>();
00108
00109 target->header.stamp = uas->synchronise_stamp(tgt.time_boot_ms);
00110 target->coordinate_frame = tgt.coordinate_frame;
00111 target->type_mask = tgt.type_mask;
00112 target->latitude = tgt.lat_int / 1e7;
00113 target->longitude = tgt.lon_int / 1e7;
00114 target->altitude = tgt.alt;
00115 tf::vectorEigenToMsg(velocity, target->velocity);
00116 tf::vectorEigenToMsg(af, target->acceleration_or_force);
00117 target->yaw = yaw;
00118 target->yaw_rate = yaw_rate;
00119
00120 target_global_pub.publish(target);
00121 }
00122
00123 void handle_attitude_target(const mavlink_message_t *msg, uint8_t sysid, uint8_t compid) {
00124 mavlink_attitude_target_t tgt;
00125 mavlink_msg_attitude_target_decode(msg, &tgt);
00126
00127
00128
00129 auto orientation = UAS::transform_orientation_ned_enu(
00130 UAS::transform_orientation_baselink_aircraft(Eigen::Quaterniond(tgt.q[0], tgt.q[1], tgt.q[2], tgt.q[3])));
00131
00132 auto body_rate = UAS::transform_frame_baselink_aircraft(Eigen::Vector3d(tgt.body_roll_rate, tgt.body_pitch_rate, tgt.body_yaw_rate));
00133
00134 auto target = boost::make_shared<mavros_msgs::AttitudeTarget>();
00135
00136 target->header.stamp = uas->synchronise_stamp(tgt.time_boot_ms);
00137 target->type_mask = tgt.type_mask;
00138 tf::quaternionEigenToMsg(orientation, target->orientation);
00139 tf::vectorEigenToMsg(body_rate, target->body_rate);
00140 target->thrust = tgt.thrust;
00141
00142 target_attitude_pub.publish(target);
00143 }
00144
00145
00147 void set_position_target_global_int(uint32_t time_boot_ms, uint8_t coordinate_frame, uint8_t type_mask,
00148 int32_t lat_int, int32_t lon_int, float alt,
00149 Eigen::Vector3d &velocity,
00150 Eigen::Vector3d &af,
00151 float yaw, float yaw_rate) {
00152 mavlink_message_t msg;
00153 mavlink_msg_set_position_target_global_int_pack_chan(UAS_PACK_CHAN(uas), &msg,
00154 time_boot_ms,
00155 UAS_PACK_TGT(uas),
00156 coordinate_frame,
00157 type_mask,
00158 lat_int, lon_int, alt,
00159 velocity.x(), velocity.y(), velocity.z(),
00160 af.x(), af.y(), af.z(),
00161 yaw, yaw_rate);
00162 UAS_FCU(uas)->send_message(&msg);
00163 }
00164
00166 void set_attitude_target(uint32_t time_boot_ms,
00167 uint8_t type_mask,
00168 Eigen::Quaterniond &orientation,
00169 Eigen::Vector3d &body_rate,
00170 float thrust) {
00171 float q[4];
00172 UAS::quaternion_to_mavlink(orientation, q);
00173
00174 mavlink_message_t msg;
00175 mavlink_msg_set_attitude_target_pack_chan(UAS_PACK_CHAN(uas), &msg,
00176 time_boot_ms,
00177 UAS_PACK_TGT(uas),
00178 type_mask,
00179 q,
00180 body_rate.x(), body_rate.y(), body_rate.z(),
00181 thrust);
00182 UAS_FCU(uas)->send_message(&msg);
00183 }
00184
00185
00186
00187 void local_cb(const mavros_msgs::PositionTarget::ConstPtr &req) {
00188 Eigen::Vector3d position, velocity, af;
00189 float yaw, yaw_rate;
00190
00191 tf::pointMsgToEigen(req->position, position);
00192 tf::vectorMsgToEigen(req->velocity, velocity);
00193 tf::vectorMsgToEigen(req->acceleration_or_force, af);
00194
00195
00196 position = UAS::transform_frame_enu_ned(position);
00197 velocity = UAS::transform_frame_enu_ned(velocity);
00198 af = UAS::transform_frame_enu_ned(af);
00199 yaw = UAS::transform_frame_yaw_enu_ned(req->yaw);
00200 yaw_rate = UAS::transform_frame_yaw_enu_ned(req->yaw_rate);
00201
00202 set_position_target_local_ned(
00203 req->header.stamp.toNSec() / 1000000,
00204 req->coordinate_frame,
00205 req->type_mask,
00206 position.x(), position.y(), position.z(),
00207 velocity.x(), velocity.y(), velocity.z(),
00208 af.x(), af.y(), af.z(),
00209 yaw, yaw_rate);
00210 }
00211
00212 void global_cb(const mavros_msgs::GlobalPositionTarget::ConstPtr &req) {
00213 Eigen::Vector3d velocity, af;
00214 float yaw, yaw_rate;
00215
00216 tf::vectorMsgToEigen(req->velocity, velocity);
00217 tf::vectorMsgToEigen(req->acceleration_or_force, af);
00218
00219
00220 velocity = UAS::transform_frame_enu_ned(velocity);
00221 af = UAS::transform_frame_enu_ned(af);
00222 yaw = UAS::transform_frame_yaw_enu_ned(req->yaw);
00223 yaw_rate = UAS::transform_frame_yaw_enu_ned(req->yaw_rate);
00224
00225 set_position_target_global_int(
00226 req->header.stamp.toNSec() / 1000000,
00227 req->coordinate_frame,
00228 req->type_mask,
00229 req->latitude * 1e7,
00230 req->longitude * 1e7,
00231 req->altitude,
00232 velocity, af,
00233 yaw, yaw_rate);
00234 }
00235
00236 void attitude_cb(const mavros_msgs::AttitudeTarget::ConstPtr &req) {
00237 Eigen::Quaterniond desired_orientation;
00238 Eigen::Vector3d baselink_angular_rate;
00239
00240
00241 tf::quaternionMsgToEigen(req->orientation, desired_orientation);
00242
00243
00244
00245 auto ned_desired_orientation = UAS::transform_orientation_enu_ned(
00246 UAS::transform_orientation_baselink_aircraft(desired_orientation));
00247
00248 auto body_rate = UAS::transform_frame_baselink_aircraft(baselink_angular_rate);
00249
00250 tf::vectorMsgToEigen(req->body_rate, body_rate);
00251
00252 set_attitude_target(
00253 req->header.stamp.toNSec() / 1000000,
00254 req->type_mask,
00255 ned_desired_orientation,
00256 body_rate,
00257 req->thrust);
00258 }
00259 };
00260 };
00261
00262 PLUGINLIB_EXPORT_CLASS(mavplugin::SetpointRawPlugin, mavplugin::MavRosPlugin)