Wiselib
wiselib.testing/external_interface/isense/isense_distance.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 __INTERNAL_INTERFACE_ISENSE_DISTANCE__
00020 #define __INTERNAL_INTERFACE_ISENSE_DISTANCE__
00021 
00022 #include "internal_interface/routing_table/routing_table_static_array.h"
00023 #include "external_interface/isense/isense_os.h"
00024 
00025 #include "external_interface/isense/isense_types.h"
00026 //#include "util/delegates/delegate.hpp"
00027 //#include <isense/os.h>
00028 //#include <isense/radio.h>
00029 //#include <isense/dispatcher.h>
00030 
00031 namespace wiselib
00032 {
00033 
00034    template<typename OsModel_P,
00035             typename Radio_P = typename OsModel_P::TxRadio,
00036             int TABLE_SIZE = 20>
00037    class iSenseDistanceModel
00038    {
00039    public:
00040       typedef OsModel_P OsModel;
00041 
00042       typedef isense::Os iSenseOs;
00043 
00044       typedef Radio_P Radio;
00045       typedef typename Radio::node_id_t node_id_t;
00046       typedef typename Radio::block_data_t block_data_t;
00047       typedef typename Radio::size_t size_t;
00048       typedef typename Radio::ExtendedData ExtendedData;
00049 
00050       typedef iSenseDistanceModel<OsModel, Radio, TABLE_SIZE> self_type;
00051       typedef self_type* self_pointer_t;
00052 
00053       typedef double distance_t;
00054       typedef StaticArrayRoutingTable<OsModel, Radio, TABLE_SIZE, distance_t> DistanceMap;
00055       typedef typename DistanceMap::iterator DistanceMapIterator;
00056       // --------------------------------------------------------------------
00057       enum ErrorCodes
00058       {
00059          SUCCESS = OsModel::SUCCESS,
00060          ERR_UNSPEC = OsModel::ERR_UNSPEC
00061       };
00062       // --------------------------------------------------------------------
00063       iSenseDistanceModel( iSenseOs& os )
00064          : os_( os )
00065       {}
00066       // --------------------------------------------------------------------
00067       int init( Radio& radio)
00068       {
00069          radio_ = &radio;
00070          radio_->template reg_recv_callback<self_type, self_type::receive>( this );
00071          return SUCCESS;
00072       }
00073       // --------------------------------------------------------------------
00074       distance_t distance( node_id_t to )
00075       {
00076          distance_t distance = -1.0;
00077 
00078          DistanceMapIterator it = distances_.find( to );
00079          if ( it != distances_.end() )
00080             distance = it->second;
00081 
00082          return distance;
00083       };
00084 
00085    private:
00086       void receive( node_id_t id, size_t len, block_data_t* data, const ExtendedData& exdata )
00087            {
00088               // TODO: Why does the lqi-radio returns (255 - lqi)? hence, we transform it back here...
00089               uint16 lqi = 255 - exdata.link_metric();
00090               distance_t distance = 0;
00091 
00092               if ( lqi >= 118 )
00093                  distance = 1.0;
00094               else
00095                  distance = (-0.2 * lqi) + 25;
00096      //          else if ( lqi < 118 && lqi >= 110 )
00097      //             distance = 2.0;
00098      //          else if ( lqi < 110 && lqi >= 104 )
00099      //             distance = 3.0;
00100      //          else if ( lqi < 104 && lqi >= 100 )
00101      //             distance = 4.0;
00102      //          else if ( lqi < 100 && lqi >= 94 )
00103      //             distance = 5.0;
00104      //          else if ( lqi < 94 && lqi >= 88 )
00105      //             distance = 6.0;
00106      //          else if ( lqi < 88 && lqi >= 83 )
00107      //             distance = 7.0;
00108      //          else if ( lqi < 83 && lqi >= 78 )
00109      //             distance = 8.0;
00110      //          else if ( lqi < 78 && lqi >= 73 )
00111      //             distance = 9.0;
00112      //          else
00113      //             distance = 10.0;
00114 
00115               distances_[id] = distance;
00116            }
00117 
00118 
00119       iSenseOs& os()
00120       { return os_; }
00121 
00122       iSenseOs& os_;
00123 
00124       DistanceMap distances_;
00125       Radio *radio_;
00126    };
00127 
00128 }
00129 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines