Note: Goby version 1 (shown here) is now considered obsolete. Please use version 2 for new projects, and consider upgrading old projects.

Goby Underwater Autonomy Project  Series: 1.1, revision: 163, released on 2013-02-06 14:23:27 -0500
moos/pAcommsHandler/pAcommsHandler.h
00001 // t. schneider tes@mit.edu 06.05.08
00002 // ocean engineering graudate student - mit / whoi joint program
00003 // massachusetts institute of technology (mit)
00004 // laboratory for autonomous marine sensing systems (lamss)
00005 // 
00006 // this is pAcommsHandler.h, part of pAcommsHandler
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 #ifndef pAcommsHandlerH
00025 #define pAcommsHandlerH
00026 
00027 #include <iostream>
00028 #include <fstream>
00029 
00030 #include <boost/date_time/posix_time/posix_time.hpp>
00031 
00032 #include "MOOSLIB/MOOSApp.h"
00033 
00034 #include "goby/acomms.h"
00035 #include "goby/util.h"
00036 
00037 #include "MOOSLIB/MOOSLib.h"
00038 #include "MOOSUtilityLib/MOOSGeodesy.h"
00039 
00040 #include "goby/moos/libmoos_util/dynamic_moos_vars.h"
00041 #include "goby/moos/libmoos_util/modem_id_convert.h"
00042 #include "goby/moos/libmoos_util/tes_moos_app.h"
00043 
00044 #include "pAcommsHandler_config.pb.h"
00045 
00046 
00047 namespace goby {
00048     namespace acomms {
00049         namespace protobuf {
00050             class ModemDataTransmission;
00051         }
00052     } 
00053 }
00054 
00055 // data
00056 const std::string MOOS_VAR_INCOMING_DATA = "ACOMMS_INCOMING_DATA";
00057 const std::string MOOS_VAR_OUTGOING_DATA = "ACOMMS_OUTGOING_DATA";
00058 
00059 const bool DEFAULT_NO_ENCODE = true;
00060 const bool DEFAULT_ENCODE = false;
00061 
00062 const bool DEFAULT_NO_DECODE = true;
00063 const bool DEFAULT_DECODE = false;
00064 
00065 const unsigned DEFAULT_TCP_SHARE_PORT = 11000;
00066 
00067 // largest allowed moos packet
00068 // something like 40000 (see PKT_TMP_BUFFER_SIZE in MOOSCommPkt.cpp)
00069 // but that uses some space for serialization characters
00070 const unsigned MAX_MOOS_PACKET = 35000; 
00071 
00072 // serial feed
00073 const std::string MOOS_VAR_NMEA_OUT = "ACOMMS_NMEA_OUT";
00074 const std::string MOOS_VAR_NMEA_IN = "ACOMMS_NMEA_IN";
00075 
00076 // ranging responses
00077 const std::string MOOS_VAR_RANGING = "ACOMMS_RANGE_RESPONSE";
00078 const std::string MOOS_VAR_COMMAND_RANGING = "ACOMMS_RANGE_COMMAND";
00079 
00080 // acoustic acknowledgments get written here
00081 const std::string MOOS_VAR_ACK = "ACOMMS_ACK";
00082 
00083 // expired messages (ttl ends)
00084 const std::string MOOS_VAR_EXPIRE = "ACOMMS_EXPIRE";
00085 
00086 const std::string MOOS_VAR_QSIZE = "ACOMMS_QSIZE";
00087 
00088 
00089 // communications quality statistics
00090 // const std::string MOOS_VAR_STATS = "ACOMMS_STATS";
00091 
00092 const std::string MOOS_VAR_CYCLE_UPDATE = "ACOMMS_MAC_CYCLE_UPDATE"; // preferred
00093 const std::string MOOS_VAR_POLLER_UPDATE = "ACOMMS_POLLER_UPDATE"; // legacy
00094 
00095 const std::string MOOS_VAR_FLUSH_QUEUE = "ACOMMS_FLUSH_QUEUE"; 
00096 
00097 
00098 
00099 struct IP
00100 {
00101 IP(const std::string& ip = "", unsigned port = DEFAULT_TCP_SHARE_PORT)
00102 : ip(ip),
00103         port(port)
00104         { }
00105 
00106     std::string ip_and_port() const
00107         {
00108             std::stringstream ss;
00109             ss << ip << ":" << port;
00110             return ss.str();
00111         }
00112     
00113     std::string ip;
00114     unsigned port;
00115 };
00116     
00117 
00118 
00119 class CpAcommsHandler : public TesMoosApp
00120 {
00121   public:
00122     static CpAcommsHandler* get_instance();
00123     
00124   private:
00125     CpAcommsHandler();
00126     ~CpAcommsHandler();
00127     void loop();     // from TesMoosApp
00128     void do_subscriptions();
00129     void process_configuration();
00130 
00131     // read the internal driver part of the .moos file
00132     // void read_driver_parameters(CProcessConfigReader& config);
00133     // read the internal mac part of the .moos file
00134     // void read_internal_mac_parameters(CProcessConfigReader& config);
00135     // read the message queueing (XML / send, receive = ) part of the .moos file
00136     // void read_queue_parameters(CProcessConfigReader& config);
00137     
00138     // from QueueManager
00139     void queue_incoming_data(const goby::acomms::protobuf::ModemDataTransmission& message);
00140     void queue_ack(const goby::acomms::protobuf::ModemDataAck & message);
00141     void queue_on_demand(const goby::acomms::protobuf::ModemDataRequest& request_msg,
00142                          goby::acomms::protobuf::ModemDataTransmission* data_msg);
00143     void queue_expire(const goby::acomms::protobuf::ModemDataExpire& message);
00144     void queue_qsize(const goby::acomms::protobuf::QueueSize& size);
00145     
00146     void handle_mac_cycle_update(const CMOOSMsg& msg);
00147     void handle_ranging_request(const CMOOSMsg& msg);
00148     void handle_message_push(const CMOOSMsg& msg);
00149     void handle_flush_queue(const CMOOSMsg& msg);
00150     
00151     // from MMDriver
00152     // publish raw NMEA stream from the modem ($CA)
00153     void modem_raw_in(const goby::acomms::protobuf::ModemMsgBase& base_msg);
00154     // publish raw NMEA stream to the modem ($CC)
00155     void modem_raw_out(const goby::acomms::protobuf::ModemMsgBase& base_msg);
00156     // write ping (ranging) responses
00157     void modem_range_reply(const goby::acomms::protobuf::ModemRangingReply& message);
00158 
00159  
00160     void dccl_inbox(const CMOOSMsg& msg);
00161     void dccl_loop();
00162 
00163     void pack(unsigned dccl_id, goby::acomms::protobuf::ModemDataTransmission* modem_message);
00164     void unpack(goby::acomms::protobuf::ModemDataTransmission modem_message);
00165 
00166     void handle_tcp_share(goby::acomms::protobuf::ModemDataTransmission* modem_message);
00167     
00168     void alg_power_to_dB(goby::acomms::DCCLMessageVal& val_to_mod);
00169     void alg_dB_to_power(goby::acomms::DCCLMessageVal& val_to_mod);
00170 
00171     void alg_to_upper(goby::acomms::DCCLMessageVal& val_to_mod);
00172     void alg_to_lower(goby::acomms::DCCLMessageVal& val_to_mod);
00173     void alg_angle_0_360(goby::acomms::DCCLMessageVal& angle);
00174     void alg_angle_n180_180(goby::acomms::DCCLMessageVal& angle);
00175 
00176     void alg_TSD_to_soundspeed(goby::acomms::DCCLMessageVal& val,
00177                                const std::vector<goby::acomms::DCCLMessageVal>& ref_vals);
00178     
00179 
00180     void alg_add(goby::acomms::DCCLMessageVal& val,
00181                  const std::vector<goby::acomms::DCCLMessageVal>& ref_vals);
00182     
00183     void alg_subtract(goby::acomms::DCCLMessageVal& val,
00184                       const std::vector<goby::acomms::DCCLMessageVal>& ref_vals);
00185 
00186     void alg_abs(goby::acomms::DCCLMessageVal& val_to_mod);
00187     
00188     void alg_lat2utm_y(goby::acomms::DCCLMessageVal& val,
00189                        const std::vector<goby::acomms::DCCLMessageVal>& ref_vals);
00190 
00191     void alg_lon2utm_x(goby::acomms::DCCLMessageVal& val,
00192                        const std::vector<goby::acomms::DCCLMessageVal>& ref_vals);
00193 
00194     void alg_utm_x2lon(goby::acomms::DCCLMessageVal& val,
00195                        const std::vector<goby::acomms::DCCLMessageVal>& ref_vals);
00196     
00197     void alg_utm_y2lat(goby::acomms::DCCLMessageVal& val,
00198                        const std::vector<goby::acomms::DCCLMessageVal>& ref_vals);
00199 
00200     void alg_modem_id2name(goby::acomms::DCCLMessageVal& in);
00201     void alg_modem_id2type(goby::acomms::DCCLMessageVal& in);
00202     void alg_name2modem_id(goby::acomms::DCCLMessageVal& in);
00203 
00204     void alg_lat2hemisphere_initial(goby::acomms::DCCLMessageVal& val_to_mod);
00205     void alg_lon2hemisphere_initial(goby::acomms::DCCLMessageVal& val_to_mod);
00206     
00207     void alg_unix_time2nmea_time(goby::acomms::DCCLMessageVal& val_to_mod);
00208 
00209     void alg_lat2nmea_lat(goby::acomms::DCCLMessageVal& val_to_mod);
00210     void alg_lon2nmea_lon(goby::acomms::DCCLMessageVal& val_to_mod);    
00211     
00212   private:
00213 
00214     // ours ($CCCFG,SRC,modem_id_)
00215     int modem_id_;    
00216 
00217     static pAcommsHandlerConfig cfg_;
00218     
00219     //DCCL parsing
00220     goby::acomms::DCCLCodec dccl_;
00221 
00222     // manages queues and does additional packing
00223     goby::acomms::QueueManager queue_manager_;
00224     
00225     // driver class that interfaces to the modem
00226     goby::acomms::ModemDriverBase* driver_;
00227 
00228     // MAC
00229     goby::acomms::MACManager mac_;
00230 
00231     std::map<std::string, goby::acomms::protobuf::QueueKey> out_moos_var2queue_;
00232     std::map<goby::acomms::protobuf::QueueKey, std::string> in_queue2moos_var_;
00233     
00234     CMOOSGeodesy geodesy_;
00235     
00236     tes::ModemIdConvert modem_lookup_;
00237 
00238     // buffer for <repeat> messages
00239     // maps message <id> onto pubsub encoding map
00240     std::map<unsigned, std::map<std::string, std::vector<goby::acomms::DCCLMessageVal> > > repeat_buffer_;
00241     std::map<unsigned, unsigned> repeat_count_;
00242 
00243     std::map<IP, goby::util::TCPClient*> tcp_share_map_;
00244     goby::util::TCPServer* tcp_share_server_;
00245 
00246     static CpAcommsHandler* inst_;    
00247 };
00248 
00249 inline bool operator<(const IP& a, const IP& b)
00250 { return a.ip < b.ip; }
00251 
00252 
00253 #endif 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends