Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 42

Wireless Sensor Network Localization Simulator v1.

1
Written by Abdelhady Mohammad Naguib May,2011
Thank you for using the WSN Localization Simulator.

Figure 1: WSN Localization Simulator

INTRODUCTION
A wireless sensor network (WSN) is a network composed of a large number of sensor nodes,
which are deployed in the monitoring field. With the rapid development of WSNs, providing
development tools such as simulation environment before deploying real nodes in physical
environments is getting more important. A well simulation environment can help developers
build their prototype models to know the interactions and the behavior of each node. In addition,
most of WSN applications will deploy a large number of nodes in a simulation environment.
However, the simulation speed depends on the simulation fidelity and scale.
WSN localization is the operation of determining the position of sensor nodes; this
position is estimated and not accurate. Because GPS have high cost and works outdoors only,
there are many localization algorithm that tries to be more accurate in determining the position of

sensor nodes (i.e. have low localization error). In order to effectively design and develop new
localization algorithms targeted towards WSNs, there is a strong need to use an equally effective
and reliable simulation environment. Sensor nodes are, by design, tiny and inexpensive, but due
to their need to work in large numbers make full-scale testing a fairly expensive process to be
carried out using real hardware. Analytical modeling using tools such as MATLAB help in
acquiring a quick insight, but they fail to provide anything close to realistic results that can be
acquired through proper implementations and good simulation tools.

The problem and its solution:


Most of WSNs simulators don't support one or two of the following criteria: easy, extendable and
scalable, so that we design and implement an integrated framework used for localization in
WSNs and available for the analysis of different types of localization schemes (Range-free,
Range-based and Hybrid localization algorithms). The obtained framework is simple and easy to
be used to demonstrate the performance evaluation and comparison of different types of
localization schemes. It can also be extended to include many localization algorithms by future
researchers.

Design criteria for our framework:


1- Supports completely large scale networks, e.g. 400 nodes x 400 nodes
which are fully interconnected which implies 160,000 network connections.
So, our proposed framework is scalable.
2- The network can be deployed based on a wide range of parameters:
network size, sensor nodes communication distance, locator radius
(communication range), sector angle (rotation angle) and locator beam
width.
3- Supports Multithreading, where the simulated algorithm runs on a
separate thread from the thread controlling the developer interface.
4- Packaging all simulator classes into a referenced DLL file for better
modularity design.
5- Ease of implementing various localization algorithms by encapsulating
each one into a separate DLL file. Developers can write their own localization
algorithm into a DLL file and reference it into our framework.
6- Can be extended to implement many different localization algorithms with
different categories (range-free, range-based and a hybrid of both).
7- Suitable developer interface to enable deploying and moving sensor and
locator nodes and changing various simulation parameters.

Framework Architecture
Our framework is composed of two main components: simulator and a
localization algorithm. A simulator includes two classes; the first one is
network DLL file which contains all simulator classes (e.g. sensor class,
locator class, packet class ...etc.) and the second one is developer interface
class which contains all control functions (e.g. run and stop simulator,
painting method, etc.). The second component of our framework is the
localization algorithm which is written by the developer into a DLL file and
referencing this file into our framework, so that the developer has the ability
to write his localization algorithm but under certain design specifications as
shown in Fig. 2.
Set_Parameters (locator range, beam width )
{
}
Connect (list of locators, list of sensors )
{
}
Send (list of sensors)
{
}
Localize (list of sensors, locator range )
{
}
Figure2: Localization Algorithm Design Specifications

The first method (Set_Parameters) is responsible to get all control data from user interface. The
second method is used to test the connectivity between a sensor node and a locator node. The
third method (Send), after a valid connection this method is used to transmit packets between
different nodes. Finally, after getting all necessary packets from locator nodes, a sensor node can
estimate its position using the fourth method (Localize).
As shown in Fig. 3, we illustrate the sequence diagram for submitting parameters from the user
through simulator and getting the response back from the localization algorithm. First, the user
set the required parameters via user interface and the simulator receives these parameters and
applies it using Set_Parameters method which gives the needed argument to the localization
algorithm. Second, the algorithm will reply to the simulator. Third, calling Connect method to
test the connectivity between different nodes, if there is a connection, fill array list of
connections between different nodes. Fourth, calling send method to carry out sending packets
between connected nodes by filling array list which contains the sent packets for each sensor
node. Finally, calling Localize method to estimate the position of a sensor node and returning the
results back to the user into an external text file.

User

Algorithm

Simulator
Input Parameters

Get Parameters

Set_Parameters

Parameters set

Request for connection

Connect
Connection established

Request for sending


Send
Packets sent

Request for localization


Localize
Position estimated
Results

Figure 3: Sequence Diagram

Customizing a Localization Algorithm


A localization algorithm can be customized as follows:
1- Creating a class library (DLL).
2- Writing a localization algorithm according to the design specifications mentioned above.
3- Reference this class library as shown in the snapshot of Fig. 4.

Adding a Reference

Figure 4: Adding Component File (DLL)

Finally, the proposed framework is easy to use, scalable that supports 400x400 nodes and
extendable which allows developers to write their own localization algorithms and referencing it
to our framework.

How does this framework help developers?


This framework allows developers to write their own localization algorithms (DLL file)
according to the design specifications mentioned above. Hence, their algorithms can be
referenced to our framework as shown in Fig. 3.

How does the code actually work?


Our framework is composed of two main components: simulator and a
localization algorithm. A simulator includes two classes; the first one is
network DLL file which contains all simulator classes as shown below:

1- Network Class
Which is used for initialization of user interface parameters.
public WirelessSensorNetwork(int NoSensors, int iMaxX, int iMaxY, int
LocatorRadius, int sangle1, int sangle2, int sangle3, int sangle4, int
sangle5, int sangle6, int sangle7, int sangle8, int Sweepangle, int stepx,
int stepy, bool radio1, bool radio2, int combo1, int combo2, int combo3,
int BeaconRadius, int noBeacons, bool checkBeacons, int noMobSensors, int
noRLocators){
this.radio1 = radio1;
this.radio2 = radio2;
this.combo1 = combo1;
this.combo2 = combo2;
this.combo3 = combo3;
this.stepx = stepx;
this.stepy = stepy;
this.noBeacons = noBeacons;
this.checkBeacons = checkBeacons;
this.noMobSensors = noMobSensors;
this.noRLocators = noRLocators;
this.NoSensors = NoSensors;
this.iMaxX = iMaxX;
this.iMaxY = iMaxY;

sensors = null;
locators = null;
r = new Random((int)System.DateTime.Now.Ticks);
this.LocatorRadius = LocatorRadius;
this.BeaconRadius = BeaconRadius;
this.sangle1 = sangle1;
this.sangle2 = sangle2;
this.sangle3 = sangle3;
this.sangle4 = sangle4;
this.sangle5 = sangle5;
this.sangle6 = sangle6;
this.sangle7 = sangle7;
this.sangle8 = sangle8;
this.Sweepangle = Sweepangle;
// build the network
BuildNetwork();

BuildNetwork() method allows deploying of various network objects: sensor, locator and beacon
nodes as follows:
public void BuildNetwork()
{
aSensors = new ArrayList();
aLocators = new ArrayList();
aBeacons = new ArrayList();

//Array of Locators
//Array of Beacons

if (radio1 == true && radio2 == false)


{
if (combo1 == 4)
{
int m = 0;
while ((aLocators.Count) < 2)
{
if (m == 0)
{
Locator locator = new Locator(m, 20, 520, LocatorRadius, sangle1);
aLocators.Insert(m, locator);
}
else if (m == 1)
{
Locator locator = new Locator(m, 520, 520, LocatorRadius, sangle2);
aLocators.Insert(m, locator);
}
m++;

}
int s = 0, x = 70, y = 70;
while (aSensors.Count < 25)
{
WirelessSensor sensor = new WirelessSensor(x, y);
aSensors.Insert(s, sensor);
if (x < 470) x += 100;
else { x = 70; y += 100; }
s++;
}
}
else
{
int s = 0;
while (aSensors.Count < NoSensors)
{
WirelessSensor sensor = new WirelessSensor(r.Next(iMaxX - 10) + 5,
r.Next(iMaxY - 10) + 5);
aSensors.Insert(s, sensor);
}
}
}

s++;

//Adding Locators to network


if (combo1 == 0)
{
int m = 0;
while ((aLocators.Count) < noRLocators)
{
Locator locator = new Locator(m, r.Next(iMaxX - 10) + 5, r.Next(iMaxY - 10)
+ 5, LocatorRadius, r.Next(0, 315));
aLocators.Insert(m, locator);
m++;
}
}
else if (combo1 == 1)
{
int x = 20, y = 20;
int m = 0;
while ((aLocators.Count) < 8)
{
if (m == 0)
{
x = 20; y = 20;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle1);
aLocators.Insert(m, locator);
}
else if (m == 1)
{
x = 270; y = 20;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle2);
aLocators.Insert(m, locator);
}
else if (m == 2)
{
x = 520; y = 20;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle3);
aLocators.Insert(m, locator);
}
else if (m == 3)
{
x = 20; y = 270;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle4);
aLocators.Insert(m, locator);
}
else if (m == 4)
{
x = 520; y = 270;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle5);
aLocators.Insert(m, locator);
}
else if (m == 5)
{
x = 20; y = 520;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle6);
aLocators.Insert(m, locator);
}

