Goby3 3.2.3
2025.05.13
Loading...
Searching...
No Matches
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
45class CMOOSCommClient;
46
47namespace goby
48{
49namespace moos
50{
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);
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
127 MOOSInterface& moos() { return moos_; }
128 void loop();
129
130 private:
131 MOOSInterface moos_;
133};
134
135template <template <class> class ThreadType>
137 public ThreadType<goby::apps::moos::protobuf::GobyMOOSGatewayConfig>
138{
139 public:
141 : TranslatorBase(config),
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
ThreadType< goby::apps::moos::protobuf::GobyMOOSGatewayConfig > & goby()
BasicTranslator(const goby::apps::moos::protobuf::GobyMOOSGatewayConfig &config)
void add_wildcard_trigger(const std::string &moos_var_wildcard, const std::string &moos_app_wildcard, const std::function< void(const CMOOSMsg &)> &func)
std::map< std::string, CMOOSMsg > & buffer()
void add_buffer(const std::string &moos_var)
friend bool TranslatorOnConnectCallBack(void *TranslatorBase)
void add_trigger(const std::string &moos_var, const std::function< void(const CMOOSMsg &)> &func)
friend bool TranslatorOnConnectCallBack(void *TranslatorBase)
TranslatorBase(const goby::apps::moos::protobuf::GobyMOOSGatewayConfig &config)
bool TranslatorOnConnectCallBack(void *TranslatorBase)
The global namespace for the Goby project.
util::FlexOstream glog
Access the Goby logger through this object.
std::chrono::time_point< SystemClock > time_point
static time_point now() noexcept
Returns the current system time unless SimulatorSettings::using_sim_time is set to true,...