22 #include <boost/assign.hpp> 24 #include "goby/acomms/connect.h" 26 #include "iFrontSeat.h" 27 #include "legacy_translator.h" 29 #include "goby/moos/frontseat/bluefin/bluefin.pb.h" 35 FrontSeatLegacyTranslator::FrontSeatLegacyTranslator(iFrontSeat* fs) : ifs_(fs), request_id_(0)
37 using boost::assign::operator+=;
39 if (ifs_->cfg_.legacy_cfg().subscribe_ctd())
41 std::vector<std::string> ctd_params;
42 ctd_params +=
"CONDUCTIVITY",
"TEMPERATURE",
"PRESSURE",
"SALINITY";
43 for (std::vector<std::string>::const_iterator it = ctd_params.begin(),
44 end = ctd_params.end();
46 { ifs_->subscribe(
"CTD_" + *it, &FrontSeatLegacyTranslator::handle_mail_ctd,
this, 1); }
48 ctd_sample_.set_temperature(std::numeric_limits<double>::quiet_NaN());
49 ctd_sample_.set_pressure(std::numeric_limits<double>::quiet_NaN());
50 ctd_sample_.set_salinity(std::numeric_limits<double>::quiet_NaN());
51 ctd_sample_.set_lat(std::numeric_limits<double>::quiet_NaN());
52 ctd_sample_.set_lon(std::numeric_limits<double>::quiet_NaN());
56 if (ifs_->cfg_.legacy_cfg().subscribe_desired())
58 std::vector<std::string> desired_params;
59 desired_params +=
"HEADING",
"SPEED",
"DEPTH",
"PITCH",
"ROLL",
"Z_RATE",
"ALTITUDE";
60 for (std::vector<std::string>::const_iterator it = desired_params.begin(),
61 end = desired_params.end();
64 ifs_->subscribe(
"DESIRED_" + *it,
65 &FrontSeatLegacyTranslator::handle_mail_desired_course,
this, 1);
69 if (ifs_->cfg_.legacy_cfg().subscribe_acomms_raw())
72 "ACOMMS_RAW_INCOMING",
73 boost::bind(&FrontSeatLegacyTranslator::handle_mail_modem_raw,
this, _1, INCOMING));
75 "ACOMMS_RAW_OUTGOING",
76 boost::bind(&FrontSeatLegacyTranslator::handle_mail_modem_raw,
this, _1, OUTGOING));
79 if (ifs_->cfg_.legacy_cfg().pub_sub_bf_commands())
81 ifs_->subscribe(
"BUOYANCY_CONTROL",
82 &FrontSeatLegacyTranslator::handle_mail_buoyancy_control,
this);
83 ifs_->subscribe(
"TRIM_CONTROL", &FrontSeatLegacyTranslator::handle_mail_trim_control,
this);
84 ifs_->subscribe(
"FRONTSEAT_BHVOFF",
85 &FrontSeatLegacyTranslator::handle_mail_frontseat_bhvoff,
this);
86 ifs_->subscribe(
"FRONTSEAT_SILENT",
87 &FrontSeatLegacyTranslator::handle_mail_frontseat_silent,
this);
88 ifs_->subscribe(
"BACKSEAT_ABORT", &FrontSeatLegacyTranslator::handle_mail_backseat_abort,
93 &FrontSeatLegacyTranslator::handle_driver_data_from_frontseat);
95 if (ifs_->cfg_.legacy_cfg().publish_fs_bs_ready())
98 &FrontSeatLegacyTranslator::set_fs_bs_ready_flags);
101 void FrontSeatLegacyTranslator::handle_driver_data_from_frontseat(
104 if (data.has_node_status() && ifs_->cfg_.legacy_cfg().publish_nav())
108 ctd_sample_.set_lat(status.global_fix().lat());
109 ctd_sample_.set_lon(status.global_fix().lon());
112 ifs_->publish(
"NAV_X", status.local_fix().x());
113 ifs_->publish(
"NAV_Y", status.local_fix().y());
114 ifs_->publish(
"NAV_LAT", status.global_fix().lat());
115 ifs_->publish(
"NAV_LONG", status.global_fix().lon());
117 if (status.local_fix().has_z())
118 ifs_->publish(
"NAV_Z", status.local_fix().z());
119 if (status.global_fix().has_depth())
120 ifs_->publish(
"NAV_DEPTH", status.global_fix().depth());
122 const double pi = 3.14159;
123 if (status.pose().has_heading())
125 double yaw = -status.pose().heading() * pi / 180.0;
126 while (yaw < -pi) yaw += 2 * pi;
127 while (yaw >= pi) yaw -= 2 * pi;
128 ifs_->publish(
"NAV_YAW", yaw);
129 ifs_->publish(
"NAV_HEADING", status.pose().heading());
132 ifs_->publish(
"NAV_SPEED", status.speed());
134 if (status.pose().has_pitch())
135 ifs_->publish(
"NAV_PITCH", status.pose().pitch() * pi / 180.0);
136 if (status.pose().has_roll())
137 ifs_->publish(
"NAV_ROLL", status.pose().roll() * pi / 180.0);
139 if (status.global_fix().has_altitude())
140 ifs_->publish(
"NAV_ALTITUDE", status.global_fix().altitude());
143 if (status.global_fix().lat_source() == goby::moos::protobuf::GPS &&
144 status.global_fix().lon_source() == goby::moos::protobuf::GPS)
146 std::stringstream ss;
147 ss <<
"Timestamp=" << std::setprecision(15) << status.time();
148 ifs_->publish(
"GPS_UPDATE_RECEIVED", ss.str());
152 if (data.HasExtension(gpb::bluefin_data) && ifs_->cfg_.legacy_cfg().pub_sub_bf_commands())
155 if (bf_data.has_trim_status())
157 std::stringstream trim_report;
160 trim_report <<
"status=" <<
static_cast<int>(trim.status())
161 <<
",error=" << static_cast<int>(trim.error())
162 <<
",trim_pitch=" << trim.pitch_trim_degrees()
163 <<
",trim_roll=" << trim.roll_trim_degrees();
165 ifs_->publish(
"TRIM_REPORT", trim_report.str());
168 if (bf_data.has_buoyancy_status())
170 std::stringstream buoyancy_report;
172 buoyancy_report <<
"status=" <<
static_cast<int>(buoyancy.status())
173 <<
",error=" << static_cast<int>(buoyancy.error())
174 <<
",buoyancy=" << buoyancy.buoyancy_newtons();
175 ifs_->publish(
"BUOYANCY_REPORT", buoyancy_report.str());
180 void FrontSeatLegacyTranslator::handle_mail_ctd(
const CMOOSMsg& msg)
182 const std::string& key = msg.GetKey();
183 if (key ==
"CTD_CONDUCTIVITY")
189 ctd_sample_.set_conductivity(msg.GetDouble());
191 else if (key ==
"CTD_TEMPERATURE")
194 ctd_sample_.set_temperature(msg.GetDouble());
198 ctd_sample_.set_time(msg.GetTime());
200 *data.mutable_ctd_sample() = ctd_sample_;
201 ifs_->frontseat_->compute_missing(data.mutable_ctd_sample());
203 ifs_->publish_pb(ifs_->cfg_.moos_var().prefix() + ifs_->cfg_.moos_var().data_to_frontseat(),
206 else if (key ==
"CTD_PRESSURE")
212 const double dBar_TO_Pascal = 1e4;
213 ctd_sample_.set_pressure(msg.GetDouble() * dBar_TO_Pascal);
215 else if (key ==
"CTD_SALINITY")
218 ctd_sample_.set_salinity(msg.GetDouble());
222 void FrontSeatLegacyTranslator::handle_mail_desired_course(
const CMOOSMsg& msg)
224 const std::string& key = msg.GetKey();
225 if (key ==
"DESIRED_SPEED")
227 desired_course_.set_speed(msg.GetDouble());
228 desired_course_.set_time(msg.GetTime());
230 *command.mutable_desired_course() = desired_course_;
231 command.set_response_requested(
true);
232 command.set_request_id(LEGACY_REQUEST_IDENTIFIER + request_id_++);
234 ifs_->publish_pb(ifs_->cfg_.moos_var().prefix() + ifs_->cfg_.moos_var().command_request(),
237 else if (key ==
"DESIRED_HEADING")
239 desired_course_.set_heading(msg.GetDouble());
241 else if (key ==
"DESIRED_DEPTH")
243 desired_course_.set_depth(msg.GetDouble());
245 else if (key ==
"DESIRED_PITCH")
247 desired_course_.set_pitch(msg.GetDouble());
249 else if (key ==
"DESIRED_ROLL")
251 desired_course_.set_roll(msg.GetDouble());
253 else if (key ==
"DESIRED_Z_RATE")
255 desired_course_.set_z_rate(msg.GetDouble());
257 else if (key ==
"DESIRED_ALTITUDE")
259 desired_course_.set_altitude(msg.GetDouble());
263 void FrontSeatLegacyTranslator::handle_mail_modem_raw(
const CMOOSMsg& msg,
264 ModemRawDirection direction)
273 *data.MutableExtension(gpb::bluefin_data)->mutable_micro_modem_raw_out() = raw;
276 *data.MutableExtension(gpb::bluefin_data)->mutable_micro_modem_raw_in() = raw;
280 ifs_->publish_pb(ifs_->cfg_.moos_var().prefix() + ifs_->cfg_.moos_var().data_to_frontseat(),
284 void FrontSeatLegacyTranslator::set_fs_bs_ready_flags(goby::moos::protobuf::InterfaceState
state)
287 if (status.frontseat_state() == gpb::FRONTSEAT_ACCEPTING_COMMANDS)
288 ifs_->publish(
"FRONTSEAT_READY", 1);
290 ifs_->publish(
"FRONTSEAT_READY", 0);
292 if (status.helm_state() == gpb::HELM_DRIVE)
293 ifs_->publish(
"BACKSEAT_READY", 1);
295 ifs_->publish(
"BACKSEAT_READY", 0);
298 void FrontSeatLegacyTranslator::handle_mail_buoyancy_control(
const CMOOSMsg& msg)
300 if (goby::util::as<bool>(boost::trim_copy(msg.GetString())))
303 command.set_response_requested(
true);
304 command.set_request_id(LEGACY_REQUEST_IDENTIFIER + request_id_++);
306 bluefin_command->set_command(gpb::BluefinExtraCommands::BUOYANCY_ADJUST);
308 publish_command(command);
312 void FrontSeatLegacyTranslator::handle_mail_trim_control(
const CMOOSMsg& msg)
314 if (goby::util::as<bool>(boost::trim_copy(msg.GetString())))
317 command.set_response_requested(
true);
318 command.set_request_id(LEGACY_REQUEST_IDENTIFIER + request_id_++);
320 bluefin_command->set_command(gpb::BluefinExtraCommands::TRIM_ADJUST);
322 publish_command(command);
326 void FrontSeatLegacyTranslator::handle_mail_frontseat_bhvoff(
const CMOOSMsg& msg)
328 if (goby::util::as<bool>(boost::trim_copy(msg.GetString())))
331 command.set_response_requested(
true);
332 command.set_request_id(LEGACY_REQUEST_IDENTIFIER + request_id_++);
334 bluefin_command->set_command(gpb::BluefinExtraCommands::CANCEL_CURRENT_BEHAVIOR);
336 publish_command(command);
340 void FrontSeatLegacyTranslator::handle_mail_frontseat_silent(
const CMOOSMsg& msg)
343 command.set_response_requested(
true);
344 command.set_request_id(LEGACY_REQUEST_IDENTIFIER + request_id_++);
346 bluefin_command->set_command(gpb::BluefinExtraCommands::SILENT_MODE);
348 if (goby::util::as<bool>(boost::trim_copy(msg.GetString())))
349 bluefin_command->set_silent_mode(gpb::BluefinExtraCommands::SILENT);
351 bluefin_command->set_silent_mode(gpb::BluefinExtraCommands::NORMAL);
353 publish_command(command);
356 void FrontSeatLegacyTranslator::handle_mail_backseat_abort(
const CMOOSMsg& msg)
359 command.set_response_requested(
true);
360 command.set_request_id(LEGACY_REQUEST_IDENTIFIER + request_id_++);
362 bluefin_command->set_command(gpb::BluefinExtraCommands::ABORT_MISSION);
364 if (goby::util::as<int>(msg.GetDouble()) == 0)
365 bluefin_command->set_abort_reason(gpb::BluefinExtraCommands::SUCCESSFUL_MISSION);
367 bluefin_command->set_abort_reason(gpb::BluefinExtraCommands::ABORT_WITH_ERRORS);
369 publish_command(command);
374 ifs_->publish_pb(ifs_->cfg_.moos_var().prefix() + ifs_->cfg_.moos_var().command_request(),
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...
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.