else if (m == 6)
{
x = 270; y = 520;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle7);
aLocators.Insert(m, locator);
}
else if (m == 7)
{
x = 520; y = 520;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle8);
aLocators.Insert(m, locator);
}
m++;
}

}
else if (combo1 == 2)
{
int x = 20, y = 20;
int m = 0;
while ((aLocators.Count) < 5)
{
if (m == 0)
{
x = 270; y = 20;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle1);
aLocators.Insert(m, locator);
}
else if (m == 1)
{
x = 20; y = 270;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle2);
aLocators.Insert(m, locator);
}
else if (m == 2)
{
x = 270; y = 270;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle3);
aLocators.Insert(m, locator);
}
else if (m == 3)
{
x = 520; y = 270;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle4);
aLocators.Insert(m, locator);
}
else if (m == 4)
{
x = 270; y = 520;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle5);
aLocators.Insert(m, locator);
}
m++;

}
}
else if (combo1 == 3)
{
int x = 20, y = 20;
int m = 0;
while ((aLocators.Count) < 6)
{
if (m == 0)
{
x = 145; y = 100;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle1);
aLocators.Insert(m, locator);
}
else if (m == 1)
{
x = 270; y = 100;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle2);
aLocators.Insert(m, locator);
}
else if (m == 2)
{
x = 395; y = 100;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle3);
aLocators.Insert(m, locator);
}
else if (m == 3)
{
x = 145; y = 400;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle4);
aLocators.Insert(m, locator);
}
else if (m == 4)
{
x = 270; y = 400;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle5);
aLocators.Insert(m, locator);
}
else if (m == 5)
{
x = 395; y = 400;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle5);
aLocators.Insert(m, locator);
}
}
}

m++;

//Deployment of Beacons
if (checkBeacons == true)
{
int b = 0;
while (aBeacons.Count < noBeacons)
{
Beacon beacon = new Beacon(b, r.Next(iMaxX - 10), r.Next(iMaxY - 10),
BeaconRadius);
aBeacons.Insert(b, beacon);
b++;
}
}
}

2- Wireless Sensor Node Class


public class WirelessSensor
{

public
public
public
public

ArrayList
ArrayList
ArrayList
int x, y,

apkts = null; //packets between this node and any locator


bpkts = null; //packets between this node and any beacon
Connections;
dx, dy;
// the coordinates of the node

public WirelessSensor(int x, int y) //constructor for static sensor


{
this.x = x;
this.y = y;
Connections = new ArrayList();
}
public WirelessSensor(int x, int y, int dx, int dy) //Mobile sensor const.
{
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
Connections = new ArrayList();

}
}

3- Wireless Sensor Connection Class


