mavros_plugin.h
Go to the documentation of this file.
1 
10 /*
11  * Copyright 2013,2014,2015 Vladimir Ermakov.
12  *
13  * This file is part of the mavros package and subject to the license terms
14  * in the top-level LICENSE file of the mavros repository.
15  * https://github.com/mavlink/mavros/tree/master/LICENSE.md
16  */
17 
18 #pragma once
19 
20 #include <tuple>
21 #include <vector>
22 #include <functional>
24 #include <mavconn/interface.h>
25 #include <mavros/mavros_uas.h>
26 
27 namespace mavros {
28 namespace plugin {
29 
30 using mavros::UAS;
31 typedef std::lock_guard<std::recursive_mutex> lock_guard;
32 typedef std::unique_lock<std::recursive_mutex> unique_lock;
33 
38 {
39 private:
40  PluginBase(const PluginBase&) = delete;
41 
42 public:
46  using HandlerInfo = std::tuple<mavlink::msgid_t, const char*, size_t, HandlerCb>;
48  using Subscriptions = std::vector<HandlerInfo>;
49 
50  // pluginlib return boost::shared_ptr
53 
54  virtual ~PluginBase() {};
55 
61  virtual void initialize(UAS &uas) {
62  m_uas = &uas;
63  }
64 
68  virtual Subscriptions get_subscriptions() = 0;
69 
70 protected:
75  PluginBase() : m_uas(nullptr) {};
76 
77  UAS *m_uas;
78 
79  // TODO: filtered handlers
80 
87  template<class _C>
88  HandlerInfo make_handler(const mavlink::msgid_t id, void (_C::*fn)(const mavlink::mavlink_message_t *msg, const mavconn::Framing framing)) {
89  auto bfn = std::bind(fn, static_cast<_C*>(this), std::placeholders::_1, std::placeholders::_2);
90  const auto type_hash_ = typeid(mavlink::mavlink_message_t).hash_code();
91 
92  return HandlerInfo{ id, nullptr, type_hash_, bfn };
93  }
94 
100  template<class _C, class _T>
101  HandlerInfo make_handler(void (_C::*fn)(const mavlink::mavlink_message_t*, _T&)) {
102  auto bfn = std::bind(fn, static_cast<_C*>(this), std::placeholders::_1, std::placeholders::_2);
103  const auto id = _T::MSG_ID;
104  const auto name = _T::NAME;
105  const auto type_hash_ = typeid(_T).hash_code();
106 
107  return HandlerInfo{
108  id, name, type_hash_,
109  [bfn](const mavlink::mavlink_message_t *msg, const mavconn::Framing framing) {
110  if (framing != mavconn::Framing::ok)
111  return;
112 
113  mavlink::MsgMap map(msg);
114  _T obj;
115  obj.deserialize(map);
116 
117  bfn(msg, obj);
118  }
119  };
120  }
121 
125  virtual void connection_cb(bool connected) {
126  ROS_BREAK();
127  }
128 
132  inline void enable_connection_cb() {
133  m_uas->add_connection_change_handler(std::bind(&PluginBase::connection_cb, this, std::placeholders::_1));
134  }
135 };
136 } // namespace plugin
137 } // namespace mavros
std::lock_guard< std::recursive_mutex > lock_guard
Definition: mavros_plugin.h:31
msg
MAVROS Plugin base class.
Definition: mavros_plugin.h:37
std::function< void(const mavlink::mavlink_message_t *message, const Framing framing)> ReceivedCb
std::unique_lock< std::recursive_mutex > unique_lock
Definition: mavros_plugin.h:32
std::tuple< mavlink::msgid_t, const char *, size_t, HandlerCb > HandlerInfo
Tuple: MSG ID, MSG NAME, message type into hash_code, message handler callback.
Definition: mavros_plugin.h:46
HandlerInfo make_handler(const mavlink::msgid_t id, void(_C::*fn)(const mavlink::mavlink_message_t *msg, const mavconn::Framing framing))
Definition: mavros_plugin.h:88
PluginBase()
Plugin constructor Should not do anything before initialize()
Definition: mavros_plugin.h:75
UAS for plugins.
Definition: mavros_uas.h:66
void add_connection_change_handler(ConnectionCb cb)
Add connection change handler callback.
Definition: uas_data.cpp:81
virtual Subscriptions get_subscriptions()=0
Return vector of MAVLink message subscriptions (handlers)
MAVROS Plugin context.
virtual void connection_cb(bool connected)
std::vector< HandlerInfo > Subscriptions
Subscriptions vector.
Definition: mavros_plugin.h:48
HandlerInfo make_handler(void(_C::*fn)(const mavlink::mavlink_message_t *, _T &))
virtual void initialize(UAS &uas)
Plugin initializer.
Definition: mavros_plugin.h:61
#define ROS_BREAK()
mavconn::MAVConnInterface::ReceivedCb HandlerCb
generic message handler callback
Definition: mavros_plugin.h:44


mavros
Author(s): Vladimir Ermakov
autogenerated on Mon Jul 8 2019 03:20:11