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
acomms/libdccl/message_var_float.h
00001 // copyright 2008, 2009 t. schneider tes@mit.edu
00002 // 
00003 // this file is part of the Dynamic Compact Control Language (DCCL),
00004 // the goby-acomms codec. goby-acomms is a collection of libraries 
00005 // for acoustic underwater networking
00006 //
00007 // This program is free software: you can redistribute it and/or modify
00008 // it under the terms of the GNU General Public License as published by
00009 // the Free Software Foundation, either version 3 of the License, or
00010 // (at your option) any later version.
00011 //
00012 // This software is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with this software.  If not, see <http://www.gnu.org/licenses/>.
00019 
00020 #ifndef MESSAGE_VAR_FLOAT20100317H
00021 #define MESSAGE_VAR_FLOAT20100317H
00022 
00023 #include "goby/util/string.h"
00024 
00025 #include "message_var.h"
00026 
00027 #include <boost/math/special_functions/fpclassify.hpp>
00028 
00029 namespace goby
00030 {
00031     namespace acomms
00032     {   
00033         class DCCLMessageVarFloat : public DCCLMessageVar
00034         {
00035           public:
00036             DCCLMessageVarFloat(double max = std::numeric_limits<double>::max(), double min = 0, double precision = 0);
00037 
00038             virtual int calc_size() const
00039             { return is_delta() ? delta_size() : key_size(); }
00040 
00041             virtual int calc_total_size() const;
00042         
00043             void set_max(double max) {max_ = max;}
00044             void set_max(const std::string& s) { set_max(util::as<double>(s)); }
00045         
00046             void set_min(double min) {min_ = min;}
00047             void set_min(const std::string& s) { set_min(util::as<double>(s)); }
00048 
00049             void set_precision(int precision) {precision_ = precision;}
00050             void set_precision(const std::string& s) { set_precision(util::as<int>(s)); }
00051         
00052             int precision() const { return precision_; }  
00053 
00054             double min() const { return min_; }
00055             double max() const { return max_; }
00056         
00057             void set_max_delta(double max_delta)
00058             { max_delta_ = max_delta; }
00059             void set_max_delta(const std::string& s)
00060             { set_max_delta(util::as<double>(s)); }
00061 
00062             virtual DCCLType type() const { return dccl_float; }
00063 
00064             unsigned key_size() const
00065             { return ceil(log((max_-min_)*pow(10.0,static_cast<double>(precision_))+2)/log(2)); }
00066 
00067             unsigned delta_size() const
00068             { return ceil(log((2*max_delta_)*pow(10.0,static_cast<double>(precision_))+2)/log(2)); }
00069 
00070           protected:
00071             virtual DCCLMessageVal cast(double d, int precision) { return DCCLMessageVal(d, precision); }
00072             virtual void initialize_specific();
00073 
00074             void pre_encode(DCCLMessageVal& v);            
00075             
00076           private:
00077             bool is_delta() const
00078             { return using_delta_differencing() && !is_key_frame_; }
00079         
00080             bool using_delta_differencing() const
00081             { return !(boost::math::isnan)(max_delta_); }
00082         
00083             boost::dynamic_bitset<unsigned char> encode_specific(const DCCLMessageVal& v);
00084             DCCLMessageVal decode_specific(boost::dynamic_bitset<unsigned char>& b);
00085 
00086             void get_display_specific(std::stringstream& ss) const;
00087         
00088           private:
00089             double max_;
00090             double min_;
00091             int precision_;
00092 
00093             double max_delta_;
00094         };
00095 
00096     }
00097 }
00098 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends