guofeifei0822
2008-06-21, 16:08
该代码是有关于泛洪的:ft:
function x=animation_data
small=5; medium=20; large=50;
anim_def={...
{'Init_Application', 0, [0 0 0 ], small}, ...
{'Packet_Sent', 1, [0 1 0 ], small}, ...
{'Packet_Received', 1, [0 1 0 ], small}, ...
{'Collided_Packet_Received', 0, [1 0 0 ], small}, ...
{'Clock_Tick', 0, [0 0 0 ], small}, ...
{'Channel_Request', 0, [0 0 0 ], small}, ...
{'Channel_Idle_Check', 1, [1 0 0 ], small}, ...
{'Packet_Receive_Start', 0, [0 1 0 ], small}, ...
{'Packet_Receive_End', 0, [0 0 0 ], small}, ...
{'Packet_Transmit_Start', 1, [1 0 0 ], medium}, ...
{'Packet_Transmit_End', 0, [0 1 0 ], small}};
for i=1:length(anim_def)
a=anim_def{i};
x(i)=struct('event', a{1}, 'animated', a{2}, 'color', a{3}, 'size', a{4});
end
function application(S)
persistent app_data
global ID t
[t, event, ID, data]=animation_data();
[topology, mote_IDs]=prowler('GetTopologyInfo');
ix=find(mote_IDs==ID);
if ~strcmp(event, 'Init_Application')
try memory=app_data{ix}; catch memory=[]; end,
end
switch event
case 'Init_Application'
signal_strength=1;
memory=struct('send',1, 'signal_strength', signal_strength);
if ID==1
Set_Clock(1000)
end
PrintMessage('i')
case 'Packet_Sent'
PrintMessage('s')
case 'Packet_Received'
if memory.send
p=sim_params('get_app', 'P');
if isempty(p); p=.5; end
if rand<p
Send_Packet(radiostream(data.data, memory.signal_strength));
end
memory.send=0;
PrintMessage('r')
end
case 'Collided_Packet_Received'
case 'Clock_Tick'
Send_Packet(radiostream('Message for 00111 from Golomb', memory.signal_strength));
case 'GuiInfoRequest'
disp(sprintf('Memory Dump of mote ID# %d:\n',ID)); disp(memory)
case 'Application_Stopped'
case 'Application_Finished'
otherwise
error(['Bad event name for application: ' event])
end
app_data{ix}=memory;
function b=Send_Packet(data);
global ID t
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 file 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;
function [topology,mote_IDs]=topology(varargin);
Nx=10; Ny=1;
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_data
small=5; medium=20; large=50;
anim_def={...
{'Init_Application', 0, [0 0 0 ], small}, ...
{'Packet_Sent', 1, [0 1 0 ], small}, ...
{'Packet_Received', 1, [0 1 0 ], small}, ...
{'Collided_Packet_Received', 0, [1 0 0 ], small}, ...
{'Clock_Tick', 0, [0 0 0 ], small}, ...
{'Channel_Request', 0, [0 0 0 ], small}, ...
{'Channel_Idle_Check', 1, [1 0 0 ], small}, ...
{'Packet_Receive_Start', 0, [0 1 0 ], small}, ...
{'Packet_Receive_End', 0, [0 0 0 ], small}, ...
{'Packet_Transmit_Start', 1, [1 0 0 ], medium}, ...
{'Packet_Transmit_End', 0, [0 1 0 ], small}};
for i=1:length(anim_def)
a=anim_def{i};
x(i)=struct('event', a{1}, 'animated', a{2}, 'color', a{3}, 'size', a{4});
end
function application(S)
persistent app_data
global ID t
[t, event, ID, data]=animation_data();
[topology, mote_IDs]=prowler('GetTopologyInfo');
ix=find(mote_IDs==ID);
if ~strcmp(event, 'Init_Application')
try memory=app_data{ix}; catch memory=[]; end,
end
switch event
case 'Init_Application'
signal_strength=1;
memory=struct('send',1, 'signal_strength', signal_strength);
if ID==1
Set_Clock(1000)
end
PrintMessage('i')
case 'Packet_Sent'
PrintMessage('s')
case 'Packet_Received'
if memory.send
p=sim_params('get_app', 'P');
if isempty(p); p=.5; end
if rand<p
Send_Packet(radiostream(data.data, memory.signal_strength));
end
memory.send=0;
PrintMessage('r')
end
case 'Collided_Packet_Received'
case 'Clock_Tick'
Send_Packet(radiostream('Message for 00111 from Golomb', memory.signal_strength));
case 'GuiInfoRequest'
disp(sprintf('Memory Dump of mote ID# %d:\n',ID)); disp(memory)
case 'Application_Stopped'
case 'Application_Finished'
otherwise
error(['Bad event name for application: ' event])
end
app_data{ix}=memory;
function b=Send_Packet(data);
global ID t
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 file 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;
function [topology,mote_IDs]=topology(varargin);
Nx=10; Ny=1;
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;