Wiselib
wiselib.testing/algorithms/localization/distance_based/modules/position/localization_gpsfree_ncs_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_GPSFREE_NCS_MODULE_H
00020 #define __ALGORITHMS_LOCALIZATION_DISTANCE_BASED_LOCALIZATION_GPSFREE_NCS_MODULE_H
00021 
00022 #include "algorithms/localization/distance_based/modules/localization_module.h"
00023 #include "algorithms/localization/distance_based/modules/position/localization_gpsfree_ncs_messages.h"
00024 #include "algorithms/localization/distance_based/util/localization_defutils.h"
00025 
00026 
00027 namespace wiselib
00028 {
00029 
00031 
00033    template<typename OsModel_P,
00034             typename Radio_P,
00035             typename Clock_P,
00036             typename Debug_P,
00037             typename SharedData_P>
00038    class LocalizationGpsFreeNcsModule
00039       : public LocalizationModule<OsModel_P, Radio_P, SharedData_P>
00040    {
00041    public:
00042       typedef OsModel_P OsModel;
00043       typedef Radio_P Radio;
00044       typedef Clock_P Clock;
00045       typedef Debug_P Debug;
00046       typedef SharedData_P SharedData;
00047 
00048       typedef LocalizationGpsFreeNcsModule<OsModel, Radio, Clock, Debug, SharedData> self_type;
00049       typedef LocalizationModule<OsModel, Radio, SharedData> base_type;
00050 
00051       typedef typename Radio::size_t size_t;
00052       typedef typename Radio::node_id_t node_id_t;
00053       typedef typename Radio::block_data_t block_data_t;
00054 
00055       typedef typename Clock_P::time_t time_t;
00056 
00057       typedef typename SharedData::LocalCoordinateSystem LocalCoordinateSystem;
00058       typedef typename LocalCoordinateSystem::CorrectionData CorrectionData;
00059 
00060       typedef LocalizationGpsFreeNcsLcsMessage<OsModel, Radio, LocalCoordinateSystem> GpsFreeNcsLcsMessage;
00061 
00062       typedef typename SharedData::Neighborhood::NeighborhoodIterator NeighborhoodIterator;
00063 
00067       LocalizationGpsFreeNcsModule();
00069       ~LocalizationGpsFreeNcsModule();
00071 
00072 
00075 
00079       void receive( node_id_t from, size_t len, block_data_t *data );
00086       void work( void );
00088 
00089 
00092 
00095       bool finished( void );
00097 
00098       void rollback( void );
00099       // --------------------------------------------------------------------
00100       void init( Radio& radio, Clock& clock, Debug& debug, SharedData& shared_data )
00101       {
00102          radio_ = &radio;
00103          clock_ = &clock;
00104          debug_ = &debug;
00105          this->set_shared_data( shared_data );
00106       }
00107       // --------------------------------------------------------------------
00108       void set_root_node( bool root )
00109       {
00110          if ( root )
00111          {
00112             root_node_ = true;
00113             this->shared_data().set_anchor( true );
00114          }
00115          else
00116          {
00117             root_node_ = false;
00118             this->shared_data().set_anchor( false );
00119          }
00120       }
00121       // --------------------------------------------------------------------
00122       bool set_root_node( void )
00123       { return root_node_; }
00124 
00125    protected:
00126 
00129 
00131       bool process_gpsfree_ncs_lcs_message( node_id_t from, size_t len, block_data_t *data );
00133 
00134    private:
00135 
00136       enum MessageIds
00137       {
00138          GPSFREE_NCS_LCS_MESSAGE = 208
00139       };
00140 
00141       enum GPSfreeNCSState
00142       {
00143          gfncs_init,
00144          gfncs_wait_lcs,
00145          gfncs_build_ncs,
00146          gfncs_finished
00147       };
00148 
00149       GPSfreeNCSState state_;
00150 
00151       bool root_node_;
00152       bool computed_ncs_;
00153       time_t last_useful_msg_;
00154 
00155       Radio* radio_;
00156       Clock* clock_;
00157       Debug* debug_;
00158    };
00159    // ----------------------------------------------------------------------
00160    // ----------------------------------------------------------------------
00161    // ----------------------------------------------------------------------
00162    template<typename OsModel_P,
00163             typename Radio_P,
00164             typename Clock_P,
00165             typename Debug_P,
00166             typename SharedData_P>
00167    LocalizationGpsFreeNcsModule<OsModel_P, Radio_P, Clock_P, Debug_P, SharedData_P>::
00168    LocalizationGpsFreeNcsModule()
00169       : state_            ( gfncs_init ),
00170          root_node_       ( false ),
00171          computed_ncs_    ( false )
00172    {}
00173    // ----------------------------------------------------------------------
00174    template<typename OsModel_P,
00175             typename Radio_P,
00176             typename Clock_P,
00177             typename Debug_P,
00178             typename SharedData_P>
00179    LocalizationGpsFreeNcsModule<OsModel_P, Radio_P, Clock_P, Debug_P, SharedData_P>::
00180    ~LocalizationGpsFreeNcsModule()
00181    {}
00182    // ----------------------------------------------------------------------
00183    template<typename OsModel_P,
00184             typename Radio_P,
00185             typename Clock_P,
00186             typename Debug_P,
00187             typename SharedData_P>
00188    void
00189    LocalizationGpsFreeNcsModule<OsModel_P, Radio_P, Clock_P, Debug_P, SharedData_P>::
00190    receive( node_id_t from, size_t len, block_data_t *data )
00191    {
00192       switch ( data[0] )
00193       {
00194          case GPSFREE_NCS_LCS_MESSAGE:
00195             process_gpsfree_ncs_lcs_message( from, len, data );
00196             break;
00197       }
00198    }
00199    // ----------------------------------------------------------------------
00200    template<typename OsModel_P,
00201             typename Radio_P,
00202             typename Clock_P,
00203             typename Debug_P,
00204             typename SharedData_P>
00205    void
00206    LocalizationGpsFreeNcsModule<OsModel_P, Radio_P, Clock_P, Debug_P, SharedData_P>::
00207    work( void )
00208    {
00209       if ( state_ == gfncs_finished )
00210          return;
00211 
00212       // send initial message (only root_node)
00213       if ( root_node_ && state_ == gfncs_init )
00214       {
00215          state_ = gfncs_build_ncs;
00216 
00217          CorrectionData cd;
00218 
00219          this->local_coord_sys().correct_lcs_to_real_ncs( cd );
00220          this->local_coord_sys().perform_correction( cd );
00221          this->shared_data().set_position( cd.pos );
00222 
00223          GpsFreeNcsLcsMessage message;
00224          message.set_msg_id( GPSFREE_NCS_LCS_MESSAGE );
00225          message.set_local_coord_sys( &this->local_coord_sys() );
00226          radio_->send( Radio::BROADCAST_ADDRESS,
00227                        message.buffer_size(), (block_data_t*)&message );
00228 
00229          computed_ncs_ = true;
00230          state_ = gfncs_finished;
00231       }
00232       else if ( state_ == gfncs_init )
00233       {
00234          state_ = gfncs_wait_lcs;
00235       }
00236 
00237       // maybe shutdown processor
00238       if ( clock_->time() - last_useful_msg_ > this->shared_data().idle_time() )
00239       {
00240          state_ = gfncs_finished;
00241       }
00242    }
00243    // ----------------------------------------------------------------------
00244    template<typename OsModel_P,
00245             typename Radio_P,
00246             typename Clock_P,
00247             typename Debug_P,
00248             typename SharedData_P>
00249    bool
00250    LocalizationGpsFreeNcsModule<OsModel_P, Radio_P, Clock_P, Debug_P, SharedData_P>::
00251    finished( void )
00252    {
00253       return state_ == gfncs_finished;
00254    }
00255    // ----------------------------------------------------------------------
00256    template<typename OsModel_P,
00257             typename Radio_P,
00258             typename Clock_P,
00259             typename Debug_P,
00260             typename SharedData_P>
00261    void
00262    LocalizationGpsFreeNcsModule<OsModel_P, Radio_P, Clock_P, Debug_P, SharedData_P>::
00263    rollback( void )
00264    {
00265       state_ = gfncs_init;
00266       root_node_ = false;
00267       computed_ncs_ = false;
00268       last_useful_msg_ = clock_->time();
00269    }
00270    // ----------------------------------------------------------------------
00271    template<typename OsModel_P,
00272             typename Radio_P,
00273             typename Clock_P,
00274             typename Debug_P,
00275             typename SharedData_P>
00276    bool
00277    LocalizationGpsFreeNcsModule<OsModel_P, Radio_P, Clock_P, Debug_P, SharedData_P>::
00278    process_gpsfree_ncs_lcs_message( node_id_t from, size_t len, block_data_t* data )
00279    {
00280       if ( computed_ncs_ )
00281          return true;
00282 
00283       GpsFreeNcsLcsMessage* msg = (GpsFreeNcsLcsMessage*)data;
00284       last_useful_msg_ = clock_->time();
00285 
00286       CorrectionData cd;
00287       // compute NCS
00288       if ( this->local_coord_sys().correct_lcs( *msg->local_coord_sys(), cd ) )
00289       {
00290          this->local_coord_sys().perform_correction( cd );
00291          this->shared_data().set_position( cd.pos );
00292 
00293          GpsFreeNcsLcsMessage message;
00294          message.set_msg_id( GPSFREE_NCS_LCS_MESSAGE );
00295          message.set_local_coord_sys( &this->local_coord_sys() );
00296          radio_->send( Radio::BROADCAST_ADDRESS,
00297                        message.buffer_size(), (block_data_t*)&message );
00298       }
00299       else
00300       {
00301          Vec pos = msg->local_coord_sys()->node_position( radio_->id() );
00302          if ( pos != UNKNOWN_POSITION )
00303             this->shared_data().set_position( pos );
00304       }
00305 
00306       computed_ncs_ = true;
00307       state_ = gfncs_finished;
00308 
00309       return true;
00310    }
00311 
00312 }// namespace wiselib
00313 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines