% rows represent which node columes represent time
status_matrix = zeros(n+1,simulation_time);
%status of each node, the first row is master's status
next_status_timer = zeros(n+1,simulation_time);
%count the time left for current status
comm_matrix = zeros(n+1,simulation_time);
%which node has something to send at the time
ack_matrix = zeros(n+1,simulation_time);
back_off_counter=zeros(n+1,1);
first_frame_flag=zeros(n+1,1);
priority_matrix=zeros(n+1,1);
%now we only support 2 priorities high and low their DIFS is 3 and 4 slots
%plot master and slave nodes
x_position(floor(i*n/360)+1)=r*sin(i/360*2*pi);
y_position(floor(i*n/360)+1)=r*cos(i/360*2*pi);
x_position=[masterx x_position];
y_position=[mastery y_position];
h(1)=plot(x_position(1),y_position(1),'or', ...
'MarkerSize',16,'MarkerEdgeColor','k','Marker','o');
h(i)=plot(x_position(i),y_position(i),'or', ...
'MarkerSize',10,'MarkerEdgeColor','b','Marker','o');
position=[x_position;y_position];
for clock= 2: simulation_time
%random location generator
random = (rand(2,n+1)-0.5)/movepara;
position=position+random;
x_position=position(1,:);
y_position=position(2,:);
set(h(i),'XData',position(1,i),'YData',position(2,i))
if(comm_matrix(i,clock-1)==0&&status_matrix(i,clock-1)~=7)
comm_matrix(i,clock-1)=random;
%comm_table is not 0 means have something to send;
%first fill the current status matrix with sending status;
[status,timer,flag] = working_node(status_matrix(:,clock-1), ...
next_status_timer(:,clock-1),frame_size,first_frame_flag);
status_matrix(:,clock) = status;
next_status_timer(:,clock) = timer;
%now update current status and timer based on previous status and timer
%update comm_matrix also;
[status,timer,comm,counter,flag,ack] = state_machine(status_matrix(:,clock-1:clock), ...
next_status_timer(:,clock-1),comm_matrix(:,clock-1)...
,frame_size,back_off_counter,first_frame_flag, ...
priority_matrix,max_DIFS,x_position,y_position,ack_matrix(:,clock-1));
status_matrix(:,clock) = status;
next_status_timer(:,clock) = timer;
comm_matrix(:,clock) = comm;
back_off_counter = counter;
%plot the sending/Ack signal
if(sum(plot_flag(:,1))>=1)
%# Create a colored plot of the matrix values
% %# Change the colormap to gray (so higher values are
%# black and lower values are white)
textStrings = num2str(status_matrix(:),'%i');
%# Create strings from the matrix values
textStrings = strtrim(cellstr(textStrings));
%# Remove any space padding
[x,y] = meshgrid(1:size(status_matrix,2),1:size(status_matrix,1));
%# Create x and y coordinates for the strings
hStrings = text(x(:),y(:),textStrings(:));
%# Change the axes tick marks
set(gca,'YTick',1:n+1,...
'YTickLabel',{'Master','Slave1','Slave2','Slave3','Slave4','Slave5'},...
title('CSMA/CA Simulation result')
function [status, timer,flag] = working_node( ...
pre_status_matrix, pre_next_status_timer,frame_size,first_frame_flag ...
len=size(pre_status_matrix,1);
if(pre_status_matrix(i)==4 && pre_next_status_timer(i)==1)
%SIFS sending ack next time slot;
if(pre_status_matrix(i)==3 && pre_next_status_timer(i)>1)
%SIFS sending ack next time slot;
timer(i)= pre_next_status_timer(i)-1;
if(pre_status_matrix(i)==5 && pre_next_status_timer(i)>1)
%SIFS sending ack next time slot;
timer(i)= pre_next_status_timer(i)-1;
if(pre_status_matrix(i)==2 && pre_next_status_timer(i)==1)
%DIFS end start sending data;
timer(i) = frame_size(i);