22 #include "goby/common/logger.h" 23 #include "goby/util/as.h" 25 #include "abc_frontseat_driver.h" 33 const int allowed_skew = 10;
46 tcp_(abc_config_.tcp_address(), abc_config_.tcp_port()), frontseat_providing_data_(false),
47 last_frontseat_data_time_(0), frontseat_state_(
gpb::FRONTSEAT_NOT_CONNECTED)
53 int timeout = 10, i = 0;
54 while (!tcp_.active() && i < timeout)
61 void AbcFrontSeat::loop()
63 check_connection_state();
68 if (goby_time<double>() > last_frontseat_data_time_ + allowed_skew)
69 frontseat_providing_data_ =
false;
72 void AbcFrontSeat::check_connection_state()
78 glog.is(DIE) &&
glog <<
"Connection to FrontSeat failed: " << abc_config_.tcp_address()
79 <<
":" << abc_config_.tcp_port() << std::endl;
84 if (frontseat_state_ == gpb::FRONTSEAT_NOT_CONNECTED)
86 glog.is(VERBOSE) &&
glog <<
"Connected to ABC Simulator." << std::endl;
87 frontseat_state_ = gpb::FRONTSEAT_IDLE;
88 std::stringstream start_ss;
90 <<
"LAT:" << abc_config_.start().lat() <<
"," 91 <<
"LON:" << abc_config_.start().lon() <<
"," 92 <<
"DURATION:" << abc_config_.start().duration();
93 write(start_ss.str());
98 void AbcFrontSeat::try_receive()
101 while (tcp_.readline(&in))
108 catch (std::exception& e)
110 glog.is(DEBUG1) &&
glog << warn <<
"Failed to handle message: " << e.what()
116 void AbcFrontSeat::process_receive(
const std::string& s)
120 signal_raw_from_frontseat(raw_msg);
122 std::map<std::string, std::string> parsed;
123 parse_in(s, &parsed);
126 if (parsed[
"KEY"] ==
"CTRL")
128 if (parsed[
"STATE"] ==
"PAYLOAD")
129 frontseat_state_ = gpb::FRONTSEAT_ACCEPTING_COMMANDS;
130 else if (parsed[
"STATE"] ==
"AUV")
131 frontseat_state_ = gpb::FRONTSEAT_IN_CONTROL;
133 frontseat_state_ = gpb::FRONTSEAT_IDLE;
136 else if (parsed[
"KEY"] ==
"NAV")
141 glog.is(VERBOSE) &&
glog <<
"Got NAV update: " << s << std::endl;
142 status.mutable_pose()->set_heading(goby::util::as<double>(parsed[
"HEADING"]));
143 status.set_speed(goby::util::as<double>(parsed[
"SPEED"]));
144 status.mutable_global_fix()->set_depth(goby::util::as<double>(parsed[
"DEPTH"]));
145 status.mutable_global_fix()->set_lon(goby::util::as<double>(parsed[
"LON"]));
146 status.mutable_global_fix()->set_lat(goby::util::as<double>(parsed[
"LAT"]));
149 compute_missing(&status);
151 signal_data_from_frontseat(data);
153 frontseat_providing_data_ =
true;
154 last_frontseat_data_time_ = goby_time<double>();
157 else if (parsed[
"KEY"] ==
"CMD")
159 if (last_request_.response_requested())
162 response.set_request_successful(parsed[
"RESULT"] ==
"OK");
163 response.set_request_id(last_request_.request_id());
164 signal_command_response(response);
169 glog.is(VERBOSE) &&
glog <<
"Unknown message from frontseat: " << s << std::endl;
175 if (command.has_desired_course())
177 std::stringstream cmd_ss;
180 <<
"HEADING:" << desired_course.heading() <<
"," 181 <<
"SPEED:" << desired_course.speed() <<
"," 182 <<
"DEPTH:" << desired_course.depth();
185 last_request_ = command;
189 glog.is(VERBOSE) &&
glog <<
"Unhandled command: " << command.ShortDebugString()
205 bool AbcFrontSeat::frontseat_providing_data()
const 207 return frontseat_providing_data_;
210 goby::moos::protobuf::FrontSeatState AbcFrontSeat::frontseat_state()
const 212 return frontseat_state_;
215 void AbcFrontSeat::write(
const std::string& s)
219 signal_raw_to_frontseat(raw_msg);
221 tcp_.write(s +
"\r\n");
228 void AbcFrontSeat::parse_in(
const std::string& in, std::map<std::string, std::string>* out)
230 std::vector<std::string> comma_split;
231 boost::split(comma_split, in, boost::is_any_of(
","));
232 out->insert(std::make_pair(
"KEY", comma_split.at(0)));
233 for (
int i = 1, n = comma_split.size(); i < n; ++i)
235 std::vector<std::string> colon_split;
236 boost::split(colon_split, comma_split[i], boost::is_any_of(
":"));
237 out->insert(std::make_pair(colon_split.at(0), colon_split.at(1)));
Contains functions for adding color to Terminal window streams.
ReturnType goby_time()
Returns current UTC time as a boost::posix_time::ptime.
common::FlexOstream glog
Access the Goby logger through this object.