Goby3  3.1.5a
2024.05.23
moos_plugin_translator.h
Go to the documentation of this file.
1 // Copyright 2017-2022:
2 // GobySoft, LLC (2013-)
3 // Community contributors (see AUTHORS file)
4 // File authors:
5 // Toby Schneider <toby@gobysoft.org>
6 //
7 //
8 // This file is part of the Goby Underwater Autonomy Project Libraries
9 // ("The Goby Libraries").
10 //
11 // The Goby Libraries are free software: you can redistribute them and/or modify
12 // them under the terms of the GNU Lesser General Public License as published by
13 // the Free Software Foundation, either version 2.1 of the License, or
14 // (at your option) any later version.
15 //
16 // The Goby Libraries are distributed in the hope that they will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public License
22 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
23 
24 #ifndef GOBY_MOOS_MIDDLEWARE_MOOS_PLUGIN_TRANSLATOR_H
25 #define GOBY_MOOS_MIDDLEWARE_MOOS_PLUGIN_TRANSLATOR_H
26 
27 #include <functional> // for function
28 #include <map> // for map
29 #include <ostream> // for operator<<
30 #include <set> // for set
31 #include <string> // for string, basic...
32 #include <thread> // for get_id, opera...
33 #include <utility> // for pair, make_pair
34 
35 #include <MOOS/libMOOS/Comms/MOOSMsg.h> // for CMOOSMsg
36 #include <MOOS/libMOOS/DB/MsgFilter.h>
37 #include <boost/units/quantity.hpp> // for operator*
38 #include <boost/units/systems/si/frequency.hpp> // for frequency, hertz
39 
40 #include "MOOS/libMOOS/Comms/MOOSAsyncCommClient.h" // for MOOSAsyncComm...
41 #include "goby/middleware/application/multi_thread.h" // for SimpleThread
42 #include "goby/moos/protobuf/moos_gateway_config.pb.h" // for GobyMOOSGatew...
43 #include "goby/time/system_clock.h" // for SystemClock
44 
45 class CMOOSCommClient;
46 
47 namespace goby
48 {
49 namespace moos
50 {
51 bool TranslatorOnConnectCallBack(void* TranslatorBase);
52 
54 {
55  public:
57 
58  protected:
59  std::string translator_name()
60  {
61  std::stringstream ss;
62  ss << std::this_thread::get_id();
63  return std::string("goby::moos::Translator::" + ss.str());
64  }
65 
67  {
68  public:
69  // MOOS
70  void add_trigger(const std::string& moos_var,
71  const std::function<void(const CMOOSMsg&)>& func)
72  {
73  trigger_vars_.insert(std::make_pair(moos_var, func));
74  if (connected_)
75  moos_register(moos_var);
76  }
77 
78  void add_wildcard_trigger(const std::string& moos_var_wildcard,
79  const std::string& moos_app_wildcard,
80  const std::function<void(const CMOOSMsg&)>& func)
81  {
82  MOOS::MsgFilter moos_filter(moos_app_wildcard, moos_var_wildcard);
83  trigger_wildcard_vars_.insert(std::make_pair(moos_filter, func));
84  if (connected_)
85  moos_wildcard_register(moos_filter);
86  }
87 
88  void add_buffer(const std::string& moos_var)
89  {
90  buffer_vars_.insert(moos_var);
91  if (connected_)
92  moos_register(moos_var);
93  }
94  std::map<std::string, CMOOSMsg>& buffer() { return buffer_; }
95  CMOOSCommClient& comms() { return comms_; }
96  void loop();
97 
98  private:
100  void on_connect();
101 
102  void moos_register(const std::string& moos_var)
103  {
104  comms_.Register(moos_var);
105  goby::glog.is_debug1() && goby::glog << "Subscribed for MOOS variable: " << moos_var
106  << std::endl;
107  }
108  void moos_wildcard_register(const MOOS::MsgFilter& moos_filter)
109  {
110  comms_.Register(moos_filter.var_filter(), moos_filter.app_filter(), 0);
111  goby::glog.is_debug1() &&
112  goby::glog << "Subscribed for MOOS wildcard: variable: " << moos_filter.var_filter()
113  << ", app: " << moos_filter.app_filter() << std::endl;
114  }
115 
116  private:
117  std::map<std::string, std::function<void(const CMOOSMsg&)>> trigger_vars_;
118  std::map<MOOS::MsgFilter, std::function<void(const CMOOSMsg&)>> trigger_wildcard_vars_;
119  std::set<std::string> buffer_vars_;
120  std::map<std::string, CMOOSMsg> buffer_;
121  MOOS::MOOSAsyncCommClient comms_;
123  bool connected_{false};
124  };
125 
126  friend bool TranslatorOnConnectCallBack(void* TranslatorBase);
127  MOOSInterface& moos() { return moos_; }
128  void loop();
129 
130  private:
131  MOOSInterface moos_;
133 };
134 
135 template <template <class> class ThreadType>
137  public ThreadType<goby::apps::moos::protobuf::GobyMOOSGatewayConfig>
138 {
139  public:
142  ThreadType<goby::apps::moos::protobuf::GobyMOOSGatewayConfig>(
143  config, 10 * boost::units::si::hertz) // config.poll_frequency()))
144  {
145  }
146 
147  protected:
148  // Goby
149  ThreadType<goby::apps::moos::protobuf::GobyMOOSGatewayConfig>& goby() { return *this; }
150 
151  private:
152  void loop() override { this->TranslatorBase::loop(); }
153 };
154 
156 
157 } // namespace moos
158 } // namespace goby
159 
160 #endif
goby::time::SystemClock::now
static time_point now() noexcept
Returns the current system time unless SimulatorSettings::using_sim_time is set to true,...
Definition: system_clock.h:63
system_clock.h
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::moos::TranslatorBase::TranslatorOnConnectCallBack
friend bool TranslatorOnConnectCallBack(void *TranslatorBase)
goby::moos::TranslatorBase::MOOSInterface
Definition: moos_plugin_translator.h:66
goby::acomms::abc::protobuf::config
extern ::google::protobuf::internal::ExtensionIdentifier< ::goby::acomms::protobuf::DriverConfig, ::google::protobuf::internal::MessageTypeTraits< ::goby::acomms::abc::protobuf::Config >, 11, false > config
Definition: abc_driver.pb.h:203
goby::apps::moos::protobuf::GobyMOOSGatewayConfig
Definition: moos_gateway_config.pb.h:238
boost
Definition: udp_driver.h:41
goby::moos::TranslatorBase::MOOSInterface::loop
void loop()
goby::moos::TranslatorBase::MOOSInterface::TranslatorOnConnectCallBack
friend bool TranslatorOnConnectCallBack(void *TranslatorBase)
goby::moos::BasicTranslator::BasicTranslator
BasicTranslator(const goby::apps::moos::protobuf::GobyMOOSGatewayConfig &config)
Definition: moos_plugin_translator.h:140
detail::void
j template void())
Definition: json.hpp:4822
goby::moos::TranslatorBase::moos
MOOSInterface & moos()
Definition: moos_plugin_translator.h:127
CMOOSMsg
goby::util::FlexOstream::is_debug1
bool is_debug1()
Definition: flex_ostream.h:84
goby::moos::TranslatorBase::MOOSInterface::add_wildcard_trigger
void add_wildcard_trigger(const std::string &moos_var_wildcard, const std::string &moos_app_wildcard, const std::function< void(const CMOOSMsg &)> &func)
Definition: moos_plugin_translator.h:78
multi_thread.h
goby::moos::TranslatorBase
Definition: moos_plugin_translator.h:53
goby::time::SystemClock::time_point
std::chrono::time_point< SystemClock > time_point
Definition: system_clock.h:55
goby::moos::BasicTranslator
Definition: moos_plugin_translator.h:136
goby::moos::TranslatorBase::loop
void loop()
goby::moos::TranslatorBase::MOOSInterface::comms
CMOOSCommClient & comms()
Definition: moos_plugin_translator.h:95
goby::moos::TranslatorOnConnectCallBack
bool TranslatorOnConnectCallBack(void *TranslatorBase)
goby::moos::TranslatorBase::TranslatorBase
TranslatorBase(const goby::apps::moos::protobuf::GobyMOOSGatewayConfig &config)
moos_gateway_config.pb.h
goby::moos::TranslatorBase::MOOSInterface::add_trigger
void add_trigger(const std::string &moos_var, const std::function< void(const CMOOSMsg &)> &func)
Definition: moos_plugin_translator.h:70
goby::moos::TranslatorBase::MOOSInterface::buffer
std::map< std::string, CMOOSMsg > & buffer()
Definition: moos_plugin_translator.h:94
goby::glog
util::FlexOstream glog
Access the Goby logger through this object.
goby::moos::BasicTranslator::goby
ThreadType< goby::apps::moos::protobuf::GobyMOOSGatewayConfig > & goby()
Definition: moos_plugin_translator.h:149
goby::moos::TranslatorBase::translator_name
std::string translator_name()
Definition: moos_plugin_translator.h:59
goby::moos::TranslatorBase::MOOSInterface::add_buffer
void add_buffer(const std::string &moos_var)
Definition: moos_plugin_translator.h:88