无线传感器网络各类路由协议仿真 下载本文

radio=prowler('GetRadioName');

b=feval(radio, 'Send_Packet', ID, data, t);

function b=Set_Clock(alarm_time); global ID

prowler('InsertEvents2Q', make_event(alarm_time, 'Clock_Tick', ID));

function PrintMessage(msg) global ID

prowler('TextMessage', ID, msg)

function LED(msg) global ID

prowler('LED', ID, msg)

function varargout=info

title1='Application FLOOD1D';

str1={ 'This application illustrates the simple flooding algorithm.'; ... '';...

'The sender mote transmits a message. Each mote receiving the';... 'message retransmits it with probability p.' ;...

'The value of p can be set in the ''Application Parameters''';... 'window. The default value is p=0.5';... '';...

'If p is too low, the flood dies out before every mote could';... 'receive the message. If p it too high, the message is';...

'retransmitted several times after every mote have received it,' ;... 'thus causing an unnecessarily high settling time.';... '';...

'The optimal value of p highly depends on topology.';... '';...

'You can change the topology by editing the FLOOD1D_TOPOLOGY.';... '';...

'See also FLOOD2D.';... ''}; if nargout==0

helpwin({ title1, str1}, 'Application Info') else

varargout={ title1, str1}; end

function param=params;

param(1).name='P'; param(1).default=0.5;

file

function [topology,mote_IDs]=topology(varargin); Nx=10; Ny=1; % number of points on the grid

ix=1;t=[]; distx=1; disty=5;

X=1:distx:(Nx-1)*distx+1; Y=1:disty:(Ny-1)*disty+1; for i=X for j=Y

t=[t; i,j]; end end

topology=t;

mote_IDs=1:Nx*Ny;

function x=animation persistent anim_data if isempty(anim_data)

small=5; medium=20; large=50;

% Event_name Size

anim_def={...

{'Init_Application', small}, ...

{'Packet_Sent', small}, ...

{'Packet_Received', small}, ...

{'Collided_Packet_Received', small}, ...

{'Clock_Tick', small}, ...

{'Channel_Request', small}, ...

{'Channel_Idle_Check', small}, ...

{'Packet_Receive_Start', small}, ...

{'Packet_Receive_End', Animated 1, 2, 3, 4, 0, 0, 1, 0, 0, Color/{on/off/toggle} [0 0 0 ], [1 0 0 ], [1 0 0 ], [1 0 0 ], [0 0 0 ], [0 0 0 ], [1 0 0 ], [0 1 0 ], [0 0 0 ],

small}, ...

{'Packet_Transmit_Start', 1, [1 0 0 ], medium}, ...

{'Packet_Transmit_End', 1, [0 1 0 ], small}};

for i=1:length(anim_def) a=anim_def{i}; if i==1

anim_data=struct('event', a{1}, 'animated', a{2}, 'color', a{3}, 'size', a{4});

else

anim_data(i)=struct('event', a{1}, 'animated', a{2}, 'color', a{3}, 'size', a{4});

end end end

x=anim_data;

function application(S)

S; persistent app_data S; global ID t

S; [t, event, ID, data]=get_event(S);

S; [topology, mote_IDs]=prowler('GetTopologyInfo'); S; ix=find(mote_IDs==ID);

S; if ~strcmp(event, 'Init_Application')

S; try memory=app_data{ix}; catch memory=[]; end, S; end S;

SENDER_ID=sim_params('get_app', 'Start_Mote'); if isempty(SENDER_ID), SENDER_ID=1; end

switch event

case 'Init_Application' signal_strength=1;

memory=struct('send',1, 'signal_strength', signal_strength); if ID==SENDER_ID % this node starts flood Set_Clock(1000) end

PrintMessage('i') case 'Packet_Sent' memory.send=0;

PrintMessage('s')

case 'Packet_Received'

% data.data % message

% data.signal_strength % received signal strength if memory.send

p=sim_params('get_app', 'P'); if isempty(p); p=.3; end

% p=0.3; % p is forced to 0.3 / Comment this line if you want to use it for optimization

if rand

Send_Packet(radiostream(data.data, memory.signal_strength)); end

memory.send=0; PrintMessage('r') end

case 'Collided_Packet_Received'

% this is for debug purposes only

case 'Clock_Tick'

Send_Packet(radiostream('this is the message', memory.signal_strength));

case 'GuiInfoRequest' if ~isempty(memory)

disp(sprintf('Memory Dump of mote ID# %d:\\n',ID)); disp(memory) else

disp(sprintf('No memory dump available for node %d.\\n',ID)); end

case 'Application_Stopped'

% this event is called when simulation is stopped/suspended

case 'Application_Finished'

% this event is called when simulation is finished

otherwise

error(['Bad event name for application: ' event]) end

S; app_data{ix}=memory; S;

function b=Send_Packet(data);