Wiselib
wiselib.testing/algorithms/localization/distance_based/modules/position/localization_lateration_module.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  ** This file is part of the generic algorithm library Wiselib.           **
00003  ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project.      **
00004  **                                                                       **
00005  ** The Wiselib is free software: you can redistribute it and/or modify   **
00006  ** it under the terms of the GNU Lesser General Public License as        **
00007  ** published by the Free Software Foundation, either version 3 of the    **
00008  ** License, or (at your option) any later version.                       **
00009  **                                                                       **
00010  ** The Wiselib is distributed in the hope that it will be useful,        **
00011  ** but WITHOUT ANY WARRANTY; without even the implied warranty of        **
00012  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         **
00013  ** GNU Lesser General Public License for more details.                   **
00014  **                                                                       **
00015  ** You should have received a copy of the GNU Lesser General Public      **
00016  ** License along with the Wiselib.                                       **
00017  ** If not, see <http://www.gnu.org/licenses/>.                           **
00018  ***************************************************************************/
00019 #ifndef __ALGORITHMS_LOCALIZATION_DISTANCE_BASED_LOCALIZATION_LATERATION_MODULE_H
00020 #define __ALGORITHMS_LOCALIZATION_DISTANCE_BASED_LOCALIZATION_LATERATION_MODULE_H
00021 
00022 #include "config_testing.h"
00023 #include "algorithms/localization/distance_based/modules/localization_module.h"
00024 #include "algorithms/localization/distance_based/util/localization_defutils.h"
00025 #include "algorithms/localization/distance_based/math/localization_triangulation.h"
00026 #include "algorithms/localization/distance_based/math/vec.h"
00027 
00028 namespace wiselib
00029 {
00030 
00032 
00034    template<typename OsModel_P,
00035             typename Radio_P,
00036             typename Debug_P,
00037             typename SharedData_P,
00038             typename Arithmatic_P >
00039    class LocalizationLaterationModule
00040       : public LocalizationModule<OsModel_P, Radio_P, SharedData_P>
00041    {
00042 
00043    public:
00044       typedef OsModel_P OsModel;
00045       typedef Radio_P Radio;
00046       typedef Debug_P Debug;
00047       typedef SharedData_P SharedData;
00048       typedef Arithmatic_P Arithmatic;
00049 
00050 
00051       typedef LocalizationLaterationModule<OsModel, Radio, Debug, SharedData, Arithmatic> self_type;
00052       typedef LocalizationModule<OsModel, Radio, SharedData> base_type;
00053 
00054       typedef typename Radio::size_t size_t;
00055       typedef typename Radio::node_id_t node_id_t;
00056       typedef typename Radio::block_data_t block_data_t;
00057 
00058       typedef typename SharedData::Neighborhood Neighborhood;
00059       typedef typename SharedData::NeighborInfoList NeighborInfoList;
00060 
00064       LocalizationLaterationModule();
00066       ~LocalizationLaterationModule();
00068 
00071 
00073       void receive( node_id_t from, size_t len, block_data_t *data )
00074       {}
00077       void work( void );
00079 
00082 
00084       bool finished( void );
00086       void rollback(void );
00087 
00088       void init( Radio& radio, Debug& debug, SharedData& shared_data ) {
00089          radio_ = &radio;
00090 
00091          debug_ = &debug;
00092          //shared_data_ = &shared_data;
00093          this->shared_data().is_anchor();
00094       }
00095 
00096 
00097    private:
00098 
00099       enum LaterationState
00100       {
00101          lat_wait,
00102          lat_work,
00103          lat_finished
00104       };
00105 
00106       LaterationState state_;
00107       Radio* radio_;
00108 
00109       Debug* debug_;
00110       //SharedData* shared_data_;
00111 
00112    };
00113    // ----------------------------------------------------------------------
00114    // ----------------------------------------------------------------------
00115    // ----------------------------------------------------------------------
00116    template<typename OsModel_P,
00117             typename Radio_P,
00118             typename Debug_P,
00119             typename SharedData_P,
00120             typename Arithmatic_P>
00121    LocalizationLaterationModule<OsModel_P, Radio_P, Debug_P, SharedData_P,Arithmatic_P>::
00122    LocalizationLaterationModule()
00123       : state_( lat_wait )
00124    {}
00125    // ----------------------------------------------------------------------
00126    template<typename OsModel_P,
00127             typename Radio_P,
00128             typename Debug_P,
00129             typename SharedData_P,
00130             typename Arithmatic_P>
00131    LocalizationLaterationModule<OsModel_P, Radio_P, Debug_P, SharedData_P,Arithmatic_P>::
00132    ~LocalizationLaterationModule()
00133    {}
00134    // ----------------------------------------------------------------------
00135    template<typename OsModel_P,
00136             typename Radio_P,
00137             typename Debug_P,
00138             typename SharedData_P,
00139             typename Arithmatic_P>
00140    void
00141    LocalizationLaterationModule<OsModel_P, Radio_P, Debug_P, SharedData_P,Arithmatic_P>::
00142    work( void )
00143    {
00144 
00145 
00146       // if anchor, do not change position
00147       if ( this->shared_data().is_anchor() )
00148          state_ = lat_finished;
00149 
00150       if ( state_ == lat_finished )
00151          return;
00152 
00153       if ( state_ == lat_wait )
00154          state_ = lat_work;
00155 
00156       Vec<Arithmatic> est_pos;
00157       NeighborInfoList neighbors;
00158 
00159       collect_neighbors<OsModel, Neighborhood, NeighborInfoList, Arithmatic>( this->neighborhood(), lat_anchors, neighbors );
00160       if ( neighbors.empty() )
00161       {
00162 //#ifdef LOCALIZATION_DISTANCEBASED_LATERATION_DEBUG
00163         if(radio_->id()==0x1c72)
00164           debug_->debug(  "LocalizationLaterationModule: No neighbors found\n" );
00165 //#endif
00166           return;
00167       }
00168 //#ifdef LOCALIZATION_DISTANCEBASED_LATERATION_DEBUG
00169      if(radio_->id()==0x1c72)
00170         debug_->debug(  "LocalizationLaterationModule: %d neigbous found\n",neighbors.size() );
00171 
00172 //#endif
00173 
00174 
00175       // Execute lateration two times. First, to simply get the
00176       // estimated position. Second, use the estimated position
00177       // in the lateration phase as new parameter in least squares
00178       // approach. Particularly if you know only 3 anchors, the new
00179       // parameter in the second pass is very useful.
00180       if ( est_pos_lateration<OsModel, NeighborInfoList, Arithmatic>( neighbors, est_pos, lat_anchors, false ) &&
00181            est_pos_lateration<OsModel, NeighborInfoList, Arithmatic>( neighbors, est_pos, lat_anchors, true ) )
00182       {
00183          if ( !this->shared_data().check_residue() ||
00184                check_residue<OsModel, NeighborInfoList, Arithmatic>( neighbors, est_pos, lat_anchors,
00185                               this->shared_data().communication_range() ) )
00186             this->shared_data().set_position( est_pos );
00187       }
00188 
00189       #ifdef LOCALIZATION_DISTANCEBASED_LATERATION_DEBUG
00190            if(radio_->id()==0x9999)
00191               debug_->debug(  "LocalizationLaterationModule:after caluculting ");
00192 
00193       #endif
00194 
00195       state_ = lat_finished;
00196    }
00197    // ----------------------------------------------------------------------
00198    template<typename OsModel_P,
00199             typename Radio_P,
00200             typename Debug_P,
00201             typename SharedData_P,
00202             typename Arithmatic_P>
00203    bool
00204    LocalizationLaterationModule<OsModel_P, Radio_P, Debug_P, SharedData_P,Arithmatic_P>::
00205    finished( void )
00206    {
00207       return state_ == lat_finished;
00208    }
00209    // ----------------------------------------------------------------------
00210    template<typename OsModel_P,
00211             typename Radio_P,
00212             typename Debug_P,
00213             typename SharedData_P,
00214             typename Arithmatic_P>
00215    void
00216    LocalizationLaterationModule<OsModel_P, Radio_P, Debug_P, SharedData_P,Arithmatic_P>::
00217    rollback(void)
00218    {
00219       state_ = lat_wait;
00220      // shared_data_->reset_neighborhood_();
00221    }
00222 
00223 }// namespace wiselib
00224 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines