24 #include <boost/algorithm/string.hpp> 25 #include <boost/signals2.hpp> 27 #include "goby/acomms/connect.h" 28 #include "goby/common/logger.h" 29 #include "goby/common/time.h" 31 #include "iFrontSeat.h" 39 iFrontSeat* iFrontSeat::inst_ = 0;
40 void* iFrontSeat::driver_library_handle_ = 0;
42 int main(
int argc,
char* argv[])
45 char* driver_lib_path = getenv(
"IFRONTSEAT_DRIVER_LIBRARY");
48 std::cerr <<
"Loading iFrontSeat driver library: " << driver_lib_path << std::endl;
49 iFrontSeat::driver_library_handle_ = dlopen(driver_lib_path, RTLD_LAZY);
50 if (!iFrontSeat::driver_library_handle_)
52 std::cerr <<
"Failed to open library: " << driver_lib_path << std::endl;
58 std::cerr <<
"Environmental variable IFRONTSEAT_DRIVER_LIBRARY must be set with name of " 59 "the dynamic library containing the specific driver to use." 64 return goby::moos::run<iFrontSeat>(argc, argv);
67 iFrontSeat* iFrontSeat::get_instance()
70 inst_ =
new iFrontSeat();
77 driver_load_func driver_load_ptr =
78 (driver_load_func)dlsym(iFrontSeat::driver_library_handle_,
"frontseat_driver_load");
81 glog.is(DIE) &&
glog <<
"Function frontseat_driver_load in library defined in " 82 "IFRONTSEAT_DRIVER_LIBRARY does not exist." 88 glog.is(DIE) &&
glog <<
"Function frontseat_driver_load in library defined in " 89 "IFRONTSEAT_DRIVER_LIBRARY returned a null pointer." 95 iFrontSeat::iFrontSeat() :
GobyMOOSApp(&cfg_), frontseat_(load_driver(&cfg_)), translator_(this)
98 subscribe(cfg_.moos_var().prefix() + cfg_.moos_var().command_request(),
99 &iFrontSeat::handle_mail_command_request,
this);
101 &iFrontSeat::handle_driver_command_response);
104 subscribe(cfg_.moos_var().prefix() + cfg_.moos_var().data_to_frontseat(),
105 &iFrontSeat::handle_mail_data_to_frontseat,
this);
107 &iFrontSeat::handle_driver_data_from_frontseat);
110 subscribe(cfg_.moos_var().prefix() + cfg_.moos_var().raw_out(),
111 &iFrontSeat::handle_mail_raw_out,
this);
113 &iFrontSeat::handle_driver_raw_in);
116 &iFrontSeat::handle_driver_raw_out);
119 subscribe(
"IVPHELM_STATE", &iFrontSeat::handle_mail_helm_state,
this);
121 register_timer(cfg_.status_period(), boost::bind(&iFrontSeat::status_loop,
this));
124 void iFrontSeat::loop()
126 frontseat_->do_work();
128 if (cfg_.exit_on_error() && (frontseat_->state() == gpb::INTERFACE_FS_ERROR ||
129 frontseat_->state() == gpb::INTERFACE_HELM_ERROR))
132 glog <<
"Error state detected and `exit_on_error` == true, so quitting. Bye!" 137 void iFrontSeat::status_loop()
139 glog.is(DEBUG1) &&
glog <<
"Status: " << frontseat_->status().ShortDebugString() << std::endl;
140 publish_pb(cfg_.moos_var().prefix() + cfg_.moos_var().status(), frontseat_->status());
143 void iFrontSeat::handle_mail_command_request(
const CMOOSMsg& msg)
145 if (frontseat_->state() != gpb::INTERFACE_COMMAND)
148 glog <<
"Not sending command because the interface is not in the command state" 155 frontseat_->send_command_to_frontseat(command);
159 void iFrontSeat::handle_mail_data_to_frontseat(
const CMOOSMsg& msg)
161 if (frontseat_->state() != gpb::INTERFACE_COMMAND &&
162 frontseat_->state() != gpb::INTERFACE_LISTEN)
165 glog <<
"Not sending data because the interface is not in the command or listen state" 172 frontseat_->send_data_to_frontseat(data);
176 void iFrontSeat::handle_mail_raw_out(
const CMOOSMsg& msg)
179 if (msg.GetSource() == GetAppName())
182 if (frontseat_->state() != gpb::INTERFACE_COMMAND &&
183 frontseat_->state() != gpb::INTERFACE_LISTEN)
186 glog <<
"Not sending raw because the interface is not in the command or listen state" 193 frontseat_->send_raw_to_frontseat(raw);
197 void iFrontSeat::handle_mail_helm_state(
const CMOOSMsg& msg)
199 std::string sval = msg.GetString();
201 if (boost::iequals(sval,
"drive"))
202 frontseat_->set_helm_state(gpb::HELM_DRIVE);
203 else if (boost::iequals(sval,
"park"))
204 frontseat_->set_helm_state(gpb::HELM_PARK);
206 frontseat_->set_helm_state(gpb::HELM_NOT_RUNNING);
209 void iFrontSeat::handle_driver_command_response(
212 publish_pb(cfg_.moos_var().prefix() + cfg_.moos_var().command_response(), response);
215 void iFrontSeat::handle_driver_data_from_frontseat(
218 publish_pb(cfg_.moos_var().prefix() + cfg_.moos_var().data_from_frontseat(), data);
219 if (data.has_node_status())
220 publish_pb(cfg_.moos_var().prefix() + cfg_.moos_var().node_status(), data.node_status());
225 publish_pb(cfg_.moos_var().prefix() + cfg_.moos_var().raw_in(), data);
230 publish_pb(cfg_.moos_var().prefix() + cfg_.moos_var().raw_out(), data);
void parse_for_moos(const std::string &in, google::protobuf::Message *msg)
Parses the string in to Google Protocol Buffers message msg. All errors are written to the goby::util...
ReturnType goby_time()
Returns current UTC time as a boost::posix_time::ptime.
void connect(Signal *signal, Slot slot)
connect a signal to a slot (e.g. function pointer)
common::FlexOstream glog
Access the Goby logger through this object.
void subscribe(int socket_id, boost::function< void(const ProtoBufMessage &)> handler=boost::function< void(const ProtoBufMessage &)>(), const std::string &group="")
Subscribe to a message (of any type derived from google::protobuf::Message)