How can i implement Active, Idle and Sleep Modes to a Node using Timer or without Timer?

I want to implement active, idle and sleep modes in LEACH protocol. How can i do this using timer? is there there any method exists in MAT LAB?

 Accepted Answer

You could do it using a timer, but that would restrict you to code that took no longer than real-time, and if your code was more efficient than real-time then you would be unable to model at accelerated rates.
I suggest that you consider a point: if you use timers, then what is going to happen when you put in a breakpoint to debug the code?

2 Comments

Is there any other way of it excluding timer, i mean programming trick?
Simulating time. Keep an event queue sorted by time. Process events in order.

Sign in to comment.

More Answers (1)

Thank you Sir, can you share any sample code, please.

3 Comments

This should not be difficult for you to write. Create a struct to represent events. The event queue is a struct array of individual events. Keep the struct array sorted by time the event is intended to start. At each step, act on the first entry in the queue.
If you have multiple entries with the same time stamp, it is important that you simulate the confusion caused by multiple simultaneous reads and writes to the same data.
Sir, how i can select nodes randomly from two different structures. the file is attached for consideration, please.
num_nodes = [length(first_structure), length(second_structure), length(third_structure), ...];
total_number_of_nodes = sum(num_nodes);
running_total = cumsum([0, num_nodes]);
idx = randi(total_number_of_nodes);
struct_number = find(running_total <= idx, 1, 'first');
offset = idx - running_total(struct_number);
switch struct_number
case 1:
random_node = first_structure(offset);
case 2:
random_node = second_structure(offset);
case 3:
random_node = third_structure(offset);
case 4:
random_node = fourth_structure(offset);
otherwise:
error('How did structure_number get to be %d??', struct_number);
end
The code would be somewhat simpler and less error-prone if all of the data was in one structure, like
num_nodes = structfun(@length, master_structure);
...
random_node = master_structure(struct_number).node_structure(offset);

Sign in to comment.

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!