% BY Ajay Jatav
% email id:- ajayjatav43@yahoo.in
% Chandigarh India
% create the gui and name it as timesync, drags the required item in gui and copy and paste the codes on new file.
function varargout=wsntimesync(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, 'gui_Singleton', gui_Singleton, 'gui_OpeningFcn', @wsntimesync_OpeningFcn, 'gui_OutputFcn', @WSNSIM_OutputFcn, 'gui_LayoutFcn', [], 'gui_Callback', []);
if (nargin && ischar(varargin{1}))
gui_State.gui_Callback = str2func(varargin{1});
end
if (nargout)
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:})
else
gui_mainfcn(gui_State, varargin{:})
end
end % End initialization code
% --- Executes just before WSNSim is made visible.
function wsntimesync_OpeningFcn(hObject, eventdata, handles, varargin)
GlobalVars()
clear workspace clc
handles.output = hObject; guidata(hObject, handles)
axes(handles.GridAxes); cla;
sensorLogoHandle = imshow('sensorLogo.jpg')
axis([get(sensorLogoHandle, 'XData') get(sensorLogoHandle, 'YData')])
SetSamples(handles, 'initAll', 'hide') % Set default values
if (nodesDispersedF)
ClearHandles(handles)
end
set(handles.syncType_RBS, 'Value',0)
set(handles.syncType_TPSN, 'Value',0)
set(handles.syncType_Source, 'Value',0)
set(handles.syncType_Hybrid, 'Value',0)
switch syncType
case 'RBS'
set(handles.syncType_RBS, 'Value',1)
case 'TPSN'
set(handles.syncType_TPSN, 'Value',1)
case 'source'
set(handles.syncType_Source, 'Value',1)
case 'hybrid'
set(handles.syncType_Hybrid, 'Value',1)
end
simFileInfo = dir('WSNSim.m')
set(handles.clearGrid, 'String','Quit')
set(handles.statusText, 'String','Enter Grid parameters...')
set(handles.sourceNodeID, 'String','')
set(handles.curLocX, 'String','')
set(handles.curLocY, 'String','')
set(handles.analyzedNode, 'String','')
set(handles.pwrRem, 'String','')
set(handles.WSNSim, 'Name', sprintf('WSN Simulator 2.0 - %s', simFileInfo.date))
end
% --- Outputs from this function are returned to the command line.
function varargout = WSNSIM_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output
reqVer = '7.1.0.246 (R14) Service Pack 3'
if (version ~= reqVer) % Incorrect Matlab version detected
msgbox('Matlab R14 SP3 is required to ensure proper GUI performance.')
end
end
% ================= BUTTON FUNCTIONS =================
function editParams_Callback(hObject, eventdata, handles)
ParamEdit()
end
function recharge_Callback(hObject, eventdata, handles)
GlobalVars()
if (nodesDispersedF)
nodePower = nodePowerMax .* ones(1, nodeCount) % Maximize the power of each node
deadNodes = 0;
set(handles.depletedNodes, 'String',sprintf('%g / %.3g%%', deadNodes, 100*deadNodes/nodeCount))
UpdateAnalyzedNode(handles, closestNode)
UpdatePowerAxes(handles, 'noCalc')
axes(handles.GridAxes)
PlotGrid() % Redraw the nodes to refresh their color
end
end
function clearGrid_Callback(hObject, eventdata, handles)
GlobalVars()
if (networkSyncF) % Clear the sync lines first
set(handles.syncTime, 'String','')
set(handles.numTX, 'String','')
set(handles.numRX, 'String','')
set(handles.droppedTX, 'String','')
UpdateGridAxes(handles)
DrawFlooding(0)
PlotGrid()
networkSyncF = 0;
elseif (networkLvlDiscF) % Clear the lvl-disc lines if no sync has been performed
parent = []
closestNode = sourceNode
ClearHandles(handles)
UpdateGridAxes(handles)
PlotGrid()
set(handles.orphNodes, 'String','')
set(handles.statusText, 'String','Enter Flood parameters...')
networkLvlDiscF = 0
elseif (nodesDispersedF) % Clear nodes and reset to the opening screen
wsntimesync_OpeningFcn(hObject, eventdata, handles, 1)
axes(handles.PwrAxes); cla axis([0 1 0 1])
set(handles.orphNodes, 'String','')
set(handles.depletedNodes, 'String','')
set(handles.sourceNodeID, 'String','')
set(handles.curLocX, 'String','')
set(handles.curLocY, 'String','')
set(handles.analyzedNode, 'String','')
set(handles.pwrRem, 'String','')
set(handles.clearGrid, 'String','Quit')
nodesDispersedF = 0
else
close
end
end
function plotGrid_Callback(hObject, eventdata, handles)
GlobalVars(); clc
set(handles.clearGrid, 'String','Clear Grid')
set(handles.statusText, 'String','Initializing Network...')
if (~dataLoadedF) % Set variables here to avoid disrupting loaded values
SetSamples(handles, 'initArrays', 'hide') % Set default values
networkChangedF = 0
nodesDispersedF = 1
networkLvlDiscF = 0
networkSyncF = 0
closestNode = sourceNode
else
dataLoadedF = 0
end
UpdateGridAxes(handles)
PlotGrid()
if (~networkChangedF && networkLvlDiscF)
% Needed in case the data has been loaded and we want the flooding lines...
NetworkChangedF flag must be 0 to redraw the flooding lines
DrawFlooding(0)
end
UpdatePowerAxes(handles, 'noCalc')
ClearHandles(handles)
set(handles.depletedNodes, 'String',sprintf('%g / %.3g%%', deadNodes, 100*deadNodes/nodeCount))
set(handles.statusText, 'String','Enter Flood parameters...')
set(handles.analyzedNode, 'String',sourceNode)
set(handles.pwrRem, 'String',nodePower(sourceNode))
set(handles.flood, 'Enable','on')
set(handles.synchronize, 'Enable','on')
end
function flood_Callback(hObject, eventdata, handles)
GlobalVars(); clc
if (networkChangedF || ~nodesDispersedF || dataLoadedF)
plotGrid_Callback(hObject, eventdata, handles)
end
if (nodePower(sourceNode) < 2)
% If source node can only transmit one more time, elect a new source
ElectNewSource(handles, sourceNode)
tempStr = get(handles.statusText, 'String')
tempColor = get(handles.statusText, 'BackgroundColor')
set(handles.statusText, 'String',sprintf('Source node is depleted. New source node is %g', sourceNode))
set(handles.statusText, 'BackgroundColor','red')
pause(2)
set(handles.statusText, 'String',tempStr)
set(handles.statusText, 'BackgroundColor',tempColor)
end
parent = []
maxDistanceLow = nthroot(PtWLow/PrThresholdWLow, pathLossCoeff)
maxDistanceHigh = nthroot(PtWHigh/PrThresholdWHigh, pathLossCoeff)
closestNode = sourceNode
set(handles.statusText, 'String','Simulating flooding...')
set(handles.plotGrid, 'Enable','off')
set(handles.clearGrid, 'Enable','off')
set(handles.flood, 'Enable','off')
set(handles.synchronize, 'Enable','off')
set(handles.syncType_RBS, 'Enable','off')
set(handles.syncType_TPSN, 'Enable','off')
set(handles.syncType_Source, 'Enable','off')
set(handles.syncType_Hybrid, 'Enable','off')
set(handles.FloodTest, 'Enable','off')
set(handles.EnergyTest, 'Enable','off')
set(handles.TimeTest, 'Enable','off')
set(handles.editParams, 'Enable','off')
set(handles.recharge, 'Enable','off')
pause(0.001)
UpdateGridAxes(handles)
PlotGrid()
s = cputime
BasicDecayFlooding(handles)
floodTime = cputime - s
DrawFlooding(pauseInt)
ClearHandles(handles)
PlotGrid()
UpdatePowerAxes(handles)
UpdateAnalyzedNode(handles, closestNode)
UpdateNodeStatus(handles)
set(handles.plotGrid, 'Enable','on')
set(handles.clearGrid, 'Enable','on')
set(handles.flood, 'Enable','on')
set(handles.synchronize, 'Enable','on')
set(handles.syncType_RBS, 'Enable','on')
set(handles.syncType_TPSN, 'Enable','on')
set(handles.syncType_Source, 'Enable','on')
set(handles.syncType_Hybrid, 'Enable','on')
set(handles.FloodTest, 'Enable','on')
set(handles.EnergyTest, 'Enable','on')
set(handles.TimeTest, 'Enable','on')
set(handles.editParams, 'Enable','on')
set(handles.recharge, 'Enable','on')
set(handles.floodTime, 'String',floodTime)
set(handles.numTX, 'String',numTx)
set(handles.numRX, 'String',numRx)
if (tracebackF)
Traceback(close