tunnel.cpp
Go to the documentation of this file.
1 #include <algorithm>
2 #include <mavros/mavros_plugin.h>
3 #include <mavros_msgs/Tunnel.h>
4 #include <stdexcept>
5 
6 namespace mavros
7 {
8 namespace extra_plugins
9 {
11 {
12  public:
13  TunnelPlugin() : PluginBase(), nh_("~tunnel") {}
14 
15  void initialize(UAS& uas_) override
16  {
18  sub_ = nh_.subscribe("in", 10, &TunnelPlugin::ros_callback, this);
19  pub_ = nh_.advertise<mavros_msgs::Tunnel>("out", 10);
20  }
21 
23  {
25  }
26 
27  private:
31 
33  {
34  try
35  {
36  const auto mav_tunnel =
37  copy_tunnel<mavros_msgs::Tunnel, mavlink::common::msg::TUNNEL>(
38  *ros_tunnel);
39 
40  UAS_FCU(m_uas)->send_message_ignore_drop(mav_tunnel);
41  }
42  catch (const std::overflow_error& e)
43  {
44  ROS_ERROR_STREAM_NAMED("tunnel", e.what());
45  }
46  }
47 
48  void mav_callback(const mavlink::mavlink_message_t*,
49  mavlink::common::msg::TUNNEL& mav_tunnel)
50  {
51  try
52  {
53  const auto ros_tunnel =
54  copy_tunnel<mavlink::common::msg::TUNNEL, mavros_msgs::Tunnel>(
55  mav_tunnel);
56 
57  pub_.publish(ros_tunnel);
58  }
59  catch (const std::overflow_error& e)
60  {
61  ROS_ERROR_STREAM_NAMED("tunnel", e.what());
62  }
63  }
64 
65  template <typename From, typename To>
66  static To copy_tunnel(const From& from) noexcept(false)
67  {
68  static constexpr auto max_payload_length =
69  sizeof(mavlink::common::msg::TUNNEL::payload) /
70  sizeof(mavlink::common::msg::TUNNEL::payload[0]);
71 
72  if (from.payload_length > max_payload_length)
73  {
74  throw std::overflow_error("too long payload length");
75  }
76 
77  auto to = To{};
78 
79  to.target_system = from.target_system;
80  to.target_component = from.target_component;
81  to.payload_type = from.payload_type;
82  to.payload_length = from.payload_length;
83  std::copy(from.payload.begin(),
84  from.payload.begin() + from.payload_length,
85  to.payload.begin());
86 
87  return to;
88  }
89 };
90 } // namespace extra_plugins
91 } // namespace mavros
92 
std::shared_ptr< MAVConnInterface const > ConstPtr
#define ROS_ERROR_STREAM_NAMED(name, args)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void initialize(UAS &uas_) override
Definition: tunnel.cpp:15
HandlerInfo make_handler(const mavlink::msgid_t id, void(_C::*fn)(const mavlink::mavlink_message_t *msg, const mavconn::Framing framing))
void mav_callback(const mavlink::mavlink_message_t *, mavlink::common::msg::TUNNEL &mav_tunnel)
Definition: tunnel.cpp:48
void publish(const boost::shared_ptr< M > &message) const
#define UAS_FCU(uasobjptr)
static To copy_tunnel(const From &from) noexcept(false)
Definition: tunnel.cpp:66
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
Subscriptions get_subscriptions() override
Definition: tunnel.cpp:22
void ros_callback(const mavros_msgs::Tunnel::ConstPtr &ros_tunnel)
Definition: tunnel.cpp:32
std::vector< HandlerInfo > Subscriptions
void initialize(UAS &uas_) override
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)


mavros_extras
Author(s): Vladimir Ermakov , Amilcar Lucas
autogenerated on Tue Jun 13 2023 02:17:59