If the distance between sensor node and other sensor node or locator node is less than the
transmission range, this class will be called.
// This class represents a communications link between two wireless devices.
public class WirelessSensorConnection
{
public
public
public
public
public
public
public

Locator sLocator;
Beacon sBeacon;
WirelessSensor sSender; // the upstream sensor
WirelessSensor sReceiver; // the downstream sensor
pkt pkt = null;
int Sangle;
//start angle of locator sector
short Prlevel;
//Power levels for PTA algorithm

//constructor for directed antenna SeRLoc-HiRLoc


public WirelessSensorConnection(Locator sLocator, WirelessSensor
Receiver, int Sangle)
{

this.sLocator = sLocator;
this.sReceiver = sReceiver;
this.Sangle = Sangle;

//constructor for omnidirectional antenna (Centroid algorithm)


public WirelessSensorConnection(Locator sLocator, WirelessSensor sReceiver)
{
this.sLocator = sLocator;
this.sReceiver = sReceiver;
}
//constructor for sensder,reciver and power level for PTA algorithm
public WirelessSensorConnection(Locator sLocator, WirelessSensor sReceiver,
short Prlevel)
{
this.sLocator = sLocator;
this.sReceiver = sReceiver;
this.Prlevel = Prlevel;
}
//constructor for beacon,locator and start angle for sector (DIL algorithm)
public WirelessSensorConnection(Beacon sBeacon, Locator sLocator,
WirelessSensor sReceiver, int Sangle)
{
this.sBeacon = sBeacon;
this.sLocator = sLocator;
this.sReceiver = sReceiver;
this.Sangle = Sangle;
}
}

4- Mobile Sensor Node Class


// This class represents (and manages) a list of mobile sensors.
public class Sensorlist
{
public ArrayList aSensors;
// the array of sensors
public Mutex mutexSensor; // a mutex to control access to the sensor list
private int iMaxX, iMaxY; // the maximum X and Y coordinates of a sensor
private Random r;
// a random number generator
public Sensorlist(int iMaxX, int iMaxY, Random r)
{
this.iMaxX = iMaxX;
this.iMaxY = iMaxY;
this.r = r;
aSensors = new ArrayList();
mutexSensor = new Mutex();
}
//Random movement (speed, direction)
// this function creates a new sensor and adds it to the sensor list
public void RandomMotion(int stepx, int stepy){
int iBorder = r.Next(4);
if (iBorder == 0) // add to left border, traveling right and up or down
aSensors.Add(new WirelessSensor(-5, r.Next(iMaxY), r.Next(stepx + 2) + stepx,
r.Next(stepy + 4) - stepy));
else if (iBorder == 1) // add to top border, traveling down and left or right
aSensors.Add(new WirelessSensor(r.Next(iMaxX), -5, r.Next(stepx + 4) - stepx,
r.Next(stepy + 2) + stepy));
else if (iBorder == 2) // add to right border, traveling left and up or down
aSensors.Add(new WirelessSensor(iMaxX + 5, r.Next(iMaxY), r.Next(stepx) stepx, r.Next(stepy + 4) - stepy));
else if (iBorder == 3) // add to bottom border, traveling up and left or right
aSensors.Add(new WirelessSensor(r.Next(iMaxX), iMaxY + 5, r.Next(stepx + 4) stepx, r.Next(stepy) - stepy));
}
//straight movement (directions: right,left,top or down)
//speed: 1 step(dx or dy) = 8.333 m/sec = 30 km/hour, 2 step = 16.666 m/sec
= //60 km/hour, and so on
public void DirectedMotion1(int stepx, int stepy)
{
int iBorder = r.Next(4); //four borders
if (iBorder == 0) // add to left border, traveling right
aSensors.Add(new WirelessSensor(20, r.Next(iMaxY), stepx, 0));
else if (iBorder == 1) // add to top border, traveling down
aSensors.Add(new WirelessSensor(r.Next(iMaxX), 20, 0, stepy));
else if (iBorder == 2) // add to right border, traveling left
aSensors.Add(new WirelessSensor(520, r.Next(iMaxY), -stepx, 0));
else if (iBorder == 3) // add to bottom border, traveling up
aSensors.Add(new WirelessSensor(r.Next(iMaxX), 520, 0, -stepy));
}

//straight movement (directions: right,left,top or down)


public void DirectedMotion2(int stepx, int stepy)
{
aSensors.Add(new WirelessSensor(r.Next(20, 520), r.Next(20, 520), stepx,
stepy));
}
//straight movement (directions: right,left,top or down)
public void DirectedMotion3(int stepx, int stepy){
aSensors.Add(new WirelessSensor(r.Next(20, 520), r.Next(20, 520), stepx,
stepy));
int iBorder = r.Next(4);
if (iBorder == 0)
aSensors.Add(new WirelessSensor(r.Next(iMaxX), r.Next(iMaxY), stepx, 0));
else if (iBorder == 1)
aSensors.Add(new WirelessSensor(r.Next(iMaxX), r.Next(iMaxY), 0, stepy));
else if (iBorder == 2)
aSensors.Add(new WirelessSensor(r.Next(iMaxX), r.Next(iMaxY), -stepx, 0));
else if (iBorder == 3)
aSensors.Add(new WirelessSensor(r.Next(iMaxX), r.Next(iMaxY), 0, -stepy));
}
// the following function moves the sensors around, and removes those that
have //traveled out of bounds
public void Update1()
//directed motion
{
ArrayList aRemoveSensors = new ArrayList();
foreach (WirelessSensor sensor in aSensors)
{
if ((sensor.y == 20 || sensor.y == 80 || sensor.y == 140 || sensor.y == 200 ||
sensor.y == 260 || sensor.y == 320 || sensor.y == 380 || sensor.y == 440 ||
sensor.y == 500))
sensor.x += sensor.dx;
else if ((sensor.y == 50 || sensor.y == 110 || sensor.y == 170 || sensor.y ==
230 || sensor.y == 290 || sensor.y == 350 || sensor.y == 410 || sensor.y ==
470 || sensor.y == 530))
sensor.x -= sensor.dx;
if (sensor.x >= 520)
sensor.y += 30;
else if (sensor.x <= 20)
sensor.y += 30;
if ((sensor.x < -5) || (sensor.y < -5) || (sensor.x > iMaxX + 5) || (sensor.y
> iMaxY + 5))
aRemoveSensors.Add(sensor);
}
foreach (WirelessSensor sensor in aRemoveSensors)
aSensors.Remove(sensor);
}

// this function moves the sensors around, and removes those that have
traveled out of bounds
public void Update()
//traditional motion
{
ArrayList aRemoveSensors = new ArrayList();
foreach (WirelessSensor sensor in aSensors)
{
sensor.x += sensor.dx;
sensor.y += sensor.dy;
if ((sensor.x < -5) || (sensor.y < -5) || (sensor.x >
iMaxX + 5) || (sensor.y > iMaxY + 5))
aRemoveSensors.Add(sensor);
}
foreach (WirelessSensor sensor in aRemoveSensors)
aSensors.Remove(sensor);
}
}

5- Packet Class
public class pkt
{
public int x, y, sangle;
public int ID;
public short prlevel;
public pkt(int ID, int x, int y)
//Omnidirectional antenna
{
this.ID = ID;
this.x = x;
this.y = y;
}
public pkt(int ID, int x, int y, int sangle) //Directed antenna
{
this.ID = ID;
this.x = x;
this.y = y;
this.sangle = sangle;
}
public pkt(int ID, int x, int y, short prlevel) //omin-power levels
{
this.prlevel = prlevel;
this.ID = ID;
this.x = x;
this.y = y;
}
}

6- Locator Node Class


Locator node is a sensor node but with larger transmission range and known posirion.
public class Locator
{
public int id, x, y, dx, dy, sangle, dsangle;
public int LocatorRadius;
public short prlevel;
public Locator(int id, int x, int y, int LocatorRadius, int sangle)
{
this.x = x;
this.y = y;
this.id = id;
this.LocatorRadius = LocatorRadius;
this.sangle = sangle;
}
}

7- Beacon Node Class


Beacon node the same as locator node. Used with DIL algorithm.
public class Beacon
{
public int id, x, y;
public int BeaconRadius;
public Beacon(int id, int x, int y, int BeaconRadius)
{
this.x = x;
this.y = y;
this.id = id;
this.BeaconRadius = BeaconRadius;
}
}

The second class of simulator component is developer interface class which


contains all control functions (e.g. run and stop simulator, painting method,
timer event handler,etc.).

Paint method works as follows:


private void picNetwork_Paint(object sender, System.Windows.Forms.PaintEventArgs e){
if (network != null) {
network.bPainting = true;
network.bPaint = false;
}
Font font = new Font("Times New Roman", 7.0f);
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
// draw background
g.FillRectangle(System.Drawing.Brushes.FloralWhite, e.ClipRectangle);
// draw network objects (if network has been deployed)
if (network != null) {
// draw static sensor nodes
int j = 0;
foreach (networkdll.Class1.WirelessSensor sensor in network.aSensors)
{
g.DrawEllipse(Pens.Blue, sensor.x - 4, sensor.y - 4, 9, 9);
g.FillEllipse(Brushes.Azure, sensor.x - 4, sensor.y - 4, 9, 9);
j++;
}
// draw mobile sensor nodes
if ((network.bRunningSimulation == true) && (network.sensors != null))
{
network.sensors.mutexSensor.WaitOne();
foreach (networkdll.Class1.WirelessSensor sensor in network.sensors.aSensors)
{
g.DrawEllipse(Pens.Blue, sensor.x - 4, sensor.y - 4, 9, 9);
g.FillEllipse(Brushes.Azure, sensor.x - 4, sensor.y - 4, 9, 9);

}
network.sensors.mutexSensor.ReleaseMutex();

Pen
Pen
Pen
Pen
Pen
Pen

locatorPen
sectorPen1
sectorPen2
sectorPen3
sectorPen4
sectorPen5

=
=
=
=
=
=

Pens.Black;
Pens.Green;
Pens.Brown;
Pens.Red;
Pens.Orange;
Pens.Olive;

Brush lbrush = Brushes.Red;


Brush lBrush = Brushes.CadetBlue;
Brush Brush = Brushes.Black;
int prsep = 50; //separation between power levels
if (network.LocatorRadius > 100)
{
if (network.combo3 == 0) prsep = 50;
else if (network.combo3 == 1) prsep = 20;
}
else
{
if (network.combo3 == 0) prsep = 10;
else if (network.combo3 == 1) prsep = 5;
}
//draw Locator nodes
foreach (networkdll.Class1.Locator locator in network.aLocators)
{
if (int.Parse(textBox10.Text) > 8 && comboBox1.SelectedIndex==0)
{
if (checkBox6.Checked) g.DrawPie(sectorPen1, locator.x - network.LocatorRadius,
locator.y - network.LocatorRadius, network.LocatorRadius * 2, network.LocatorRadius
* 2, network.sangle1, network.Sweepangle);
if (checkBox14.Checked)
{
g.DrawEllipse(sectorPen2, locator.x - int.Parse(textBox8.Text) + prsep, locator.y
- int.Parse(textBox8.Text) + prsep, (int.Parse(textBox8.Text) - prsep) * 2,
(int.Parse(textBox8.Text) - prsep) * 2);
g.DrawEllipse(sectorPen3, locator.x - int.Parse(textBox8.Text) + 2 * prsep,
locator.y - int.Parse(textBox8.Text) + 2 * prsep, (int.Parse(textBox8.Text) - 2 *
prsep) * 2, (int.Parse(textBox8.Text) - 2 * prsep) * 2);
g.DrawEllipse(sectorPen4, locator.x - int.Parse(textBox8.Text) + 3 * prsep,
locator.y - int.Parse(textBox8.Text) + 3 * prsep, (int.Parse(textBox8.Text) - 3 *
prsep) * 2, (int.Parse(textBox8.Text) - 3 * prsep) * 2);
g.DrawEllipse(sectorPen5, locator.x - int.Parse(textBox8.Text) + 4 * prsep,
locator.y - int.Parse(textBox8.Text) + 4 * prsep, (int.Parse(textBox8.Text) - 4 *
prsep) * 2, (int.Parse(textBox8.Text) - 4 * prsep) * 2);
}

g.DrawEllipse(locatorPen, locator.x - 5, locator.y - 5, 10, 10);


g.FillEllipse(lBrush, locator.x - 5, locator.y - 5, 10, 10);
g.DrawString(locator.id.ToString(), font, lbrush, locator.x + 5, locator.y + 5);
g.DrawString("(" + locator.x + " , " + locator.y + ")", font, lbrush, locator.x
+ 5, locator.y - 15);
}
else
{

if (locator.id == 0)
{
if (checkBox5.Checked) g.DrawPie(sectorPen1, locator.x - network.LocatorRadius,
locator.y - network.LocatorRadius, network.LocatorRadius * 2, network.LocatorRadius
* 2, network.sangle1, network.Sweepangle);
if (checkBox6.Checked) g.DrawEllipse(sectorPen1, locator.x - network.LocatorRadius,
locator.y - network.LocatorRadius, network.LocatorRadius * 2, network.LocatorRadius
* 2);
if (checkBox14.Checked)
{
g.DrawEllipse(sectorPen2, locator.x - network.LocatorRadius + prsep, locator.y network.LocatorRadius + prsep, (network.LocatorRadius - prsep) * 2,
(network.LocatorRadius - prsep) * 2);
g.DrawEllipse(sectorPen3, locator.x - network.LocatorRadius + 2 * prsep, locator.y network.LocatorRadius + 2 * prsep, (network.LocatorRadius - 2 * prsep) * 2,
(network.LocatorRadius - 2 * prsep) * 2);
g.DrawEllipse(sectorPen4, locator.x - network.LocatorRadius + 3 * prsep, locator.y network.LocatorRadius + 3 * prsep, (network.LocatorRadius - 3 * prsep) * 2,
(network.LocatorRadius - 3 * prsep) * 2);
g.DrawEllipse(sectorPen5, locator.x - network.LocatorRadius + 4 * prsep, locator.y network.LocatorRadius + 4 * prsep, (network.LocatorRadius - 4 * prsep) * 2,
(network.LocatorRadius - 4 * prsep) * 2);
}
}
if (locator.id == 1)
{
if (checkBox5.Checked) g.DrawPie(sectorPen1, locator.x - network.LocatorRadius,
locator.y - network.LocatorRadius, network.LocatorRadius * 2, network.LocatorRadius
* 2, network.sangle2, network.Sweepangle);
if (checkBox6.Checked) g.DrawEllipse(sectorPen1, locator.x - network.LocatorRadius,
locator.y - network.LocatorRadius, network.LocatorRadius * 2, network.LocatorRadius
* 2);

if (checkBox14.Checked)
{
g.DrawEllipse(sectorPen2, locator.x - network.LocatorRadius + prsep, locator.y network.LocatorRadius + prsep, (network.LocatorRadius - prsep) * 2,
(network.LocatorRadius - prsep) * 2);
g.DrawEllipse(sectorPen3, locator.x - network.LocatorRadius + 2 * prsep,
locator.y - network.LocatorRadius + 2 * prsep, (network.LocatorRadius - 2 * prsep) *
2, (network.LocatorRadius - 2 * prsep) * 2);
g.DrawEllipse(sectorPen4, locator.x - network.LocatorRadius + 3 * prsep, locator.y
- network.LocatorRadius + 3 * prsep, (network.LocatorRadius - 3 * prsep) * 2,
(network.LocatorRadius - 3 * prsep) * 2);
g.DrawEllipse(sectorPen5, locator.x - network.LocatorRadius + 4 * prsep, locator.y
- network.LocatorRadius + 4 * prsep, (network.LocatorRadius - 4 * prsep) * 2,
(network.LocatorRadius - 4 * prsep) * 2);
}
}
if (locator.id == 2)
{
if (checkBox5.Checked) g.DrawPie(sectorPen1, locator.x - network.LocatorRadius,
locator.y - network.LocatorRadius, network.LocatorRadius * 2, network.LocatorRadius
* 2, network.sangle3, network.Sweepangle);
if (checkBox6.Checked) g.DrawEllipse(sectorPen1, locator.x - network.LocatorRadius,
locator.y - network.LocatorRadius, network.LocatorRadius * 2, network.LocatorRadius
* 2);
if (checkBox14.Checked)
{
g.DrawEllipse(sectorPen2, locator.x - network.LocatorRadius + prsep, locator.y network.LocatorRadius + prsep, (network.LocatorRadius - prsep) * 2,
(network.LocatorRadius - prsep) * 2);
g.DrawEllipse(sectorPen3, locator.x - network.LocatorRadius + 2 * prsep, locator.y
- network.LocatorRadius + 2 * prsep, (network.LocatorRadius - 2 * prsep) * 2,
(network.LocatorRadius - 2 * prsep) * 2);
g.DrawEllipse(sectorPen4, locator.x - network.LocatorRadius + 3 * prsep, locator.y
- network.LocatorRadius + 3 * prsep, (network.LocatorRadius - 3 * prsep) * 2,
(network.LocatorRadius - 3 * prsep) * 2);
g.DrawEllipse(sectorPen5, locator.x - network.LocatorRadius + 4 * prsep, locator.y network.LocatorRadius + 4 * prsep, (network.LocatorRadius - 4 * prsep) * 2,
(network.LocatorRadius - 4 * prsep) * 2);
}
}
if (locator.id == 3)
{
if (checkBox5.Checked) g.DrawPie(sectorPen1, locator.x - network.LocatorRadius,
locator.y - network.LocatorRadius, network.LocatorRadius * 2, network.LocatorRadius
* 2, network.sangle4, network.Sweepangle);
if (checkBox6.Checked) g.DrawEllipse(sectorPen1, locator.x - network.LocatorRadius,
locator.y - network.LocatorRadius, network.LocatorRadius * 2, network.LocatorRadius
* 2);

if (checkBox14.Checked)
{
g.DrawEllipse(sectorPen2, locator.x - network.LocatorRadius + prsep, locator.y network.LocatorRadius + prsep, (network.LocatorRadius - prsep) * 2,
(network.LocatorRadius - prsep) * 2);
g.DrawEllipse(sectorPen3, locator.x - network.LocatorRadius + 2 * prsep, locator.y network.LocatorRadius + 2 * prsep, (network.LocatorRadius - 2 * prsep) * 2,
(network.LocatorRadius - 2 * prsep) * 2);
g.DrawEllipse(sectorPen4, locator.x - network.LocatorRadius + 3 * prsep, locator.y
- network.LocatorRadius + 3 * prsep, (network.LocatorRadius - 3 * prsep) * 2,
(network.LocatorRadius - 3 * prsep) * 2);
g.DrawEllipse(sectorPen5, locator.x - network.LocatorRadius + 4 * prsep, locator.y network.LocatorRadius + 4 * prsep, (network.LocatorRadius - 4 * prsep) * 2,
(network.LocatorRadius - 4 * prsep) * 2);
}
}
if (locator.id == 4)
{
if (checkBox5.Checked) g.DrawPie(sectorPen1, locator.x - network.LocatorRadius,
locator.y - network.LocatorRadius, network.LocatorRadius * 2, network.LocatorRadius
* 2, network.sangle5, network.Sweepangle);
if (checkBox6.Checked) g.DrawEllipse(sectorPen1, locator.x - network.LocatorRadius,
locator.y - network.LocatorRadius, network.LocatorRadius * 2, network.LocatorRadius
* 2);
if (checkBox14.Checked)
{
g.DrawEllipse(sectorPen2, locator.x - network.LocatorRadius + prsep, locator.y network.LocatorRadius + prsep, (network.LocatorRadius - prsep) * 2,
(network.LocatorRadius - prsep) * 2);
g.DrawEllipse(sectorPen3, locator.x - network.LocatorRadius + 2 * prsep, locator.y
- network.LocatorRadius + 2 * prsep, (network.LocatorRadius - 2 * prsep) * 2,
(network.LocatorRadius - 2 * prsep) * 2);
g.DrawEllipse(sectorPen4, locator.x - network.LocatorRadius + 3 * prsep, locator.y
- network.LocatorRadius + 3 * prsep, (network.LocatorRadius - 3 * prsep) * 2,
(network.LocatorRadius - 3 * prsep) * 2);
g.DrawEllipse(sectorPen5, locator.x - network.LocatorRadius + 4 * prsep, locator.y
- network.LocatorRadius + 4 * prsep, (network.LocatorRadius - 4 * prsep) * 2,
(network.LocatorRadius - 4 * prsep) * 2);
}
}

if (locator.id == 5)
{
if (checkBox5.Checked) g.DrawPie(sectorPen1, locator.x - network.LocatorRadius,
locator.y - network.LocatorRadius, network.LocatorRadius * 2, network.LocatorRadius
* 2, network.sangle6, network.Sweepangle);
if (checkBox6.Checked) g.DrawEllipse(sectorPen1, locator.x - network.LocatorRadius,
locator.y - network.LocatorRadius, network.LocatorRadius * 2, network.LocatorRadius
* 2);
if (checkBox14.Checked)
{
g.DrawEllipse(sectorPen2, locator.x - network.LocatorRadius + prsep, locator.y network.LocatorRadius + prsep, (network.LocatorRadius - prsep) * 2,
(network.LocatorRadius - prsep) * 2);
g.DrawEllipse(sectorPen3, locator.x - network.LocatorRadius + 2 * prsep, locator.y network.LocatorRadius + 2 * prsep, (network.LocatorRadius - 2 * prsep) * 2,
(network.LocatorRadius - 2 * prsep) * 2);
g.DrawEllipse(sectorPen4, locator.x - network.LocatorRadius + 3 * prsep, locator.y
- network.LocatorRadius + 3 * prsep, (network.LocatorRadius - 3 * prsep) * 2,
(network.LocatorRadius - 3 * prsep) * 2);
g.DrawEllipse(sectorPen5, locator.x - network.LocatorRadius + 4 * prsep, locator.y
- network.LocatorRadius + 4 * prsep, (network.LocatorRadius - 4 * prsep) * 2,
(network.LocatorRadius - 4 * prsep) * 2);
}
}
if (locator.id == 6)
{
if (checkBox5.Checked) g.DrawPie(sectorPen1, locator.x - network.LocatorRadius,
locator.y - network.LocatorRadius, network.LocatorRadius * 2, network.LocatorRadius
* 2, network.sangle7, network.Sweepangle);
if (checkBox6.Checked) g.DrawEllipse(sectorPen1, locator.x - network.LocatorRadius,
locator.y - network.LocatorRadius, network.LocatorRadius * 2, network.LocatorRadius
* 2);
if (checkBox14.Checked)
{
g.DrawEllipse(sectorPen2, locator.x - network.LocatorRadius + prsep, locator.y network.LocatorRadius + prsep, (network.LocatorRadius - prsep) * 2,
(network.LocatorRadius - prsep) * 2);
g.DrawEllipse(sectorPen3, locator.x - network.LocatorRadius + 2 * prsep, locator.y network.LocatorRadius + 2 * prsep, (network.LocatorRadius - 2 * prsep) * 2,
(network.LocatorRadius - 2 * prsep) * 2);
g.DrawEllipse(sectorPen4, locator.x - network.LocatorRadius + 3 * prsep, locator.y network.LocatorRadius + 3 * prsep, (network.LocatorRadius - 3 * prsep) * 2,
(network.LocatorRadius - 3 * prsep) * 2);

g.DrawEllipse(sectorPen5, locator.x - network.LocatorRadius + 4 * prsep, locator.y network.LocatorRadius + 4 * prsep, (network.LocatorRadius - 4 * prsep) * 2,


(network.LocatorRadius - 4 * prsep) * 2);
}
}
if (locator.id == 7)
{
if (checkBox5.Checked) g.DrawPie(sectorPen1, locator.x - network.LocatorRadius,
locator.y - network.LocatorRadius, network.LocatorRadius * 2, network.LocatorRadius
* 2, network.sangle8, network.Sweepangle);
if (checkBox6.Checked) g.DrawEllipse(sectorPen1, locator.x - network.LocatorRadius,
locator.y - network.LocatorRadius, network.LocatorRadius * 2, network.LocatorRadius
* 2);
if (checkBox14.Checked)
{
g.DrawEllipse(sectorPen2, locator.x - network.LocatorRadius + prsep, locator.y network.LocatorRadius + prsep, (network.LocatorRadius - prsep) * 2,
(network.LocatorRadius - prsep) * 2);
g.DrawEllipse(sectorPen3, locator.x - network.LocatorRadius + 2 * prsep, locator.y network.LocatorRadius + 2 * prsep, (network.LocatorRadius - 2 * prsep) * 2,
(network.LocatorRadius - 2 * prsep) * 2);
g.DrawEllipse(sectorPen4, locator.x - network.LocatorRadius + 3 * prsep, locator.y network.LocatorRadius + 3 * prsep, (network.LocatorRadius - 3 * prsep) * 2,
(network.LocatorRadius - 3 * prsep) * 2);
g.DrawEllipse(sectorPen5, locator.x - network.LocatorRadius + 4 * prsep, locator.y network.LocatorRadius + 4 * prsep, (network.LocatorRadius - 4 * prsep) * 2,
(network.LocatorRadius - 4 * prsep) * 2);
}
}
g.DrawEllipse(locatorPen, locator.x - 5, locator.y - 5, 10, 10);
g.FillEllipse(lBrush, locator.x - 5, locator.y - 5, 10, 10);
g.DrawString(locator.id.ToString(), font, lbrush, locator.x + 5, locator.y + 5);
g.DrawString("(" + locator.x + " , " + locator.y + ")", font, lbrush, locator.x +
5, locator.y - 15);
}
}

//Draw Beacon Nodes


foreach (networkdll.Class1.Beacon beacon in network.aBeacons)
{
if (beacon.id < network.noBeacons)
g.DrawEllipse(sectorPen1, beacon.x - network.BeaconRadius, beacon.y network.BeaconRadius, network.BeaconRadius * 2, network.BeaconRadius * 2);
g.FillEllipse(lbrush, beacon.x - 5, beacon.y - 5, 10, 10);
g.DrawString(beacon.id.ToString(), font, Brush, beacon.x + 5, beacon.y + 5);
}
if (radioButton1.Checked)
//static
{
if (checkBox1.Checked)
//Estimate Position
{
if (checkBox11.Checked)
{
//here developers are allowed to call the constructor of their localization
//algorithms
HiRLoc.HiRLoc algorithm3 = new HiRLoc.HiRLoc(sender, e, network.aSensors,
network.aLocators, checkBox2.Checked, checkBox3.Checked, checkBox4.Checked,
checkBox7.Checked, checkBox8.Checked, checkBox9.Checked, checkBox10.Checked,
network.LocatorRadius, network.Sweepangle, network.sangle1, network.sangle2,
network.sangle3, network.sangle4, network.sangle5, network.sangle6, network.sangle7,
network.sangle8, txtOutputFolder.Text); //HiRLoc Algorithm
}
if (checkBox12.Checked)
{
SeRLoc.SeRLoc algorithm2 = new SeRLoc.SeRLoc(sender, e, network.aSensors,
network.aLocators, checkBox2.Checked, checkBox3.Checked, checkBox4.Checked,
checkBox7.Checked, checkBox8.Checked, checkBox9.Checked, checkBox10.Checked,
network.LocatorRadius, network.Sweepangle, network.sangle1, network.sangle2,
network.sangle3, network.sangle4, network.sangle5, network.sangle6, network.sangle7,
network.sangle8, txtOutputFolder.Text); //SeRLoc Algorithm
}
if (checkBox13.Checked)
{
centroid.centroid algorithm1 = new centroid.centroid(sender, e, network.aSensors,
network.aLocators, checkBox2.Checked, checkBox3.Checked, checkBox4.Checked,
checkBox7.Checked, checkBox8.Checked, checkBox9.Checked, checkBox10.Checked,
network.LocatorRadius, txtOutputFolder.Text); //Centroid Algorithm
}
if (checkBox14.Checked)
{
PTA.PTA algorithm6 = new PTA.PTA(sender, e, network.aSensors, network.aLocators,/*
checkBox2.Checked,*/ checkBox14.Checked, network.LocatorRadius,
comboBox3.SelectedIndex, txtOutputFolder.Text); //PTA Algorithm
}
if (checkBox15.Checked)
{
ADLA.ADLA algorithm4 = new ADLA.ADLA(sender, e, network.aSensors, network.aLocators,
radioButton3.Checked, radioButton4.Checked, int.Parse(textBox3.Text),
int.Parse(textBox4.Text), int.Parse(textBox5.Text), network.LocatorRadius,
network.Sweepangle, network.sangle1, network.sangle2, network.sangle3,
network.sangle4, network.sangle5, network.sangle6, network.sangle7, network.sangle8,
txtOutputFolder.Text); //ADLA Algorithm
}

if (checkBox16.Checked)
{
HADLA.HADLA algorithm5 = new HADLA.HADLA(sender, e, network.aSensors,
network.aLocators, radioButton3.Checked, radioButton4.Checked,
int.Parse(textBox3.Text), int.Parse(textBox4.Text), int.Parse(textBox5.Text),
network.LocatorRadius, network.Sweepangle, network.sangle1, network.sangle2,
network.sangle3, network.sangle4, network.sangle5, network.sangle6, network.sangle7,
network.sangle8, txtOutputFolder.Text); //HADLA Algorithm
}
if (checkBox17.Checked)
{
DIL.DIL algorithm7 = new DIL.DIL(sender, e, network.aSensors, network.aLocators,
network.aBeacons, radioButton3.Checked, radioButton4.Checked,
int.Parse(textBox3.Text), double.Parse(textBox4.Text), int.Parse(textBox5.Text),
network.LocatorRadius, network.BeaconRadius, network.Sweepangle, network.sangle1,
network.sangle2, network.sangle3, network.sangle4, network.sangle5, network.sangle6,
network.sangle7, network.sangle8, txtOutputFolder.Text); //DIL Algorithm
}
if (checkBox18.Checked)
{
RAL.RAL algorithm8 = new RAL.RAL(sender, e, network.aSensors, network.aLocators,
checkBox1.Checked, network.LocatorRadius, network.Sweepangle,network.sangle1,
network.sangle2, network.sangle3, network.sangle4, network.sangle5, network.sangle6,
network.sangle7, network.sangle8, txtOutputFolder.Text); //RAL Algorithm
}
network.BeaconRadius = trackBar1.Value;
network.LocatorRadius = trackLocatorRadius.Value;
network.Sweepangle = trackSweepAngle.Value;
network.sangle1 = tracksangle1.Value;
network.sangle2 = tracksangle2.Value;
network.sangle3 = tracksangle3.Value;
network.sangle4 = tracksangle4.Value;
network.sangle5 = tracksangle5.Value;
network.sangle6 = tracksangle6.Value;
network.sangle7 = tracksangle7.Value;
network.sangle8 = tracksangle8.Value;
network.combo3 = comboBox3.SelectedIndex;
label1.Text = trackLocatorRadius.Value.ToString();
label2.Text = trackBar1.Value.ToString();
label3.Text = trackSweepAngle.Value.ToString();
}
}

if (radioButton2.Checked)
//Mobile
{
if (checkBox1.Checked)
//Estimate Position
{
network.sensors.mutexSensor.WaitOne();
if (checkBox11.Checked)
{
HiRLoc.HiRLoc algorithm3 = new HiRLoc.HiRLoc(sender, e, network.sensors.aSensors,
network.aLocators, checkBox2.Checked, checkBox3.Checked, checkBox4.Checked,
checkBox7.Checked, checkBox8.Checked, checkBox9.Checked, checkBox10.Checked,
network.LocatorRadius, network.Sweepangle, network.sangle1, network.sangle2,
network.sangle3, network.sangle4, network.sangle5, network.sangle6, network.sangle7,
network.sangle8, txtOutputFolder.Text); //HiRLoc Algorithm
}
if (checkBox12.Checked)
{
SeRLoc.SeRLoc algorithm2 = new SeRLoc.SeRLoc(sender, e, network.sensors.aSensors,
network.aLocators, checkBox2.Checked, checkBox3.Checked, checkBox4.Checked,
checkBox7.Checked, checkBox8.Checked, checkBox9.Checked, checkBox10.Checked,
network.LocatorRadius, network.Sweepangle, network.sangle1, network.sangle2,
network.sangle3, network.sangle4, network.sangle5, network.sangle6, network.sangle7,
network.sangle8, txtOutputFolder.Text); //SeRLoc Algorithm
}
if (checkBox13.Checked)
{
centroid.centroid algorithm1 = new centroid.centroid(sender, e,
network.sensors.aSensors, network.aLocators, checkBox2.Checked, checkBox3.Checked,
checkBox4.Checked, checkBox7.Checked, checkBox8.Checked, checkBox9.Checked,
checkBox10.Checked, network.LocatorRadius, txtOutputFolder.Text);//Centroid
//Algorithm
}
if (checkBox14.Checked)
{
PTA.PTA algorithm6 = new PTA.PTA(sender, e, network.sensors.aSensors,
network.aLocators, checkBox14.Checked, network.LocatorRadius,
comboBox3.SelectedIndex, txtOutputFolder.Text); //PTA Algorithm
}
if (checkBox15.Checked)
{
ADLA.ADLA algorithm4 = new ADLA.ADLA(sender, e, network.sensors.aSensors,
network.aLocators, radioButton3.Checked, radioButton4.Checked,
int.Parse(textBox3.Text), int.Parse(textBox4.Text), int.Parse(textBox5.Text),
network.LocatorRadius, network.Sweepangle, network.sangle1, network.sangle2,
network.sangle3, network.sangle4, network.sangle5, network.sangle6, network.sangle7,
network.sangle8, txtOutputFolder.Text); //ADLA Algorithm
}
if (checkBox16.Checked)
{
HADLA.HADLA algorithm5 = new HADLA.HADLA(sender, e, network.sensors.aSensors,
network.aLocators, radioButton3.Checked, radioButton4.Checked,
int.Parse(textBox3.Text), int.Parse(textBox4.Text), int.Parse(textBox5.Text),
network.LocatorRadius, network.Sweepangle, network.sangle1, network.sangle2,
network.sangle3, network.sangle4, network.sangle5, network.sangle6, network.sangle7,
network.sangle8, txtOutputFolder.Text); //HADLA Algorithm
}

if (checkBox17.Checked)
{
DIL.DIL algorithm7 = new DIL.DIL(sender, e, network.sensors.aSensors,
network.aLocators, network.aBeacons, radioButton3.Checked, radioButton4.Checked,
int.Parse(textBox3.Text), int.Parse(textBox4.Text), int.Parse(textBox5.Text),
network.LocatorRadius, network.BeaconRadius, network.Sweepangle, network.sangle1,
network.sangle2, network.sangle3, network.sangle4, network.sangle5, network.sangle6,
network.sangle7, network.sangle8, txtOutputFolder.Text); //DIL Algorithm
}
if (checkBox18.Checked)
{
RAL.RAL algorithm8 = new RAL.RAL(sender, e, network.sensors.aSensors,
network.aLocators, checkBox1.Checked, network.LocatorRadius, network.Sweepangle,
network.sangle1, network.sangle2, network.sangle3, network.sangle4, network.sangle5,
network.sangle6, network.sangle7, network.sangle8, txtOutputFolder.Text);
//RAL Algorithm
}
network.sensors.mutexSensor.ReleaseMutex();
network.BeaconRadius = trackBar1.Value;
network.LocatorRadius = trackLocatorRadius.Value;
network.Sweepangle = trackSweepAngle.Value;
network.sangle1 = tracksangle1.Value;
network.sangle2 = tracksangle2.Value;
network.sangle3 = tracksangle3.Value;
network.sangle4 = tracksangle4.Value;
network.sangle5 = tracksangle5.Value;
network.sangle6 = tracksangle6.Value;
network.sangle7 = tracksangle7.Value;
network.sangle8 = tracksangle8.Value;
network.combo3 = comboBox3.SelectedIndex;
label1.Text = trackLocatorRadius.Value.ToString();
label2.Text = trackBar1.Value.ToString();
label3.Text = trackSweepAngle.Value.ToString();
}
}

}
// finish painting
if (network != null)
network.bPainting = false;

What is going on inside the code snippets?


The second component of our framework is localization algorithm that is written in DLL file by
a developer then referencing it to the simulator and running it. We take a sample localization
algorithm as an example to show how the code actually works.

High Resolution Localization algorithm (HiRLoc)


Any localization algorithm consists of four methods: set_parameters, connect, send and localize.
These methods differ from one to another.
Localization algorithm constructor:
public HiRLoc(object sender, PaintEventArgs e, ArrayList asensor, ArrayList
alocator, bool checkBox2, bool checkBox3, bool checkBox4, bool checkBox5, bool
checkBox6, bool checkBox7, bool checkBox8, float locatorRadius, float sweepangle,
int sangle1, int sangle2, int sangle3, int sangle4, int sangle5, int sangle6, int
sangle7, int sangle8, String textbox)
{
set_parameters(locatorRadius, sweepangle, sangle1, sangle2, sangle3, sangle4,
sangle5, sangle6, sangle7, sangle8);
connect(asensor, alocator);
send(asensor);
localiza(sender, e, asensor, checkBox2, checkBox3, checkBox4, checkBox5,
checkBox6, checkBox7, checkBox8, locatorRadius, textbox);
}

1- Set_parameters method, used to receive initialization parameters from user interface:


public void set_parameters(float locatorRadius, float sweepangle, int sangle1, int
sangle2, int sangle3, int sangle4, int sangle5, int sangle6, int sangle7, int
sangle8)
{
this.locatorRadius = locatorRadius;
this.sweepangle = sweepangle;
this.sangle1 = sangle1;
this.sangle2 = sangle2;
this.sangle3 = sangle3;
this.sangle4 = sangle4;
this.sangle5 = sangle5;
this.sangle6 = sangle6;
this.sangle7 = sangle7;
this.sangle8 = sangle8;
}

2- Connect method, used to test the connectivity between sensor node and locator node.
public void connect(ArrayList asensor, ArrayList alocator)
{
for (int j = 0; j < asensor.Count; j++)
{
networkdll.Class1.WirelessSensor jSensor =
networkdll.Class1.WirelessSensor)asensor[j];
jSensor.apkts = new ArrayList();
jSensor.Connections = new ArrayList();
for (int i = 0; i < alocator.Count; i++)
{
networkdll.Class1.Locator iLocator =
(networkdll.Class1.Locator)alocator[i];
if (i == 0) iLocator.sangle = sangle1;
if (i == 1) iLocator.sangle = sangle2;
if (i == 2) iLocator.sangle = sangle3;
if (i == 3) iLocator.sangle = sangle4;
if (i == 4) iLocator.sangle = sangle5;
if (i == 5) iLocator.sangle = sangle6;
if (i == 6) iLocator.sangle = sangle7;
if (i == 7) iLocator.sangle = sangle8;
float lsRadius = (float)(Math.Sqrt(Math.Pow(iLocator.x - jSensor.x, 2) +
Math.Pow(iLocator.y - jSensor.y, 2)));
int theta1 = (int)Math.Floor((Math.Acos(Math.Abs(jSensor.x - iLocator.x) /
lsRadius) * (180 * 7 / 22)));
int theta = (int)Math.Floor((Math.Acos(Math.Abs(jSensor.y - iLocator.y) / lsRadius)
* (180 * 7 / 22)));
int theta2 = theta + 90;
int theta3 = theta1 + 180;
int theta4 = 270 + theta;
start = System.DateTime.Now.Ticks;
if (lsRadius <= locatorRadius)
{
int res = 5;
//resolution for rotation
if (jSensor.x >= iLocator.x && jSensor.y >= iLocator.y) //Region 1
{
if (theta1 >= iLocator.sangle && theta1 <= (iLocator.sangle + sweepangle))
{
if (theta1 == iLocator.sangle && theta1 <= (iLocator.sangle + sweepangle))
{
jSensor.Connections.Add(new networkdll.Class1.WirelessSensorConnection(iLocator,
jSensor, (iLocator.sangle - res)));
}

else
{
for (int k = 1; k < sweepangle; k++)
{
if (theta1 == iLocator.sangle)
{
jSensor.Connections.Add(new networkdll.Class1.WirelessSensorConnection(iLocator,
jSensor, (iLocator.sangle - res))); break;//,iLocator.sweep));
}
else iLocator.sangle = (iLocator.sangle + 1);
}
}
}
}
else if (jSensor.x < iLocator.x && jSensor.y >= iLocator.y) //Region 2
{
if (theta2 >= iLocator.sangle && theta2 <= (iLocator.sangle + sweepangle))
{
if (theta2 == iLocator.sangle && theta2 <= (iLocator.sangle + sweepangle))
{
jSensor.Connections.Add(new networkdll.Class1.WirelessSensorConnection(iLocator,
jSensor, (iLocator.sangle - res)));
}
else
{
for (int k = 1; k < sweepangle; k++)
{
if (theta2 == iLocator.sangle)
{
jSensor.Connections.Add(new networkdll.Class1.WirelessSensorConnection(iLocator,
jSensor, (iLocator.sangle - res))); break;
}
else iLocator.sangle = (iLocator.sangle + 1);
}
}
}
}
else if (jSensor.x <= iLocator.x && jSensor.y < iLocator.y) //Region 3
{
if (theta3 >= iLocator.sangle && theta3 <= (iLocator.sangle + sweepangle))
{
if (theta3 == iLocator.sangle && theta3 <= (iLocator.sangle + sweepangle))
{
jSensor.Connections.Add(new networkdll.Class1.WirelessSensorConnection(iLocator,
jSensor, (iLocator.sangle - res)));
}

else
{
for (int k = 1; k < sweepangle; k++)
{
if (theta3 == iLocator.sangle)
{
jSensor.Connections.Add(new networkdll.Class1.WirelessSensorConnection(iLocator,
jSensor, (iLocator.sangle - res))); break;
}
else iLocator.sangle = (iLocator.sangle + 1);
}
}
}
}
else if (jSensor.x > iLocator.x && jSensor.y < iLocator.y) //Region 4
{
if (theta4 >= iLocator.sangle && theta4 <= (iLocator.sangle + sweepangle))
{
if (theta4 == iLocator.sangle && theta4 <= (iLocator.sangle + sweepangle))
{
jSensor.Connections.Add(new networkdll.Class1.WirelessSensorConnection(iLocator,
jSensor, (iLocator.sangle - res)));
}
else
{
for (int k = 1; k < sweepangle; k++)
{
if (theta4 == iLocator.sangle)
{
jSensor.Connections.Add(new networkdll.Class1.WirelessSensorConnection(iLocator,
jSensor, (iLocator.sangle - res))); break;
}
else iLocator.sangle = (iLocator.sangle +
1);
}
}
}
}
}
}
}

3- send method, when the connection is established between sender and receiver, sender will
carry out sending packets using send method.
public void send(ArrayList asensor)
{
foreach (networkdll.Class1.WirelessSensor sensor in asensor)
{
foreach (networkdll.Class1.WirelessSensorConnection connection in
sensor.Connections)
{
sensor.apkts.Add(new
networkdll.Class1.pkt(connection.sLocator.id, connection.sLocator.x,
connection.sLocator.y, connection.Sangle));
}
}
}

4- localize method, is the core of localization algorithm, where all calculations run.
public void localize(object sender, PaintEventArgs e, ArrayList asensor, bool
checkBox2, bool checkBox3, bool checkBox4, bool checkBox5, bool checkBox6, bool
checkBox7, bool checkBox8, float locatorRadius, String textbox)
{
Graphics g = e.Graphics;
Brush sbrush = Brushes.BlueViolet;
String opc8 = "", opc7 = "", opc6 = "", opc5 = "", opc4 = "", opc3 = "", opc2 =
"";
//printing into Excel
String op8 = "", op7 = "", op6 = "", op5 = "", op4 = "", op3 = "", op2 = "";
String time = "";
//to calculate algorithm running time
int d = 0;
int ersum2 = 0, count2 = 0, ersum3 = 0, count3 = 0, ersum4 = 0, count4 = 0,
ersum5 = 0, count5 = 0, ersum6 = 0, count6 = 0, ersum7 = 0, count7 = 0, ersum8 =
0, count8 = 0;
foreach (networkdll.Class1.WirelessSensor sensor in asensor)
{
int[] hLHs = new int[9]; //Locators heard by sensor node
int[] hax = new int[9]; //x coordinate of locators heard
int[] hay = new int[9]; //y coordinate of locators heard
int[] hsa = new int[9]; //start angle of locators heard
int t2 = 0, Xest, Yest;
foreach (networkdll.Class1.pkt packet in sensor.apkts)
{
hLHs[t2] = packet.ID;
hax[t2] = packet.x;
hay[t2] = packet.y;
hsa[t2] = packet.sangle;
t2++;
if (sensor.apkts.Count == 8 && t2 == 8)

//eight

if (checkBox8)
{

float
float
float
float
float
float
float
float
float
float
float
float
float

if (sensor.apkts.Count == 8 && t2 == 8)
//eight sectors "locators"
{
int xg, yg = 20, MG = 0;
Xest = 0; Yest = 0;
for (int c = 1; c < 51; c++)
//columns
{
xg = 20;
for (int r = 1; r < 51; r++)
//rows
{
int gridscore = 0;
lp1Radius = (float)Math.Sqrt(Math.Pow(hax[0] - xg, 2) + Math.Pow(hay[0] - yg, 2));
lp2Radius = (float)Math.Sqrt(Math.Pow(hax[1] - xg, 2) + Math.Pow(hay[1] - yg, 2));
lp3Radius = (float)Math.Sqrt(Math.Pow(hax[2] - xg, 2) + Math.Pow(hay[2] - yg, 2));
lp4Radius = (float)Math.Sqrt(Math.Pow(hax[3] - xg, 2) + Math.Pow(hay[3] - yg, 2));
lp5Radius = (float)Math.Sqrt(Math.Pow(hax[4] - xg, 2) + Math.Pow(hay[4] - yg, 2));
lp6Radius = (float)Math.Sqrt(Math.Pow(hax[5] - xg, 2) + Math.Pow(hay[5] - yg, 2));
lp7Radius = (float)Math.Sqrt(Math.Pow(hax[6] - xg, 2) + Math.Pow(hay[6] - yg, 2));
lp8Radius = (float)Math.Sqrt(Math.Pow(hax[7] - xg, 2) + Math.Pow(hay[7] - yg, 2));
if (lp1Radius <= locatorRadius)
{
theta1 = (float)(Math.Acos(Math.Abs(hax[0] - xg) / lp1Radius) * (180 * 7 / 22));
theta = (float)(Math.Acos(Math.Abs(hay[0] - yg) / lp1Radius) * (180 * 7 / 22));
theta2 = theta + 90;
theta3 = theta1 + 180;
theta4 = 270 + theta;
if (xg >= hax[0] && yg >= hay[0]) //Region 1
{
if (theta1 >= hsa[0] && theta1 <= (hsa[0] + 10)) gridscore++;
}
else if (xg <= hax[0] && yg >= hay[0]) //Region 2
{
if (theta2 >= hsa[0] && theta2 <= (hsa[0] + 10)) gridscore++;
}
else if (xg <= hax[0] && yg <= hay[0]) //Region 3
{
if (theta3 >= hsa[0] && theta3 <= (hsa[0] + 10)) gridscore++;
}
else if (xg >= hax[0] && yg <= hay[0]) //Region 4
{
if (theta4 >= hsa[0] && theta4 <= (hsa[0] + 10)) gridscore++;
}
}

float
float
float
float
float

if (lp2Radius <= locatorRadius)


{
theta1 = (float)(Math.Acos(Math.Abs(hax[1] - xg) / lp2Radius) * (180 * 7 / 22));
theta = (float)(Math.Acos(Math.Abs(hay[1] - yg) / lp2Radius) * (180 * 7 / 22));
theta2 = theta + 90;
theta3 = theta1 + 180;
theta4 = 270 + theta;

if (xg >= hax[1] && yg >= hay[1]) //Region 1


{
if (theta1 >= hsa[1] && theta1 <= (hsa[1] + 10)) gridscore++;
}
else if (xg <= hax[1] && yg >= hay[1]) //Region 2
{
if (theta2 >= hsa[1] && theta2 <= (hsa[1] + 10)) gridscore++;
}
else if (xg <= hax[1] && yg <= hay[1]) //Region 3
{
if (theta3 >= hsa[1] && theta3 <= (hsa[1] + 10)) gridscore++;

float
float
float
float
float

}
else if (xg >= hax[1] && yg <= hay[1]) //Region 4
{
if (theta4 >= hsa[1] && theta4 <= (hsa[1] + 10)) gridscore++;
}
}
if (lp3Radius <= locatorRadius)
{
theta1 = (float)(Math.Acos(Math.Abs(hax[2] - xg) / lp3Radius) * (180 * 7 / 22));
theta = (float)(Math.Acos(Math.Abs(hay[2] - yg) / lp3Radius) * (180 * 7 / 22));
theta2 = theta + 90;
theta3 = theta1 + 180;
theta4 = 270 + theta;
if (xg >= hax[2] && yg >= hay[2]) //Region 1
{
if (theta1 >= hsa[2] && theta1 <= (hsa[2] + 10)) gridscore++;
}
else if (xg <= hax[2] && yg >= hay[2]) //Region 2
{
if (theta2 >= hsa[2] && theta2 <= (hsa[2] + 10)) gridscore++;
}
else if (xg <= hax[2] && yg <= hay[2]) //Region 3
{
if (theta3 >= hsa[2] && theta3 <= (hsa[2] + 10)) gridscore++;

float
float
float
float
float

}
else if (xg >= hax[2] && yg <= hay[2]) //Region 4
{
if (theta4 >= hsa[2] && theta4 <= (hsa[2] + 10)) gridscore++;
}
}
if (lp4Radius <= locatorRadius)
{
theta1 = (float)(Math.Acos(Math.Abs(hax[3] - xg) / lp4Radius) * (180 * 7 / 22));
theta = (float)(Math.Acos(Math.Abs(hay[3] - yg) / lp4Radius) * (180 * 7 / 22));
theta2 = theta + 90;
theta3 = theta1 + 180;
theta4 = 270 + theta;

if (xg >= hax[3] && yg >= hay[3]) //Region 1


{
if (theta1 >= hsa[3] && theta1 <= (hsa[3] + 10)) gridscore++;
}
else if (xg <= hax[3] && yg >= hay[3]) //Region 2
{
if (theta2 >= hsa[3] && theta2 <= (hsa[3] + 10)) gridscore++;
}
else if (xg <= hax[3] && yg <= hay[3]) //Region 3
{
if (theta3 >= hsa[3] && theta3 <= (hsa[3] + 10)) gridscore++;

float
float
float
float
float

}
else if (xg >= hax[3] && yg <= hay[3]) //Region 4
{
if (theta4 >= hsa[3] && theta4 <= (hsa[3] + 10)) gridscore++;
}
}
if (lp5Radius <= locatorRadius)
{
theta1 = (float)(Math.Acos(Math.Abs(hax[4] - xg) / lp5Radius) * (180 * 7 / 22));
theta = (float)(Math.Acos(Math.Abs(hay[4] - yg) / lp5Radius) * (180 * 7 / 22));
theta2 = theta + 90;
theta3 = theta1 + 180;
theta4 = 270 + theta;
if (xg >= hax[4] && yg >= hay[4]) //Region 1
{
if (theta1 >= hsa[4] && theta1 <= (hsa[4] + 10)) gridscore++;
}
else if (xg <= hax[4] && yg >= hay[4]) //Region 2
{
if (theta2 >= hsa[4] && theta2 <= (hsa[4] + 10)) gridscore++;
}
else if (xg <= hax[4] && yg <= hay[4]) //Region 3
{
if (theta3 >= hsa[4] && theta3 <= (hsa[4] + 10)) gridscore++;

float
float
float
float
float

}
else if (xg >= hax[4] && yg <= hay[4]) //Region 4
{
if (theta4 >= hsa[4] && theta4 <= (hsa[4] + 10)) gridscore++;
}
}
if (lp6Radius <= locatorRadius)
{
theta1 = (float)(Math.Acos(Math.Abs(hax[5] - xg) / lp6Radius) * (180 * 7 / 22));
theta = (float)(Math.Acos(Math.Abs(hay[5] - yg) / lp6Radius) * (180 * 7 / 22));
theta2 = theta + 90;
theta3 = theta1 + 180;
theta4 = 270 + theta;

if (xg >= hax[5] && yg >= hay[5]) //Region 1


{
if (theta1 >= hsa[5] && theta1 <= (hsa[5] + 10)) gridscore++;
}
else if (xg <= hax[5] && yg >= hay[5]) //Region 2
{
if (theta2 >= hsa[5] && theta2 <= (hsa[5] + 10)) gridscore++;
}
else if (xg <= hax[5] && yg <= hay[5]) //Region 3
{
if (theta3 >= hsa[5] && theta3 <= (hsa[5] + 10)) gridscore++;

float
float
float
float
float

}
else if (xg >= hax[5] && yg <= hay[5]) //Region 4
{
if (theta4 >= hsa[5] && theta4 <= (hsa[5] + 10)) gridscore++;
}
}
if (lp7Radius <= locatorRadius)
{
theta1 = (float)(Math.Acos(Math.Abs(hax[6] - xg) / lp7Radius) * (180 * 7 / 22));
theta = (float)(Math.Acos(Math.Abs(hay[6] - yg) / lp7Radius) * (180 * 7 / 22));
theta2 = theta + 90;
theta3 = theta1 + 180;
theta4 = 270 + theta;
if (xg >= hax[6] && yg >= hay[6]) //Region 1
{
if (theta1 >= hsa[6] && theta1 <= (hsa[6] + 10)) gridscore++;
}
else if (xg <= hax[6] && yg >= hay[6]) //Region 2
{
if (theta2 >= hsa[6] && theta2 <= (hsa[6] + 10)) gridscore++;
}
else if (xg <= hax[6] && yg <= hay[6]) //Region 3
{
if (theta3 >= hsa[6] && theta3 <= (hsa[6] + 10)) gridscore++;

float
float
float
float
float

}
else if (xg >= hax[6] && yg <= hay[6]) //Region 4
{
if (theta4 >= hsa[6] && theta4 <= (hsa[6] + 10)) gridscore++;
}
}
if (lp8Radius <= locatorRadius)
{
theta1 = (float)(Math.Acos(Math.Abs(hax[7] - xg) / lp8Radius) * (180 * 7 / 22));
theta = (float)(Math.Acos(Math.Abs(hay[7] - yg) / lp8Radius) * (180 * 7 / 22));
theta2 = theta + 90;
theta3 = theta1 + 180;
theta4 = 270 + theta;

if (xg >= hax[7] && yg >= hay[7]) //Region 1


{
if (theta1 >= hsa[7] && theta1 <= (hsa[7] + 10)) gridscore++;
}
else if (xg <= hax[7] && yg >= hay[7]) //Region 2
{
if (theta2 >= hsa[7] && theta2 <= (hsa[7] + 10)) gridscore++;
}
else if (xg <= hax[7] && yg <= hay[7]) //Region 3
{
if (theta3 >= hsa[7] && theta3 <= (hsa[7] + 10)) gridscore++;
}
else if (xg >= hax[7] && yg <= hay[7]) //Region 4
{
if (theta4 >= hsa[7] && theta4 <= (hsa[7] + 10)) gridscore++;
}
}
if (gridscore == 8)
{
MG++; Xest += xg; Yest += yg;
}
xg += 10;
}
yg += 10;
}
if (MG != 0)
{
int erx, ery;
//error (x,y)
erx = Math.Abs(((Xest / MG) - sensor.x));
ery = Math.Abs(((Yest / MG) - sensor.y));
count8++;
ersum8 += (int)Math.Sqrt(erx * erx + ery * ery);
//write output to Excel file
op8 += "," + d + "," + (Xest / MG) + "," + (Yest / MG) + "," + sensor.x + "," + sensor.y
+ "," + erx + "," + ery + "," + (int)Math.Sqrt(erx * erx + ery * ery) + "\n";
//paint estimated position
g.FillEllipse(sbrush, (Xest / MG) - 5, (Yest / MG) - 5, 10, 10);
}
}
if (count8 != 0) opc8 = "," + count8 + "," + "," + "," + "," + " Average Localization
Error =" + (ersum8 / count8) + "\n";
}
}

d++;
}

The above code is for eight locators' intersection, the rest of the code is the same but with seven,
six, five, four, three, and two locators' intersection. The estimated position is calculated and
painted, then it will be written to an Excel file a shown below:

end = System.DateTime.Now.Ticks;
executiontime = end - start;
time = " " + (double)(executiontime / 10000);
using (StreamWriter w = new StreamWriter (textbox,false,Encoding.UTF8))
{
w.WriteLine("LHs" + "," + "Node" + "," + "X Estimated" + "," + "Y Estimated" + "," +
"X Actual" + "," + "Y Actual" + "," + "Ex" + "," + "Ey" + "," + "Error");
w.WriteLine("Two" + op2 + "No. of Sensors =" + "," + opc2 + "\nThree" + op3 + "No. of
Sensors =" + "," + opc3 + "\nFour" + op4 + "No. of Sensors =" + "," + opc4 + "\nFive" +
op5 + "No. of Sensors =" + "," + opc5 + "\nSix" + op6 + "No. of Sensors =" + "," + opc6
+ "\nSeven" + op7 + "No. of Sensors =" + "," + opc7 + "\nEight" + op8 + "No. of Sensors
=" + "," + opc8 + "\n Execution Time = " + time + " ms");
}
}

How to simulate a localization algorithm? (e.g. HiRLoc)


1- Choose deployment strategy of locators (e.g. grid).
2- Choose locator antenna type.
3- Click on deploy WSN button.
4- Click on start simulation.
5- Check Estimate position.
6- Choose localization algorithm (check HiRLoc).
7- Write the name and location of Excel file which contains the actual and estimated position,
number of localized sensor nodes and running time of that algorithm.
8- Check number of crossed sectors (2,3,4,5,6,).
9- Estimated position of sensor nodes will be painted on the area with BlueViolet color.
10- Click5on Stop
simulation button.

9
2
6
8
3
4

10
Figure 5: Steps of HiRloc Localization

Output File:

Design criteria for our framework:


1- Supports completely large scale networks, e.g. 400 nodes x 400 nodes which are fully
interconnected which implies 160,000 network connections. So, our proposed framework is
scalable.

2- The network can be deployed based on a wide range of parameters: network size, sensor nodes
communication distance, locator radius (communication range), sector angle (rotation angle) and
locator beam width.
3- Supports Multithreading, where the simulated algorithm runs on a separate thread from the
thread controlling the developer interface.
4- Packaging all simulator classes into a referenced DLL file for better modularity design.
5- Ease of implementing various localization algorithms by encapsulating each one into a
separate DLL file. Developers can write their own localization algorithm into a DLL file and
reference it into our framework.
6- Can be extended to implement many different localization algorithms with different categories
(range-free, range-based and a hybrid of both).
7- Suitable developer interface to enable deploying and moving sensor and locator nodes and
changing various simulation parameters.

CONCLUSION
We have presented a framework to develop WSN localization algorithms. The framework allows
developers to design the required localization algorithm by writing the code into a class library
file and referencing it to the framework. Our framework has been validated using different types
of localization algorithms: range-free, range-based and hybrid localization for WSNs. Our
framework results had been compared to the selected localization algorithms results and it was
found that they were compatible with the original results of these algorithms in their papers. Our
framework can be extended to implement other localization algorithms and can be used by future
researchers.

History
This is a Wireless Sensor Network Localization Simulator v1.1 written by Abdelhady
Mohammad Naguib in May 2011. Thank you for using the WSN Localization Simulator.

You might also like