LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking > Linux - Wireless Networking
User Name
Password
Linux - Wireless Networking This forum is for the discussion of wireless networking in Linux.

Notices


Reply
  Search this Thread
Old 01-20-2016, 04:22 AM   #1
mmontazeri35@yahoo.com
LQ Newbie
 
Registered: Jan 2016
Posts: 5

Rep: Reputation: Disabled
localization under water aquasim


I simulated a simple localization protocol for terrestrial with ns2, , there are two new agents, response agent and request agent , two new packet headers ,......... . I want to simulate this protocol for underwater with aquasim, What changes need to be applied to each of other layers?

I think All I need to change is the function that provide the distance ( TOA instead of RSSI) the rest still be the same .

Last edited by mmontazeri35@yahoo.com; 01-30-2016 at 01:13 AM.
 
Old 01-20-2016, 07:31 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by mmontazeri35@yahoo.com View Post
Hi every body. I simulated a simple localization protocol for terrestrial with ns2, , there are two new agents: response agent and request agent , two new packet header ,an application program to localize nodes in this protocol and there is not any changes in other layer ( lower layer ) . I want to simulate this protocol for under water with aquasim, What changes need to be applied to each of the layers?

I did not changes in application layer and packet header and agents(just use under water sensor node instead mobile node ) , for lower layer I use existing under water layer in aquasim but I did not get any result . what changes do I do ?
Since you don't actually post your code, tell us what you actually changed, or provide any details, how should we know? Read the "Question Guidelines" link in my posting signature. We are happy to help with SPECIFIC problems/question, but this isn't the place to come for personalized tutorials or how-to guides.
 
1 members found this post helpful.
Old 01-29-2016, 03:55 PM   #3
mmontazeri35@yahoo.com
LQ Newbie
 
Registered: Jan 2016
Posts: 5

Original Poster
Rep: Reputation: Disabled
Localization under water protocol with aquasim

