21 #include <geometry_msgs/PoseStamped.h> 23 #include <mavros_msgs/SetMavFrame.h> 24 #include <geographic_msgs/GeoPoseStamped.h> 26 #include <GeographicLib/Geocentric.hpp> 29 namespace std_plugins {
30 using mavlink::common::MAV_FRAME;
41 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
44 sp_nh(
"~setpoint_position"),
52 PluginBase::initialize(uas_);
62 << tf_frame_id <<
" -> " << tf_child_frame_id);
79 std::string mav_frame_str;
129 using mavlink::common::MAV_FRAME;
137 const uint16_t ignore_all_except_xyz_y = (1 << 11) | (7 << 6) | (7 << 3);
140 if (static_cast<MAV_FRAME>(mav_frame) == MAV_FRAME::BODY_NED ||
static_cast<MAV_FRAME>(
mav_frame) == MAV_FRAME::BODY_OFFSET_NED) {
148 if (mav_frame == MAV_FRAME::BODY_NED || mav_frame == MAV_FRAME::BODY_OFFSET_NED) {
158 ignore_all_except_xyz_y,
160 Eigen::Vector3d::Zero(),
161 Eigen::Vector3d::Zero(),
191 using mavlink::common::POSITION_TARGET_TYPEMASK;
193 uint16_t type_mask = uint16_t(POSITION_TARGET_TYPEMASK::VX_IGNORE)
194 | uint16_t(POSITION_TARGET_TYPEMASK::VY_IGNORE)
195 | uint16_t(POSITION_TARGET_TYPEMASK::VZ_IGNORE)
196 | uint16_t(POSITION_TARGET_TYPEMASK::AX_IGNORE)
197 | uint16_t(POSITION_TARGET_TYPEMASK::AY_IGNORE)
198 | uint16_t(POSITION_TARGET_TYPEMASK::AZ_IGNORE);
200 Eigen::Quaterniond attitude;
206 req->header.stamp.toNSec() / 1000000,
207 uint8_t(MAV_FRAME::GLOBAL_INT),
209 req->pose.position.latitude * 1e7,
210 req->pose.position.longitude * 1e7,
211 req->pose.position.altitude,
212 Eigen::Vector3d::Zero(),
213 Eigen::Vector3d::Zero(),
231 GeographicLib::Geocentric earth(GeographicLib::Constants::WGS84_a(), GeographicLib::Constants::WGS84_f());
233 Eigen::Vector3d goal_gps(req->pose.position.latitude, req->pose.position.longitude, req->pose.position.altitude);
236 Eigen::Vector3d current_ecef;
237 earth.Forward(current_gps.x(), current_gps.y(), current_gps.z(),
238 current_ecef.x(), current_ecef.y(), current_ecef.z());
241 Eigen::Vector3d goal_ecef;
242 earth.Forward(goal_gps.x(), goal_gps.y(), goal_gps.z(),
243 goal_ecef.x(), goal_ecef.y(), goal_ecef.z());
246 Eigen::Vector3d ecef_offset = goal_ecef - current_ecef;
251 Eigen::Quaterniond q;
256 sp.translation() = current_local_pos + enu_offset;
258 sp.linear() = q.toRotationMatrix();
261 if ((req->header.stamp.toNSec() / 1000000) > old_gps_stamp) {
262 old_gps_stamp = req->header.stamp.toNSec() / 1000000;
275 current_gps = {msg->latitude, msg->longitude, msg->altitude};
286 bool set_mav_frame_cb(mavros_msgs::SetMavFrame::Request &req, mavros_msgs::SetMavFrame::Response &res)
288 mav_frame =
static_cast<MAV_FRAME>(req.mav_frame);
290 sp_nh.
setParam(
"mav_frame", mav_frame_str);
MAVROS Plugin base class.
std::shared_ptr< MAVConnInterface const > ConstPtr
#define ROS_WARN_THROTTLE_NAMED(rate, name,...)
ros::Subscriber setpointg_sub
Global setpoint.
mavlink::common::MAV_FRAME mav_frame_from_str(const std::string &mav_frame)
Retreive MAV_FRAME from name.
bool set_mav_frame_cb(mavros_msgs::SetMavFrame::Request &req, mavros_msgs::SetMavFrame::Response &res)
std::string tf_child_frame_id
ros::ServiceServer mav_frame_srv
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void poseMsgToEigen(const geometry_msgs::Pose &m, Eigen::Affine3d &e)
friend class SetPositionTargetGlobalIntMixin
This mixin adds TF2 listener thread to plugin.
uint32_t old_gps_stamp
old time gps time stamp in [ms], to check if new gps msg is received
void setpointg2l_cb(const geographic_msgs::GeoPoseStamped::ConstPtr &req)
void tf2_start(const char *_thd_name, void(SetpointPositionPlugin::*cbp)(const geometry_msgs::TransformStamped &))
start tf listener
#define ROS_INFO_STREAM_NAMED(name, args)
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
void set_position_target_local_ned(uint32_t time_boot_ms, uint8_t coordinate_frame, uint16_t type_mask, Eigen::Vector3d p, Eigen::Vector3d v, Eigen::Vector3d af, float yaw, float yaw_rate)
Message specification: https://mavlink.io/en/messages/common.html#SET_POSITION_TARGET_LOCAL_NED.
void setpoint_cb(const geometry_msgs::PoseStamped::ConstPtr &req)
T transform_orientation_enu_ned(const T &in)
Transform from attitude represented WRT ENU frame to attitude represented WRT NED frame...
void local_cb(const geometry_msgs::PoseStamped::ConstPtr &msg)
Subscriptions get_subscriptions() override
Return vector of MAVLink message subscriptions (handlers)
EIGEN_MAKE_ALIGNED_OPERATOR_NEW SetpointPositionPlugin()
PluginBase()
Plugin constructor Should not do anything before initialize()
T transform_orientation_absolute_frame_aircraft_baselink(const T &in)
Transform from attitude represented WRT aircraft frame to attitude represented WRT base_link frame...
friend class TF2ListenerMixin
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val) const
ros::Subscriber setpoint_sub
Eigen::Vector3d current_gps
geodetic coordinates LLA
Mixin for setpoint plugins.
std::string to_string(timesync_mode e)
ros::Subscriber gps_sub
current GPS
Eigen::Vector3d to_eigen(const geometry_msgs::Point r)
Helper to convert common ROS geometry_msgs::Point to Eigen::Vector3d.
T transform_frame_baselink_aircraft(const T &in)
Transform data expressed in Baselink frame to Aircraft frame.
friend class SetPositionTargetLocalNEDMixin
T transform_frame_enu_ned(const T &in)
Transform data expressed in ENU to NED frame.
void transformMsgToEigen(const geometry_msgs::Transform &m, Eigen::Affine3d &e)
void gps_cb(const sensor_msgs::NavSatFix::ConstPtr &msg)
void transform_cb(const geometry_msgs::TransformStamped &transform)
void setpointg_cb(const geographic_msgs::GeoPoseStamped::ConstPtr &req)
This mixin adds set_position_target_local_ned()
std::vector< HandlerInfo > Subscriptions
Subscriptions vector.
void set_position_target_global_int(uint32_t time_boot_ms, uint8_t coordinate_frame, uint16_t type_mask, int32_t lat_int, int32_t lon_int, float alt, Eigen::Vector3d v, Eigen::Vector3d af, float yaw, float yaw_rate)
Message specification: https://mavlink.io/en/messages/common.html#SET_POSITION_TARGET_GLOBAL_INT.
double quaternion_get_yaw(const Eigen::Quaterniond &q)
Get Yaw angle from quaternion.
bool getParam(const std::string &key, std::string &s) const
T transform_frame_ecef_enu(const T &in, const T &map_origin)
Transform data expressed in ECEF frame to ENU frame.
ros::Subscriber setpointg2l_sub
Global setpoint converted to local setpoint.
void quaternionMsgToEigen(const geometry_msgs::Quaternion &m, Eigen::Quaterniond &e)
T transform_orientation_baselink_aircraft(const T &in)
Transform from attitude represented WRT baselink frame to attitude represented WRT body frame...
void send_position_target(const ros::Time &stamp, const Eigen::Affine3d &tr)
Send setpoint to FCU position controller.
Setpoint position plugin.
This mixin adds set_position_target_global_int()
void initialize(UAS &uas_) override
Plugin initializer.
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
void setParam(const std::string &key, const XmlRpc::XmlRpcValue &v) const
ros::NodeHandle spg_nh
to get local position and gps coord which are not under sp_h()
ros::Subscriber local_sub
current local ENU
constexpr std::underlying_type< _T >::type enum_value(_T e)
Eigen::Vector3d current_local_pos
Current local position in ENU.