Goby Underwater Autonomy Project
Series: 1.1, revision: 163, released on 2013-02-06 14:23:27 -0500
|
00001 // t. schneider tes@mit.edu 02.19.09 00002 // ocean engineering graduate student - mit / whoi joint program 00003 // massachusetts institute of technology (mit) 00004 // laboratory for autonomous marine sensing systems (lamss) 00005 // 00006 // this is iCommander.cpp 00007 // 00008 // see the readme file within this directory for information 00009 // pertaining to usage and purpose of this script. 00010 // 00011 // This program is free software: you can redistribute it and/or modify 00012 // it under the terms of the GNU General Public License as published by 00013 // the Free Software Foundation, either version 3 of the License, or 00014 // (at your option) any later version. 00015 // 00016 // This software is distributed in the hope that it will be useful, 00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 // GNU General Public License for more details. 00020 // 00021 // You should have received a copy of the GNU General Public License 00022 // along with this software. If not, see <http://www.gnu.org/licenses/>. 00023 00024 #include "iCommander.h" 00025 #include "goby/moos/libmoos_util/tes_moos_app.h" 00026 #include "goby/protobuf/modem_message.pb.h" 00027 00028 using namespace std; 00029 using boost::trim_copy; 00030 using namespace goby::acomms; 00031 using goby::util::glogger; 00032 00033 iCommanderConfig CiCommander::cfg_; 00034 CommanderCdk CiCommander::gui_; 00035 CMOOSGeodesy CiCommander::geodesy_; 00036 tes::ModemIdConvert CiCommander::modem_lookup_; 00037 goby::acomms::DCCLCodec CiCommander::dccl_; 00038 boost::mutex CiCommander::gui_mutex_; 00039 CiCommander* CiCommander::inst_ = 0; 00040 00041 00042 CiCommander* CiCommander::get_instance() 00043 { 00044 if(!inst_) 00045 inst_ = new CiCommander(); 00046 return inst_; 00047 } 00048 00049 // Construction / Destruction 00050 CiCommander::CiCommander() 00051 : TesMoosApp(&cfg_), 00052 command_gui_thread_(boost::bind(&CommandGui::run, &command_gui_)), 00053 start_time_(MOOSTime()), 00054 is_started_up_(false) 00055 { 00056 00057 if (!cfg_.common().has_lat_origin() || !cfg_.common().has_lon_origin()) 00058 { 00059 glogger() << die << "no lat_origin or lon_origin specified in configuration. this is required for geodesic conversion" << std::endl; 00060 } 00061 else 00062 { 00063 if(geodesy_.Initialise(cfg_.common().lat_origin(), cfg_.common().lon_origin())) 00064 glogger() << "success!" << std::endl; 00065 else 00066 glogger() << die << "could not initialize geodesy" << std::endl; 00067 } 00068 00069 if(cfg_.has_modem_id_lookup_path()) 00070 glogger() << modem_lookup_.read_lookup_file(cfg_.modem_id_lookup_path()) << std::endl; 00071 else 00072 glogger() << warn << "no modem_id_lookup_path in moos file. this is required for conversions between modem_id and vehicle name / type." << std::endl; 00073 00074 dccl_.merge_cfg(cfg_.dccl_cfg()); 00075 00076 00077 for(int i = 0, n = cfg_.show_variable_size(); i < n; ++i) 00078 { 00079 show_vars_[cfg_.show_variable(i)] = ""; 00080 subscribe(cfg_.show_variable(i), &CiCommander::inbox, this); 00081 } 00082 00083 00084 subscribe("ACOMMS_ACK", &CiCommander::inbox, this); 00085 subscribe("ACOMMS_EXPIRE", &CiCommander::inbox, this); 00086 } 00087 00088 CiCommander::~CiCommander() 00089 { } 00090 00091 // OnNewMail: called when new mail (previously registered for) 00092 // has arrived. 00093 void CiCommander::inbox(const CMOOSMsg& msg) 00094 { 00095 string key = msg.GetKey(); 00096 bool is_dbl = msg.IsDouble(); 00097 // bool is_str = p->IsString(); 00098 00099 double dval = msg.GetDouble(); 00100 string sval = msg.GetString(); 00101 00102 // uncomment as needed 00103 // double msg_time = msg.GetTime(); 00104 // string msg_src = msg.GetSource(); 00105 // string msg_community = msg.GetCommunity(); 00106 00107 if(msg.GetTime() < start_time_) 00108 { 00109 std::cerr << "ignoring normal mail from " << msg.GetKey() << " from before we started" << std::endl; 00110 } 00111 else if(MOOSStrCmp("ACOMMS_ACK", key)) 00112 { 00113 goby::acomms::protobuf::ModemDataAck ack_msg; 00114 parse_for_moos(sval, &ack_msg); 00115 00116 try 00117 { 00118 vector<string> mesg; 00119 DCCLHeaderDecoder head_decoder(ack_msg.orig_msg().data()); 00120 mesg.push_back(string("</B>Message </40>acknowledged<!40> from queue: " + dccl_.id2name(ack_msg.orig_msg().queue_key().id()))); 00121 mesg.push_back(string(" for destination: " + modem_lookup_.get_name_from_id(ack_msg.orig_msg().base().dest()))); 00122 mesg.push_back(string(" at time: " + ack_msg.orig_msg().base().time())); 00123 gui_.disp_info(mesg); 00124 } 00125 catch(...) 00126 { } 00127 00128 00129 } 00130 else if(MOOSStrCmp("ACOMMS_EXPIRE", key)) 00131 { 00132 goby::acomms::protobuf::ModemDataExpire expire_msg; 00133 parse_for_moos(sval, &expire_msg); 00134 00135 try 00136 { 00137 vector<string> mesg; 00138 DCCLHeaderDecoder head_decoder(expire_msg.orig_msg().data()); 00139 mesg.push_back(string("</B>Message </16>expired<!16> from queue: " + dccl_.id2name(expire_msg.orig_msg().queue_key().id()))); 00140 mesg.push_back(string(" for destination: " + modem_lookup_.get_name_from_id(expire_msg.orig_msg().base().dest()))); 00141 mesg.push_back(string(" at time: " + expire_msg.orig_msg().base().time())); 00142 gui_.disp_info(mesg); 00143 } 00144 catch(...) 00145 { } 00146 00147 } 00148 else 00149 { 00150 map<string, string>::iterator it = show_vars_.find(key); 00151 if(it != show_vars_.end()) 00152 { 00153 if(is_dbl) 00154 show_vars_[key] = goby::util::as<string>(dval); 00155 else 00156 show_vars_[key] = sval; 00157 } 00158 vector<string> mesg; 00159 for(map<string, string>::iterator it = show_vars_.begin(), n = show_vars_.end(); it!=n; ++it) 00160 mesg.push_back(string(it->first + ": " + it->second)); 00161 //gui_.disp_lower_info(mesg); 00162 } 00163 } 00164 00165 00166 void CiCommander::loop() 00167 { 00168 if(!is_started_up_) 00169 { 00170 command_gui_.initialize(); 00171 gui_.set_lower_box_size(show_vars_.size()+2); 00172 is_started_up_ = true; 00173 00174 } 00175 }