Quote:
Originally Posted by TB0ne View Post
Since you don't actually post your code, tell us what you actually changed, or provide any details, how should we know? Read the "Question Guidelines" link in my posting signature. We are happy to help with SPECIFIC problems/question, but this isn't the place to come for personalized tutorials or how-to guides.
as you see in below , distanse function is RSSI, I changed distance function to TOA in order to use following code for underwater, ( I think applied changes are true but it has some errors because I didn't considre processing delay in layers ) , What changes need to be applied to each of layers in order to use following code for underwater with aquasim ?

it is localization code :
#ifndef ns_location_packet_h
#define ns_location_packet_h

#define HDR_LOCREQ(p) (hdr_locreq::access(p)) // location request
#define HDR_LOCRES(p) (hdr_locres::access(p)) // location response
#define LOCRES_PORT 532 // same port reserved for netnews

class Location;

//Location request header structure
struct hdr_locreq
{
static int offset_;

inline static int& offset() {return offset_; }
inline static hdr_locreq* access(const Packet* p) {
return (hdr_locreq*) p->access(offset_);
}
};

//Location response header structure
struct hdr_locres
{
Location node_location_;

static int offset_;
inline static int& offset() {return offset_; }
inline static hdr_locres* access(const Packet* p) {
return (hdr_locres*) p->access(offset_);
}
Location& loc() { return (node_location_);}
};
#endif




#ifndef ns_location_request_h
#define ns_location_request_h

#include "agent.h"
#include "locationdiscovery.h"

class LocDisApp;

//nodes broadcast a location request beacon
class LocReqAgent : public Agent
{
public:
LocReqAgent();
LocReqAgent(packet_t);
~LocReqAgent();

virtual void attachApp(LocDisApp* app);
virtual void broadcast();

protected:
LocDisApp* ldapp_;
};
#endif

#include "locationrequest.h"
#include "locationpacket.h"

// Binding C++ and OTcl classes
int hdr_locreq:ffset_;

static class LocReqHeaderClass : public PacketHeaderClass {
public:
LocReqHeaderClass() : PacketHeaderClass("PacketHeader/LocReq", sizeof(hdr_locreq)) {
bind_offset(&(hdr_locreq:ffset_));
}
} class_locreqhdr;

static class LocReqAgentClass : public TclClass {
public:
LocReqAgentClass() : TclClass("Agent/LocReq"){}
TclObject* create(int, const char*const*) {
return (new LocReqAgent() );
}
} class_locreq_agent;

// Constructor
LocReqAgent::LocReqAgent() : Agent(PT_LOCREQ), ldapp_(0)
{
bind("packetSize_", &size_);
}

LocReqAgent::LocReqAgent(packet_t type) : Agent(type)
{
bind("packetSize_", &size_);
}
// Distructor
LocReqAgent::~LocReqAgent()
{
delete ldapp_;
}

// Attach the application with the agent
void LocReqAgent::attachApp(LocDisApp* app)
{
ldapp_ = ( LocDisApp*) app;
}
// Construct a "location request" packet (PT_LOCREQ) that will be broadcasted to neighbouring nodes
void LocReqAgent::broadcast()
{
Packet *p = Packet::alloc();
hdr_cmn *ch = HDR_CMN(p);
hdr_ip *iph = HDR_IP(p);

ch->ptype() = PT_LOCREQ;
ch->next_hop_ = IP_BROADCAST;

iph->saddr() = Agent::addr();
iph->daddr() = IP_BROADCAST;
iph->sport() = RT_PORT;
iph->dport() = RT_PORT;
iph->ttl_ = 1;

// send packet to the link layer
send(p, 0);
}

#ifndef ns_location_response_h
#define ns_location_response_h

#include "agent.h"
#include "ip.h"
#include "locationdiscovery.h"
#include "locationrequest.h"

class LocDisApp;

class LocResAgent : public Agent {

friend class LocDisApp;

public:
LocResAgent();
LocResAgent(packet_t type);
~LocResAgent();

virtual void attachApp(LocDisApp* app);

protected:
virtual void recv(Packet*, Handler*);
virtual void send_res(Packet*, Handler*);
virtual void recv_res(Packet*, Handler*);
inline NsObject* get_link_layer();

LocDisApp* ldapp_;
bool set_target_; // this flag is used to indicate that the link layer has been already setted as target
};
#endif


#include "locationresponse.h"
#include "locationpacket.h"

// Binding C++ and OTcl classes

int hdr_locres:ffset_;

static class LocResHeaderClass : public PacketHeaderClass {
public:
LocResHeaderClass() : PacketHeaderClass("PacketHeader/LocRes", sizeof(hdr_locres)) {
bind_offset(&(hdr_locres:ffset_));
}
} class_locreshdr;

static class LocResAgentClass : public TclClass {
public:
LocResAgentClass() : TclClass("Agent/LocRes") {}
TclObject* create(int, const char*const*) {
return (new LocResAgent());
}
} class_locres_agent;


// Constructor
LocResAgent::LocResAgent() : Agent(PT_LOCRES), ldapp_(0), set_target_(false)
{
bind("packetSize_", &size_);
}

LocResAgent::LocResAgent(packet_t type) : Agent(type)
{
bind("packetSize_", &size_);
}

// Distructor
LocResAgent::~LocResAgent()
{
delete [] ldapp_;
}

// Attach the application with the agent
void LocResAgent::attachApp(LocDisApp* app)
{
ldapp_ = (LocDisApp*) app;
}

// Two types of packets could be received; either request or response packets
void LocResAgent::recv(Packet* p, Handler*)
{
hdr_cmn* ch = HDR_CMN(p);

if (ch->ptype() == PT_LOCREQ)
send_res(p, 0);
else if (ch->ptype() == PT_LOCRES)
recv_res(p, 0);

Packet::free(p);
}

// Send a location response packet that include the the location information of the sending node
void LocResAgent::send_res(Packet* p, Handler*)
{
if(!ldapp_)
return;

// Unknown nodes do not sent response
if (ldapp_->get_node_att() == UNKNOWN)
return;

Packet* npkt = Packet::alloc();
if (!npkt)
return;

hdr_ip* oiph = HDR_IP(p);

hdr_cmn *ch = HDR_CMN(npkt);
hdr_ip* niph = HDR_IP(npkt);
hdr_locres* lres = HDR_LOCRES(npkt);

ch->ptype() = PT_LOCRES;

niph->saddr() = Agent::addr();
niph->sport() = LOCRES_PORT;
niph->daddr() = oiph->src_.addr_;
niph->dport() = LOCRES_PORT;
niph->ttl_ = 1;

lres->loc() = ldapp_->get_node_loc();

// Set link layer as target
if(!set_target_)
{
target_ = get_link_layer();
set_target_ = true;
}

double delay_;
delay_ = Random::uniform(0.0, 0.5);

//Send the response packet after a delay
Scheduler::instance().schedule(target_, npkt, delay_);
}

// Send the received response packet to the location discovery application for further processing
void LocResAgent::recv_res(Packet* p, Handler*)
{
if(!ldapp_)
return;

// BEACON nodes drop response packets
if (ldapp_->get_node_att() == BEACON)
return;

hdr_cmn* ch = hdr_cmn::access(p);
// The location discovery application will process the received packet
ldapp_->process_response(ch->size(), p);
}

// Obtain the C++ compiled object for the link layer
NsObject* LocResAgent::get_link_layer()
{
Tcl& tcl = Tcl::instance();
tcl.evalf("%s set node_", this->name());
tcl.evalf("%s set ll_(0)",tcl.result());
return (NsObject *)TclObject::lookup(tcl.result());
}

#ifndef ns_location_discovery_h
#define ns_location_discovery_h
#include <vector>
#include <string>
#include <fstream>
#include "tclcl.h"
#include "random.h"
#include "location.h"
#include "packet.h"
#include "agent.h"
#include "locationresponse.h"
#include "position.h"
#include "nearestposition.h"
#include "mobilenode.h"
#include "support.h"
#include "mmse.h"

class LocResAgent;
class LocReqAgent;
class LocDisApp;

// Timer for sending location request packets
class ReqTimer : public TimerHandler {
public:
ReqTimer( LocDisApp* t) : TimerHandler(), t_(t) {}
inline virtual void expire(Event*);
protected:
LocDisApp* t_;
};

// Timer for location estimation
class EstimateTimer : public TimerHandler {
public:
EstimateTimer( LocDisApp* t) : TimerHandler(), t_(t) {}
inline virtual void expire(Event*);
protected:
LocDisApp* t_;
};

// Timer for recording node's location information into the trace file
class OutputTimer : public TimerHandler {
public:
OutputTimer( LocDisApp* t) : TimerHandler(), t_(t) {}
inline virtual void expire(Event*);
protected:
LocDisApp* t_;
};

class LocDisApp : public Application {

friend class Position;
friend class NearestPosition;
friend class RefinePosition;

public:
LocDisApp();
~LocDisApp();
virtual void timeout();
virtual void location_estimate();
virtual void output();
virtual int command(int argc, const char*const* argv);
virtual void process_response(int, Packet*);
inline int get_node_att() { return node_attribute_;} // Get the node attribute
inline int get_method_type() { return method_;} // Get the localisation method that should be used
inline Location get_node_loc() { return node_location_;} // Get the estimated location of the node
inline void get_upper(double *x, double *y) { *x = maxX; *y = maxY;} // Get the upper boundary of the topography
inline void set_final(bool f) { final_ = f;} // Get the value of the final_ flag
inline void set_ref(int n) { no_ref_ = n;} // Set the value of the number of references used in the estimation
inline int get_dist_error() {return distance_error_;} // Get the value of distance_error

protected:
virtual void start();
virtual void init();
virtual void node_attribute();
virtual Location node_location();
virtual double next_request();
virtual void add_response_data(ResData*);
virtual void show_color();
virtual double location_error();
void set_up_target();

Position *position_; // Pointer object of Position class
LocReqAgent *agent_request_; // Pointer object of LocReqAgent class
LocResAgent *agent_response_; // Pointer object of LocResAgent class
MobileNode* mobile_node_; // Pointer object of MobileNode class
ReqTimer req_timer_; // Request timer
EstimateTimer est_timer_; // Estimation timer
OutputTimer output_timer_; // output recording timer
// A vector of ResData is used to store the location information that extracted from the received location response packets
vector<ResData> data_;
Location node_location_; // The node's estimated location
double req_freq_; // Used to specify how frequent the node broadcast a location request packet
double start_time_; // Used to specify when to start broadcasting location request either directly or after a random time
// based on the value of random_
double loc_error_; // the location error
double maxX; // is the x-coordination of the upper boundary of the topography
double maxY; // is the y-coordination of the upper boundary of the topography
bool color_changed_; // Flag used to indicate if the colour of the node is already changed
bool final_; // Flag used to indicate when to stop estimating the node location
int node_attribute_; // used to sepcify the node attribute; beacon, unknown, or reference
int random_; // specify the start time of broadcasting either directly or after a random time
int distance_error_; // used to determine if a distance measurement error will be added or not
int max_requests_; // the maximum number of location request packets that can be send
int requesting_; // Indicate if the node should continue broadcasting a location request packet or it should stop sending it
int reqno_; // specify the number of location request packets that have been sent
int show_color_; // determine if the colour of the node should be changed based on its attribute or not
int method_; // specify the localisation method that will be used in the estimation
int no_ref_; // number of references (or beacons) used in the estimation
};
#endif

#include "locationdiscovery.h"
#include "locationpacket.h"
#include "refineposition.h"

// Binding C++ and OTcl classes
static class LocDisAppClass : public TclClass {
public:
LocDisAppClass() : TclClass("Application/LocDiscovery") {}
TclObject* create(int, const char*const*) {
return (new LocDisApp);
}
} class_locdis_app;

// Constructor
LocDisApp::LocDisApp(): agent_request_(0), agent_response_(0), req_timer_(this), est_timer_(this), output_timer_(this),
node_location_(), start_time_(0), loc_error_(-1), color_changed_(false), final_(false),
node_attribute_(0), requesting_(0), reqno_(0), no_ref_(0)

{
bind("distanceError_", &distance_error_);
bind("random_", &random_);
bind("reqFreq_", &req_freq_);
bind("maxRequests_", &max_requests_);
bind("showColor_", &show_color_);
bind("method_", &method_);
}

// Distructor
LocDisApp::~LocDisApp()
{
delete agent_request_;
delete agent_response_;
delete position_;
delete mobile_node_;
}

// command function allows the access of the compiled hierarchy from the interpreted hierarchy.
// here it is used to invoke the attach-agent command
// attach-agent is used to make a connection between LocDisApp and the agents (LocReqAgent and LocResAgent)
int LocDisApp::command(int argc, const char*const* argv)
{
Tcl& tcl = Tcl::instance();

// Attach agent to the application
if (argc == 3)
{
if (strcmp(argv[1], "attach-agent") == 0)
{
agent_ = (Agent*) TclObject::lookup(argv[2]);
if (agent_ == 0)
{
tcl.resultf("no such agent %s", argv[2]);
return(TCL_ERROR);
}
if (agent_->get_pkttype() == PT_LOCREQ)
{
agent_request_ = (LocReqAgent*) agent_;
agent_request_->attachApp(this);
}

else if (agent_->get_pkttype() == PT_LOCRES)
{
agent_response_ = (LocResAgent*) agent_;
agent_response_->attachApp(this);
}
else
{
tcl.resultf("The agent %s type is neither PT_LOCREQ nor PT_LOCRES", argv[2]);
return(TCL_ERROR);
}
return(TCL_OK);
}
}
return (Application::command(argc, argv));
}

// This function is used to start the LocDisApp, which can be invoked from the OTcl domain
void LocDisApp::start()
{
init();
req_timer_.sched(start_time_);
output_timer_.sched(0.0);
}

// this function is for initialisation purpose, which set the initial values of several variables
// also change the colour of the nodes based on their attribute
void LocDisApp::init()
{
if(agent_)
{
Tcl& tcl = Tcl::instance();

tcl.evalf("%s set node_", agent_->name());
mobile_node_ = (MobileNode*) TclObject::lookup(tcl.result());

Topography* T_;
T_ = mobile_node_->get_topography();

maxX = T_->upperX();
maxY = T_->upperY();

node_attribute(); // Specify the node attribute
show_color(); // change the colour of the nodes
}

if (agent_response_)
{
set_up_target(); // Set response agent as an up-target

if (node_attribute_ == BEACON)
node_location_ = node_location(); // Get the acutual location of the node (only for beacon nodes)
}
if (agent_request_ && (node_attribute_ == UNKNOWN || node_attribute_ == REFERENCE))
{
requesting_ = 1;
if (random_)
start_time_ = Random::uniform(1.0, 5.0);

// Specify which localisation algorithm should be run
switch (method_)
{
case SINGLE:
position_ = new Position(this);
break;
case NEAREST3:
position_ = new NearestPosition(this);
break;
case REFINE:
position_ = new RefinePosition(this);
break;
}
}
}

// Specify the node attribute (beacon, referece or unknown)
void LocDisApp::node_attribute()
{
Tcl& tcl = Tcl::instance();
tcl.evalf("%s set node_", agent_->name());
tcl.evalf("%s attribute",tcl.result());
char attr[20];
strcpy(attr, tcl.result());

if (strcmp(attr, "BEACON") == 0)
node_attribute_ = BEACON;
else if (strcmp(attr, "REFERENCE") == 0)
node_attribute_ = REFERENCE;
else if (strcmp(attr, "UNKNOWN") == 0)
node_attribute_ = UNKNOWN;
else
tcl.eval("puts "node attribute undefined!"");
}

// if show_color is enabled then change the color of the node based on its attribute
void LocDisApp::show_color()
{
if (show_color_ == 0)
return;

char *color = new char[20];
switch (node_attribute_)
{
case BEACON:
color = "blue";
break;
case UNKNOWN:
color = "yellow";
break;
case REFERENCE:
color = "red";
break;
default:
color = "black";
}

Tcl& tcl = Tcl::instance();
tcl.evalf("%s set node_", agent_->name());
tcl.evalf("%s color %s", tcl.result(), color);
}

// Get the acutual location of the node
Location LocDisApp::node_location()
{
double x, y, z;
Location loc;

mobile_node_->getLoc(&x, &y, &z);

loc.setLocation(x, y, z);
return loc;
}

// Schedule the broadcasting of location request packet and the estimation time
void LocDisApp::timeout()
{
if (!requesting_)
return;

if (final_)
{
requesting_ = 0;
return;
}

data_.clear();
agent_request_->broadcast(); // broadcast a location request packet
double next_request_ = next_request();
double next_estimate_ = next_request_ / 3.0;

if (next_request_ > 0)
{
req_timer_.resched(next_request_); //schedule the next request
est_timer_.resched(next_estimate_); //schedule the next estimate
}
else
{
requesting_ = 0;
est_timer_.resched(1.0); //schedule the final estimate
}
}

// Specify the time of the next request that will be used to broadcast a new location request packet
double LocDisApp::next_request()
{
double nr = req_freq_;

if (random_)
nr += req_freq_ * Random::uniform(-0.5, 0.5);

if (++reqno_ < max_requests_)
return(nr);
else
return(-1);
}

// Extract the location information from the received location response packet and store it in a ResData vector
void LocDisApp:rocess_response(int size, Packet* pkt)
{
hdr_ip* iph = HDR_IP(pkt);
hdr_locres* lrh = HDR_LOCRES(pkt);

nsaddr_t add = iph->src_.addr_;
Location loc = lrh->node_location_;
double rxp = pkt->txinfo_.RxPr;

ResData rdata(add, loc, rxp);

add_response_data(&rdata);
}

// In this fuction the data is added without any order.
// However the data can be ranked based on a specific attribute such as rxp
void LocDisApp::add_response_data(ResData* rdata)
{
data_.insert(data_.begin(), *rdata);
}

// Estimate the node location by calling the estimate function of a specific localisation algorithm that determined early (init())
void LocDisApp::location_estimate()
{
if (data_.size() < 3)
return;

Location loc_;

if(!position_->estimate(&loc_))
return;

node_location_ = loc_;
node_attribute_ = REFERENCE; //When unknown node estimate its position it becomes reference node
loc_error_ = location_error(); //estimate the location error

req_freq_ += 1.0;

// if the unknown node get it's position then it becomes reference node then it's color should be changed
if (!color_changed_)
{
show_color();
color_changed_ = true;
}
// In this way the location info is logged directly and only once after the estimation.

// to log the information every second the next line should be removed and the output timer should be resched every 1 sec
output_timer_.resched(0.0);
}

// Estimate the location error
double LocDisApp::location_error()
{
Location actual_location_;
double error_;

actual_location_ = node_location();

error_ = actual_location_.distance(&node_location_);

error_ = error_ * 100.0 / MAX_RANGE;

return error_;
}

// Log the location information into the trace file
void LocDisApp:utput()
{
double x, y, z;

// this can be used to log the information every 1 sec.
//output_timer_.resched(1.0);

if (loc_error_ < 0)
return;

//get the estimated location
node_location_.getLocation(&x, &y, &z);
//log node's location ifno
mobile_node_->log_loc(loc_error_, no_ref_, x, y);
}

// Set response agent as an up-target to handle the received packets from link layer
void LocDisApp::set_up_target()
{
if(!agent_response_)
return;

Tcl& tcl = Tcl::instance();
tcl.evalf("%s set node_", agent_response_->name());
tcl.evalf("[%s set ll_(0)] up-target %s",tcl.result(), agent_response_->name());
}

// this function will be invoked at the expiration of the request timer
void ReqTimer::expire(Event *)
{
t_->timeout();
}
// this function will be invoked at the expiration of the estimate timer
void EstimateTimer::expire(Event *)
{
t_->location_estimate();
}
// this function will be invoked at the expiration of the output timer
void OutputTimer::expire(Event *)
{
t_->output();
}


#include "support.h"
class LocDisApp;
struct ReferenceNode;

class Position
{
public:
Position(LocDisApp *l);
virtual ~Position();
virtual bool estimate(Location*);

protected:
virtual bool init();
virtual void set_ref_nodes();
double measure_distance_RSS(double Pr);
void boundary(Location * loc_);
LocDisApp* ldis_;
ReferenceNode *ref_nodes_; // this pointer hold the required information to apply the MMSE technique to estimate the node position.
// This information are the location of and the distance to the reference nodes.
int num_ref_; // the number of references that will be used to estimate the node location
};
#endif

#include "locationdiscovery.h"
// Constructor
Position::Position(LocDisApp* l) : ldis_(l), num_ref_(0) {}
// Distructor
Position::~Position()
{
delete [] ref_nodes_;
delete ldis_;
}

// Estimate node position
bool Position::estimate(Location* loc_)
{
init();

MMSE mmse;

if(!mmse.estimate(ref_nodes_, num_ref_, loc_))
return FAIL;

boundary(loc_);

ldis_->set_ref(num_ref_);

ldis_->set_final(true);

return SUCCESS;
}

bool Position::init()
{
num_ref_ = ldis_->data_.size();
ref_nodes_ = new ReferenceNode[num_ref_];
set_ref_nodes();
return SUCCESS;
}

// This function extract the required information to apply the MMSE technique from the data_ vector, which hold the received information
// from the neighbour references. This information will be stored in ref_nodes_
void Position::set_ref_nodes()
{
double rxp;
int i;

for (i = 0; i < num_ref_; i++)
{
rxp = ldis_->data_.at(i).getrxp();
ref_nodes_[i].loc_ = ldis_->data_.at(i).getloc();
ref_nodes_[i].distance_ = measure_distance_RSS(rxp);
}
}

// Using Friis free space equation to estimate the measure distance between the node and the reference (or beacon) node
double Position::measure_distance_RSS(double Pr)
{
double Pt = TX_POWER;
double Gt = TX_GAIN;
double Gr = RX_GAIN;
double lambda = LAMBDA;
double L = SYS_LOSS;
double distance;
RNG error;
double mean, sd;

distance = sqrt((Pt * Gt * Gr) / (L * Pr)) * lambda / (4 * PI);

if (ldis_->get_dist_error())
{
// Add error
// Increasing the standard deviation (sd) will increase the level of error
mean = 0.001 * distance;
sd = 0.01 * distance;

distance += error.normal(mean, sd);
}

return distance;
}

// This function is used to make sure that the estimated position is within the boundary of the topography
void Position::boundary(Location * loc_)
{
double maxX, maxY;
ldis_->get_upper(&maxX, &maxY);

if (loc_->getx() < 0)
loc_->setx(0);
if (loc_->getx() > maxX)
loc_->setx(maxX);
if (loc_->gety() < 0)
loc_->sety(0);
if (loc_->gety() > maxY)
loc_->sety(maxY);
}
 
Old 01-29-2016, 04:02 PM   #4
mmontazeri35@yahoo.com
LQ Newbie
 
Registered: Jan 2016
Posts: 5

Original Poster
Rep: Reputation: Disabled
Localization under water protocol with aquasim

Quote:
Originally Posted by TB0ne View Post
Since you don't actually post your code, tell us what you actually changed, or provide any details, how should we know? Read the "Question Guidelines" link in my posting signature. We are happy to help with SPECIFIC problems/question, but this isn't the place to come for personalized tutorials or how-to guides.
The rest of the code:

#ifndef ns_support_h
#define ns_support_h

#include "location.h"
#include "packet.h"

#define SUCCESS true
#define FAIL false

//node attribute:
#define BEACON 1
#define REFERENCE 2
#define UNKNOWN 3

// location estimation method:
#define SINGLE 1 //Which implemented in position.h,.cc
#define NEAREST3 2 //Which implemented in nearestposition.h,.cc
#define REFINE 3 //Which implemented in refineposition.h,.cc

#define SPEED_OF_LIGHT 300000000 // 3 * 10^8 m/s
#define PI 3.1415926535897

// from the terminal cd to ~/ns-allinone-2.34/ns-2.34/indep-utils/propagation$ then run:
// ./threshold -m FreeSpace -r 0.95 50

//this mean that the propagation-model is FreeSpace and 95% packets is correctly received at the distance of 50m.
//then you will get the configuration parameters as following:
//distance = 50
//propagation model: FreeSpace

//Selected parameters:
//transmit power: 0.281838
//frequency: 9.14e+08
//transmit antenna gain: 1
//receive antenna gain: 1
//system loss: 1

//Which can be used to set following:
#define TX_POWER 0.281838 // transmit power
#define TX_GAIN 1.0 // transmitter gain
#define RX_GAIN 1.0 // receiver gain
#define SYS_LOSS 1.0 // system loss (L >= 1)
#define FREQ 914000000.0 // 9.14e+08
#define LAMBDA (SPEED_OF_LIGHT / FREQ) // 0.328227571
#define MAX_RANGE 50.0 // max transmission range

inline double square(double t) {return t * t;}

// This class is used to capture the data that is required by the localisation system
// Generally this data include the address and location of the reference nodes that send the location response packets,
// and the power at which these packet are received
// Implementing new algorithms could require adding extra attributes to this class
class ResData : public TclObject
{
public:
ResData() : src_add_(0), src_loc_(), pkt_rxp_(0) {}

ResData(nsaddr_t sd, Location sl, double rp) : src_add_(sd), src_loc_(sl), pkt_rxp_(rp) {}

inline double getrxp() {return pkt_rxp_;}
inline Location getloc() {return src_loc_;}
inline nsaddr_t getadd() {return src_add_;}

inline void setResData(nsaddr_t sd, Location sl, double rp)
{
src_add_ = sd;
src_loc_ = sl;
pkt_rxp_ = rp;
}
inline void getResData(nsaddr_t *sd, Location *sl, double *rp)
{
*sd = src_add_;
*sl = src_loc_;
*rp = pkt_rxp_;
}
virtual bool is_equal(ResData* rd)
{
if ((src_add_ == rd->src_add_) &&
(src_loc_.is_equal(&rd->src_loc_)) &&
(pkt_rxp_ == rd->pkt_rxp_))
return true;
else
return false;
}

protected:
nsaddr_t src_add_; // source ip address
Location src_loc_; // source location
double pkt_rxp_; // power with which pkt is received
};

// This structure consists of two members, which represent the location of and the distance to the reference node
struct ReferenceNode
{
Location loc_;
double distance_;
};
#endif

#ifndef ns_mmse_h
#define ns_mmse_h

#include "locationdiscovery.h"
#include "location.h"

struct ReferenceNode;

class MMSE
{
public:
MMSE() : num_ref_(0) {}
~MMSE();
virtual bool estimate(ReferenceNode *, int, Location*);

protected:
double inline square(double t) {return t * t;}
virtual void init(ReferenceNode *);
virtual void multiply_XT_X(double Z[][2]);
virtual void multiply_XT_Y(double W[]);
virtual bool inverse_XTX(double Z[][2]);
double **X;
double *Y;
int num_ref_; // Number of references that will be used in the estimation
};
#endif

#include "mmse.h"
// Distructor
MMSE::~MMSE()
{
delete [] X;
delete [] Y;
}

// Solve the equation: b = Inverse(XT * X) * XT * Y
// b is the estimated location (loc)
bool MMSE::estimate(ReferenceNode *ref_nodes_, int n, Location* loc_)
{
double Z[2][2] = {0.0};
double W[2] = {0.0};

num_ref_ = n;

init(ref_nodes_);

multiply_XT_X(Z);

if (!inverse_XTX(Z))
return FAIL;

multiply_XT_Y(W);

// multiply Z by W to get the location
double x, y;

x = Z[0][0] * W[0] + Z[0][1] * W[1];
y = Z[1][0] * W[0] + Z[1][1] * W[1];

loc_->setx(x);
loc_->sety(y);

return SUCCESS;
}

/***************************************************************
* create the matrices X and Y:
* where m is the number of reference nodes minuse 1
*
* | 2(x0 - x1) 2(y0 - y1) |
* | 2(x0 - x2) 2(y0 - y2) |
* | 2(x0 - x3) 2(y0 - y3) |
* X = | . . |
* | . . |
* | . . |
* | 2(x0 - xm) 2(y0 - ym) |
*
*
*
* | x0^2 + y0^2 - d0^2 - x1^2 - y1^2 + d1^2 |
* | x0^2 + y0^2 - d0^2 - x2^2 - y2^2 + d2^2 |
* | x0^2 + y0^2 - d0^2 - x3^2 - y3^2 + d3^2 |
* Y = | . |
* | . |
* | . |
* | x0^2 + y0^2 - d0^2 - xm^2 - ym^2 + dm^2 |
*
*
****************************************************************/

void MMSE::init(ReferenceNode *ref_nodes_)
{
double x0, y0, d0;
double x, y, d, t;
int i;

Y = new double [num_ref_ -1];

X = new double *[num_ref_ -1];

for (i = 0; i < num_ref_ - 1; i++)
X[i] = new double[2];

x0 = ref_nodes_[0].loc_.getx();
y0 = ref_nodes_[0].loc_.gety();
d0 = ref_nodes_[0].distance_;

t = square(x0) + square(y0) - square(d0);

for (i = 0; i < num_ref_ - 1; i++)
{
x = ref_nodes_[i + 1].loc_.getx();
y = ref_nodes_[i + 1].loc_.gety();
d = ref_nodes_[i + 1].distance_;

X[i][0] = 2 * (x0 - x);
X[i][1] = 2 * (y0 - y);

Y[i] = t - square(x) - square(y) + square(d);
}
}
// calculate the multiplication of XT by X (Z = XT * X) the result matrix (Z) is always 2 x 2 matrix
void MMSE::multiply_XT_X(double Z[][2])
{
int i;

for (i = 0; i < num_ref_ - 1; i++)
{
Z[0][0] += square(X[i][0]);
Z[0][1] += X[i][0] * X[i][1];
Z[1][1] += square(X[i][1]);
}
Z[1][0] = Z[0][1];
}

// Z is the inverse of (XT * X)
bool MMSE::inverse_XTX(double Z[][2])
{
double determinant_;

determinant_ = Z[0][0] * Z[1][1] - Z[0][1] * Z[1][0];

if (determinant_ == 0)
return FAIL;

double t = Z[0][0];

Z[0][0] = Z[1][1] / determinant_;
Z[0][1] *= -1 / determinant_;
Z[1][0] *= -1 / determinant_;
Z[1][1] = t / determinant_;

return SUCCESS;
}

// W is the multiplication of XT by Y (W = XT * Y) W is always 2 x 1 matirx
void MMSE::multiply_XT_Y(double W[])
{
int i;

for (i = 0; i < num_ref_ - 1; i++)
{
W[0] += X[i][0] * Y[i];
W[1] += X[i][1] * Y[i];
}
}

Last edited by mmontazeri35@yahoo.com; 01-29-2016 at 04:16 PM.
 
Old 01-29-2016, 04:04 PM   #5
mmontazeri35@yahoo.com
LQ Newbie
 
Registered: Jan 2016
Posts: 5

Original Poster
Rep: Reputation: Disabled
Localization under water protocol with aquasim

Quote:
Originally Posted by TB0ne View Post
Since you don't actually post your code, tell us what you actually changed, or provide any details, how should we know? Read the "Question Guidelines" link in my posting signature. We are happy to help with SPECIFIC problems/question, but this isn't the place to come for personalized tutorials or how-to guides.
as you see in below , distanse function is RSSI, I changed distance function to TOA in order to use following code for underwater, ( I think applied changes are true but it has some errors because I didn't considre processing delay in layers ) , What changes need to be applied to each of layers in order to use following code for underwater with aquasim ?

it is localization code :
#ifndef ns_location_packet_h
#define ns_location_packet_h

#define HDR_LOCREQ(p) (hdr_locreq::access(p)) // location request
#define HDR_LOCRES(p) (hdr_locres::access(p)) // location response
#define LOCRES_PORT 532 // same port reserved for netnews

class Location;

//Location request header structure
struct hdr_locreq
{
static int offset_;

inline static int& offset() {return offset_; }
inline static hdr_locreq* access(const Packet* p) {
return (hdr_locreq*) p->access(offset_);
}
};

//Location response header structure
struct hdr_locres
{
Location node_location_;

static int offset_;
inline static int& offset() {return offset_; }
inline static hdr_locres* access(const Packet* p) {
return (hdr_locres*) p->access(offset_);
}
Location& loc() { return (node_location_);}
};
#endif




#ifndef ns_location_request_h
#define ns_location_request_h

#include "agent.h"
#include "locationdiscovery.h"

class LocDisApp;

//nodes broadcast a location request beacon
class LocReqAgent : public Agent
{
public:
LocReqAgent();
LocReqAgent(packet_t);
~LocReqAgent();

virtual void attachApp(LocDisApp* app);
virtual void broadcast();

protected:
LocDisApp* ldapp_;
};
#endif

#include "locationrequest.h"
#include "locationpacket.h"

// Binding C++ and OTcl classes
int hdr_locreq:ffset_;

static class LocReqHeaderClass : public PacketHeaderClass {
public:
LocReqHeaderClass() : PacketHeaderClass("PacketHeader/LocReq", sizeof(hdr_locreq)) {
bind_offset(&(hdr_locreq:ffset_));
}
} class_locreqhdr;

static class LocReqAgentClass : public TclClass {
public:
LocReqAgentClass() : TclClass("Agent/LocReq"){}
TclObject* create(int, const char*const*) {
return (new LocReqAgent() );
}
} class_locreq_agent;

// Constructor
LocReqAgent::LocReqAgent() : Agent(PT_LOCREQ), ldapp_(0)
{
bind("packetSize_", &size_);
}

LocReqAgent::LocReqAgent(packet_t type) : Agent(type)
{
bind("packetSize_", &size_);
}
// Distructor
LocReqAgent::~LocReqAgent()
{
delete ldapp_;
}

// Attach the application with the agent
void LocReqAgent::attachApp(LocDisApp* app)
{
ldapp_ = ( LocDisApp*) app;
}
// Construct a "location request" packet (PT_LOCREQ) that will be broadcasted to neighbouring nodes
void LocReqAgent::broadcast()
{
Packet *p = Packet::alloc();
hdr_cmn *ch = HDR_CMN(p);
hdr_ip *iph = HDR_IP(p);

ch->ptype() = PT_LOCREQ;
ch->next_hop_ = IP_BROADCAST;

iph->saddr() = Agent::addr();
iph->daddr() = IP_BROADCAST;
iph->sport() = RT_PORT;
iph->dport() = RT_PORT;
iph->ttl_ = 1;

// send packet to the link layer
send(p, 0);
}

#ifndef ns_location_response_h
#define ns_location_response_h

#include "agent.h"
#include "ip.h"
#include "locationdiscovery.h"
#include "locationrequest.h"

class LocDisApp;

class LocResAgent : public Agent {

friend class LocDisApp;

public:
LocResAgent();
LocResAgent(packet_t type);
~LocResAgent();

virtual void attachApp(LocDisApp* app);

protected:
virtual void recv(Packet*, Handler*);
virtual void send_res(Packet*, Handler*);
virtual void recv_res(Packet*, Handler*);
inline NsObject* get_link_layer();

LocDisApp* ldapp_;
bool set_target_; // this flag is used to indicate that the link layer has been already setted as target
};
#endif


#include "locationresponse.h"
#include "locationpacket.h"

// Binding C++ and OTcl classes

int hdr_locres:ffset_;

static class LocResHeaderClass : public PacketHeaderClass {
public:
LocResHeaderClass() : PacketHeaderClass("PacketHeader/LocRes", sizeof(hdr_locres)) {
bind_offset(&(hdr_locres:ffset_));
}
} class_locreshdr;

static class LocResAgentClass : public TclClass {
public:
LocResAgentClass() : TclClass("Agent/LocRes") {}
TclObject* create(int, const char*const*) {
return (new LocResAgent());
}
} class_locres_agent;


// Constructor
LocResAgent::LocResAgent() : Agent(PT_LOCRES), ldapp_(0), set_target_(false)
{
bind("packetSize_", &size_);
}

LocResAgent::LocResAgent(packet_t type) : Agent(type)
{
bind("packetSize_", &size_);
}

// Distructor
LocResAgent::~LocResAgent()
{
delete [] ldapp_;
}

// Attach the application with the agent
void LocResAgent::attachApp(LocDisApp* app)
{
ldapp_ = (LocDisApp*) app;
}

// Two types of packets could be received; either request or response packets
void LocResAgent::recv(Packet* p, Handler*)
{
hdr_cmn* ch = HDR_CMN(p);

if (ch->ptype() == PT_LOCREQ)
send_res(p, 0);
else if (ch->ptype() == PT_LOCRES)
recv_res(p, 0);

Packet::free(p);
}

// Send a location response packet that include the the location information of the sending node
void LocResAgent::send_res(Packet* p, Handler*)
{
if(!ldapp_)
return;

// Unknown nodes do not sent response
if (ldapp_->get_node_att() == UNKNOWN)
return;

Packet* npkt = Packet::alloc();
if (!npkt)
return;

hdr_ip* oiph = HDR_IP(p);

hdr_cmn *ch = HDR_CMN(npkt);
hdr_ip* niph = HDR_IP(npkt);
hdr_locres* lres = HDR_LOCRES(npkt);

ch->ptype() = PT_LOCRES;

niph->saddr() = Agent::addr();
niph->sport() = LOCRES_PORT;
niph->daddr() = oiph->src_.addr_;
niph->dport() = LOCRES_PORT;
niph->ttl_ = 1;

lres->loc() = ldapp_->get_node_loc();

// Set link layer as target
if(!set_target_)
{
target_ = get_link_layer();
set_target_ = true;
}

double delay_;
delay_ = Random::uniform(0.0, 0.5);

//Send the response packet after a delay
Scheduler::instance().schedule(target_, npkt, delay_);
}

// Send the received response packet to the location discovery application for further processing
void LocResAgent::recv_res(Packet* p, Handler*)
{
if(!ldapp_)
return;

// BEACON nodes drop response packets
if (ldapp_->get_node_att() == BEACON)
return;

hdr_cmn* ch = hdr_cmn::access(p);
// The location discovery application will process the received packet
ldapp_->process_response(ch->size(), p);
}

// Obtain the C++ compiled object for the link layer
NsObject* LocResAgent::get_link_layer()
{
Tcl& tcl = Tcl::instance();
tcl.evalf("%s set node_", this->name());
tcl.evalf("%s set ll_(0)",tcl.result());
return (NsObject *)TclObject::lookup(tcl.result());
}

#ifndef ns_location_discovery_h
#define ns_location_discovery_h
#include <vector>
#include <string>
#include <fstream>
#include "tclcl.h"
#include "random.h"
#include "location.h"
#include "packet.h"
#include "agent.h"
#include "locationresponse.h"
#include "position.h"
#include "nearestposition.h"
#include "mobilenode.h"
#include "support.h"
#include "mmse.h"

class LocResAgent;
class LocReqAgent;
class LocDisApp;

// Timer for sending location request packets
class ReqTimer : public TimerHandler {
public:
ReqTimer( LocDisApp* t) : TimerHandler(), t_(t) {}
inline virtual void expire(Event*);
protected:
LocDisApp* t_;
};

// Timer for location estimation
class EstimateTimer : public TimerHandler {
public:
EstimateTimer( LocDisApp* t) : TimerHandler(), t_(t) {}
inline virtual void expire(Event*);
protected:
LocDisApp* t_;
};

// Timer for recording node's location information into the trace file
class OutputTimer : public TimerHandler {
public:
OutputTimer( LocDisApp* t) : TimerHandler(), t_(t) {}
inline virtual void expire(Event*);
protected:
LocDisApp* t_;
};

class LocDisApp : public Application {

friend class Position;
friend class NearestPosition;
friend class RefinePosition;

public:
LocDisApp();
~LocDisApp();
virtual void timeout();
virtual void location_estimate();
virtual void output();
virtual int command(int argc, const char*const* argv);
virtual void process_response(int, Packet*);
inline int get_node_att() { return node_attribute_;} // Get the node attribute
inline int get_method_type() { return method_;} // Get the localisation method that should be used
inline Location get_node_loc() { return node_location_;} // Get the estimated location of the node
inline void get_upper(double *x, double *y) { *x = maxX; *y = maxY;} // Get the upper boundary of the topography
inline void set_final(bool f) { final_ = f;} // Get the value of the final_ flag
inline void set_ref(int n) { no_ref_ = n;} // Set the value of the number of references used in the estimation
inline int get_dist_error() {return distance_error_;} // Get the value of distance_error

protected:
virtual void start();
virtual void init();
virtual void node_attribute();
virtual Location node_location();
virtual double next_request();
virtual void add_response_data(ResData*);
virtual void show_color();
virtual double location_error();
void set_up_target();

Position *position_; // Pointer object of Position class
LocReqAgent *agent_request_; // Pointer object of LocReqAgent class
LocResAgent *agent_response_; // Pointer object of LocResAgent class
MobileNode* mobile_node_; // Pointer object of MobileNode class
ReqTimer req_timer_; // Request timer
EstimateTimer est_timer_; // Estimation timer
OutputTimer output_timer_; // output recording timer
// A vector of ResData is used to store the location information that extracted from the received location response packets
vector<ResData> data_;
Location node_location_; // The node's estimated location
double req_freq_; // Used to specify how frequent the node broadcast a location request packet
double start_time_; // Used to specify when to start broadcasting location request either directly or after a random time
// based on the value of random_
double loc_error_; // the location error
double maxX; // is the x-coordination of the upper boundary of the topography
double maxY; // is the y-coordination of the upper boundary of the topography
bool color_changed_; // Flag used to indicate if the colour of the node is already changed
bool final_; // Flag used to indicate when to stop estimating the node location
int node_attribute_; // used to sepcify the node attribute; beacon, unknown, or reference
int random_; // specify the start time of broadcasting either directly or after a random time
int distance_error_; // used to determine if a distance measurement error will be added or not
int max_requests_; // the maximum number of location request packets that can be send
int requesting_; // Indicate if the node should continue broadcasting a location request packet or it should stop sending it
int reqno_; // specify the number of location request packets that have been sent
int show_color_; // determine if the colour of the node should be changed based on its attribute or not
int method_; // specify the localisation method that will be used in the estimation
int no_ref_; // number of references (or beacons) used in the estimation
};
#endif

#include "locationdiscovery.h"
#include "locationpacket.h"
#include "refineposition.h"

// Binding C++ and OTcl classes
static class LocDisAppClass : public TclClass {
public:
LocDisAppClass() : TclClass("Application/LocDiscovery") {}
TclObject* create(int, const char*const*) {
return (new LocDisApp);
}
} class_locdis_app;

// Constructor
LocDisApp::LocDisApp(): agent_request_(0), agent_response_(0), req_timer_(this), est_timer_(this), output_timer_(this),
node_location_(), start_time_(0), loc_error_(-1), color_changed_(false), final_(false),
node_attribute_(0), requesting_(0), reqno_(0), no_ref_(0)

{
bind("distanceError_", &distance_error_);
bind("random_", &random_);
bind("reqFreq_", &req_freq_);
bind("maxRequests_", &max_requests_);
bind("showColor_", &show_color_);
bind("method_", &method_);
}

// Distructor
LocDisApp::~LocDisApp()
{
delete agent_request_;
delete agent_response_;
delete position_;
delete mobile_node_;
}

// command function allows the access of the compiled hierarchy from the interpreted hierarchy.
// here it is used to invoke the attach-agent command
// attach-agent is used to make a connection between LocDisApp and the agents (LocReqAgent and LocResAgent)
int LocDisApp::command(int argc, const char*const* argv)
{
Tcl& tcl = Tcl::instance();

// Attach agent to the application
if (argc == 3)
{
if (strcmp(argv[1], "attach-agent") == 0)
{
agent_ = (Agent*) TclObject::lookup(argv[2]);
if (agent_ == 0)
{
tcl.resultf("no such agent %s", argv[2]);
return(TCL_ERROR);
}
if (agent_->get_pkttype() == PT_LOCREQ)
{
agent_request_ = (LocReqAgent*) agent_;
agent_request_->attachApp(this);
}

else if (agent_->get_pkttype() == PT_LOCRES)
{
agent_response_ = (LocResAgent*) agent_;
agent_response_->attachApp(this);
}
else
{
tcl.resultf("The agent %s type is neither PT_LOCREQ nor PT_LOCRES", argv[2]);
return(TCL_ERROR);
}
return(TCL_OK);
}
}
return (Application::command(argc, argv));
}

// This function is used to start the LocDisApp, which can be invoked from the OTcl domain
void LocDisApp::start()
{
init();
req_timer_.sched(start_time_);
output_timer_.sched(0.0);
}

// this function is for initialisation purpose, which set the initial values of several variables
// also change the colour of the nodes based on their attribute
void LocDisApp::init()
{
if(agent_)
{
Tcl& tcl = Tcl::instance();

tcl.evalf("%s set node_", agent_->name());
mobile_node_ = (MobileNode*) TclObject::lookup(tcl.result());

Topography* T_;
T_ = mobile_node_->get_topography();

maxX = T_->upperX();
maxY = T_->upperY();

node_attribute(); // Specify the node attribute
show_color(); // change the colour of the nodes
}

if (agent_response_)
{
set_up_target(); // Set response agent as an up-target

if (node_attribute_ == BEACON)
node_location_ = node_location(); // Get the acutual location of the node (only for beacon nodes)
}
if (agent_request_ && (node_attribute_ == UNKNOWN || node_attribute_ == REFERENCE))
{
requesting_ = 1;
if (random_)
start_time_ = Random::uniform(1.0, 5.0);

// Specify which localisation algorithm should be run
switch (method_)
{
case SINGLE:
position_ = new Position(this);
break;
case NEAREST3:
position_ = new NearestPosition(this);
break;
case REFINE:
position_ = new RefinePosition(this);
break;
}
}
}

// Specify the node attribute (beacon, referece or unknown)
void LocDisApp::node_attribute()
{
Tcl& tcl = Tcl::instance();
tcl.evalf("%s set node_", agent_->name());
tcl.evalf("%s attribute",tcl.result());
char attr[20];
strcpy(attr, tcl.result());

if (strcmp(attr, "BEACON") == 0)
node_attribute_ = BEACON;
else if (strcmp(attr, "REFERENCE") == 0)
node_attribute_ = REFERENCE;
else if (strcmp(attr, "UNKNOWN") == 0)
node_attribute_ = UNKNOWN;
else
tcl.eval("puts "node attribute undefined!"");
}

// if show_color is enabled then change the color of the node based on its attribute
void LocDisApp::show_color()
{
if (show_color_ == 0)
return;

char *color = new char[20];
switch (node_attribute_)
{
case BEACON:
color = "blue";
break;
case UNKNOWN:
color = "yellow";
break;
case REFERENCE:
color = "red";
break;
default:
color = "black";
}

Tcl& tcl = Tcl::instance();
tcl.evalf("%s set node_", agent_->name());
tcl.evalf("%s color %s", tcl.result(), color);
}

// Get the acutual location of the node
Location LocDisApp::node_location()
{
double x, y, z;
Location loc;

mobile_node_->getLoc(&x, &y, &z);

loc.setLocation(x, y, z);
return loc;
}

// Schedule the broadcasting of location request packet and the estimation time
void LocDisApp::timeout()
{
if (!requesting_)
return;

if (final_)
{
requesting_ = 0;
return;
}

data_.clear();
agent_request_->broadcast(); // broadcast a location request packet
double next_request_ = next_request();
double next_estimate_ = next_request_ / 3.0;

if (next_request_ > 0)
{
req_timer_.resched(next_request_); //schedule the next request
est_timer_.resched(next_estimate_); //schedule the next estimate
}
else
{
requesting_ = 0;
est_timer_.resched(1.0); //schedule the final estimate
}
}

// Specify the time of the next request that will be used to broadcast a new location request packet
double LocDisApp::next_request()
{
double nr = req_freq_;

if (random_)
nr += req_freq_ * Random::uniform(-0.5, 0.5);

if (++reqno_ < max_requests_)
return(nr);
else
return(-1);
}

// Extract the location information from the received location response packet and store it in a ResData vector
void LocDisApp:rocess_response(int size, Packet* pkt)
{
hdr_ip* iph = HDR_IP(pkt);
hdr_locres* lrh = HDR_LOCRES(pkt);

nsaddr_t add = iph->src_.addr_;
Location loc = lrh->node_location_;
double rxp = pkt->txinfo_.RxPr;

ResData rdata(add, loc, rxp);

add_response_data(&rdata);
}

// In this fuction the data is added without any order.
// However the data can be ranked based on a specific attribute such as rxp
void LocDisApp::add_response_data(ResData* rdata)
{
data_.insert(data_.begin(), *rdata);
}

// Estimate the node location by calling the estimate function of a specific localisation algorithm that determined early (init())
void LocDisApp::location_estimate()
{
if (data_.size() < 3)
return;

Location loc_;

if(!position_->estimate(&loc_))
return;

node_location_ = loc_;
node_attribute_ = REFERENCE; //When unknown node estimate its position it becomes reference node
loc_error_ = location_error(); //estimate the location error

req_freq_ += 1.0;

// if the unknown node get it's position then it becomes reference node then it's color should be changed
if (!color_changed_)
{
show_color();
color_changed_ = true;
}
// In this way the location info is logged directly and only once after the estimation.

// to log the information every second the next line should be removed and the output timer should be resched every 1 sec
output_timer_.resched(0.0);
}

// Estimate the location error
double LocDisApp::location_error()
{
Location actual_location_;
double error_;

actual_location_ = node_location();

error_ = actual_location_.distance(&node_location_);

error_ = error_ * 100.0 / MAX_RANGE;

return error_;
}

// Log the location information into the trace file
void LocDisApp:utput()
{
double x, y, z;

// this can be used to log the information every 1 sec.
//output_timer_.resched(1.0);

if (loc_error_ < 0)
return;

//get the estimated location
node_location_.getLocation(&x, &y, &z);
//log node's location ifno
mobile_node_->log_loc(loc_error_, no_ref_, x, y);
}

// Set response agent as an up-target to handle the received packets from link layer
void LocDisApp::set_up_target()
{
if(!agent_response_)
return;

Tcl& tcl = Tcl::instance();
tcl.evalf("%s set node_", agent_response_->name());
tcl.evalf("[%s set ll_(0)] up-target %s",tcl.result(), agent_response_->name());
}

// this function will be invoked at the expiration of the request timer
void ReqTimer::expire(Event *)
{
t_->timeout();
}
// this function will be invoked at the expiration of the estimate timer
void EstimateTimer::expire(Event *)
{
t_->location_estimate();
}
// this function will be invoked at the expiration of the output timer
void OutputTimer::expire(Event *)
{
t_->output();
}


#include "support.h"
class LocDisApp;
struct ReferenceNode;

class Position
{
public:
Position(LocDisApp *l);
virtual ~Position();
virtual bool estimate(Location*);

protected:
virtual bool init();
virtual void set_ref_nodes();
double measure_distance_RSS(double Pr);
void boundary(Location * loc_);
LocDisApp* ldis_;
ReferenceNode *ref_nodes_; // this pointer hold the required information to apply the MMSE technique to estimate the node position.
// This information are the location of and the distance to the reference nodes.
int num_ref_; // the number of references that will be used to estimate the node location
};
#endif

#include "locationdiscovery.h"
// Constructor
Position::Position(LocDisApp* l) : ldis_(l), num_ref_(0) {}
// Distructor
Position::~Position()
{
delete [] ref_nodes_;
delete ldis_;
}

// Estimate node position
bool Position::estimate(Location* loc_)
{
init();

MMSE mmse;

if(!mmse.estimate(ref_nodes_, num_ref_, loc_))
return FAIL;

boundary(loc_);

ldis_->set_ref(num_ref_);

ldis_->set_final(true);

return SUCCESS;
}

bool Position::init()
{
num_ref_ = ldis_->data_.size();
ref_nodes_ = new ReferenceNode[num_ref_];
set_ref_nodes();
return SUCCESS;
}

// This function extract the required information to apply the MMSE technique from the data_ vector, which hold the received information
// from the neighbour references. This information will be stored in ref_nodes_
void Position::set_ref_nodes()
{
double rxp;
int i;

for (i = 0; i < num_ref_; i++)
{
rxp = ldis_->data_.at(i).getrxp();
ref_nodes_[i].loc_ = ldis_->data_.at(i).getloc();
ref_nodes_[i].distance_ = measure_distance_RSS(rxp);
}
}

// Using Friis free space equation to estimate the measure distance between the node and the reference (or beacon) node
double Position::measure_distance_RSS(double Pr)
{
double Pt = TX_POWER;
double Gt = TX_GAIN;
double Gr = RX_GAIN;
double lambda = LAMBDA;
double L = SYS_LOSS;
double distance;
RNG error;
double mean, sd;

distance = sqrt((Pt * Gt * Gr) / (L * Pr)) * lambda / (4 * PI);

if (ldis_->get_dist_error())
{
// Add error
// Increasing the standard deviation (sd) will increase the level of error
mean = 0.001 * distance;
sd = 0.01 * distance;

distance += error.normal(mean, sd);
}

return distance;
}

// This function is used to make sure that the estimated position is within the boundary of the topography
void Position::boundary(Location * loc_)
{
double maxX, maxY;
ldis_->get_upper(&maxX, &maxY);

if (loc_->getx() < 0)
loc_->setx(0);
if (loc_->getx() > maxX)
loc_->setx(maxX);
if (loc_->gety() < 0)
loc_->sety(0);
if (loc_->gety() > maxY)
loc_->sety(maxY);
}
 
Old 01-29-2016, 04:24 PM   #6
mmontazeri35@yahoo.com
LQ Newbie
 
Registered: Jan 2016
Posts: 5

Original Poster
Rep: Reputation: Disabled
Localization under water protocol with aquasim

Quote:
Originally Posted by TB0ne View Post
Since you don't actually post your code, tell us what you actually changed, or provide any details, how should we know? Read the "Question Guidelines" link in my posting signature. We are happy to help with SPECIFIC problems/question, but this isn't the place to come for personalized tutorials or how-to guides.

Thank you for your time.
two above posts Have been replaced.
 
  


Reply

Tags
aquasim, ns2



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Aquasim:How insert mac protocol to aquasim vaj-bor Linux - Software 35 03-15-2017 06:43 PM
NS2 and Aquasim Preeti1910 Linux - Software 48 10-15-2016 01:09 PM
How to Aquasim nancygoyal Linux - Newbie 1 07-06-2014 02:44 PM
aquasim varshitha Linux - Wireless Networking 0 01-29-2011 09:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking > Linux - Wireless Networking

All times are GMT -5. The time now is 04:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration