JaiaBot  1.19.0
JaiaBot micro-AUV software
jaiabot_gateway_plugin.h
Go to the documentation of this file.
1 #ifndef JAIABOT_LIB_MOOS_GATEWAY_H
2 #define JAIABOT_LIB_MOOS_GATEWAY_H
3 
4 #include <goby/moos/middleware/moos_plugin_translator.h>
5 #include <goby/moos/protobuf/moos_gateway_config.pb.h>
6 
7 #include "jaiabot/groups.h"
10 
11 #include "jaiabot_gateway_config.pb.h"
12 namespace jaiabot
13 {
14 namespace moos
15 {
16 class IvPHelmTranslation : public goby::moos::Translator
17 {
18  public:
19  IvPHelmTranslation(const goby::apps::moos::protobuf::GobyMOOSGatewayConfig& cfg)
20  : goby::moos::Translator(cfg)
21  {
22  // once get a datum update, wait until the next NAV_X (published from NodeStatus)
23  // before we send any new IvPBehaviorUpdate messages. If not, we can get a race condition where
24  // the Helm completes a Transit if the current position (in X,Y) is the same as the recovery position (in X,Y).
25  goby().interprocess().subscribe<goby::middleware::groups::datum_update>(
26  [this](const goby::middleware::protobuf::DatumUpdate& datum_update) {
27  goby::glog.is_debug1() && goby::glog << "Datum update received." << std::endl;
28  pending_datum_update_ = true;
29  });
30  // use NAV_X as a proxy for a new full NAV_* update (via goby::moos::FrontSeatTranslation from NodeStatus)
31  // once received, flush any pending update
32  moos().add_trigger("NAV_X", [this](const CMOOSMsg&) {
33  pending_datum_update_ = false;
34  if (pending_bhv_update_)
35  {
36  goby::glog.is_debug1() &&
37  goby::glog << "NAV_X received, flushing pending IvPBehaviorUpdate" << std::endl;
38 
39  publish_bhv_update(*pending_bhv_update_);
40  pending_bhv_update_.reset();
41  }
42  });
43 
44  goby().interprocess().subscribe<jaiabot::groups::mission_ivp_behavior_update>(
45  [this](const protobuf::IvPBehaviorUpdate& update) { publish_bhv_update(update); });
46 
47  goby().interprocess().subscribe<jaiabot::groups::mission_report>(
48  [this](const protobuf::MissionReport& report) {
49  moos().comms().Notify("JAIABOT_MISSION_STATE",
50  jaiabot::protobuf::MissionState_Name(report.state()));
51  });
52  moos().add_trigger("JAIABOT_TRANSIT_COMPLETE", [this](const CMOOSMsg& msg) {
53  if (msg.GetString() == "true")
54  {
55  protobuf::IvPBehaviorReport report;
56  report.mutable_transit()->set_waypoint_reached(true);
57  goby().interprocess().publish<jaiabot::groups::mission_ivp_behavior_report>(report);
58  }
59  });
60  }
61 
62  private:
63  void publish_bhv_update(const protobuf::IvPBehaviorUpdate& update);
64 
65  private:
66  bool pending_datum_update_{false};
67  std::unique_ptr<protobuf::IvPBehaviorUpdate> pending_bhv_update_;
68 
69 }; // namespace moos
70 
71 class AllMessagesForLoggingTranslation : public goby::moos::Translator
72 {
73  public:
74  AllMessagesForLoggingTranslation(const goby::apps::moos::protobuf::GobyMOOSGatewayConfig& cfg)
75  : goby::moos::Translator(cfg),
76  jaiabot_cfg_(cfg.GetExtension(jaiabot::protobuf::jaiabot_gateway_config)),
77  omit_var_(jaiabot_cfg_.logging_omit_var_regex()),
78  omit_app_(jaiabot_cfg_.logging_omit_app_regex())
79  {
80  moos().add_wildcard_trigger(
81  "*", "*",
82  [this](const CMOOSMsg& msg)
83  {
84  if (jaiabot_cfg_.has_logging_omit_var_regex() &&
85  std::regex_match(msg.m_sKey, omit_var_))
86  return;
87  if (jaiabot_cfg_.has_logging_omit_app_regex() &&
88  std::regex_match(msg.m_sSrc, omit_app_))
89  return;
90 
91  protobuf::MOOSMessage pb_msg;
92  pb_msg.set_type(static_cast<protobuf::MOOSMessage::Type>(msg.m_cDataType));
93  pb_msg.set_key(msg.m_sKey);
94  switch (pb_msg.type())
95  {
96  case protobuf::MOOSMessage::TYPE_DOUBLE: pb_msg.set_dvalue(msg.m_dfVal); break;
97  case protobuf::MOOSMessage::TYPE_STRING: pb_msg.set_svalue(msg.m_sVal); break;
98  case protobuf::MOOSMessage::TYPE_BINARY_STRING:
99  pb_msg.set_bvalue(msg.m_sVal);
100  break;
101  }
102  pb_msg.set_unixtime(msg.m_dfTime);
103  pb_msg.set_id(msg.m_nID);
104  pb_msg.set_source(msg.m_sSrc);
105  pb_msg.set_source_aux(msg.m_sSrcAux);
106  pb_msg.set_community(msg.m_sOriginatingCommunity);
107  interprocess().publish<jaiabot::groups::moos>(pb_msg);
108  });
109  }
110 
111  private:
112  const jaiabot::protobuf::MOOSGatewayConfig jaiabot_cfg_;
113  std::regex omit_var_;
114  std::regex omit_app_;
115 };
116 
117 } // namespace moos
118 } // namespace jaiabot
119 
120 #endif
goby
Definition: udp_driver.h:59
jaiabot::protobuf::MOOSMessage_Type
MOOSMessage_Type
Definition: moos.pb.h:68
jaiabot::moos::IvPHelmTranslation::IvPHelmTranslation
IvPHelmTranslation(const goby::apps::moos::protobuf::GobyMOOSGatewayConfig &cfg)
Definition: jaiabot_gateway_plugin.h:19
jaiabot::protobuf::MOOSMessage::set_type
void set_type(::jaiabot::protobuf::MOOSMessage_Type value)
Definition: moos.pb.h:414
jaiabot::moos::IvPHelmTranslation
Definition: jaiabot_gateway_plugin.h:16
moos.pb.h
jaiabot::protobuf::MissionReport
Definition: mission.pb.h:637
jaiabot::groups::mission_ivp_behavior_update
constexpr goby::middleware::Group mission_ivp_behavior_update
Definition: groups.h:58
jaiabot::protobuf::IvPBehaviorUpdate
Definition: mission.pb.h:3501
jaiabot::moos::AllMessagesForLoggingTranslation
Definition: jaiabot_gateway_plugin.h:71
jaiabot::moos::AllMessagesForLoggingTranslation::AllMessagesForLoggingTranslation
AllMessagesForLoggingTranslation(const goby::apps::moos::protobuf::GobyMOOSGatewayConfig &cfg)
Definition: jaiabot_gateway_plugin.h:74
jaiabot
Definition: config.pb.h:56
jaia::msg
extern ::google::protobuf::internal::ExtensionIdentifier< ::google::protobuf::MessageOptions, ::google::protobuf::internal::MessageTypeTraits< ::jaia::MessageOptions >, 11, false > msg
Definition: option_extensions.pb.h:796
jaiabot::protobuf::MOOSMessage
Definition: moos.pb.h:90
groups.h
jaiabot::protobuf::MissionState_Name
const ::std::string & MissionState_Name(MissionState value)
Definition: mission.pb.h:265
jaiabot::groups::mission_report
constexpr goby::middleware::Group mission_report
Definition: groups.h:57
jaiabot::groups::moos
constexpr goby::middleware::Group moos
Definition: groups.h:106
mission.pb.h